| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
abstract class sfWidgetForm extends sfWidget |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Constructor. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * id_format: The format for the generated HTML id attributes (%s by default) |
|---|
| 27 |
* * is_hidden: true if the form widget must be hidden, false otherwise (false by default) |
|---|
| 28 |
* * needs_multipart: true if the form widget needs a multipart form, false otherwise (false by default) |
|---|
| 29 |
* |
|---|
| 30 |
* @param array $options An array of options |
|---|
| 31 |
* @param array $attributes An array of default HTML attributes |
|---|
| 32 |
* |
|---|
| 33 |
* @see sfWidget |
|---|
| 34 |
*/ |
|---|
| 35 |
public function __construct($options = array(), $attributes = array()) |
|---|
| 36 |
{ |
|---|
| 37 |
$this->addOption('id_format', '%s'); |
|---|
| 38 |
$this->addOption('is_hidden', false); |
|---|
| 39 |
$this->addOption('needs_multipart', false); |
|---|
| 40 |
|
|---|
| 41 |
parent::__construct($options, $attributes); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* Sets the format for HTML id attributes. |
|---|
| 46 |
* |
|---|
| 47 |
* @param string $format The format string (must contain a %s for the id placeholder) |
|---|
| 48 |
*/ |
|---|
| 49 |
public function setIdFormat($format) |
|---|
| 50 |
{ |
|---|
| 51 |
$this->setOption('id_format', $format); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
* Gets the HTML format string for id attributes. |
|---|
| 56 |
* |
|---|
| 57 |
* @return string The format string |
|---|
| 58 |
*/ |
|---|
| 59 |
public function getIdFormat() |
|---|
| 60 |
{ |
|---|
| 61 |
return $this->getOption('id_format'); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
* Returns true if the widget is hidden. |
|---|
| 66 |
* |
|---|
| 67 |
* @return Boolean true if the widget is hidden, false otherwise |
|---|
| 68 |
*/ |
|---|
| 69 |
public function isHidden() |
|---|
| 70 |
{ |
|---|
| 71 |
return $this->getOption('is_hidden'); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
* Sets the hidden flag for the widget. |
|---|
| 76 |
* |
|---|
| 77 |
* @param bool $boolean true if the widget must be hidden, false otherwise |
|---|
| 78 |
*/ |
|---|
| 79 |
public function setHidden($boolean) |
|---|
| 80 |
{ |
|---|
| 81 |
$this->setOption('is_hidden', (boolean) $boolean); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* Returns true if the widget needs a multipart form. |
|---|
| 86 |
* |
|---|
| 87 |
* @return bool true if the widget needs a multipart form, false otherwise |
|---|
| 88 |
*/ |
|---|
| 89 |
public function needsMultipartForm() |
|---|
| 90 |
{ |
|---|
| 91 |
return $this->getOption('needs_multipart'); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
* Renders a HTML tag. |
|---|
| 96 |
* |
|---|
| 97 |
* The id attribute is added automatically to the array of attributes if none is specified. |
|---|
| 98 |
* If uses for "id_format" option to generate the id. |
|---|
| 99 |
* |
|---|
| 100 |
* @param string The tag name |
|---|
| 101 |
* @param array An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 102 |
* |
|---|
| 103 |
* @return string An HTML tag string |
|---|
| 104 |
*/ |
|---|
| 105 |
public function renderTag($tag, $attributes = array()) |
|---|
| 106 |
{ |
|---|
| 107 |
if (empty($tag)) |
|---|
| 108 |
{ |
|---|
| 109 |
return ''; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
$attributes = $this->fixFormId($attributes); |
|---|
| 113 |
|
|---|
| 114 |
return sprintf('<%s%s%s', $tag, $this->attributesToHtml($attributes), self::$xhtml ? ' />' : (strtolower($tag) == 'input' ? '>' : sprintf('></%s>', $tag))); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
* Renders a HTML content tag. |
|---|
| 119 |
* |
|---|
| 120 |
* The id attribute is added automatically to the array of attributes if none is specified. |
|---|
| 121 |
* If uses for "id_format" option to generate the id. |
|---|
| 122 |
* |
|---|
| 123 |
* @param string $tag The tag name |
|---|
| 124 |
* @param string $content The content of the tag |
|---|
| 125 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 126 |
* |
|---|
| 127 |
* @return string An HTML tag string |
|---|
| 128 |
*/ |
|---|
| 129 |
public function renderContentTag($tag, $content = null, $attributes = array()) |
|---|
| 130 |
{ |
|---|
| 131 |
return parent::renderContentTag($tag, $content, $this->fixFormId($attributes)); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
* Adds an HTML id attributes to the array of attributes if none is given and a name attribute exists. |
|---|
| 136 |
* |
|---|
| 137 |
* @param array $attributes An array of attributes |
|---|
| 138 |
* |
|---|
| 139 |
* @return array An array of attributes with an id. |
|---|
| 140 |
*/ |
|---|
| 141 |
protected function fixFormId($attributes) |
|---|
| 142 |
{ |
|---|
| 143 |
if (!isset($attributes['id']) && isset($attributes['name'])) |
|---|
| 144 |
{ |
|---|
| 145 |
$attributes['id'] = $this->generateId($attributes['name'], isset($attributes['value']) ? $attributes['value'] : null); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
return $attributes; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
* Returns a formatted id based on the field name and optionally on the field value. |
|---|
| 153 |
* |
|---|
| 154 |
* This function determines the proper form field id name based on the parameters. If a form field has an |
|---|
| 155 |
* array value as a name we need to convert them to proper and unique ids like so: |
|---|
| 156 |
* |
|---|
| 157 |
* <samp> |
|---|
| 158 |
* name[] => name (if value == null) |
|---|
| 159 |
* name[] => name_value (if value != null) |
|---|
| 160 |
* name[bob] => name_bob |
|---|
| 161 |
* name[item][total] => name_item_total |
|---|
| 162 |
* </samp> |
|---|
| 163 |
* |
|---|
| 164 |
* @param string $name The field name |
|---|
| 165 |
* @param string $value The field value |
|---|
| 166 |
* |
|---|
| 167 |
* @return string The field id or null. |
|---|
| 168 |
*/ |
|---|
| 169 |
public function generateId($name, $value = null) |
|---|
| 170 |
{ |
|---|
| 171 |
if (false === $this->getOption('id_format')) |
|---|
| 172 |
{ |
|---|
| 173 |
return null; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
if (strstr($name, '[')) |
|---|
| 178 |
{ |
|---|
| 179 |
$name = str_replace(array('[]', '][', '[', ']'), array((!is_null($value) ? '_'.$value : ''), '_', '_', ''), $name); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
if (false !== strpos($this->getOption('id_format'), '%s')) |
|---|
| 183 |
{ |
|---|
| 184 |
return sprintf($this->getOption('id_format'), $name); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
return $name; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
* Generates a two chars range |
|---|
| 192 |
* |
|---|
| 193 |
* @param int $start |
|---|
| 194 |
* @param int $stop |
|---|
| 195 |
* @return array |
|---|
| 196 |
*/ |
|---|
| 197 |
static protected function generateTwoCharsRange($start, $stop) |
|---|
| 198 |
{ |
|---|
| 199 |
$results = array(); |
|---|
| 200 |
for ($i = $start; $i <= $stop; $i++) |
|---|
| 201 |
{ |
|---|
| 202 |
$results[$i] = sprintf('%02d', $i); |
|---|
| 203 |
} |
|---|
| 204 |
return $results; |
|---|
| 205 |
} |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|