file.html.twig 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. {% extends "forms/field.html.twig" %}
  2. {% set defaults = config.plugins.form %}
  3. {% set files = defaults.files|merge(field|default([])) %}
  4. {% set limit = not field.multiple ? 1 : files.limit %}
  5. {% do config.set('forms.dropzone.enabled', true) %}
  6. {% macro bytesToSize(bytes) -%}
  7. {% spaceless %}
  8. {% set kilobyte = 1024 %}
  9. {% set megabyte = kilobyte * 1024 %}
  10. {% set gigabyte = megabyte * 1024 %}
  11. {% set terabyte = gigabyte * 1024 %}
  12. {% if bytes < kilobyte %}
  13. {{ bytes ~ ' B' }}
  14. {% elseif bytes < megabyte %}
  15. {{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
  16. {% elseif bytes < gigabyte %}
  17. {{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
  18. {% elseif bytes < terabyte %}
  19. {{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
  20. {% else %}
  21. {{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
  22. {% endif %}
  23. {% endspaceless %}
  24. {%- endmacro %}
  25. {% macro preview(path, value, global) %}
  26. {% if value %}
  27. {% set uri = global.grav.uri %}
  28. {% set files = global.files %}
  29. {% set config = global.grav.config %}
  30. {% set route = global.context.route() %}
  31. {% set type = global.context.content() is not null ? 'pages' : global.plugin ? 'plugins' : global.theme ? 'themes' : 'config' %}
  32. {% set blueprint_name = global.blueprints.getFilename %}
  33. {% if type == 'pages' %}
  34. {% set blueprint_name = type ~ '/' ~ blueprint_name %}
  35. {% endif %}
  36. {% set blueprint = base64_encode(blueprint_name) %}
  37. {% set real_path = global.form.getPagePathFromToken(path) %}
  38. {% set remove = uri.addNonce(global.base_url_relative ~
  39. '/media.json' ~
  40. '/route' ~ config.system.param_sep ~ base64_encode(global.base_path ~ '/' ~ real_path) ~
  41. '/task' ~ config.system.param_sep ~ 'removeFileFromBlueprint' ~
  42. '/proute' ~ config.system.param_sep ~ base64_encode(route) ~
  43. '/blueprint' ~ config.system.param_sep ~ blueprint ~
  44. '/type' ~ config.system.param_sep ~ type ~
  45. '/field' ~ config.system.param_sep ~ files.name ~
  46. '/path' ~ config.system.param_sep ~ base64_encode(value.path), 'admin-form', 'admin-nonce') %}
  47. {% set file = value|merge({remove: remove, path: (uri.rootUrl == '/' ? '/' : uri.rootUrl ~ '/' ~ real_path) }) %}
  48. <div class="hidden" data-file="{{ file|json_encode|e('html_attr') }}"></div>
  49. {% endif %}
  50. {% endmacro %}
  51. {% block input %}
  52. {% set page_can_upload = exists or (type == 'page' and not exists and not (field.destination starts with '@self' or field.destination starts with 'self@')) %}
  53. {% set settings = {name: field.name, paramName: (scope ~ field.name)|fieldName ~ (files.multiple ? '[]' : ''), limit: limit, filesize: form_max_filesize, accept: files.accept, resolution: files.resolution, resizeWidth: files.resizeWidth, resizeHeight: files.resizeHeight, resizeQuality: files.resizeQuality } %}
  54. {% set dropzoneSettings = field.dropzone %}
  55. <div class="{{ form_field_wrapper_classes ?: 'form-input-wrapper' }} dropzone files-upload form-input-file {{ field.size }}" data-grav-file-settings="{{ settings|json_encode|e('html_attr') }}" data-dropzone-options="{{ dropzoneSettings|json_encode|e('html_attr') }}">
  56. <input
  57. {# required attribute structures #}
  58. {% block input_attributes %}
  59. type="file"
  60. {% if files.multiple %}multiple="multiple"{% endif %}
  61. {% if files.accept %}accept="{{ files.accept|join(',') }}"{% endif %}
  62. {% if field.disabled %}disabled="disabled"{% endif %}
  63. {% if field.random_name %}random="true"{% endif %}
  64. {% if required %}required="required"{% endif %}
  65. {{ parent() }}
  66. {% endblock %}
  67. />
  68. {% for path, file in value %}
  69. {{ _self.preview(path, file, _context) }}
  70. {% endfor %}
  71. {% include 'forms/fields/hidden/hidden.html.twig' with {field: {name: '_json.' ~ field.name}, value:value|json_encode|e('html_attr')} %}
  72. </div>
  73. {% do assets.addJs('jquery', 101) %}
  74. {% do assets.addJs('plugin://form/assets/form.min.js', { 'group': 'bottom', 'loading': 'defer' }) %}
  75. {% do assets.addCss('plugin://form/assets/dropzone.min.css', { 'group': 'form' }) %}
  76. {{ assets.css('form')|raw }}
  77. {% do assets.addInlineJs("
  78. window.GravForm = window.GravForm || {};
  79. window.GravForm.config = {
  80. current_url: '" ~ uri.route(true) ~"',
  81. base_url_relative: '" ~ base_url_relative ~ "',
  82. param_sep: '"~ config.system.param_sep ~ "',
  83. form_nonce: '" ~ form.getNonce ~ "',
  84. };
  85. window.GravForm.translations = {};
  86. window.GravForm.translations['PLUGIN_FORM'] = {
  87. 'DROPZONE_CANCEL_UPLOAD': " ~ 'PLUGIN_FORM.DROPZONE_CANCEL_UPLOAD'|t|json_encode ~ ",
  88. 'DROPZONE_CANCEL_UPLOAD_CONFIRMATION': " ~ 'PLUGIN_FORM.DROPZONE_CANCEL_UPLOAD_CONFIRMATION'|t|json_encode ~ ",
  89. 'DROPZONE_DEFAULT_MESSAGE': " ~ 'PLUGIN_FORM.DROPZONE_DEFAULT_MESSAGE'|t|json_encode ~ ",
  90. 'DROPZONE_FALLBACK_MESSAGE': " ~ 'PLUGIN_FORM.DROPZONE_FALLBACK_MESSAGE'|t|json_encode ~ ",
  91. 'DROPZONE_FALLBACK_TEXT': " ~ 'PLUGIN_FORM.DROPZONE_FALLBACK_TEXT'|t|json_encode ~ ",
  92. 'DROPZONE_FILE_TOO_BIG': " ~ 'PLUGIN_FORM.DROPZONE_FILE_TOO_BIG'|t|json_encode ~ ",
  93. 'DROPZONE_INVALID_FILE_TYPE': " ~ 'PLUGIN_FORM.DROPZONE_INVALID_FILE_TYPE'|t|json_encode ~ ",
  94. 'DROPZONE_MAX_FILES_EXCEEDED': " ~ 'PLUGIN_FORM.DROPZONE_MAX_FILES_EXCEEDED'|t|json_encode ~ ",
  95. 'DROPZONE_REMOVE_FILE': " ~ 'PLUGIN_FORM.DROPZONE_REMOVE_FILE'|t|json_encode ~ ",
  96. 'DROPZONE_REMOVE_FILE_CONFIRMATION': " ~ 'PLUGIN_FORM.DROPZONE_REMOVE_FILE_CONFIRMATION'|t|json_encode ~ ",
  97. 'DROPZONE_RESPONSE_ERROR': " ~ 'PLUGIN_FORM.DROPZONE_RESPONSE_ERROR'|t|json_encode ~ ",
  98. 'RESOLUTION_MIN': " ~ 'PLUGIN_FORM.RESOLUTION_MIN'|t|json_encode ~ ",
  99. 'RESOLUTION_MAX': " ~ 'PLUGIN_FORM.RESOLUTION_MAX'|t|json_encode ~ "
  100. };
  101. ", { 'group': 'bottom' }) %}
  102. {% endblock %}