Development

/branches/1.1/lib/helper/ObjectAdminHelper.php

You must first sign up to be able to contribute.

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

Revision 8832, 6.7 kB (checked in by noel, 7 years ago)

fixed include_remove option not used in ObjectAdminHelper? (closes #2079)

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