Passed
Push — scrutinizer-code-quality ( 09f5a1...c4c5fb )
by Adam
56:05 queued 14:08
created

LayoutManager::LayoutManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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