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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once('include/SugarObjects/forms/PersonFormBase.php');
40 class LeadFormBase extends PersonFormBase {
42 var $moduleName = 'Leads';
43 var $objectName = 'Lead';
48 * This function returns the SQL String used for initial duplicate Leads check
50 * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
51 * @param $focus sugarbean
52 * @param $prefix String value of prefix that may be present in $_POST variables
53 * @return SQL String of the query that should be used for the initial duplicate lookup check
55 public function getDuplicateQuery($focus, $prefix='')
57 $query = "SELECT id, first_name, last_name, account_name, title FROM leads ";
59 // Bug #46427 : Records from other Teams shown on Potential Duplicate Contacts screen during Lead Conversion
62 $query .= " WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";
64 //Use the first and last name from the $_POST to filter. If only last name supplied use that
65 if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0) {
66 $query .= " (first_name='". $_POST[$prefix.'first_name'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
68 $query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
74 function getWideFormBody($prefix, $mod='', $formname=''){
75 if(!ACLController::checkAccess('Leads', 'edit', true)){
79 $temp_strings = $mod_strings;
81 global $current_language;
82 $mod_strings = return_module_language($current_language, $mod);
86 global $app_list_strings;
87 $primary_address_country_options = get_select_options_with_id($app_list_strings['countries_dom'], '');
88 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
89 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
90 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
91 $lbl_phone = $mod_strings['LBL_OFFICE_PHONE'];
92 $lbl_address = $mod_strings['LBL_PRIMARY_ADDRESS'];
93 $user_id = $current_user->id;
94 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
96 <input type="hidden" name="${prefix}record" value="">
97 <input type="hidden" name="${prefix}status" value="New">
98 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
99 <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> $lbl_last_name<br><input name='${prefix}last_name' type="text" value=""></td></tr>
100 <tr><td colspan='4'><hr></td></tr>
101 <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>
102 <tr><td colspan='4'><hr></td></tr>
103 <tr><td nowrap colspan='4'>$lbl_address<br><input type='text' name='${prefix}primary_address_street' size='80'></td></tr>
104 <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>
105 <tr><td colspan='4'><hr></td></tr>
106 <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>
107 <tr><td colspan='4'><hr></td></tr>
108 <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>
109 <tr><td nowrap colspan='4'>${mod_strings['LBL_DESCRIPTION']}<br><textarea cols='80' rows='4' name='${prefix}description' ></textarea></td></tr></table>
114 $javascript = new javascript();
115 $javascript->setFormName($formname);
116 $javascript->setSugarBean(new Lead());
117 $javascript->addField('email1','false',$prefix);
118 $javascript->addField('email2','false',$prefix);
119 $javascript->addRequiredFields($prefix);
120 $form .=$javascript->getScript();
121 $mod_strings = $temp_strings;
125 function getFormBody($prefix, $mod='', $formname=''){
126 if(!ACLController::checkAccess('Leads', 'edit', true)){
130 $temp_strings = $mod_strings;
132 global $current_language;
133 $mod_strings = return_module_language($current_language, $mod);
136 global $current_user;
137 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
138 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
139 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
140 $lbl_phone = $mod_strings['LBL_PHONE'];
141 $user_id = $current_user->id;
142 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
144 <input type="hidden" name="${prefix}record" value="">
145 <input type="hidden" name="${prefix}email2" value="">
146 <input type="hidden" name="${prefix}status" value="New">
147 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
148 <p> $lbl_first_name<br>
149 <input name="${prefix}first_name" type="text" value=""><br>
150 $lbl_last_name <span class="required">$lbl_required_symbol</span><br>
151 <input name='${prefix}last_name' type="text" value=""><br>
153 <input name='${prefix}phone_work' type="text" value=""><br>
154 $lbl_email_address<br>
155 <input name='${prefix}email1' type="text" value=""></p>
160 $javascript = new javascript();
161 $javascript->setFormName($formname);
162 $javascript->setSugarBean(new Lead());
163 $javascript->addField('email1','false',$prefix);
164 $javascript->addField('email2','false',$prefix);
165 $javascript->addRequiredFields($prefix);
166 $form .=$javascript->getScript();
167 $mod_strings = $temp_strings;
171 function getForm($prefix, $mod='Leads'){
172 if(!ACLController::checkAccess('Leads', 'edit', true)){
176 global $current_language;
177 $mod_strings = return_module_language($current_language, $mod);
178 }else global $mod_strings;
181 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
182 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
183 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
186 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
189 <form name="${prefix}LeadSave" onSubmit="return check_form('${prefix}LeadSave')" method="POST" action="index.php">
190 <input type="hidden" name="${prefix}module" value="Leads">
191 <input type="hidden" name="${prefix}action" value="Save">
193 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
195 <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 $the_form .= get_left_form_footer();
200 $the_form .= get_validate_record_js();
208 function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
210 require_once('modules/Campaigns/utils.php');
211 require_once('include/formbase.php');
213 if(empty($exist_lead)) {
217 $focus = $exist_lead;
220 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))){
223 $focus = populateFromPost($prefix, $focus);
224 if(!$focus->ACLAccess('Save')){
225 ACLController::displayNoAccess(true);
229 //Check for duplicate Leads
230 if (empty($_POST['record']) && empty($_POST['dup_checked']))
232 $duplicateLeads = $this->checkForDuplicates($prefix);
234 if(isset($duplicateLeads))
236 //Set the redirect location to call the ShowDuplicates action. This will map to view.showduplicates.php
237 $location='module=Leads&action=ShowDuplicates';
241 if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
242 $get .= '&inbound_email_id='.$_POST['inbound_email_id'];
245 if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
246 $get .= '&Leadsrelate_to='.$_POST['relate_to'];
248 if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
249 $get .= '&Leadsrelate_id='.$_POST['relate_id'];
252 //add all of the post fields to redirect get string
253 foreach ($focus->column_fields as $field)
255 if (!empty($focus->$field) && !is_object($focus->$field))
257 $get .= "&Leads$field=".urlencode($focus->$field);
261 foreach ($focus->additional_column_fields as $field)
263 if (!empty($focus->$field))
265 $get .= "&Leads$field=".urlencode($focus->$field);
269 if($focus->hasCustomFields()) {
270 foreach($focus->field_defs as $name=>$field) {
271 if (!empty($field['source']) && $field['source'] == 'custom_fields')
273 $get .= "&Leads$name=".urlencode($focus->$name);
279 $emailAddress = new SugarEmailAddress();
280 $get .= $emailAddress->getFormBaseURL($focus);
283 //create list of suspected duplicate lead ids in redirect get string
285 foreach ($duplicateLeads as $lead)
287 $get .= "&duplicate[$i]=".$lead['id'];
291 //add return_module, return_action, and return_id to redirect get string
292 $get .= "&return_module=";
293 if(!empty($_POST['return_module']))
295 $get .= $_POST['return_module'];
300 $get .= "&return_action=";
301 if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
302 if(!empty($_POST['return_id'])) $get .= "&return_id=".$_POST['return_id'];
303 if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
304 if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
306 // for InboundEmail flow
307 if(!empty($_POST['start'])) $get .= '&start='.$_POST['start'];
309 $_SESSION['SHOW_DUPLICATES'] = $get;
311 if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
314 $json = getJSONobj();
315 echo $json->encode(array('status' => 'dupe', 'get' => $location));
316 } else if(!empty($_REQUEST['ajax_load'])) {
317 echo "<script>SUGAR.ajaxUI.loadContent('index.php?$location');</script>";
319 if(!empty($_POST['to_pdf']))
321 $location .= '&to_pdf='.$_POST['to_pdf'];
323 header("Location: index.php?$location");
329 if (!isset($_POST[$prefix.'email_opt_out'])) $focus->email_opt_out = 0;
330 if (!isset($_POST[$prefix.'do_not_call'])) $focus->do_not_call = 0;
333 if(!empty($GLOBALS['check_notify'])) {
334 $focus->save($GLOBALS['check_notify']);
341 $return_id = $focus->id;
343 if (isset($_POST[$prefix.'prospect_id']) && !empty($_POST[$prefix.'prospect_id'])) {
344 $prospect=new Prospect();
345 $prospect->retrieve($_POST[$prefix.'prospect_id']);
346 $prospect->lead_id=$focus->id;
347 // Set to keep email in target
348 $prospect->in_workflow = true;
351 //if prospect id exists, make sure we are coming from prospect detail
352 if(strtolower($_POST['return_module']) =='prospects' && strtolower($_POST['return_action']) == 'detailview'){
353 //create campaing_log entry
355 if(isset($focus->campaign_id) && $focus->campaign_id != null){
356 campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
361 ///////////////////////////////////////////////////////////////////////////////
362 //// INBOUND EMAIL HANDLING
363 ///////////////////////////////////////////////////////////////////////////////
364 if(isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) {
365 if(!isset($current_user)) {
366 global $current_user;
369 // fake this case like it's already saved.
371 $email = new Email();
372 $email->retrieve($_REQUEST['inbound_email_id']);
373 $email->parent_type = 'Leads';
374 $email->parent_id = $focus->id;
375 $email->assigned_user_id = $current_user->id;
376 $email->status = 'read';
378 $email->load_relationship('leads');
379 $email->leads->add($focus->id);
381 header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=".$_REQUEST['inbound_email_id']."&parent_id=".$email->parent_id."&parent_type=".$email->parent_type.'&start='.$_REQUEST['start']);
384 //// END INBOUND EMAIL HANDLING
385 ///////////////////////////////////////////////////////////////////////////////
387 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
389 handleRedirect($return_id, 'Leads');