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

Debug toolbar