sfWidgetFormInputFileEditable.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetFormInputFileEditable represents an upload HTML input tag with the possibility
  11. * to remove a previously uploaded file.
  12. *
  13. * @package symfony
  14. * @subpackage widget
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: sfWidgetFormInputFileEditable.class.php 11544 2008-09-14 17:40:07Z fabien $
  17. */
  18. class sfWidgetFormInputFileEditable extends sfWidgetFormInputFile
  19. {
  20. /**
  21. * Constructor.
  22. *
  23. * Available options:
  24. *
  25. * * file_src: The current image web source path (required)
  26. * * edit_mode: A Boolean: true to enabled edit mode, false otherwise
  27. * * is_image: Whether the file is a displayable image
  28. * * with_delete: Whether to add a delete checkbox or not
  29. * * delete_label: The delete label used by the template
  30. * * template: The HTML template to use to render this widget
  31. * The available placeholders are:
  32. * * input (the image upload widget)
  33. * * delete (the delete checkbox)
  34. * * delete_label (the delete label text)
  35. * * file (the file tag)
  36. *
  37. * In edit mode, this widget renders an additional widget named after the
  38. * file upload widget with a "_delete" suffix. So, when creating a form,
  39. * don't forget to add a validator for this additional field.
  40. *
  41. * @param array $options An array of options
  42. * @param array $attributes An array of default HTML attributes
  43. *
  44. * @see sfWidgetFormInputFile
  45. */
  46. protected function configure($options = array(), $attributes = array())
  47. {
  48. parent::configure($options, $attributes);
  49. $this->setOption('type', 'file');
  50. $this->setOption('needs_multipart', true);
  51. $this->addRequiredOption('file_src');
  52. $this->addOption('is_image', false);
  53. $this->addOption('edit_mode', true);
  54. $this->addOption('with_delete', true);
  55. $this->addOption('delete_label', 'remove the current file');
  56. $this->addOption('template', '%file%<br />%input%<br />%delete% %delete_label%');
  57. }
  58. /**
  59. * @param string $name The element name
  60. * @param string $value The value displayed in this widget
  61. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  62. * @param array $errors An array of errors for the field
  63. *
  64. * @return string An HTML tag string
  65. *
  66. * @see sfWidgetForm
  67. */
  68. public function render($name, $value = null, $attributes = array(), $errors = array())
  69. {
  70. $input = parent::render($name, $value, $attributes, $errors);
  71. if (!$this->getOption('edit_mode'))
  72. {
  73. return $input;
  74. }
  75. if ($this->getOption('with_delete'))
  76. {
  77. $deleteName = ']' == substr($name, -1) ? substr($name, 0, -1).'_delete]' : $name.'_delete';
  78. $delete = $this->renderTag('input', array_merge(array('type' => 'checkbox', 'name' => $deleteName), $attributes));
  79. $deleteLabel = $this->renderContentTag('label', $this->getOption('delete_label'), array_merge(array('for' => $this->generateId($deleteName))));
  80. }
  81. else
  82. {
  83. $delete = '';
  84. $deleteLabel = '';
  85. }
  86. return strtr($this->getOption('template'), array('%input%' => $input, '%delete%' => $delete, '%delete_label%' => $deleteLabel, '%file%' => $this->getFileAsTag($attributes)));
  87. }
  88. protected function getFileAsTag($attributes)
  89. {
  90. if ($this->getOption('is_image'))
  91. {
  92. return false !== $this->getOption('file_src') ? $this->renderTag('img', array_merge(array('src' => $this->getOption('file_src'))), $attributes) : '';
  93. }
  94. else
  95. {
  96. return $this->getOption('file_src');
  97. }
  98. }
  99. }