Test Failed
Push — CI ( 0f01dd...c95a04 )
by Adam
55:13
created

LeadFormBase::handleSave()   F

Complexity

Conditions 46
Paths > 20000

Size

Total Lines 185
Code Lines 98

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 185
rs 2
cc 46
eloc 98
nc 29186
nop 5

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 - 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
require_once('include/SugarObjects/forms/PersonFormBase.php');
42
43
class LeadFormBase extends PersonFormBase {
44
45
var $moduleName = 'Leads';
46
var $objectName = 'Lead';
47
48
/**
49
 * getDuplicateQuery
50
 *
51
 * This function returns the SQL String used for initial duplicate Leads check
52
 *
53
 * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
54
 * @param $focus sugarbean
55
 * @param $prefix String value of prefix that may be present in $_POST variables
56
 * @return SQL String of the query that should be used for the initial duplicate lookup check
57
 */
58
public function getDuplicateQuery($focus, $prefix='')
59
{
60
	$query = "SELECT id, first_name, last_name, account_name, title FROM leads ";
61
62
    // Bug #46427 : Records from other Teams shown on Potential Duplicate Contacts screen during Lead Conversion
63
    // add team security
64
65
    $query .= " WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";
66
67
    //Use the first and last name from the $_POST to filter.  If only last name supplied use that
68
	if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0) {
69
		$query .= " (first_name='". $_POST[$prefix.'first_name'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
70
	} else {
71
		$query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
72
	}
73
    return $query;
74
}
75
76
77
function getWideFormBody($prefix, $mod='', $formname=''){
78
if(!ACLController::checkAccess('Leads', 'edit', true)){
79
		return '';
80
	}
81
global $mod_strings;
82
$temp_strings = $mod_strings;
83
if(!empty($mod)){
84
	global $current_language;
85
	$mod_strings = return_module_language($current_language, $mod);
86
}
87
		global $app_strings;
88
		global $current_user;
89
		global $app_list_strings;
90
		$primary_address_country_options = get_select_options_with_id($app_list_strings['countries_dom'], '');
91
		$lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
92
		$lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
93
		$lbl_last_name = $mod_strings['LBL_LAST_NAME'];
94
		$lbl_phone = $mod_strings['LBL_OFFICE_PHONE'];
95
		$lbl_address =  $mod_strings['LBL_PRIMARY_ADDRESS'];
96
		$user_id = $current_user->id;
97
		$lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
98
		$form = <<<EOQ
99
		<input type="hidden" name="${prefix}record" value="">
100
		<input type="hidden" name="${prefix}status" value="New">
101
		<input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
102
		<table class='evenListRow' border='0' width='100%'><tr><td nowrap cospan='1'>$lbl_first_name<br><input name="${prefix}first_name" type="text" value=""></td><td colspan='1'><FONT class="required">$lbl_required_symbol</FONT>&nbsp;$lbl_last_name<br><input name='${prefix}last_name' type="text" value=""></td></tr>
103
		<tr><td colspan='4'><hr></td></tr>
104
		<tr><td nowrap colspan='1'>${mod_strings['LBL_TITLE']}<br><input name='${prefix}title' type="text" value=""></td><td nowrap colspan='1'>${mod_strings['LBL_DEPARTMENT']}<br><input name='${prefix}department' type="text" value=""></td></tr>
105
		<tr><td colspan='4'><hr></td></tr>
106
		<tr><td nowrap colspan='4'>$lbl_address<br><input type='text' name='${prefix}primary_address_street' size='80'></td></tr>
107
		<tr><td> ${mod_strings['LBL_CITY']}<BR><input name='${prefix}primary_address_city'  maxlength='100' value=''></td><td>${mod_strings['LBL_STATE']}<BR><input name='${prefix}primary_address_state'  maxlength='100' value=''></td><td>${mod_strings['LBL_POSTAL_CODE']}<BR><input name='${prefix}primary_address_postalcode'  maxlength='100' value=''></td><td>${mod_strings['LBL_COUNTRY']}<BR><select name='${prefix}primary_address_country' size='1'>{$primary_address_country_options}</select></td></tr>
108
		<tr><td colspan='4'><hr></td></tr>
109
		<tr><td nowrap >$lbl_phone<br><input name='${prefix}phone_work' type="text" value=""></td><td nowrap >${mod_strings['LBL_MOBILE_PHONE']}<br><input name='${prefix}phone_mobile' type="text" value=""></td><td nowrap >${mod_strings['LBL_FAX_PHONE']}<br><input name='${prefix}phone_fax' type="text" value=""></td><td nowrap >${mod_strings['LBL_HOME_PHONE']}<br><input name='${prefix}phone_home' type="text" value=""></td></tr>
110
		<tr><td colspan='4'><hr></td></tr>
111
		<tr><td nowrap colspan='1'>$lbl_email_address<br><input name='${prefix}email1' type="text" value=""></td><td nowrap colspan='1'>${mod_strings['LBL_OTHER_EMAIL_ADDRESS']}<br><input name='${prefix}email2' type="text" value=""></td></tr>
112
		<tr><td nowrap colspan='4'>${mod_strings['LBL_DESCRIPTION']}<br><textarea cols='80' rows='4' name='${prefix}description' ></textarea></td></tr></table>
113
114
EOQ;
115
116
117
$javascript = new javascript();
118
$javascript->setFormName($formname);
119
$javascript->setSugarBean(new Lead());
120
$javascript->addField('email1','false',$prefix);
121
$javascript->addField('email2','false',$prefix);
122
$javascript->addRequiredFields($prefix);
123
$form .=$javascript->getScript();
124
$mod_strings = $temp_strings;
125
return $form;
126
}
127
128
function getFormBody($prefix, $mod='', $formname=''){
129
	if(!ACLController::checkAccess('Leads', 'edit', true)){
130
		return '';
131
	}
132
global $mod_strings;
133
$temp_strings = $mod_strings;
134
if(!empty($mod)){
135
	global $current_language;
136
	$mod_strings = return_module_language($current_language, $mod);
137
}
138
		global $app_strings;
139
		global $current_user;
140
		$lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
141
		$lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
142
		$lbl_last_name = $mod_strings['LBL_LAST_NAME'];
143
		$lbl_phone = $mod_strings['LBL_PHONE'];
144
		$user_id = $current_user->id;
145
		$lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
146
		$form = <<<EOQ
147
		<input type="hidden" name="${prefix}record" value="">
148
		<input type="hidden" name="${prefix}email2" value="">
149
		<input type="hidden" name="${prefix}status" value="New">
150
		<input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
151
<p>		$lbl_first_name<br>
152
		<input name="${prefix}first_name" type="text" value=""><br>
153
		$lbl_last_name <span class="required">$lbl_required_symbol</span><br>
154
		<input name='${prefix}last_name' type="text" value=""><br>
155
		$lbl_phone<br>
156
		<input name='${prefix}phone_work' type="text" value=""><br>
157
		$lbl_email_address<br>
158
		<input name='${prefix}email1' type="text" value=""></p>
159
160
EOQ;
161
162
163
$javascript = new javascript();
164
$javascript->setFormName($formname);
165
$javascript->setSugarBean(new Lead());
166
$javascript->addField('email1','false',$prefix);
167
$javascript->addField('email2','false',$prefix);
168
$javascript->addRequiredFields($prefix);
169
$form .=$javascript->getScript();
170
$mod_strings = $temp_strings;
171
return $form;
172
173
}
174
function getForm($prefix, $mod='Leads'){
175
	if(!ACLController::checkAccess('Leads', 'edit', true)){
176
		return '';
177
	}
178
if(!empty($mod)){
179
	global $current_language;
180
	$mod_strings = return_module_language($current_language, $mod);
181
}else global $mod_strings;
182
global $app_strings;
183
184
$lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
185
$lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
186
$lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
187
188
189
$the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
190
$the_form .= <<<EOQ
191
192
		<form name="${prefix}LeadSave" onSubmit="return check_form('${prefix}LeadSave')" method="POST" action="index.php">
193
			<input type="hidden" name="${prefix}module" value="Leads">
194
			<input type="hidden" name="${prefix}action" value="Save">
195
EOQ;
196
$the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
197
$the_form .= <<<EOQ
198
		<p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="${prefix}button" value="  $lbl_save_button_label  " ></p>
199
		</form>
200
201
EOQ;
202
$the_form .= get_left_form_footer();
203
$the_form .= get_validate_record_js();
204
205
return $the_form;
206
207
208
}
209
210
211
function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
212
213
    require_once('modules/Campaigns/utils.php');
214
	require_once('include/formbase.php');
215
216
	if(empty($exist_lead)) {
217
        $focus = new Lead();
218
    }
219
    else {
220
        $focus = $exist_lead;
221
    }
222
223
	if($useRequired &&  !checkRequired($prefix, array_keys($focus->required_fields))){
224
		return null;
225
	}
226
	$focus = populateFromPost($prefix, $focus);
227
	if(!$focus->ACLAccess('Save')){
228
		ACLController::displayNoAccess(true);
229
		sugar_cleanup(true);
230
	}
231
232
    //Check for duplicate Leads
233
    if (empty($_POST['record']) && empty($_POST['dup_checked']))
234
    {
235
		$duplicateLeads = $this->checkForDuplicates($prefix);
236
237
        if(isset($duplicateLeads))
238
        {
239
            //Set the redirect location to call the ShowDuplicates action.  This will map to view.showduplicates.php
240
			$location='module=Leads&action=ShowDuplicates';
241
242
			$get = '';
243
244
			if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
245
				$get .= '&inbound_email_id='.$_POST['inbound_email_id'];
246
			}
247
248
			if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
249
				$get .= '&Leadsrelate_to='.$_POST['relate_to'];
250
			}
251
			if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
252
				$get .= '&Leadsrelate_id='.$_POST['relate_id'];
253
			}
