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