Development

/branches/1.0/lib/helper/ObjectAdminHelper.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/helper/ObjectAdminHelper.php

Revision 9176, 6.7 kB (checked in by FabianLange, 7 years ago)

fixed include_remove in admin helper. this was already in r8831 but got reverted as side effect. closes #2079 again

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