ObjectAdminHelper.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. require_once dirname(__FILE__).'/FormHelper.php';
  3. require_once dirname(__FILE__).'/JavascriptBaseHelper.php';
  4. require_once dirname(__FILE__).'/I18NHelper.php';
  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. * ObjectHelper for admin generator.
  14. *
  15. * @package symfony
  16. * @subpackage helper
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @version SVN: $Id: ObjectAdminHelper.php 11783 2008-09-25 16:21:27Z fabien $
  19. */
  20. function object_admin_input_file_tag($object, $method, $options = array())
  21. {
  22. $options = _parse_attributes($options);
  23. $name = _convert_method_to_name($method, $options);
  24. $html = '';
  25. $value = _get_object_value($object, $method);
  26. if ($value)
  27. {
  28. if ($include_link = _get_option($options, 'include_link'))
  29. {
  30. $relativeUploadDirName = ltrim(str_replace(sfConfig::get('sf_web_dir'), '', sfConfig::get('sf_upload_dir')), '/\\');
  31. $image_path = image_path('/'.$relativeUploadDirName.'/'.$include_link.'/'.$value);
  32. $image_text = ($include_text = _get_option($options, 'include_text')) ? __($include_text) : __('[show file]');
  33. $html .= sprintf('<a onclick="window.open(this.href);return false;" href="%s">%s</a>', $image_path, $image_text)."\n";
  34. }
  35. if ($include_remove = _get_option($options, 'include_remove'))
  36. {
  37. $html .= checkbox_tag(strpos($name, ']') !== false ? substr($name, 0, -1).'_remove]' : $name).' '.($include_remove !== true ? __($include_remove) : __('remove file'))."\n";
  38. }
  39. }
  40. return input_file_tag($name, $options)."\n<br />".$html;
  41. }
  42. function object_admin_double_list($object, $method, $options = array(), $callback = null)
  43. {
  44. $options = _parse_attributes($options);
  45. $options['multiple'] = true;
  46. $options['class'] = 'sf_admin_multiple';
  47. if (!isset($options['size']))
  48. {
  49. $options['size'] = 10;
  50. }
  51. $label_all = __(isset($options['unassociated_label']) ? $options['unassociated_label'] : 'Unassociated');
  52. $label_assoc = __(isset($options['associated_label']) ? $options['associated_label'] : 'Associated');
  53. // get the lists of objects
  54. list($all_objects, $objects_associated, $associated_ids) = _get_object_list($object, $method, $options, $callback);
  55. $objects_unassociated = array();
  56. foreach ($all_objects as $object)
  57. {
  58. if (!in_array($object->getPrimaryKey(), $associated_ids))
  59. {
  60. $objects_unassociated[] = $object;
  61. }
  62. }
  63. // remove non html option
  64. unset($options['through_class']);
  65. // override field name
  66. unset($options['control_name']);
  67. $name = _convert_method_to_name($method, $options);
  68. $name1 = 'unassociated_'.$name;
  69. $name2 = 'associated_'.$name;
  70. $select1 = select_tag($name1, options_for_select(_get_options_from_objects($objects_unassociated), '', $options), $options);
  71. $options['class'] = 'sf_admin_multiple-selected';
  72. $select2 = select_tag($name2, options_for_select(_get_options_from_objects($objects_associated), '', $options), $options);
  73. $html =
  74. '<div>
  75. <div style="float: left">
  76. <div style="font-weight: bold; padding-bottom: 0.5em">%s</div>
  77. %s
  78. </div>
  79. <div style="float: left">
  80. %s<br />
  81. %s
  82. </div>
  83. <div style="float: left">
  84. <div style="font-weight: bold; padding-bottom: 0.5em">%s</div>
  85. %s
  86. </div>
  87. <br style="clear: both" />
  88. </div>
  89. ';
  90. sfContext::getInstance()->getResponse()->addJavascript(sfConfig::get('sf_admin_web_dir').'/js/double_list.js');
  91. return sprintf($html,
  92. $label_all,
  93. $select1,
  94. submit_image_tag(sfConfig::get('sf_admin_web_dir').'/images/next.png', "style=\"border: 0\" onclick=\"double_list_move('{$name1}', '{$name2}'); return false;\""),
  95. submit_image_tag(sfConfig::get('sf_admin_web_dir').'/images/previous.png', "style=\"border: 0\" onclick=\"double_list_move('{$name2}', '{$name1}'); return false;\""),
  96. $label_assoc,
  97. $select2
  98. );
  99. }
  100. function object_admin_select_list($object, $method, $options = array(), $callback = null)
  101. {
  102. $options = _parse_attributes($options);
  103. $options['multiple'] = true;
  104. $options['class'] = 'sf_admin_multiple';
  105. if (!isset($options['size']))
  106. {
  107. $options['size'] = 10;
  108. }
  109. // get the lists of objects
  110. list($objects, $objects_associated, $ids) = _get_object_list($object, $method, $options, $callback);
  111. // remove non html option
  112. unset($options['through_class']);
  113. // override field name
  114. unset($options['control_name']);
  115. $name = 'associated_'._convert_method_to_name($method, $options);
  116. return select_tag($name, options_for_select(_get_options_from_objects($objects), $ids, $options), $options);
  117. }
  118. function object_admin_check_list($object, $method, $options = array(), $callback = null)
  119. {
  120. $options = _parse_attributes($options);
  121. // get the lists of objects
  122. list($objects, $objects_associated, $assoc_ids) = _get_object_list($object, $method, $options, $callback);
  123. // override field name
  124. unset($options['control_name']);
  125. $name = 'associated_'._convert_method_to_name($method, $options).'[]';
  126. $html = '';
  127. if (!empty($objects))
  128. {
  129. // which method to call?
  130. $methodToCall = '__toString';
  131. foreach (array('__toString', 'toString', 'getPrimaryKey') as $method)
  132. {
  133. if (method_exists($objects[0], $method))
  134. {
  135. $methodToCall = $method;
  136. break;
  137. }
  138. }
  139. $html .= "<ul class=\"sf_admin_checklist\">\n";
  140. foreach ($objects as $related_object)
  141. {
  142. $relatedPrimaryKey = $related_object->getPrimaryKey();
  143. // multi primary key handling
  144. if (is_array($relatedPrimaryKey))
  145. {
  146. $relatedPrimaryKeyHtmlId = implode('/', $relatedPrimaryKey);
  147. }
  148. else
  149. {
  150. $relatedPrimaryKeyHtmlId = $relatedPrimaryKey;
  151. }
  152. $html .= '<li>'.checkbox_tag($name, $relatedPrimaryKeyHtmlId, in_array($relatedPrimaryKey, $assoc_ids)).' <label for="'.get_id_from_name($name, $relatedPrimaryKeyHtmlId).'">'.$related_object->$methodToCall()."</label></li>\n";
  153. }
  154. $html .= "</ul>\n";
  155. }
  156. return $html;
  157. }
  158. function _get_propel_object_list($object, $method, $options)
  159. {
  160. // get the lists of objects
  161. $through_class = _get_option($options, 'through_class');
  162. $objects = sfPropelManyToMany::getAllObjects($object, $through_class);
  163. $objects_associated = sfPropelManyToMany::getRelatedObjects($object, $through_class);
  164. $ids = array_map(create_function('$o', 'return $o->getPrimaryKey();'), $objects_associated);
  165. return array($objects, $objects_associated, $ids);
  166. }
  167. function _get_object_list($object, $method, $options, $callback)
  168. {
  169. $object = $object instanceof sfOutputEscaper ? $object->getRawValue() : $object;
  170. // the default callback is the propel one
  171. if (!$callback)
  172. {
  173. $callback = '_get_propel_object_list';
  174. }
  175. return call_user_func($callback, $object, $method, $options);
  176. }