Development

/branches/1.3/lib/helper/ObjectHelper.php

You must first sign up to be able to contribute.

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

Revision 23810, 9.7 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 require_once dirname(__FILE__).'/FormHelper.php';
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 $object         An object.
26  * @param  string $method         An object column.
27  * @param  array  $options        Date options.
28  * @param  string $default_value  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 $object         An object.
46  * @param  string $method         An object column.
47  * @param  array  $options        Textarea options.
48  * @param  string $default_value  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, $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     $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     // value method exists?
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  * Returns a list html tag.
99  *
100  * @param  object $object         An object or the selected value
101  * @param  string $method         An object column.
102  * @param  array  $options        Input options (related_class option is mandatory).
103  * @param  string $default_value  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   $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     // construct select option list
160     $first = true;
161     foreach ($objects as $tmp_object)
162     {
163       if ($first)
164       {
165         // multi primary keys handling
166         $multi_primary_keys = is_array($tmp_object->$key_method()) ? true : false;
167
168         // which method to call?
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  * Returns a hidden input html tag.
212  *
213  * @param  object $object         An object.
214  * @param  string $method         An object column.
215  * @param  array  $options        Input options.
216  * @param  string $default_value  Input default value.
217  *
218  * @return string An html string which represents a hidden input tag.
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  * Returns a input html tag.
232  *
233  * @param  object $object         An object.
234  * @param  string $method         An object column.
235  * @param  array  $options        Input options.
236  * @param  string $default_value  Input default value.
237  *
238  * @return string An html string which represents an input tag.
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  * Returns a checkbox html tag.
252  *
253  * @param  object $object         An object.
254  * @param  string $method         An object column.
255  * @param  array  $options        Checkbox options.
256  * @param  bool   $default_value  Checkbox value.
257  *
258  * @return string An html string which represents a checkbox tag.
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 // returns default_value if object value is null
291 // method is either a string or: array('method',array('param1','param2'))
292 function _get_object_value($object, $method, $default_value = null, $param = null)
293 {
294   // compatibility with the array syntax
295   if (is_string($method))
296   {
297     $param = ($param == null ? array() : array($param));
298     $method = array($method, $param);
299   }
300
301   // method exists?
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  * Returns the name of the class of an decorated object
314  *
315  * @param  object $object An object that might be wrapped in an sfOutputEscaperObjectDecorator(-derivative)
316  *
317  * @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
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
Note: See TracBrowser for help on using the browser.