123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- require_once dirname(__FILE__).'/FormHelper.php';
- function object_input_date_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $value = _get_object_value($object, $method, $default_value, $param = 'Y-m-d G:i');
- return input_date_tag(_convert_method_to_name($method, $options), $value, $options);
- }
- function object_textarea_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $value = _get_object_value($object, $method, $default_value);
- return textarea_tag(_convert_method_to_name($method, $options), $value, $options);
- }
- function objects_for_select($options, $value_method, $text_method = null, $selected = null, $html_options = array())
- {
- $select_options = array();
- foreach ($options as $option)
- {
-
- $method_exists = ($text_method == '__toString') ? method_exists($option, $text_method) : is_callable(array($option, $text_method));
- if ($text_method && !$method_exists)
- {
- throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $text_method, _get_class_decorated($option)));
- }
-
- $method_exists = ($value_method == '__toString') ? method_exists($option, $value_method) : is_callable(array($option, $value_method));
- if (!$method_exists)
- {
- throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $value_method, _get_class_decorated($option)));
- }
- $value = $option->$value_method();
- $key = ($text_method != null) ? $option->$text_method() : $value;
- $select_options[$value] = $key;
- }
- return options_for_select($select_options, $selected, $html_options);
- }
- function object_select_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $related_class = _get_option($options, 'related_class', false);
- if (false === $related_class && preg_match('/^get(.+?)Id$/', $method, $match))
- {
- $related_class = $match[1];
- }
- $peer_method = _get_option($options, 'peer_method');
- $text_method = _get_option($options, 'text_method');
- $key_method = _get_option($options, 'key_method', 'getPrimaryKey');
- $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, $peer_method), $text_method, $key_method);
- if ($value = _get_option($options, 'include_custom'))
- {
- $select_options = array('' => $value) + $select_options;
- }
- else if (_get_option($options, 'include_title'))
- {
- $select_options = array('' => '-- '._convert_method_to_name($method, $options).' --') + $select_options;
- }
- else if (_get_option($options, 'include_blank'))
- {
- $select_options = array('' => '') + $select_options;
- }
- if (is_object($object))
- {
- $value = _get_object_value($object, $method, $default_value);
- }
- else
- {
- $value = $object;
- }
- $option_tags = options_for_select($select_options, $value, $options);
- return select_tag(_convert_method_to_name($method, $options), $option_tags, $options);
- }
- function _get_options_from_objects($objects, $text_method = null, $key_method = 'getPrimaryKey')
- {
- $select_options = array();
- if ($objects)
- {
-
- $first = true;
- foreach ($objects as $tmp_object)
- {
- if ($first)
- {
-
- $multi_primary_keys = is_array($tmp_object->$key_method()) ? true : false;
-
- $methodToCall = '';
- foreach (array($text_method, '__toString', 'toString', $key_method) as $method)
- {
- if (method_exists($tmp_object, $method))
- {
- $methodToCall = $method;
- break;
- }
- }
- $first = false;
- }
- $key = $multi_primary_keys ? implode('/', $tmp_object->$key_method()) : $tmp_object->$key_method();
- $value = $tmp_object->$methodToCall();
- $select_options[$key] = $value;
- }
- }
- return $select_options;
- }
- function object_select_country_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $value = _get_object_value($object, $method, $default_value);
- return select_country_tag(_convert_method_to_name($method, $options), $value, $options);
- }
- function object_select_language_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $value = _get_object_value($object, $method, $default_value);
- return select_language_tag(_convert_method_to_name($method, $options), $value, $options);
- }
- function object_input_hidden_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $value = _get_object_value($object, $method, $default_value);
- return input_hidden_tag(_convert_method_to_name($method, $options), $value, $options);
- }
- function object_input_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $value = _get_object_value($object, $method, $default_value);
- return input_tag(_convert_method_to_name($method, $options), $value, $options);
- }
- function object_checkbox_tag($object, $method, $options = array(), $default_value = null)
- {
- $options = _parse_attributes($options);
- $checked = (boolean) _get_object_value($object, $method, $default_value);
- return checkbox_tag(_convert_method_to_name($method, $options), isset($options['value']) ? $options['value'] : 1, $checked, $options);
- }
- function _convert_method_to_name($method, &$options)
- {
- $name = _get_option($options, 'control_name');
- if (!$name)
- {
- if (is_array($method))
- {
- $name = implode('-',$method[1]);
- }
- else
- {
- $name = sfInflector::underscore($method);
- $name = preg_replace('/^get_?/', '', $name);
- }
- }
- return $name;
- }
- function _get_object_value($object, $method, $default_value = null, $param = null)
- {
-
- if (is_string($method))
- {
- $param = ($param == null ? array() : array($param));
- $method = array($method, $param);
- }
-
- if (!is_callable(array($object, $method[0])))
- {
- throw new sfViewException(sprintf('Method "%s" doesn\'t exist for object of class "%s".', $method[0], _get_class_decorated($object)));
- }
- $object_value = call_user_func_array(array($object, $method[0]), $method[1]);
- return ($default_value !== null && $object_value === null) ? $default_value : $object_value;
- }
- function _get_class_decorated($object)
- {
- if ($object instanceof sfOutputEscaperObjectDecorator)
- {
- return sprintf('%s (decorated with %s)', get_class($object->getRawValue()), get_class($object));
- }
- else
- {
- return get_class($object);
- }
- }
|