file.html.twig 6.7 KB

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