FileWidget.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Widget;
  16. use Cake\View\Form\ContextInterface;
  17. /**
  18. * Input widget class for generating a file upload control.
  19. *
  20. * This class is intended as an internal implementation detail
  21. * of Cake\View\Helper\FormHelper and is not intended for direct use.
  22. */
  23. class FileWidget implements WidgetInterface
  24. {
  25. /**
  26. * Constructor
  27. *
  28. * @param \Cake\View\StringTemplate $templates Templates list.
  29. */
  30. public function __construct($templates)
  31. {
  32. $this->_templates = $templates;
  33. }
  34. /**
  35. * Render a file upload form widget.
  36. *
  37. * Data supports the following keys:
  38. *
  39. * - `name` - Set the input name.
  40. * - `escape` - Set to false to disable HTML escaping.
  41. *
  42. * All other keys will be converted into HTML attributes.
  43. * Unlike other input objects the `val` property will be specifically
  44. * ignored.
  45. *
  46. * @param array $data The data to build a file input with.
  47. * @param \Cake\View\Form\ContextInterface $context The current form context.
  48. * @return string HTML elements.
  49. */
  50. public function render(array $data, ContextInterface $context)
  51. {
  52. $data += [
  53. 'name' => '',
  54. 'escape' => true,
  55. 'templateVars' => [],
  56. ];
  57. unset($data['val']);
  58. return $this->_templates->format('file', [
  59. 'name' => $data['name'],
  60. 'templateVars' => $data['templateVars'],
  61. 'attrs' => $this->_templates->formatAttributes(
  62. $data,
  63. ['name']
  64. )
  65. ]);
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function secureFields(array $data)
  71. {
  72. $fields = [];
  73. foreach (['name', 'type', 'tmp_name', 'error', 'size'] as $suffix) {
  74. $fields[] = $data['name'] . '[' . $suffix . ']';
  75. }
  76. return $fields;
  77. }
  78. }