| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* sfWidgetFormInputFileEditable represents an upload HTML input tag with the possibility
|
|
|
13 |
* to remove a previously uploaded file.
|
|
|
14 |
*
|
|
|
15 |
* @package symfony
|
|
|
16 |
* @subpackage widget
|
|
|
17 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
18 |
* @version SVN: $Id: sfWidgetFormInputFileEditable.class.php 30762 2010-08-25 12:33:33Z fabien $
|
|
|
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 when in edit mode
|
|
|
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 |
* Renders the widget.
|
|
|
65 |
*
|
|
|
66 |
* @param string $name The element name
|
|
|
67 |
* @param string $value The value displayed in this widget
|
|
|
68 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
|
|
|
69 |
* @param array $errors An array of errors for the field
|
|
|
70 |
*
|
|
|
71 |
* @return string An HTML tag string
|
|
|
72 |
*
|
|
|
73 |
* @see sfWidgetForm
|
|
|
74 |
*/
|
|
|
75 |
public function render($name, $value = null, $attributes = array(), $errors = array())
|
|
|
76 |
{
|
|
|
77 |
$input = parent::render($name, $value, $attributes, $errors);
|
|
|
78 |
|
|
|
79 |
if (!$this->getOption('edit_mode'))
|
|
|
80 |
{
|
|
|
81 |
return $input;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if ($this->getOption('with_delete'))
|
|
|
85 |
{
|
|
|
86 |
$deleteName = ']' == substr($name, -1) ? substr($name, 0, -1).'_delete]' : $name.'_delete';
|
|
|
87 |
|
|
|
88 |
$delete = $this->renderTag('input', array_merge(array('type' => 'checkbox', 'name' => $deleteName), $attributes));
|
|
|
89 |
$deleteLabel = $this->translate($this->getOption('delete_label'));
|
|
|
90 |
$deleteLabel = $this->renderContentTag('label', $deleteLabel, array_merge(array('for' => $this->generateId($deleteName))));
|
|
|
91 |
}
|
|
|
92 |
else
|
|
|
93 |
{
|
|
|
94 |
$delete = '';
|
|
|
95 |
$deleteLabel = '';
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return strtr($this->getOption('template'), array('%input%' => $input, '%delete%' => $delete, '%delete_label%' => $deleteLabel, '%file%' => $this->getFileAsTag($attributes)));
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
protected function getFileAsTag($attributes)
|
|
|
102 |
{
|
|
|
103 |
if ($this->getOption('is_image'))
|
|
|
104 |
{
|
|
|
105 |
return false !== $this->getOption('file_src') ? $this->renderTag('img', array_merge(array('src' => $this->getOption('file_src')), $attributes)) : '';
|
|
|
106 |
}
|
|
|
107 |
else
|
|
|
108 |
{
|
|
|
109 |
return $this->getOption('file_src');
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
}
|