254
255
			//add all of the post fields to redirect get string
256
			foreach ($focus->column_fields as $field)
257
			{
258
				if (!empty($focus->$field) && !is_object($focus->$field))
259
				{
260
					$get .= "&Leads$field=".urlencode($focus->$field);
261
				}
262
			}
263
264
			foreach ($focus->additional_column_fields as $field)
265
			{
266
				if (!empty($focus->$field))
267
				{
268
					$get .= "&Leads$field=".urlencode($focus->$field);
269
				}
270
			}
271
272
			if($focus->hasCustomFields()) {
273
				foreach($focus->field_defs as $name=>$field) {
274
					if (!empty($field['source']) && $field['source'] == 'custom_fields')
275
					{
276
						$get .= "&Leads$name=". (!empty($focus->$name) ? urlencode($focus->$name) : '');
277
					}
278
				}
279
			}
280
281
282
			$emailAddress = new SugarEmailAddress();
283
			$get .= $emailAddress->getFormBaseURL($focus);
284
285
286
			//create list of suspected duplicate lead ids in redirect get string
287
			$i=0;
288
			foreach ($duplicateLeads as $lead)
289
			{
290
				$get .= "&duplicate[$i]=".$lead['id'];
291
				$i++;
292
			}
293
294
			//add return_module, return_action, and return_id to redirect get string
