ObjectAdminHelper.php 6.7 KB

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