Development

/branches/1.3/lib/helper/ObjectAdminHelper.php

You must first sign up to be able to contribute.

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

Revision 23810, 6.8 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

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