Development

/branches/1.0/lib/helper/ObjectHelper.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/helper/ObjectHelper.php

Revision 5149, 9.1 kB (checked in by fabien, 7 years ago)

fixed bug in _get_options_from_objects (closes #1526)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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$
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     // construct select option list
158     $first = true;
159     foreach ($objects as $tmp_object)
160     {
161       if ($first)
162       {
163         // multi primary keys handling
164         $multi_primary_keys = is_array($tmp_object->getPrimaryKey()) ? true : false;
165
166         // which method to call?
167         $methodToCall = '';
168         foreach (array($text_method, '__toString', 'toString', 'getPrimaryKey') as $method)
169         {
170           if (is_callable(array($tmp_object, $method)))
171           {
172             $methodToCall = $method;
173             break;
174           }
175         }
176
177         $first = false;
178       }
179
180       $key   = $multi_primary_keys ? implode('/', $tmp_object->getPrimaryKey()) : $tmp_object->getPrimaryKey();
181       $value = $tmp_object->$methodToCall();
182
183       $select_options[$key] = $value;
184     }
185   }
186
187   return $select_options;
188 }
189
190 function object_select_country_tag($object, $method, $options = array(), $default_value = null)
191 {
192   $options = _parse_attributes($options);
193
194   $value = _get_object_value($object, $method, $default_value);
195
196   return select_country_tag(_convert_method_to_name($method, $options), $value, $options);
197 }
198
199 function object_select_language_tag($object, $method, $options = array(), $default_value = null)
200 {
201   $options = _parse_attributes($options);
202
203   $value = _get_object_value($object, $method, $default_value);
204
205   return select_language_tag(_convert_method_to_name($method, $options), $value, $options);
206 }
207
208 /**
209  * Returns a hidden input html tag.
210  *
211  * @param object An object.
212  * @param string An object column.
213  * @param array Input options.
214  * @param bool Input default value.
215  *
216  * @return string An html string which represents a hidden input tag.
217  *
218  */
219 function object_input_hidden_tag($object, $method, $options = array(), $default_value = null)
220 {
221   $options = _parse_attributes($options);
222
223   $value = _get_object_value($object, $method, $default_value);
224
225   return input_hidden_tag(_convert_method_to_name($method, $options), $value, $options);
226 }
227
228 /**
229  * Returns a input html tag.
230  *
231  * @param object An object.
232  * @param string An object column.
233  * @param array Input options.
234  * @param bool Input default value.
235  *
236  * @return string An html string which represents an input tag.
237  *
238  */
239 function object_input_tag($object, $method, $options = array(), $default_value = null)
240 {
241   $options = _parse_attributes($options);
242
243   $value = _get_object_value($object, $method, $default_value);
244
245   return input_tag(_convert_method_to_name($method, $options), $value, $options);
246 }
247
248 /**
249  * Returns a checkbox html tag.
250  *
251  * @param object An object.
252  * @param string An object column.
253  * @param array Checkbox options.
254  * @param bool Checkbox value.
255  *
256  * @return string An html string which represents a checkbox tag.
257  *
258  */
259 function object_checkbox_tag($object, $method, $options = array(), $default_value = null)
260 {
261   $options = _parse_attributes($options);
262
263   $checked = (boolean) _get_object_value($object, $method, $default_value);
264
265   return checkbox_tag(_convert_method_to_name($method, $options), isset($options['value']) ? $options['value'] : 1, $checked, $options);
266 }
267
268 function _convert_method_to_name($method, &$options)
269 {
270   $name = _get_option($options, 'control_name');
271
272   if (!$name)
273   {
274     if (is_array($method))
275     {
276       $name = implode('-',$method[1]);
277     }
278     else
279     {
280       $name = sfInflector::underscore($method);
281       $name = preg_replace('/^get_?/', '', $name);
282     }
283   }
284
285   return $name;
286 }
287
288 // returns default_value if object value is null
289 // method is either a string or: array('method',array('param1','param2'))
290 function _get_object_value($object, $method, $default_value = null, $param = null)
291 {
292   // compatibility with the array syntax
293   if (is_string($method))
294   {
295     $param = ($param == null ? array() : array($param));
296     $method = array($method, $param);
297   }
298  
299   // method exists?
300   if (!is_callable(array($object, $method[0])))
301   {
302     $error = 'Method "%s" doesn\'t exist for object of class "%s"';
303     $error = sprintf($error, $method[0], _get_class_decorated($object));
304
305     throw new sfViewException($error);
306   }
307
308   $object_value = call_user_func_array(array($object, $method[0]), $method[1]);
309
310   return ($default_value !== null && $object_value === null) ? $default_value : $object_value;
311 }
312
313 /**
314  * Returns the name of the class of an decorated object
315  *
316  * @param object An object that might be wrapped in an sfOutputEscaperObjectDecorator(-derivative)
317  *
318  * @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
319  */
320 function _get_class_decorated($object)
321 {
322   if ($object instanceof sfOutputEscaperObjectDecorator)
323   {
324     return sprintf('%s (decorated with %s)', get_class($object->getRawValue()), get_class($object));
325   }
326   else
327   {
328     return get_class($object);
329   }
330 }
331
Note: See TracBrowser for help on using the browser.