app/Plugin/ProductPlus42/Resource/template/admin/Product/upload_js.twig line 1

Open in your IDE?
  1. {#
  2. * Plugin Name : ProductPlus
  3. *
  4. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  5. * http://www.bratech.co.jp/
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. #}
  10. <script>
  11. $(function() {
  12.     {% for ProductItem in ProductItems %}
  13.         {% if ProductItem.input_type == constant('Plugin\\ProductPlus42\\Entity\\ProductItem::IMAGE_TYPE') %}
  14.             {% set name = 'productplus_' ~ ProductItem.id %}
  15.             {% set image_name = 'productplus_' ~ ProductItem.id ~ '_images' %}
  16.             {% set add_name = 'productplus_' ~ ProductItem.id ~ '_add_images' %}
  17.             {% set delete_name = 'productplus_' ~ ProductItem.id ~ '_delete_images' %}
  18.             // ファイルアップロード
  19.             var inputFileElement = document.querySelector('#admin_product_productplus_{{ ProductItem.id}}');
  20.             FilePond.setOptions({
  21.                 server: {
  22.                     process: {
  23.                         url: '{{ path('admin_product_image_process') }}',
  24.                         headers: {
  25.                             'ECCUBE-CSRF-TOKEN': $('meta[name="eccube-csrf-token"]').attr('content'),
  26.                             'X-Requested-With': 'XMLHttpRequest'
  27.                         }
  28.                     },
  29.                     load: {
  30.                         url: '{{ path('admin_productplus_image_load') }}?source=',
  31.                         headers: {
  32.                             'X-Requested-With': 'XMLHttpRequest'
  33.                         }
  34.                     },
  35.                     revert: {
  36.                         url: '{{ path('admin_product_image_revert') }}',
  37.                         headers: {
  38.                             'ECCUBE-CSRF-TOKEN': $('meta[name="eccube-csrf-token"]').attr('content'),
  39.                             'X-Requested-With': 'XMLHttpRequest'
  40.                         }
  41.                     }
  42.                 }
  43.             });
  44.             var pond_{{ name }} = FilePond.create(inputFileElement, {
  45.                 allowFileTypeValidation: true,
  46.                 acceptedFileTypes: [
  47.                     'image/gif',
  48.                     'image/png',
  49.                     'image/jpeg'
  50.                 ],
  51.                 allowFileSizeValidation: true,
  52.                 maxFileSize: 10000000,
  53.                 maxFiles: 10,
  54.                 allowBrowse: true,
  55.                 allowDrop: true,
  56.                 allowReorder: true,
  57.                 labelIdle: '<i class="fa fa-cloud-upload fa-3x text-ec-lightGray mx-3 align-middle" aria-hidden="true" style="font-size: 40px"></i>{{ 'admin.common.drag_and_drop_image_description'|trans }}<span class="filepond--label-action">{{ 'admin.common.file_select'|trans }}</span>',
  58.                 // 保存されている画像のロード
  59.                 files: [
  60.                     {% for image in form[image_name] %}
  61.                     {
  62.                         source: '{{ image.vars.value }}',
  63.                         options: {
  64.                             type: 'local'
  65.                         }
  66.                     },
  67.                     {% endfor %}
  68.                     // 追加してすぐの画像のロード. バリデーションエラーの場合など.
  69.                     {% for add_image in form[add_name] %}
  70.                     {
  71.                         source: '{{ add_image.vars.value }}',
  72.                         options: {
  73.                             type: 'local'
  74.                         }
  75.                     },
  76.                     {% endfor %}
  77.                 ]
  78.             });
  79.             // 画像が追加されたら add_images にファイル名を追加する
  80.             var proto_add_{{ name }} = '{{ form_widget(form[add_name].vars.prototype) }}';
  81.             pond_{{ name }}.on('processfile', function(error, file) {
  82.                 if (error) {
  83.                     console.log(error);
  84.                 } else {
  85.                     $('#upload-zone-{{ProductItem.id}}').append(
  86.                         $(proto_add_{{ name }}.replace(/__name__/g, file.id))
  87.                             .val(file.serverId)
  88.                             .addClass('productplus_{{ProductItem.id}}_add_images')
  89.                     );
  90.                 }
  91.             });
  92.             // 画像が削除されたら delete_images にファイル名を追加する
  93.             var proto_del_{{ name }} = '{{ form_widget(form[delete_name].vars.prototype) }}';
  94.             pond_{{ name }}.on('removefile', function(error, file) {
  95.                 if (error) {
  96.                     console.log(error);
  97.                 } else {
  98.                     // file.serverId にはアップロードしたファイル名が格納される.
  99.                     // DBに登録されると URL path が入るためファイル名のみ抜き出す.
  100.                     if (file.serverId) {
  101.                         $('#upload-zone-{{ProductItem.id}}').append(
  102.                             $(proto_del_{{ name }}.replace(/__name__/g, file.id))
  103.                                 .val(file.serverId.split('/').pop())
  104.                                 .addClass('productplus_{{ProductItem.id}}_del_images')
  105.                         );
  106.                     }
  107.                     // 追加してすぐ削除した画像があれば削除する
  108.                     $('#upload-zone-{{ProductItem.id}}').find('#admin_product_productplus_{{ProductItem.id}}_add_images_' + file.id).remove(); // 追加してすぐ削除した画像
  109.                     $('#upload-zone-{{ProductItem.id}}').find('.productplus_{{ProductItem.id}}_add_images[value="' + file.filename + '"]').remove(); // 追加後, バリデーションエラーが発生した後に削除した画像
  110.                 }
  111.             });
  112.             pond_{{ name }}.on('reorderfiles', function(files, origin, target) {
  113.                 $image_files = $('[name^="admin_product[productplus_{{ProductItem.id}}_images]"]');
  114.                 $target = $image_files.parent();
  115.                 $image_files.remove();
  116.                 for(index in files){
  117.                     $target.append('<input type="hidden" id="admin_product_productplus_{{ProductItem.id}}_images_' + index + '" name="admin_product[productplus_{{ProductItem.id}}_images]['+ index +']" value="'+ files[index].filename +'" class="productplus_{{ProductItem.id}}_images">');
  118.                 }
  119.             });
  120.             // バリデーションエラーが出た場合に画像を保持するための hidden を追加しておく
  121.             var proto_image_{{ name }} = '{{ form_widget(form[image_name].vars.prototype) }}';
  122.             {% for image in form[image_name] %}
  123.                 $('#upload-zone-{{ProductItem.id}}').append(
  124.                     $(proto_image_{{ name }}.replace(/__name__/g, '{{ loop.index0 }}'))
  125.                         .val('{{ image.vars.value }}')
  126.                         .addClass('productplus_{{ProductItem.id}}_images')
  127.                 );
  128.             {% endfor %}
  129.             {% for add_image in form[add_name] %}
  130.                 $('#upload-zone-{{ProductItem.id}}').append(
  131.                     $('{{ form_widget(add_image) }}')
  132.                         .val('{{ add_image.vars.value }}')
  133.                         .addClass('productplus_{{ProductItem.id}}_add_images')
  134.                 );
  135.             {% endfor %}
  136.             {% for delete_image in form[delete_name] %}
  137.                 $('#upload-zone-{{ProductItem.id}}').append(
  138.                     $('{{ form_widget(delete_image) }}').addClass('productplus_{{ProductItem.id}}_del_images')
  139.                 );
  140.             {% endfor %}
  141.         {% endif %}
  142.     {% endfor %}
  143. })
  144. </script>