295
			$get .= "&return_module=";
296
			if(!empty($_POST['return_module']))
297
            {
298
                $get .= $_POST['return_module'];
299
            } else {
300
			    $get .= "Leads";
301
            }
302
303
            //add return_module, return_action, and return_id to redirect get string
304
			$urlData = array('return_module' => 'Leads', 'return_action' => '');
305
			foreach (array('return_module', 'return_action', 'return_id', 'popup', 'create', 'start') as $var) {
306
			    if (!empty($_POST[$var])) {
307
			        $urlData[$var] = $_POST[$var];
308
			    }
309
			}
310
			$get .= "&".http_build_query($urlData);
311
			$_SESSION['SHOW_DUPLICATES'] = $get;
312
313
            if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
314
            {
315
                ob_clean();
316
                $json = getJSONobj();
317
                echo $json->encode(array('status' => 'dupe', 'get' => $location));
318
            } else if(!empty($_REQUEST['ajax_load'])) {
319
                echo "<script>SUGAR.ajaxUI.loadContent('index.php?$location');</script>";
320
            } else {
321
                if(!empty($_POST['to_pdf']))
322
                {
323
                    $location .= '&to_pdf='.urlencode($_POST['to_pdf']);
324
                }
325
                header("Location: index.php?$location");
326
            }
