]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/LayoutManager.php
Release 6.5.0
[Github/sugarcrm.git] / include / generic / LayoutManager.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41
42
43
44 /**
45  * Form layout manager
46  * @api
47  */
48 class LayoutManager
49 {
50         var $defs = array();
51         var $widget_prefix = 'SugarWidget';
52         var $default_widget_name = 'Field';
53         var $DBHelper;
54
55         function LayoutManager()
56         {
57                 // set a sane default for context
58                 $this->defs['context'] = 'Detail';
59                 $this->DBHelper = $GLOBALS['db'];
60         }
61
62         function setAttribute($key,$value)
63         {
64                 $this->defs[$key] = $value;
65         }
66
67         function setAttributePtr($key,&$value)
68         {
69                 $this->defs[$key] = $value;
70         }
71
72         function getAttribute($key)
73         {
74                 if ( isset($this->defs[$key]))
75                 {
76                         return $this->defs[$key];
77                 } else {
78                         return null;
79                 }
80         }
81
82         // Take the class name from the widget definition and use the class to look it up
83         // $use_default will default classes to SugarWidgetFieldxxxxx
84         function getClassFromWidgetDef($widget_def, $use_default = false)
85         {
86                 static $class_map = array(
87                         'SugarWidgetSubPanelTopCreateButton' => array(
88                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
89                                 'title'=>'LBL_NEW_BUTTON_TITLE',
90                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
91                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
92                                 'ACL'=>'edit',
93                         ),
94             'SugarWidgetSubPanelTopButtonQuickCreate' => array(
95                 'widget_class'=>'SugarWidgetSubPanelTopButtonQuickCreate',
96                 'title'=>'LBL_NEW_BUTTON_TITLE',
97                 'access_key'=>'LBL_NEW_BUTTON_KEY',
98                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
99                 'ACL'=>'edit',
100             ),
101             'SugarWidgetSubPanelTopCreateLeadNameButton' => array(
102                 'widget_class'=>'SugarWidgetSubPanelTopCreateLeadNameButton',
103                 'title'=>'LBL_NEW_BUTTON_TITLE',
104                 'access_key'=>'LBL_NEW_BUTTON_KEY',
105                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
106                 'ACL'=>'edit',
107             ),
108                         'SugarWidgetSubPanelTopScheduleMeetingButton' => array(
109                                 'widget_class'=>'SugarWidgetSubPanelTopScheduleMeetingButton',
110                                 'module'=>'Meetings',
111                                 'title'=>'LBL_NEW_BUTTON_TITLE',
112                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
113                                 'form_value'=>'LNK_NEW_MEETING',
114                                 'ACL'=>'edit',
115                         ),
116                         'SugarWidgetSubPanelTopScheduleCallButton' => array(
117                                 'widget_class'=>'SugarWidgetSubPanelTopScheduleCallButton',
118                                 'module'=>'Calls',
119                                 'title'=>'LBL_NEW_BUTTON_TITLE',
120                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
121                                 'form_value'=>'LNK_NEW_CALL',
122                                 'ACL'=>'edit',
123                         ),
124                         'SugarWidgetSubPanelTopCreateTaskButton' => array(
125                                 'widget_class'=>'SugarWidgetSubPanelTopCreateTaskButton',
126                                 'module'=>'Tasks',
127                                 'title'=>'LBL_NEW_BUTTON_TITLE',
128                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
129                                 'form_value'=>'LNK_NEW_TASK',
130                                 'ACL'=>'edit',
131                         ),
132                         'SugarWidgetSubPanelTopCreateNoteButton' => array(
133                                 'widget_class'=>'SugarWidgetSubPanelTopCreateNoteButton',
134                                 'module'=>'Notes',
135                                 'title'=>'LBL_NEW_BUTTON_TITLE',
136                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
137                                 'form_value'=>'LNK_NEW_NOTE',
138                                 'ACL'=>'edit',
139                         ),
140                         'SugarWidgetSubPanelTopCreateContactAccountButton' => array(
141                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
142                                 'module'=>'Contacts',
143                                 'title'=>'LBL_NEW_BUTTON_TITLE',
144                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
145                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
146                         'additional_form_fields' => array(
147                                 'primary_address_street' => 'shipping_address_street',
148                                         'primary_address_city' => 'shipping_address_city',
149                                         'primary_address_state' => 'shipping_address_state',
150                                         'primary_address_country' => 'shipping_address_country',
151                                         'primary_address_postalcode' => 'shipping_address_postalcode',
152                                         'to_email_addrs' => 'email1'
153                                         ),
154                                 'ACL'=>'edit',
155                         ),
156                         'SugarWidgetSubPanelTopCreateContact' => array(
157                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
158                                 'module'=>'Contacts',
159                                 'title'=>'LBL_NEW_BUTTON_TITLE',
160                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
161                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
162                         'additional_form_fields' => array(
163                                 'account_id' => 'account_id',
164                                         'account_name' => 'account_name',
165                                 ),
166                                 'ACL'=>'edit',
167                         ),
168                         'SugarWidgetSubPanelTopCreateRevisionButton'=> array(
169                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
170                                 'module'=>'DocumentRevisions',
171                                 'title'=>'LBL_NEW_BUTTON_TITLE',
172                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
173                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
174                         'additional_form_fields' => array(
175                                 'parent_name'=>'document_name',
176                                         'document_name' => 'document_name',
177                                         'document_revision' => 'latest_revision',
178                                         'document_filename' => 'filename',
179                                 'document_revision_id' => 'document_revision_id',
180                                 ),
181                                 'ACL'=>'edit',
182                         ),
183
184                         'SugarWidgetSubPanelTopCreateDirectReport' => array(
185                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
186                                 'module'=>'Contacts',
187                                 'title'=>'LBL_NEW_BUTTON_TITLE',
188                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
189                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
190                         'additional_form_fields' => array(
191                                 'reports_to_name' => 'name',
192                                         'reports_to_id' => 'id',
193                                 ),
194                                 'ACL'=>'edit',
195                         ),
196                         'SugarWidgetSubPanelTopSelectFromReportButton' => array(
197                                 'widget_class'=>'SugarWidgetSubPanelTopSelectReportsButton',
198                                 'module'=>'Reports',
199                                 'title'=>'LBL_SELECT_REPORTS_BUTTON_LABEL',
200                                 'access_key'=>'LBL_SELECT_BUTTON_KEY',
201                                 'form_value'=>'LBL_SELECT_REPORTS_BUTTON_LABEL',
202                                 'ACL'=>'edit',
203                                 'add_to_passthru_data'=>array (
204                                         'return_type'=>'report',
205                                 )
206                         ),
207                          'SugarWidgetSubPanelTopCreateAccountNameButton' => array(
208                 'widget_class'=>'SugarWidgetSubPanelTopCreateAccountNameButton',
209                 'module'=>'Contacts',
210                 'title'=>'LBL_NEW_BUTTON_TITLE',
211                 'access_key'=>'LBL_NEW_BUTTON_KEY',
212                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
213                 'ACL'=>'edit',
214             ),
215                         'SugarWidgetSubPanelAddToProspectListButton' => array(
216                                 'widget_class'=>'SugarWidgetSubPanelTopSelectButton',
217                                 'module'=>'ProspectLists',
218                                 'title'=>'LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL',
219                                 'access_key'=>'LBL_ADD_TO_PROSPECT_LIST_BUTTON_KEY',
220                                 'form_value'=>'LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL',
221                                 'ACL'=>'edit',
222                                 'add_to_passthru_data'=>array (
223                                         'return_type'=>'addtoprospectlist',
224                                         'parent_module'=>'ProspectLists',
225                                         'parent_type'=>'ProspectList',
226                                         'child_id'=>'target_id',
227                                         'link_attribute'=>'target_type',
228                                         'link_type'=>'polymorphic',      //polymorphic or default
229                                 )
230                         ),
231                 );
232
233                 $fieldDef = $this->getFieldDef($widget_def);
234                 if(!empty($fieldDef) &&  !empty($fieldDef['type']) && strtolower(trim($fieldDef['type'])) == 'multienum'){
235                                 $widget_def['widget_class'] = 'Fieldmultienum';
236                 }
237                 if(!empty($fieldDef) &&  !empty($fieldDef['type']) && strtolower(trim($fieldDef['type'])) == 'bool'){
238                                 $widget_def['widget_class'] = 'Fieldbool';
239                 }
240
241         if($use_default) {
242             switch($widget_def['name']) {
243                 case 'assigned_user_id':
244                 //bug 39170 - begin
245                 case 'created_by':
246                 case 'modified_user_id':
247                 //bug 39170 - end
248                     $widget_def['widget_class'] = 'Fielduser_name';
249                 break;
250                 default:
251                     if ( isset($widget_def['type']) ) {
252                         $widget_def['widget_class'] = 'Field' . $widget_def['type'];
253                     } else {
254                         $widget_def['widget_class'] = 'Field' . $this->DBHelper->getFieldType($widget_def);
255                     }
256             }
257         }
258
259                 if(!empty($widget_def['name']) && $widget_def['name'] == 'team_set_id'){
260                         $widget_def['widget_class'] = 'Fieldteam_set_id';
261                 }
262
263                 if(empty($widget_def['widget_class']))
264                 {
265                         // Default the class to SugarWidgetField
266                         $class_name = $this->widget_prefix.$this->default_widget_name;
267                 }
268                 else
269                 {
270                         $class_name = $this->widget_prefix.$widget_def['widget_class'];
271                 }
272
273                 // Check to see if this is one of the known class mappings.
274                 if(!empty($class_map[$class_name]))
275                 {
276                         if (empty($class_map[$class_name]['widget_class'])) {
277                                 $widget = new SugarWidgetSubPanelTopButton($class_map[$class_name]);
278                         }  else {
279
280                                 if (!class_exists($class_map[$class_name]['widget_class'])) {
281                                         require_once('include/generic/SugarWidgets/'.$class_map[$class_name]['widget_class'].'.php');
282                                 }
283
284                                 $widget = new $class_map[$class_name]['widget_class']($class_map[$class_name]);
285                         }
286
287
288                         return $widget;
289                 }
290
291                 // At this point, we have a class name and we do not have a valid class defined.
292                 if(!class_exists($class_name))
293                 {
294
295                         // The class does not exist.  Try including it.
296                         if (file_exists('custom/include/generic/SugarWidgets/'.$class_name.'.php'))
297                                 require_once('custom/include/generic/SugarWidgets/'.$class_name.'.php');
298                         else if (file_exists('include/generic/SugarWidgets/'.$class_name.'.php'))
299                                 require_once('include/generic/SugarWidgets/'.$class_name.'.php');
300
301                         if(!class_exists($class_name))
302                         {
303                                 // If we still do not have a class, oops....
304                                 die("LayoutManager: Class not found:".$class_name);
305                         }
306                 }
307
308                 $widget = new $class_name($this); // cache disabled $this->getClassFromCache($class_name);
309                 return $widget;
310         }
311
312         // 27426
313         function getFieldDef($widget_def){
314         static $beanCache;
315                 if(!empty($widget_def['module']) &&!empty($GLOBALS['beanList'][$widget_def['module']]) && !empty($GLOBALS['beanFiles'][$GLOBALS['beanList'][$widget_def['module']]])){
316             if (!isset($beanCache[$widget_def['module']])){
317                 $beanCache[$widget_def['module']] = new $GLOBALS['beanList'][$widget_def['module']]();
318             }
319             $bean = $beanCache[$widget_def['module']];
320                         if(!empty($widget_def['name']) && !empty($bean->field_name_map) &&!empty($bean->field_name_map[$widget_def['name']]) ){
321                                 return $bean->field_name_map[$widget_def['name']];
322                         }
323                 }
324
325                 return null;
326         }
327
328         function widgetDisplay($widget_def, $use_default = false, $grabName = false, $grabId = false)
329         {
330                 $theclass = $this->getClassFromWidgetDef($widget_def, $use_default);
331                 $label = isset($widget_def['module']) ? $widget_def['module'] : '';
332             if (is_subclass_of($theclass, 'SugarWidgetSubPanelTopButton')) {
333             $label = $theclass->get_subpanel_relationship_name($widget_def);
334             }
335                 $theclass->setWidgetId($label);
336
337                 //#27426
338                 $fieldDef = $this->getFieldDef($widget_def);
339                 if(!empty($fieldDef) &&  !empty($fieldDef['type']) && strtolower(trim($fieldDef['type'])) == 'multienum'){
340                                 $widget_def['fields']  = sugarArrayMerge($widget_def['fields'] , $fieldDef);
341                                 $widget_def['fields']['module']  = $label;
342                 }
343                 //end
344
345         if ($grabName) {
346             return $theclass->getDisplayName();
347         }
348         if ($grabId) {
349             return $theclass->getWidgetId();
350         }
351         
352                 return $theclass->display($widget_def, null, null);
353         }
354
355         function widgetQuery($widget_def, $use_default = false)
356         {
357                 $theclass = $this->getClassFromWidgetDef($widget_def, $use_default);
358 //                              _pp($theclass);
359                 return $theclass->query($widget_def);
360         }
361
362         // display an input field
363         // module is the parent module of the def
364         function widgetDisplayInput($widget_def, $use_default = false)
365         {
366                 $theclass = $this->getClassFromWidgetDef($widget_def, $use_default);
367                 return $theclass->displayInput($widget_def);
368         }
369
370 }
371 ?>