SugarWidgetSubPanelTopButton_c::_get_form()   F
last analyzed

Complexity

Conditions 38
Paths > 20000

Size

Total Lines 162
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1482
Metric Value
cc 38
eloc 95
nc 429496.7295
nop 3
dl 0
loc 162
ccs 0
cts 137
cp 0
crap 1482
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2016 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
//custom to get rid of create button on events
43
44
45
46
class SugarWidgetSubPanelTopButton_c extends SugarWidget
47
{
48
    var $module;
49
	var $title;
50
	var $access_key;
51
	var $form_value;
52
	var $additional_form_fields;
53
	var $acl;
54
55
//TODO rename defines to layout defs and make it a member variable instead of passing it multiple layers with extra copying.
56
57
	/** Take the keys for the strings and look them up.  Module is literal, the rest are label keys
58
	*/
59
	function __construct($module='', $title='', $access_key='', $form_value='')
60
	{
61
		global $app_strings;
62
63
		if(is_array($module))
64
		{
65
			// it is really the class details from the mapping
66
			$class_details = $module;
67
68
			// If keys were passed into the constructor, translate them from keys to values.
69
			if(!empty($class_details['module']))
70
				$this->module = $class_details['module'];
71
			if(!empty($class_details['title']))
72
				$this->title = $app_strings[$class_details['title']];
73
			if(!empty($class_details['access_key']))
74
				$this->access_key = $app_strings[$class_details['access_key']];
75
			if(!empty($class_details['form_value']))
76
				$this->form_value = translate($class_details['form_value'], $this->module);
77
			if(!empty($class_details['additional_form_fields']))
78
				$this->additional_form_fields = $class_details['additional_form_fields'];
79
			if(!empty($class_details['ACL'])){
80
				$this->acl = $class_details['ACL'];
81
			}
82
		}
83
		else
84
		{
85
			$this->module = $module;
86
87
			// If keys were passed into the constructor, translate them from keys to values.
88
			if(!empty($title))
89
				$this->title = $app_strings[$title];
90
			if(!empty($access_key))
91
				$this->access_key = $app_strings[$access_key];
92
			if(!empty($form_value))
93
				$this->form_value = translate($form_value, $module);
94
		}
95
	}
96
97
    public function getWidgetId($buttonSuffix = true)
98
    {
99
    	$widgetID = parent::getWidgetId() . '_'.preg_replace('[ ]', '', strtolower($this->form_value));
100
    	if($buttonSuffix){
101
    		$widgetID .= '_button';
102
    	}
103
        return $widgetID;
104
    }
105
106
    function &_get_form($defines, $additionalFormFields = null, $asUrl = false)
107
    {
108
        global $app_strings;
109
        global $currentModule;
110
111
        // Create the additional form fields with real values if they were not passed in
112
        if(empty($additionalFormFields) && $this->additional_form_fields)
113
        {
114
            foreach($this->additional_form_fields as $key=>$value)
115
            {
116
                if(!empty($defines['focus']->$value))
117
                {
118
                    $additionalFormFields[$key] = $defines['focus']->$value;
119
                }
120
                else
121
                {
122
                    $additionalFormFields[$key] = '';
123
                }
124
            }
125
        }
126
127
128
		if(!empty($this->module))
129
        {
130
            $defines['child_module_name'] = $this->module;
131
        }
132
        else
133
        {
134
            $defines['child_module_name'] = $defines['module'];
135
        }
136
137
        $defines['parent_bean_name'] = get_class( $defines['focus']);
138
		$relationship_name = $this->get_subpanel_relationship_name($defines);
139
140
141
        $formValues = array();
142
143
        //module_button is used to override the value of module name
144
        $formValues['module'] = $defines['child_module_name'];
145
        $formValues[strtolower($defines['parent_bean_name'])."_id"] = $defines['focus']->id;
146
147
        if(isset($defines['focus']->name))
148
        {
149
            $formValues[strtolower($defines['parent_bean_name'])."_name"] = $defines['focus']->name;
150
            // #26451,add these fields for custom one-to-many relate field.
151
            if(!empty($defines['child_module_name'])){
152
                $formValues[$relationship_name."_name"] = $defines['focus']->name;
153
            	$childFocusName = !empty($GLOBALS['beanList'][$defines['child_module_name']]) ? $GLOBALS['beanList'][$defines['child_module_name']] : "";
154
            	if(!empty($GLOBALS['dictionary'][ $childFocusName ]["fields"][$relationship_name .'_name']['id_name'])){
155
            		$formValues[$GLOBALS['dictionary'][ $childFocusName ]["fields"][$relationship_name .'_name']['id_name']] = $defines['focus']->id;
156
            	}
157
            }
158
        }
159
160
        $formValues['return_module'] = $currentModule;
161
162
        if($currentModule == 'Campaigns'){
163
            $formValues['return_action'] = "DetailView";
164
        }else{
165
            $formValues['return_action'] = $defines['action'];
166
            if ( $formValues['return_action'] == 'SubPanelViewer' ) {
167
                $formValues['return_action'] = 'DetailView';
168
            }
169
        }
170
171
        $formValues['return_id'] = $defines['focus']->id;
172
        $formValues['return_relationship'] = $relationship_name;
173
        switch ( strtolower( $currentModule ) )
174
        {
175
            case 'prospects' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
176
                $name = $defines['focus']->account_name ;
177
                break ;
178
            case 'documents' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
179
                $name = $defines['focus']->document_name ;
180
                break ;
181
            case 'kbdocuments' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
182
                $name = $defines['focus']->kbdocument_name ;
183
                break ;
184
            case 'leads' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
185
            case 'contacts' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
186
                $name = $defines['focus']->first_name . " " .$defines['focus']->last_name ;
187
                break ;
188
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
189
               $name = (isset($defines['focus']->name)) ? $defines['focus']->name : "";
190
        }
191
        $formValues['return_name'] = $name;
192
193
        // TODO: move this out and get $additionalFormFields working properly
194
        if(empty($additionalFormFields['parent_type']))
195
        {
196
            if($defines['focus']->object_name=='Contact') {
197
                $additionalFormFields['parent_type'] = 'Accounts';
198
            }
199
            else {
200
                $additionalFormFields['parent_type'] = $defines['focus']->module_dir;
201
            }
202
        }
203
        if(empty($additionalFormFields['parent_name']))
204
        {
205
            if($defines['focus']->object_name=='Contact') {
206
                $additionalFormFields['parent_name'] = $defines['focus']->account_name;
207
                $additionalFormFields['account_name'] = $defines['focus']->account_name;
208
            }
209
            else {
210
                $additionalFormFields['parent_name'] = $defines['focus']->name;
211
            }
212
        }
213
        if(empty($additionalFormFields['parent_id']))
214
        {
215
            if($defines['focus']->object_name=='Contact') {
216
                $additionalFormFields['parent_id'] = $defines['focus']->account_id;
217
                $additionalFormFields['account_id'] = $defines['focus']->account_id;
218
            } else if($defines['focus']->object_name=='Contract') {
219
            	$additionalFormFields['contract_id'] = $defines['focus']->id;
220
            } else {
221
                $additionalFormFields['parent_id'] = $defines['focus']->id;
222
            }
223
        }
224
225
        if ($defines['focus']->object_name=='Opportunity') {
226
            $additionalFormFields['account_id'] = $defines['focus']->account_id;
227
            $additionalFormFields['account_name'] = $defines['focus']->account_name;
228
        }
229
230
        if (!empty($defines['child_module_name']) and $defines['child_module_name']=='Contacts' and !empty($defines['parent_bean_name']) and $defines['parent_bean_name']=='contact' ) {
231
            if (!empty($defines['focus']->id ) and !empty($defines['focus']->name)) {
232
                $formValues['reports_to_id'] = $defines['focus']->id;
233
                $formValues['reports_to_name'] = $defines['focus']->name;
234
            }
235
        }
236
        $formValues['action'] = "EditView";
237
238
        if ( $asUrl ) {
239
            $returnLink = '';
240
            foreach($formValues as $key => $value ) {
241
                $returnLink .= $key.'='.$value.'&';
242
            }
243
            foreach($additionalFormFields as $key => $value ) {
244
                $returnLink .= $key.'='.$value.'&';
245
            }
246
            $returnLink = rtrim($returnLink,'&');
247
248
            return $returnLink;
249
        } else {
250
251
            $form = 'form' . $relationship_name;
252
            $button = '<form action="index.php" method="post" name="form" id="' . $form . "\">\n";
253
            foreach($formValues as $key => $value) {
254
                $button .= "<input type='hidden' name='" . $key . "' value='" . $value . "' />\n";
255
            }
256
257
            // fill in additional form fields for all but action
258
            foreach($additionalFormFields as $key => $value) {
259
                if($key != 'action') {
260
                    $button .= "<input type='hidden' name='" . $key . "' value='" . $value . "' />\n";
261
                }
262
            }
263
264
265
        return $button;
266
        }
267
    }
268
269
	/** This default function is used to create the HTML for a simple button */
270
	function display($defines, $additionalFormFields = null, $nonbutton = false)
271
	{
272
		$temp='';
273
		$inputID = $this->getWidgetId();
274
275
		if(!empty($this->acl) && ACLController::moduleSupportsACL($defines['module'])  &&  !ACLController::checkAccess($defines['module'], $this->acl, true)){
276
			return $temp;
277
		}
278
279
		global $app_strings;
280
281
        if ( isset($_REQUEST['layout_def_key']) && $_REQUEST['layout_def_key'] == 'UserEAPM' ) {
282
            // Subpanels generally don't go on the editview, so we have to handle this special
283
            $megaLink = $this->_get_form($defines, $additionalFormFields,true);
284
            //$button = "<input title='$this->title' accesskey='$this->access_key' class='button' type='submit' name='$inputID' id='$inputID' value='$this->form_value' onclick='javascript:document.location=\"index.php?".$megaLink."\"; return false;'/>";
285
        } else {
286
            $button = $this->_get_form($defines, $additionalFormFields);
287
            //$button .= "<input title='$this->title' accesskey='$this->access_key' class='' type='submit' name='$inputID' id='$inputID' value='$this->form_value' />\n</form>";
288
            $button .= "\n</form>";
289
        }
290
291
        if ($nonbutton) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
292
           // $button = "<a onclick=''>$this->form_value";
293
        }
294
        return $button;
295
	}
