ObjectHelper.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. require_once dirname(__FILE__).'/FormHelper.php';
  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 12595 2008-11-03 08:28:49Z dwhittle $
  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. $method_exists = ($text_method == '__toString') ? method_exists($option, $text_method) : is_callable(array($option, $text_method));
  66. if ($text_method && !$method_exists)
  67. {
  68. throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $text_method, _get_class_decorated($option)));
  69. }
  70. // value method exists?
  71. $method_exists = ($value_method == '__toString') ? method_exists($option, $value_method) : is_callable(array($option, $value_method));
  72. if (!$method_exists)
  73. {
  74. throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $value_method, _get_class_decorated($option)));
  75. }
  76. $value = $option->$value_method();
  77. $key = ($text_method != null) ? $option->$text_method() : $value;
  78. $select_options[$value] = $key;
  79. }
  80. return options_for_select($select_options, $selected, $html_options);
  81. }
  82. /**
  83. * Returns a list html tag.
  84. *
  85. * @param object $object An object or the selected value
  86. * @param string $method An object column.
  87. * @param array $options Input options (related_class option is mandatory).
  88. * @param string $default_value Input default value.
  89. *
  90. * @return string A list string which represents an input tag.
  91. *
  92. */
  93. function object_select_tag($object, $method, $options = array(), $default_value = null)
  94. {
  95. $options = _parse_attributes($options);
  96. $related_class = _get_option($options, 'related_class', false);
  97. if (false === $related_class && preg_match('/^get(.+?)Id$/', $method, $match))
  98. {
  99. $related_class = $match[1];
  100. }
  101. $peer_method = _get_option($options, 'peer_method');
  102. $text_method = _get_option($options, 'text_method');
  103. $key_method = _get_option($options, 'key_method', 'getPrimaryKey');
  104. $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, $peer_method), $text_method, $key_method);
  105. if ($value = _get_option($options, 'include_custom'))
  106. {
  107. $select_options = array('' => $value) + $select_options;
  108. }
  109. else if (_get_option($options, 'include_title'))
  110. {
  111. $select_options = array('' => '-- '._convert_method_to_name($method, $options).' --') + $select_options;
  112. }
  113. else if (_get_option($options, 'include_blank'))
  114. {
  115. $select_options = array('' => '') + $select_options;
  116. }
  117. if (is_object($object))
  118. {
  119. $value = _get_object_value($object, $method, $default_value);
  120. }
  121. else
  122. {
  123. $value = $object;
  124. }
  125. $option_tags = options_for_select($select_options, $value, $options);
  126. return select_tag(_convert_method_to_name($method, $options), $option_tags, $options);
  127. }
  128. function _get_options_from_objects($objects, $text_method = null, $key_method = 'getPrimaryKey')
  129. {
  130. $select_options = array();
  131. if ($objects)
  132. {
  133. // construct select option list
  134. $first = true;
  135. foreach ($objects as $tmp_object)
  136. {
  137. if ($first)
  138. {
  139. // multi primary keys handling
  140. $multi_primary_keys = is_array($tmp_object->$key_method()) ? true : false;
  141. // which method to call?
  142. $methodToCall = '';
  143. foreach (array($text_method, '__toString', 'toString', $key_method) as $method)
  144. {
  145. if (method_exists($tmp_object, $method))
  146. {
  147. $methodToCall = $method;
  148. break;
  149. }
  150. }
  151. $first = false;
  152. }
  153. $key = $multi_primary_keys ? implode('/', $tmp_object->$key_method()) : $tmp_object->$key_method();
  154. $value = $tmp_object->$methodToCall();
  155. $select_options[$key] = $value;
  156. }
  157. }
  158. return $select_options;
  159. }
  160. function object_select_country_tag($object, $method, $options = array(), $default_value = null)
  161. {
  162. $options = _parse_attributes($options);
  163. $value = _get_object_value($object, $method, $default_value);
  164. return select_country_tag(_convert_method_to_name($method, $options), $value, $options);
  165. }
  166. function object_select_language_tag($object, $method, $options = array(), $default_value = null)
  167. {
  168. $options = _parse_attributes($options);
  169. $value = _get_object_value($object, $method, $default_value);
  170. return select_language_tag(_convert_method_to_name($method, $options), $value, $options);
  171. }
  172. /**
  173. * Returns a hidden input html tag.
  174. *
  175. * @param object $object An object.
  176. * @param string $method An object column.
  177. * @param array $options Input options.
  178. * @param string $default_value Input default value.
  179. *
  180. * @return string An html string which represents a hidden input tag.
  181. *
  182. */
  183. function object_input_hidden_tag($object, $method, $options = array(), $default_value = null)
  184. {
  185. $options = _parse_attributes($options);
  186. $value = _get_object_value($object, $method, $default_value);
  187. return input_hidden_tag(_convert_method_to_name($method, $options), $value, $options);
  188. }
  189. /**
  190. * Returns a input html tag.
  191. *
  192. * @param object $object An object.
  193. * @param string $method An object column.
  194. * @param array $options Input options.
  195. * @param string $default_value Input default value.
  196. *
  197. * @return string An html string which represents an input tag.
  198. *
  199. */
  200. function object_input_tag($object, $method, $options = array(), $default_value = null)
  201. {
  202. $options = _parse_attributes($options);
  203. $value = _get_object_value($object, $method, $default_value);
  204. return input_tag(_convert_method_to_name($method, $options), $value, $options);
  205. }
  206. /**
  207. * Returns a checkbox html tag.
  208. *
  209. * @param object $object An object.
  210. * @param string $method An object column.
  211. * @param array $options Checkbox options.
  212. * @param bool $default_value Checkbox value.
  213. *
  214. * @return string An html string which represents a checkbox tag.
  215. *
  216. */
  217. function object_checkbox_tag($object, $method, $options = array(), $default_value = null)
  218. {
  219. $options = _parse_attributes($options);
  220. $checked = (boolean) _get_object_value($object, $method, $default_value);
  221. return checkbox_tag(_convert_method_to_name($method, $options), isset($options['value']) ? $options['value'] : 1, $checked, $options);
  222. }
  223. function _convert_method_to_name($method, &$options)
  224. {
  225. $name = _get_option($options, 'control_name');
  226. if (!$name)
  227. {
  228. if (is_array($method))
  229. {
  230. $name = implode('-',$method[1]);
  231. }
  232. else
  233. {
  234. $name = sfInflector::underscore($method);
  235. $name = preg_replace('/^get_?/', '', $name);
  236. }
  237. }
  238. return $name;
  239. }
  240. // returns default_value if object value is null
  241. // method is either a string or: array('method',array('param1','param2'))
  242. function _get_object_value($object, $method, $default_value = null, $param = null)
  243. {
  244. // compatibility with the array syntax
  245. if (is_string($method))
  246. {
  247. $param = ($param == null ? array() : array($param));
  248. $method = array($method, $param);
  249. }
  250. // method exists?
  251. if (!is_callable(array($object, $method[0])))
  252. {
  253. throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $method[0], _get_class_decorated($object)));
  254. }
  255. $object_value = call_user_func_array(array($object, $method[0]), $method[1]);
  256. return ($default_value !== null && $object_value === null) ? $default_value : $object_value;
  257. }
  258. /**
  259. * Returns the name of the class of an decorated object
  260. *
  261. * @param object $object An object that might be wrapped in an sfOutputEscaperObjectDecorator(-derivative)
  262. *
  263. * @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
  264. */
  265. function _get_class_decorated($object)
  266. {
  267. if ($object instanceof sfOutputEscaperObjectDecorator)
  268. {
  269. return sprintf('%s (decorated with %s)', get_class($object->getRawValue()), get_class($object));
  270. }
  271. else
  272. {
  273. return get_class($object);
  274. }
  275. }