file.html.twig 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {% macro bytesToSize(bytes) -%}
  6. {% spaceless %}
  7. {% set kilobyte = 1024 %}
  8. {% set megabyte = kilobyte * 1024 %}
  9. {% set gigabyte = megabyte * 1024 %}
  10. {% set terabyte = gigabyte * 1024 %}
  11. {% if bytes < kilobyte %}
  12. {{ bytes ~ ' B' }}
  13. {% elseif bytes < megabyte %}
  14. {{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
  15. {% elseif bytes < gigabyte %}
  16. {{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
  17. {% elseif bytes < terabyte %}
  18. {{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
  19. {% else %}
  20. {{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
  21. {% endif %}
  22. {% endspaceless %}
  23. {%- endmacro %}
  24. {% macro preview(path, value, global) %}
  25. {% if value %}
  26. {% set uri = global.grav.uri %}
  27. {% set files = global.files %}
  28. {% set config = global.grav.config %}
  29. {% set route = global.context.route() %}
  30. {% set type = global.blueprint_type ? global.blueprint_type : global.admin.location ? global.admin.location : 'config' %}
  31. {% set blueprint_name = global.blueprints.getFilename %}
  32. {% if type == 'pages' %}
  33. {% set blueprint_name = type ~ '/' ~ blueprint_name %}
  34. {% endif %}
  35. {% set blueprint = base64_encode(blueprint_name) %}
  36. {% set real_path = global.admin.getPagePathFromToken(path) %}
  37. {% set remove = global.file_url_remove ? global.file_url_remove : (global.base_url_relative ~ '/media.json') %}
  38. {% set remove = uri.addNonce(
  39. remove ~
  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 upload_limit = config.system.media.upload_limit / 1024 / 1024 %}
  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. {% if type is not defined or page_can_upload %}
  55. {% set settings = {name: field.name, paramName: (scope ~ field.name)|fieldName ~ (files.multiple ? '[]' : ''), limit: limit, filesize: upload_limit, accept: files.accept, resolution: files.resolution, resizeWidth: files.resizeWidth, resizeHeight: files.resizeHeight, resizeQuality: files.resizeQuality } %}
  56. <div class="form-input-wrapper dropzone files-upload {% if field.fancy is not same as(false) %}form-input-file{% endif %} {{ field.size|default('xlarge') }}" data-grav-file-settings="{{ settings|json_encode|e('html_attr') }}" {% if file_url_add %}data-file-url-add="{{ file_url_add }}"{% endif %} {% if file_url_remove %}data-file-url-remove="{{ file_url_remove }}"{% endif %}>
  57. <input
  58. {# required attribute structures #}
  59. {% block input_attributes %}
  60. type="file"
  61. {% if files.multiple %}multiple="multiple"{% endif %}
  62. {% if files.accept %}accept="{{ files.accept|join(',') }}"{% endif %}
  63. {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
  64. {% if field.random_name %}random="true"{% 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|raw|json_encode} %}
  72. </div>
  73. {% else %}
  74. <span class="note">{{ "PLUGIN_ADMIN.CANNOT_ADD_FILES_PAGE_NOT_SAVED"|tu|raw }}</span>
  75. {% endif %}
  76. {% endblock %}