ObjectHelper.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. use_helper('Form');
  3. /*
  4. * This file is part of the symfony package.
  5. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * ObjectHelper.
  12. *
  13. * @package symfony
  14. * @subpackage helper
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: ObjectHelper.php 9340 2008-05-28 12:32:30Z FabianLange $
  17. */
  18. /**
  19. * Returns a html date control.
  20. *
  21. * @param object $object An object.
  22. * @param string $method An object column.
  23. * @param array $options Date options.
  24. * @param string $default_value Date default value.
  25. *
  26. * @return string An html string which represents a date control.
  27. *
  28. */
  29. function object_input_date_tag($object, $method, $options = array(), $default_value = null)
  30. {
  31. $options = _parse_attributes($options);
  32. $value = _get_object_value($object, $method, $default_value, $param = 'Y-m-d G:i');
  33. return input_date_tag(_convert_method_to_name($method, $options), $value, $options);
  34. }
  35. /**
  36. * Returns a textarea html tag.
  37. *
  38. * @param object $object An object.
  39. * @param string $method An object column.
  40. * @param array $options Textarea options.
  41. * @param string $default_value Textarea default value.
  42. *
  43. * @return string An html string which represents a textarea tag.
  44. *
  45. */
  46. function object_textarea_tag($object, $method, $options = array(), $default_value = null)
  47. {
  48. $options = _parse_attributes($options);
  49. $value = _get_object_value($object, $method, $default_value);
  50. return textarea_tag(_convert_method_to_name($method, $options), $value, $options);
  51. }
  52. /**
  53. * Accepts a container of objects, the method name to use for the value,
  54. * and the method name to use for the display.
  55. * It returns a string of option tags.
  56. *
  57. * NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
  58. */
  59. function objects_for_select($options, $value_method, $text_method = null, $selected = null, $html_options = array())
  60. {
  61. $select_options = array();
  62. foreach ($options as $option)
  63. {
  64. // text method exists?
  65. if ($text_method && !is_callable(array($option, $text_method)))
  66. {
  67. throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $text_method, _get_class_decorated($option)));
  68. }
  69. // value method exists?
  70. if (!is_callable(array($option, $value_method)))
  71. {
  72. throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $value_method, _get_class_decorated($option)));
  73. }
  74. $value = $option->$value_method();
  75. $key = ($text_method != null) ? $option->$text_method() : $value;
  76. $select_options[$value] = $key;
  77. }
  78. return options_for_select($select_options, $selected, $html_options);
  79. }
  80. /**
  81. * Returns a list html tag.
  82. *
  83. * @param object $object An object or the selected value
  84. * @param string $method An object column.
  85. * @param array $options Input options (related_class option is mandatory).
  86. * @param string $default_value Input default value.
  87. *
  88. * @return string A list string which represents an input tag.
  89. *
  90. */
  91. function object_select_tag($object, $method, $options = array(), $default_value = null)
  92. {
  93. $options = _parse_attributes($options);
  94. $related_class = _get_option($options, 'related_class', false);
  95. if (false === $related_class && preg_match('/^get(.+?)Id$/', $method, $match))
  96. {
  97. $related_class = $match[1];
  98. }
  99. $peer_method = _get_option($options, 'peer_method');
  100. $text_method = _get_option($options, 'text_method');
  101. $key_method = _get_option($options, 'key_method', 'getPrimaryKey');
  102. $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, $peer_method), $text_method, $key_method);
  103. if ($value = _get_option($options, 'include_custom'))
  104. {
  105. $select_options = array('' => $value) + $select_options;
  106. }
  107. else if (_get_option($options, 'include_title'))
  108. {
  109. $select_options = array('' => '-- '._convert_method_to_name($method, $options).' --') + $select_options;
  110. }
  111. else if (_get_option($options, 'include_blank'))
  112. {
  113. $select_options = array('' => '') + $select_options;
  114. }
  115. if (is_object($object))
  116. {
  117. $value = _get_object_value($object, $method, $default_value);
  118. }
  119. else
  120. {
  121. $value = $object;
  122. }
  123. $option_tags = options_for_select($select_options, $value, $options);
  124. return select_tag(_convert_method_to_name($method, $options), $option_tags, $options);
  125. }
  126. function _get_options_from_objects($objects, $text_method = null, $key_method = 'getPrimaryKey')
  127. {
  128. $select_options = array();
  129. if ($objects)
  130. {
  131. // construct select option list
  132. $first = true;
  133. foreach ($objects as $tmp_object)
  134. {
  135. if ($first)
  136. {
  137. // multi primary keys handling
  138. $multi_primary_keys = is_array($tmp_object->$key_method()) ? true : false;
  139. // which method to call?
  140. $methodToCall = '';
  141. foreach (array($text_method, '__toString', 'toString', $key_method) as $method)
  142. {
  143. if (is_callable(array($tmp_object, $method)))
  144. {
  145. $methodToCall = $method;
  146. break;
  147. }
  148. }
  149. $first = false;
  150. }
  151. $key = $multi_primary_keys ? implode('/', $tmp_object->$key_method()) : $tmp_object->$key_method();
  152. $value = $tmp_object->$methodToCall();
  153. $select_options[$key] = $value;
  154. }
  155. }
  156. return $select_options;
  157. }
  158. function object_select_country_tag($object, $method, $options = array(), $default_value = null)
  159. {
  160. $options = _parse_attributes($options);
  161. $value = _get_object_value($object, $method, $default_value);
  162. return select_country_tag(_convert_method_to_name($method, $options), $value, $options);
  163. }
  164. function object_select_language_tag($object, $method, $options = array(), $default_value = null)
  165. {
  166. $options = _parse_attributes($options);
  167. $value = _get_object_value($object, $method, $default_value);
  168. return select_language_tag(_convert_method_to_name($method, $options), $value, $options);
  169. }
  170. /**
  171. * Returns a hidden input html tag.
  172. *
  173. * @param object $object An object.
  174. * @param string $method An object column.
  175. * @param array $options Input options.
  176. * @param string $default_value Input default value.
  177. *
  178. * @return string An html string which represents a hidden input tag.
  179. *
  180. */
  181. function object_input_hidden_tag($object, $method, $options = array(), $default_value = null)
  182. {
  183. $options = _parse_attributes($options);
  184. $value = _get_object_value($object, $method, $default_value);
  185. return input_hidden_tag(_convert_method_to_name($method, $options), $value, $options);
  186. }
  187. /**
  188. * Returns a input html tag.
  189. *
  190. * @param object $object An object.
  191. * @param string $method An object column.
  192. * @param array $options Input options.
  193. * @param string $default_value Input default value.
  194. *
  195. * @return string An html string which represents an input tag.
  196. *
  197. */
  198. function object_input_tag($object, $method, $options = array(), $default_value = null)
  199. {
  200. $options = _parse_attributes($options);
  201. $value = _get_object_value($object, $method, $default_value);
  202. return input_tag(_convert_method_to_name($method, $options), $value, $options);
  203. }
  204. /**
  205. * Returns a checkbox html tag.
  206. *
  207. * @param object $object An object.
  208. * @param string $method An object column.
  209. * @param array $options Checkbox options.
  210. * @param bool $default_value Checkbox value.
  211. *
  212. * @return string An html string which represents a checkbox tag.
  213. *
  214. */
  215. function object_checkbox_tag($object, $method, $options = array(), $default_value = null)
  216. {
  217. $options = _parse_attributes($options);
  218. $checked = (boolean) _get_object_value($object, $method, $default_value);
  219. return checkbox_tag(_convert_method_to_name($method, $options), isset($options['value']) ? $options['value'] : 1, $checked, $options);
  220. }
  221. function _convert_method_to_name($method, &$options)
  222. {
  223. $name = _get_option($options, 'control_name');
  224. if (!$name)
  225. {
  226. if (is_array($method))
  227. {
  228. $name = implode('-',$method[1]);
  229. }
  230. else
  231. {
  232. $name = sfInflector::underscore($method);
  233. $name = preg_replace('/^get_?/', '', $name);
  234. }
  235. }
  236. return $name;
  237. }
  238. // returns default_value if object value is null
  239. // method is either a string or: array('method',array('param1','param2'))
  240. function _get_object_value($object, $method, $default_value = null, $param = null)
  241. {
  242. // compatibility with the array syntax
  243. if (is_string($method))
  244. {
  245. $param = ($param == null ? array() : array($param));
  246. $method = array($method, $param);
  247. }
  248. // method exists?
  249. if (!is_callable(array($object, $method[0])))
  250. {
  251. throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $method[0], _get_class_decorated($object)));
  252. }
  253. $object_value = call_user_func_array(array($object, $method[0]), $method[1]);
  254. return ($default_value !== null && $object_value === null) ? $default_value : $object_value;
  255. }
  256. /**
  257. * Returns the name of the class of an decorated object
  258. *
  259. * @param object $object An object that might be wrapped in an sfOutputEscaperObjectDecorator(-derivative)
  260. *
  261. * @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
  262. */
  263. function _get_class_decorated($object)
  264. {
  265. if ($object instanceof sfOutputEscaperObjectDecorator)
  266. {
  267. return sprintf('%s (decorated with %s)', get_class($object->getRawValue()), get_class($object));
  268. }
  269. else
  270. {
  271. return get_class($object);
  272. }
  273. }