| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
require_once dirname(__FILE__).'/FormHelper.php'; |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
function object_input_date_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 34 |
{ |
|---|
| 35 |
$options = _parse_attributes($options); |
|---|
| 36 |
|
|---|
| 37 |
$value = _get_object_value($object, $method, $default_value, $param = 'Y-m-d G:i'); |
|---|
| 38 |
|
|---|
| 39 |
return input_date_tag(_convert_method_to_name($method, $options), $value, $options); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
function object_textarea_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 54 |
{ |
|---|
| 55 |
$options = _parse_attributes($options); |
|---|
| 56 |
|
|---|
| 57 |
$value = _get_object_value($object, $method, $default_value); |
|---|
| 58 |
|
|---|
| 59 |
return textarea_tag(_convert_method_to_name($method, $options), $value, $options); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
function objects_for_select($options, $value_method, $text_method = null, $selected = null, $html_options = array()) |
|---|
| 70 |
{ |
|---|
| 71 |
$select_options = array(); |
|---|
| 72 |
foreach ($options as $option) |
|---|
| 73 |
{ |
|---|
| 74 |
|
|---|
| 75 |
$method_exists = ($text_method == '__toString') ? method_exists($option, $text_method) : is_callable(array($option, $text_method)); |
|---|
| 76 |
if ($text_method && !$method_exists) |
|---|
| 77 |
{ |
|---|
| 78 |
throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $text_method, _get_class_decorated($option))); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
$method_exists = ($value_method == '__toString') ? method_exists($option, $value_method) : is_callable(array($option, $value_method)); |
|---|
| 83 |
if (!$method_exists) |
|---|
| 84 |
{ |
|---|
| 85 |
throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $value_method, _get_class_decorated($option))); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
$value = $option->$value_method(); |
|---|
| 89 |
$key = ($text_method != null) ? $option->$text_method() : $value; |
|---|
| 90 |
|
|---|
| 91 |
$select_options[$value] = $key; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
return options_for_select($select_options, $selected, $html_options); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
function object_select_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 109 |
{ |
|---|
| 110 |
$options = _parse_attributes($options); |
|---|
| 111 |
|
|---|
| 112 |
$related_class = _get_option($options, 'related_class', false); |
|---|
| 113 |
if (false === $related_class && preg_match('/^get(.+?)Id$/', $method, $match)) |
|---|
| 114 |
{ |
|---|
| 115 |
$related_class = $match[1]; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
$peer_method = _get_option($options, 'peer_method'); |
|---|
| 119 |
|
|---|
| 120 |
$text_method = _get_option($options, 'text_method'); |
|---|
| 121 |
|
|---|
| 122 |
$key_method = _get_option($options, 'key_method', 'getPrimaryKey'); |
|---|
| 123 |
|
|---|
| 124 |
$select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, $peer_method), $text_method, $key_method); |
|---|
| 125 |
|
|---|
| 126 |
if ($value = _get_option($options, 'include_custom')) |
|---|
| 127 |
{ |
|---|
| 128 |
$select_options = array('' => $value) + $select_options; |
|---|
| 129 |
} |
|---|
| 130 |
else if (_get_option($options, 'include_title')) |
|---|
| 131 |
{ |
|---|
| 132 |
$select_options = array('' => '-- '._convert_method_to_name($method, $options).' --') + $select_options; |
|---|
| 133 |
} |
|---|
| 134 |
else if (_get_option($options, 'include_blank')) |
|---|
| 135 |
{ |
|---|
| 136 |
$select_options = array('' => '') + $select_options; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
if (is_object($object)) |
|---|
| 140 |
{ |
|---|
| 141 |
$value = _get_object_value($object, $method, $default_value); |
|---|
| 142 |
} |
|---|
| 143 |
else |
|---|
| 144 |
{ |
|---|
| 145 |
$value = $object; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
$option_tags = options_for_select($select_options, $value, $options); |
|---|
| 149 |
|
|---|
| 150 |
return select_tag(_convert_method_to_name($method, $options), $option_tags, $options); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
function _get_options_from_objects($objects, $text_method = null, $key_method = 'getPrimaryKey') |
|---|
| 154 |
{ |
|---|
| 155 |
$select_options = array(); |
|---|
| 156 |
|
|---|
| 157 |
if ($objects) |
|---|
| 158 |
{ |
|---|
| 159 |
|
|---|
| 160 |
$first = true; |
|---|
| 161 |
foreach ($objects as $tmp_object) |
|---|
| 162 |
{ |
|---|
| 163 |
if ($first) |
|---|
| 164 |
{ |
|---|
| 165 |
|
|---|
| 166 |
$multi_primary_keys = is_array($tmp_object->$key_method()) ? true : false; |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
$methodToCall = ''; |
|---|
| 170 |
foreach (array($text_method, '__toString', 'toString', $key_method) as $method) |
|---|
| 171 |
{ |
|---|
| 172 |
if (method_exists($tmp_object, $method)) |
|---|
| 173 |
{ |
|---|
| 174 |
$methodToCall = $method; |
|---|
| 175 |
break; |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
$first = false; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
$key = $multi_primary_keys ? implode('/', $tmp_object->$key_method()) : $tmp_object->$key_method(); |
|---|
| 183 |
$value = $tmp_object->$methodToCall(); |
|---|
| 184 |
|
|---|
| 185 |
$select_options[$key] = $value; |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
return $select_options; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
function object_select_country_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 193 |
{ |
|---|
| 194 |
$options = _parse_attributes($options); |
|---|
| 195 |
|
|---|
| 196 |
$value = _get_object_value($object, $method, $default_value); |
|---|
| 197 |
|
|---|
| 198 |
return select_country_tag(_convert_method_to_name($method, $options), $value, $options); |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
function object_select_language_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 202 |
{ |
|---|
| 203 |
$options = _parse_attributes($options); |
|---|
| 204 |
|
|---|
| 205 |
$value = _get_object_value($object, $method, $default_value); |
|---|
| 206 |
|
|---|
| 207 |
return select_language_tag(_convert_method_to_name($method, $options), $value, $options); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
function object_input_hidden_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 222 |
{ |
|---|
| 223 |
$options = _parse_attributes($options); |
|---|
| 224 |
|
|---|
| 225 |
$value = _get_object_value($object, $method, $default_value); |
|---|
| 226 |
|
|---|
| 227 |
return input_hidden_tag(_convert_method_to_name($method, $options), $value, $options); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
function object_input_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 242 |
{ |
|---|
| 243 |
$options = _parse_attributes($options); |
|---|
| 244 |
|
|---|
| 245 |
$value = _get_object_value($object, $method, $default_value); |
|---|
| 246 |
|
|---|
| 247 |
return input_tag(_convert_method_to_name($method, $options), $value, $options); |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
function object_checkbox_tag($object, $method, $options = array(), $default_value = null) |
|---|
| 262 |
{ |
|---|
| 263 |
$options = _parse_attributes($options); |
|---|
| 264 |
|
|---|
| 265 |
$checked = (boolean) _get_object_value($object, $method, $default_value); |
|---|
| 266 |
|
|---|
| 267 |
return checkbox_tag(_convert_method_to_name($method, $options), isset($options['value']) ? $options['value'] : 1, $checked, $options); |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
function _convert_method_to_name($method, &$options) |
|---|
| 271 |
{ |
|---|
| 272 |
$name = _get_option($options, 'control_name'); |
|---|
| 273 |
|
|---|
| 274 |
if (!$name) |
|---|
| 275 |
{ |
|---|
| 276 |
if (is_array($method)) |
|---|
| 277 |
{ |
|---|
| 278 |
$name = implode('-',$method[1]); |
|---|
| 279 |
} |
|---|
| 280 |
else |
|---|
| 281 |
{ |
|---|
| 282 |
$name = sfInflector::underscore($method); |
|---|
| 283 |
$name = preg_replace('/^get_?/', '', $name); |
|---|
| 284 |
} |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
return $name; |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
function _get_object_value($object, $method, $default_value = null, $param = null) |
|---|
| 293 |
{ |
|---|
| 294 |
|
|---|
| 295 |
if (is_string($method)) |
|---|
| 296 |
{ |
|---|
| 297 |
$param = ($param == null ? array() : array($param)); |
|---|
| 298 |
$method = array($method, $param); |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
if (!is_callable(array($object, $method[0]))) |
|---|
| 303 |
{ |
|---|
| 304 |
throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $method[0], _get_class_decorated($object))); |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
$object_value = call_user_func_array(array($object, $method[0]), $method[1]); |
|---|
| 308 |
|
|---|
| 309 |
return ($default_value !== null && $object_value === null) ? $default_value : $object_value; |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
function _get_class_decorated($object) |
|---|
| 320 |
{ |
|---|
| 321 |
if ($object instanceof sfOutputEscaperObjectDecorator) |
|---|
| 322 |
{ |
|---|
| 323 |
return sprintf('%s (decorated with %s)', get_class($object->getRawValue()), get_class($object)); |
|---|
| 324 |
} |
|---|
| 325 |
else |
|---|
| 326 |
{ |
|---|
| 327 |
return get_class($object); |
|---|
| 328 |
} |
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
|
|---|