| [ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 use_helper('Form'); 4 5 /* 6 * This file is part of the symfony package. 7 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 8 * 9 * For the full copyright and license information, please view the LICENSE 10 * file that was distributed with this source code. 11 */ 12 13 /** 14 * ObjectHelper. 15 * 16 * @package symfony 17 * @subpackage helper 18 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 19 * @version SVN: $Id: ObjectHelper.php 3294 2007-01-16 06:53:15Z fabien $ 20 */ 21 22 /** 23 * Returns a html date control. 24 * 25 * @param object An object. 26 * @param string An object column. 27 * @param array Date options. 28 * @param bool Date default value. 29 * 30 * @return string An html string which represents a date control. 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 * Returns a textarea html tag. 44 * 45 * @param object An object. 46 * @param string An object column. 47 * @param array Textarea options. 48 * @param bool Textarea default value. 49 * 50 * @return string An html string which represents a textarea tag. 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 * Accepts a container of objects, the method name to use for the value, 64 * and the method name to use for the display. 65 * It returns a string of option tags. 66 * 67 * NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag. 68 */ 69 function objects_for_select($options = array(), $value_method, $text_method = null, $selected = null, $html_options = array()) 70 { 71 $select_options = array(); 72 foreach ($options as $option) 73 { 74 // text method exists? 75 if ($text_method && !is_callable(array($option, $text_method))) 76 { 77 $error = sprintf('Method "%s" doesn\'t exist for object of class "%s"', $text_method, _get_class_decorated($option)); 78 throw new sfViewException($error); 79 } 80 81 // value method exists? 82 if (!is_callable(array($option, $value_method))) 83 { 84 $error = sprintf('Method "%s" doesn\'t exist for object of class "%s"', $value_method, _get_class_decorated($option)); 85 throw new sfViewException($error); 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 * Returns a list html tag. 99 * 100 * @param object An object or the selected value 101 * @param string An object column. 102 * @param array Input options (related_class option is mandatory). 103 * @param bool Input default value. 104 * 105 * @return string A list string which represents an input tag. 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 $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, $peer_method), $text_method); 123 124 if ($value = _get_option($options, 'include_custom')) 125 { 126 $select_options = array('' => $value) + $select_options; 127 } 128 else if (_get_option($options, 'include_title')) 129 { 130 $select_options = array('' => '-- '._convert_method_to_name($method, $options).' --') + $select_options; 131 } 132 else if (_get_option($options, 'include_blank')) 133 { 134 $select_options = array('' => '') + $select_options; 135 } 136 137 if (is_object($object)) 138 { 139 $value = _get_object_value($object, $method, $default_value); 140 } 141 else 142 { 143 $value = $object; 144 } 145 146 $option_tags = options_for_select($select_options, $value, $options); 147 148 return select_tag(_convert_method_to_name($method, $options), $option_tags, $options); 149 } 150 151 function _get_options_from_objects($objects, $text_method = null) 152 { 153 $select_options = array(); 154 155 if ($objects) 156 { 157 // multi primary keys handling 158 $multi_primary_keys = is_array($objects[0]->getPrimaryKey()) ? true : false; 159 160 // which method to call? 161 $methodToCall = ''; 162 foreach (array($text_method, '__toString', 'toString', 'getPrimaryKey') as $method) 163 { 164 if (is_callable(array($objects[0], $method))) 165 { 166 $methodToCall = $method; 167 break; 168 } 169 } 170 171 // construct select option list 172 foreach ($objects as $tmp_object) 173 { 174 $key = $multi_primary_keys ? implode('/', $tmp_object->getPrimaryKey()) : $tmp_object->getPrimaryKey(); 175 $value = $tmp_object->$methodToCall(); 176 177 $select_options[$key] = $value; 178 } 179 } 180 181 return $select_options; 182 } 183 184 function object_select_country_tag($object, $method, $options = array(), $default_value = null) 185 { 186 $options = _parse_attributes($options); 187 188 $value = _get_object_value($object, $method, $default_value); 189 190 return select_country_tag(_convert_method_to_name($method, $options), $value, $options); 191 } 192 193 function object_select_language_tag($object, $method, $options = array(), $default_value = null) 194 { 195 $options = _parse_attributes($options); 196 197 $value = _get_object_value($object, $method, $default_value); 198 199 return select_language_tag(_convert_method_to_name($method, $options), $value, $options); 200 } 201 202 /** 203 * Returns a hidden input html tag. 204 * 205 * @param object An object. 206 * @param string An object column. 207 * @param array Input options. 208 * @param bool Input default value. 209 * 210 * @return string An html string which represents a hidden input tag. 211 * 212 */ 213 function object_input_hidden_tag($object, $method, $options = array(), $default_value = null) 214 { 215 $options = _parse_attributes($options); 216 217 $value = _get_object_value($object, $method, $default_value); 218 219 return input_hidden_tag(_convert_method_to_name($method, $options), $value, $options); 220 } 221 222 /** 223 * Returns a input html tag. 224 * 225 * @param object An object. 226 * @param string An object column. 227 * @param array Input options. 228 * @param bool Input default value. 229 * 230 * @return string An html string which represents an input tag. 231 * 232 */ 233 function object_input_tag($object, $method, $options = array(), $default_value = null) 234 { 235 $options = _parse_attributes($options); 236 237 $value = _get_object_value($object, $method, $default_value); 238 239 return input_tag(_convert_method_to_name($method, $options), $value, $options); 240 } 241 242 /** 243 * Returns a checkbox html tag. 244 * 245 * @param object An object. 246 * @param string An object column. 247 * @param array Checkbox options. 248 * @param bool Checkbox value. 249 * 250 * @return string An html string which represents a checkbox tag. 251 * 252 */ 253 function object_checkbox_tag($object, $method, $options = array(), $default_value = null) 254 { 255 $options = _parse_attributes($options); 256 257 $checked = (boolean) _get_object_value($object, $method, $default_value); 258 259 return checkbox_tag(_convert_method_to_name($method, $options), isset($options['value']) ? $options['value'] : 1, $checked, $options); 260 } 261 262 function _convert_method_to_name($method, &$options) 263 { 264 $name = _get_option($options, 'control_name'); 265 266 if (!$name) 267 { 268 if (is_array($method)) 269 { 270 $name = implode('-',$method[1]); 271 } 272 else 273 { 274 $name = sfInflector::underscore($method); 275 $name = preg_replace('/^get_?/', '', $name); 276 } 277 } 278 279 return $name; 280 } 281 282 // returns default_value if object value is null 283 // method is either a string or: array('method',array('param1','param2')) 284 function _get_object_value($object, $method, $default_value = null, $param = null) 285 { 286 // compatibility with the array syntax 287 if (is_string($method)) 288 { 289 $param = ($param == null ? array() : array($param)); 290 $method = array($method, $param); 291 } 292 293 // method exists? 294 if (!is_callable(array($object, $method[0]))) 295 { 296 $error = 'Method "%s" doesn\'t exist for object of class "%s"'; 297 $error = sprintf($error, $method[0], _get_class_decorated($object)); 298 299 throw new sfViewException($error); 300 } 301 302 $object_value = call_user_func_array(array($object, $method[0]), $method[1]); 303 304 return ($default_value !== null && $object_value === null) ? $default_value : $object_value; 305 } 306 307 /** 308 * Returns the name of the class of an decorated object 309 * 310 * @param object An object that might be wrapped in an sfOutputEscaperObjectDecorator(-derivative) 311 * 312 * @return string The name of the class of the object being decorated for escaping, or the class of the object if it isn't decorated 313 */ 314 function _get_class_decorated($object) 315 { 316 if ($object instanceof sfOutputEscaperObjectDecorator) 317 { 318 return sprintf('%s (decorated with %s)', get_class($object->getRawValue()), get_class($object)); 319 } 320 else 321 { 322 return get_class($object); 323 } 324 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |