]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/LayoutManager.php
Release 6.2.1
[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-2011 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 require_once('include/generic/SugarWidgets/SugarWidgetSubPanelTopButton.php');
42 require_once('include/generic/SugarWidgets/SugarWidgetReportField.php');
43 require_once('include/database/DBHelper.php');
44
45 class LayoutManager
46 {
47         var $defs = array();
48         var $widget_prefix = 'SugarWidget';
49         var $default_widget_name = 'Field';
50         var $DBHelper;
51
52         function LayoutManager()
53         {
54                 // set a sane default for context
55                 $this->defs['context'] = 'Detail';
56                 $this->DBHelper = $GLOBALS['db']->getHelper();
57         }
58
59         function setAttribute($key,$value)
60         {
61                 $this->defs[$key] = $value;
62         }
63
64         function setAttributePtr($key,&$value)
65         {
66                 $this->defs[$key] = $value;
67         }
68
69         function getAttribute($key)
70         {
71                 if ( isset($this->defs[$key]))
72                 {
73                         return $this->defs[$key];
74                 } else {
75                         return null;
76                 }
77         }
78
79         // Take the class name from the widget definition and use the class to look it up
80         // $use_default will default classes to SugarWidgetFieldxxxxx
81         function getClassFromWidgetDef($widget_def, $use_default = false)
82         {
83                 static $class_map = array(
84                         'SugarWidgetSubPanelTopCreateButton' => array(
85                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
86                                 'title'=>'LBL_NEW_BUTTON_TITLE',
87                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
88                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
89                                 'ACL'=>'edit',
90                         ),
91             'SugarWidgetSubPanelTopButtonQuickCreate' => array(
92                 'widget_class'=>'SugarWidgetSubPanelTopButtonQuickCreate',
93                 'title'=>'LBL_NEW_BUTTON_TITLE',
94                 'access_key'=>'LBL_NEW_BUTTON_KEY',
95                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
96                 'ACL'=>'edit',
97             ),
98                         'SugarWidgetSubPanelTopScheduleMeetingButton' => array(
99                                 'widget_class'=>'SugarWidgetSubPanelTopScheduleMeetingButton',
100                                 'module'=>'Meetings',
101                                 'title'=>'LBL_NEW_BUTTON_TITLE',
102                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
103                                 'form_value'=>'LNK_NEW_MEETING',
104                                 'ACL'=>'edit',
105                         ),
106                         'SugarWidgetSubPanelTopScheduleCallButton' => array(
107                                 'widget_class'=>'SugarWidgetSubPanelTopScheduleCallButton',
108                                 'module'=>'Calls',
109                                 'title'=>'LBL_NEW_BUTTON_TITLE',
110                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
111                                 'form_value'=>'LNK_NEW_CALL',
112                                 'ACL'=>'edit',
113                         ),
114                         'SugarWidgetSubPanelTopCreateTaskButton' => array(
115                                 'widget_class'=>'SugarWidgetSubPanelTopCreateTaskButton',
116                                 'module'=>'Tasks',
117                                 'title'=>'LBL_NEW_BUTTON_TITLE',
118                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
119                                 'form_value'=>'LNK_NEW_TASK',
120                                 'ACL'=>'edit',
121                         ),
122                         'SugarWidgetSubPanelTopCreateNoteButton' => array(
123                                 'widget_class'=>'SugarWidgetSubPanelTopCreateNoteButton',
124                                 'module'=>'Notes',
125                                 'title'=>'LBL_NEW_BUTTON_TITLE',
126                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
127                                 'form_value'=>'LNK_NEW_NOTE',
128                                 'ACL'=>'edit',
129                         ),
130                         'SugarWidgetSubPanelTopCreateContactAccountButton' => array(
131                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
132                                 'module'=>'Contacts',
133                                 'title'=>'LBL_NEW_BUTTON_TITLE',
134                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
135                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
136                         'additional_form_fields' => array(
137                                 'primary_address_street' => 'shipping_address_street',
138                                         'primary_address_city' => 'shipping_address_city',
139                                         'primary_address_state' => 'shipping_address_state',
140                                         'primary_address_country' => 'shipping_address_country',
141                                         'primary_address_postalcode' => 'shipping_address_postalcode',
142                                         'to_email_addrs' => 'email1'
143                                         ),
144                                 'ACL'=>'edit',
145                         ),
146                         'SugarWidgetSubPanelTopCreateContact' => array(
147                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
148                                 'module'=>'Contacts',
149                                 'title'=>'LBL_NEW_BUTTON_TITLE',
150                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
151                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
152                         'additional_form_fields' => array(
153                                 'account_id' => 'account_id',
154                                         'account_name' => 'account_name',
155                                 ),
156                                 'ACL'=>'edit',
157                         ),
158                         'SugarWidgetSubPanelTopCreateRevisionButton'=> array(
159                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
160                                 'module'=>'DocumentRevisions',
161                                 'title'=>'LBL_NEW_BUTTON_TITLE',
162                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
163                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
164                         'additional_form_fields' => array(
165                                 'parent_name'=>'document_name',
166                                         'document_name' => 'document_name',
167                                         'document_revision' => 'latest_revision',
168                                         'document_filename' => 'filename',
169                                 'document_revision_id' => 'document_revision_id',
170                                 ),
171                                 'ACL'=>'edit',
172                         ),
173
174                         'SugarWidgetSubPanelTopCreateDirectReport' => array(
175                                 'widget_class'=>'SugarWidgetSubPanelTopButton',
176                                 'module'=>'Contacts',
177                                 'title'=>'LBL_NEW_BUTTON_TITLE',
178                                 'access_key'=>'LBL_NEW_BUTTON_KEY',
179                                 'form_value'=>'LBL_NEW_BUTTON_LABEL',
180                         'additional_form_fields' => array(
181                                 'reports_to_name' => 'name',
182                                         'reports_to_id' => 'id',
183                                 ),
184                                 'ACL'=>'edit',
185                         ),
186                         'SugarWidgetSubPanelTopSelectFromReportButton' => array(
187                                 'widget_class'=>'SugarWidgetSubPanelTopSelectButton',
188                                 'module'=>'Reports',
189                                 'title'=>'LBL_SELECT_REPORTS_BUTTON_LABEL',
190                                 'access_key'=>'LBL_SELECT_BUTTON_KEY',
191                                 'form_value'=>'LBL_SELECT_REPORTS_BUTTON_LABEL',
192                                 'ACL'=>'edit',
193                                 'add_to_passthru_data'=>array (
194                                         'return_type'=>'report',
195                                 )
196                         ),
197                         'SugarWidgetSubPanelAddToProspectListButton' => array(
198                                 'widget_class'=>'SugarWidgetSubPanelTopSelectButton',
199                                 'module'=>'ProspectLists',
200                                 'title'=>'LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL',
201                                 'access_key'=>'LBL_ADD_TO_PROSPECT_LIST_BUTTON_KEY',
202                                 'form_value'=>'LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL',
203                                 'ACL'=>'edit',
204                                 'add_to_passthru_data'=>array (
205                                         'return_type'=>'addtoprospectlist',
206                                         'parent_module'=>'ProspectLists',
207                                         'parent_type'=>'ProspectList',
208                                         'child_id'=>'target_id',
209                                         'link_attribute'=>'target_type',
210                                         'link_type'=>'polymorphic',      //polymorphic or default
211                                 )
212                         ),
213                 );
214
215                 $fieldDef = $this->getFieldDef($widget_def);
216                 if(!empty($fieldDef) &&  !empty($fieldDef['type']) && strtolower(trim($fieldDef['type'])) == 'multienum'){
217                                 $widget_def['widget_class'] = 'Fieldmultienum';
218                 }
219                 if(!empty($fieldDef) &&  !empty($fieldDef['type']) && strtolower(trim($fieldDef['type'])) == 'bool'){
220                                 $widget_def['widget_class'] = 'Fieldbool';
221                 }
222
223         if($use_default) {
224             switch($widget_def['name']) {
225                 case 'assigned_user_id':
226                 //bug 39170 - begin
227                 case 'created_by':
228                 case 'modified_user_id':
229                 //bug 39170 - end
230                     $widget_def['widget_class'] = 'Fielduser_name';
231                 break;
232                 default:
233                     if ( isset($widget_def['type']) ) {
234                         $widget_def['widget_class'] = 'Field' . $widget_def['type'];
235                     } else {
236                         $widget_def['widget_class'] = 'Field' . $this->DBHelper->getFieldType($widget_def);
237                     }
238             }
239         }
240
241                 if(!empty($widget_def['name']) && $widget_def['name'] == 'team_set_id'){
242                         $widget_def['widget_class'] = 'Fieldteam_set_id';
243                 }
244
245                 if(empty($widget_def['widget_class']))
246                 {
247                         // Default the class to SugarWidgetField
248                         $class_name = $this->widget_prefix.$this->default_widget_name;
249                 }
250                 else
251                 {
252                         $class_name = $this->widget_prefix.$widget_def['widget_class'];
253                 }
254
255                 // Check to see if this is one of the known class mappings.
256                 if(!empty($class_map[$class_name]))
257                 {
258                         if (empty($class_map[$class_name]['widget_class'])) {
259                                 $widget = new SugarWidgetSubPanelTopButton($class_map[$class_name]);
260                         }  else {
261
262                                 if (!class_exists($class_map[$class_name]['widget_class'])) {
263                                         require_once('include/generic/SugarWidgets/'.$class_map[$class_name]['widget_class'].'.php');
264                                 }
265
266                                 $widget = new $class_map[$class_name]['widget_class']($class_map[$class_name]);
267                         }
268
269
270                         return $widget;
271                 }
272
273                 // At this point, we have a class name and we do not have a valid class defined.
274                 if(!class_exists($class_name))
275                 {
276
277                         // The class does not exist.  Try including it.
278                         if (file_exists('custom/include/generic/SugarWidgets/'.$class_name.'.php'))
279                                 require_once('custom/include/generic/SugarWidgets/'.$class_name.'.php');
280                         else if (file_exists('include/generic/SugarWidgets/'.$class_name.'.php'))
281                                 require_once('include/generic/SugarWidgets/'.$class_name.'.php');
282
283                         if(!class_exists($class_name))
284                         {
285                                 // If we still do not have a class, oops....
286                                 die("LayoutManager: Class not found:".$class_name);
287                         }
288                 }
289
290                 $widget = new $class_name($this); // cache disabled $this->getClassFromCache($class_name);
291                 return $widget;
292         }
293
294         // 27426
295         function getFieldDef($widget_def){
296         static $beanCache;
297                 if(!empty($widget_def['module']) &&!empty($GLOBALS['beanList'][$widget_def['module']]) && !empty($GLOBALS['beanFiles'][$GLOBALS['beanList'][$widget_def['module']]])){
298             if (!isset($beanCache[$widget_def['module']])){
299                 $beanCache[$widget_def['module']] = new $GLOBALS['beanList'][$widget_def['module']]();
300             }
301             $bean = $beanCache[$widget_def['module']];
302                         if(!empty($widget_def['name']) && !empty($bean->field_name_map) &&!empty($bean->field_name_map[$widget_def['name']]) ){
303                                 return $bean->field_name_map[$widget_def['name']];
304                         }
305                 }
306
307                 return null;
308         }
309
310         function widgetDisplay($widget_def, $use_default = false)
311         {
312                 $theclass = $this->getClassFromWidgetDef($widget_def, $use_default);
313                 $label = isset($widget_def['module']) ? $widget_def['module'] : '';
314             if (is_subclass_of($theclass, 'SugarWidgetSubPanelTopButton')) {
315             $label = $theclass->get_subpanel_relationship_name($widget_def);
316             }
317                 $theclass->setWidgetId($label);
318
319                 //#27426
320                 $fieldDef = $this->getFieldDef($widget_def);
321                 if(!empty($fieldDef) &&  !empty($fieldDef['type']) && strtolower(trim($fieldDef['type'])) == 'multienum'){
322                                 $widget_def['fields']  = sugarArrayMerge($widget_def['fields'] , $fieldDef);
323                                 $widget_def['fields']['module']  = $label;
324                 }
325                 //end
326
327                 return $theclass->display($widget_def);
328         }
329
330         function widgetQuery($widget_def, $use_default = false)
331         {
332                 $theclass = $this->getClassFromWidgetDef($widget_def, $use_default);
333 //                              _pp($theclass);
334                 return $theclass->query($widget_def);
335         }
336
337         // display an input field
338         // module is the parent module of the def
339         function widgetDisplayInput($widget_def, $use_default = false)
340         {
341                 $theclass = $this->getClassFromWidgetDef($widget_def, $use_default);
342                 return $theclass->displayInput($widget_def);
343         }
344
345 }
346 ?>