296
297
	/**
298
	 * Returns a string that is the JSON encoded version of the popup request.
299
	 * Perhaps this function should be moved to a more globally accessible location?
300
	 */
301
	function _create_json_encoded_popup_request($popup_request_data)
302
	{
303
	    return json_encode($popup_request_data);
304
	}
305
306
	/**
307
	 * get_subpanel_relationship_name
308
	 * Get the relationship name based on the subapnel definition
309
	 * @param mixed $defines The subpanel definition
310
	 */
311
	function get_subpanel_relationship_name($defines) {
312
		 $relationship_name = '';
313
		 if(!empty($defines)) {
314
		 	$relationship_name = isset($defines['module']) ? $defines['module'] : '';
315
	     	$dataSource = $defines['subpanel_definition']->get_data_source_name(true);
316
         	if (!empty($dataSource)) {
317
				$relationship_name = $dataSource;
318
				//Try to set the relationship name to the real relationship, not the link.
319
				if (!empty($defines['subpanel_definition']->parent_bean->field_defs[$dataSource])
320
				 && !empty($defines['subpanel_definition']->parent_bean->field_defs[$dataSource]['relationship']))
321
				{
322
					$relationship_name = $defines['subpanel_definition']->parent_bean->field_defs[$dataSource]['relationship'];
323
				}
324
			}
325
		 }
326
		 return $relationship_name;
327
	}
328
329
}
330
?>
331