327
            return null;
328
		}
329
    }
330
331
	if (!isset($_POST[$prefix.'email_opt_out'])) $focus->email_opt_out = 0;
332
	if (!isset($_POST[$prefix.'do_not_call'])) $focus->do_not_call = 0;
333
334
    if($do_save) {
335
    	if(!empty($GLOBALS['check_notify'])) {
336
    		$focus->save($GLOBALS['check_notify']);
337
    	}
338
    	else {
339
    		$focus->save(FALSE);
340
    	}
341
    }
342
343
    $return_id = $focus->id;
344
345
	if (isset($_POST[$prefix.'prospect_id']) &&  !empty($_POST[$prefix.'prospect_id'])) {
346
		$prospect=new Prospect();
347
		$prospect->retrieve($_POST[$prefix.'prospect_id']);
348
		$prospect->lead_id=$focus->id;
349
		// Set to keep email in target
350
		$prospect->in_workflow = true;
351
		$prospect->save();
352
353
        //if prospect id exists, make sure we are coming from prospect detail
354
        if(strtolower($_POST['return_module']) =='prospects' && strtolower($_POST['return_action']) == 'detailview'){
355
            //create campaing_log entry
356
357
            if(isset($focus->campaign_id) && $focus->campaign_id != null){
358
                campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
0 ignored issues
show
Deprecated Code introduced by
The function campaign_log_lead_entry() has been deprecated with message: 62_Joneses - June 24, 2011

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
359
            }
360
        }
361
	}
362
363
	///////////////////////////////////////////////////////////////////////////////
364
	////	INBOUND EMAIL HANDLING
365
	///////////////////////////////////////////////////////////////////////////////
366
	if(isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) {
367
		if(!isset($current_user)) {
0 ignored issues
show
Bug introduced by
The variable $current_user seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
368
			global $current_user;
369
		}
370
371
		// fake this case like it's already saved.
372
373
		$email = new Email();
374
		$email->retrieve($_REQUEST['inbound_email_id']);
375
		$email->parent_type = 'Leads';
376
		$email->parent_id = $focus->id;
377
		$email->assigned_user_id = $current_user->id;
378
		$email->status = 'read';
379
		$email->save();
380
		$email->load_relationship('leads');
381
		$email->leads->add($focus->id);
382
383
            header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=".urlencode($_REQUEST['inbound_email_id'])."&parent_id=".$email->parent_id."&parent_type=".$email->parent_type.'&start='.urlencode($_REQUEST['start']));
384
            exit();
385
        }
386
        ////	END INBOUND EMAIL HANDLING
387
        ///////////////////////////////////////////////////////////////////////////////
388
389
	$GLOBALS['log']->debug("Saved record with id of ".$return_id);
390
	if($redirect){
391
		handleRedirect($return_id, 'Leads');
392
	}else{
393
		return $focus;
394
	}
395
}
396
397
398
399
}
400
401
402
?>
403