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.
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 $prefix String value of prefix that may be present in $_POST variables
52 * @return SQL String of the query that should be used for the initial duplicate lookup check
54 public function getDuplicateQuery($prefix='')
56 $query = "SELECT id, first_name, last_name, account_name, title FROM leads WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";
58 //Use the first and last name from the $_POST to filter. If only last name supplied use that
59 if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0) {
60 $query .= " (first_name='". $_POST[$prefix.'first_name'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
62 $query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
68 function getWideFormBody($prefix, $mod='', $formname=''){
69 if(!ACLController::checkAccess('Leads', 'edit', true)){
73 $temp_strings = $mod_strings;
75 global $current_language;
76 $mod_strings = return_module_language($current_language, $mod);
80 global $app_list_strings;
81 $primary_address_country_options = get_select_options_with_id($app_list_strings['countries_dom'], '');
82 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
83 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
84 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
85 $lbl_phone = $mod_strings['LBL_OFFICE_PHONE'];
86 $lbl_address = $mod_strings['LBL_PRIMARY_ADDRESS'];
87 $user_id = $current_user->id;
88 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
90 <input type="hidden" name="${prefix}record" value="">
91 <input type="hidden" name="${prefix}status" value="New">
92 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
93 <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>
94 <tr><td colspan='4'><hr></td></tr>
95 <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>
96 <tr><td colspan='4'><hr></td></tr>
97 <tr><td nowrap colspan='4'>$lbl_address<br><input type='text' name='${prefix}primary_address_street' size='80'></td></tr>
98 <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>
99 <tr><td colspan='4'><hr></td></tr>
100 <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>
101 <tr><td colspan='4'><hr></td></tr>
102 <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>
103 <tr><td nowrap colspan='4'>${mod_strings['LBL_DESCRIPTION']}<br><textarea cols='80' rows='4' name='${prefix}description' ></textarea></td></tr></table>
108 $javascript = new javascript();
109 $javascript->setFormName($formname);
110 $javascript->setSugarBean(new Lead());
111 $javascript->addField('email1','false',$prefix);
112 $javascript->addField('email2','false',$prefix);
113 $javascript->addRequiredFields($prefix);
114 $form .=$javascript->getScript();
115 $mod_strings = $temp_strings;
119 function getFormBody($prefix, $mod='', $formname=''){
120 if(!ACLController::checkAccess('Leads', 'edit', true)){
124 $temp_strings = $mod_strings;
126 global $current_language;
127 $mod_strings = return_module_language($current_language, $mod);
130 global $current_user;
131 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
132 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
133 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
134 $lbl_phone = $mod_strings['LBL_PHONE'];
135 $user_id = $current_user->id;
136 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
138 <input type="hidden" name="${prefix}record" value="">
139 <input type="hidden" name="${prefix}email2" value="">
140 <input type="hidden" name="${prefix}status" value="New">
141 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
142 <p> $lbl_first_name<br>
143 <input name="${prefix}first_name" type="text" value=""><br>
144 $lbl_last_name <span class="required">$lbl_required_symbol</span><br>
145 <input name='${prefix}last_name' type="text" value=""><br>
147 <input name='${prefix}phone_work' type="text" value=""><br>
148 $lbl_email_address<br>
149 <input name='${prefix}email1' type="text" value=""></p>
154 $javascript = new javascript();
155 $javascript->setFormName($formname);
156 $javascript->setSugarBean(new Lead());
157 $javascript->addField('email1','false',$prefix);
158 $javascript->addField('email2','false',$prefix);
159 $javascript->addRequiredFields($prefix);
160 $form .=$javascript->getScript();
161 $mod_strings = $temp_strings;
165 function getForm($prefix, $mod='Leads'){
166 if(!ACLController::checkAccess('Leads', 'edit', true)){
170 global $current_language;
171 $mod_strings = return_module_language($current_language, $mod);
172 }else global $mod_strings;
175 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
176 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
177 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
180 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
183 <form name="${prefix}LeadSave" onSubmit="return check_form('${prefix}LeadSave')" method="POST" action="index.php">
184 <input type="hidden" name="${prefix}module" value="Leads">
185 <input type="hidden" name="${prefix}action" value="Save">
187 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
189 <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>
193 $the_form .= get_left_form_footer();
194 $the_form .= get_validate_record_js();
202 function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
204 require_once('modules/Campaigns/utils.php');
205 require_once('include/formbase.php');
207 if(empty($exist_lead)) {
211 $focus = $exist_lead;
214 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))){
217 $focus = populateFromPost($prefix, $focus);
218 if(!$focus->ACLAccess('Save')){
219 ACLController::displayNoAccess(true);
223 //Check for duplicate Leads
224 if (empty($_POST['record']) && empty($_POST['dup_checked']))
226 $duplicateLeads = $this->checkForDuplicates($prefix);
228 if(isset($duplicateLeads))
230 //Set the redirect location to call the ShowDuplicates action. This will map to view.showduplicates.php
231 $location='module=Leads&action=ShowDuplicates';
235 if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
236 $get .= '&inbound_email_id='.$_POST['inbound_email_id'];
239 if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
240 $get .= '&Leadsrelate_to='.$_POST['relate_to'];
242 if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
243 $get .= '&Leadsrelate_id='.$_POST['relate_id'];
246 //add all of the post fields to redirect get string
247 foreach ($focus->column_fields as $field)
249 if (!empty($focus->$field) && !is_object($focus->$field))
251 $get .= "&Leads$field=".urlencode($focus->$field);
255 foreach ($focus->additional_column_fields as $field)
257 if (!empty($focus->$field))
259 $get .= "&Leads$field=".urlencode($focus->$field);
263 if($focus->hasCustomFields()) {
264 foreach($focus->field_defs as $name=>$field) {
265 if (!empty($field['source']) && $field['source'] == 'custom_fields')
267 $get .= "&Leads$name=".urlencode($focus->$name);
273 $emailAddress = new SugarEmailAddress();
274 $get .= $emailAddress->getFormBaseURL($focus);
277 //create list of suspected duplicate lead ids in redirect get string
279 foreach ($duplicateLeads as $lead)
281 $get .= "&duplicate[$i]=".$lead['id'];
285 //add return_module, return_action, and return_id to redirect get string
286 $get .= "&return_module=";
287 if(!empty($_POST['return_module']))
289 $get .= $_POST['return_module'];
294 $get .= "&return_action=";
295 if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
296 if(!empty($_POST['return_id'])) $get .= "&return_id=".$_POST['return_id'];
297 if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
298 if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
300 // for InboundEmail flow
301 if(!empty($_POST['start'])) $get .= '&start='.$_POST['start'];
303 $_SESSION['SHOW_DUPLICATES'] = $get;
305 if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
308 $json = getJSONobj();
309 echo $json->encode(array('status' => 'dupe', 'get' => $location));
310 } else if(!empty($_REQUEST['ajax_load'])) {
311 echo "<script>SUGAR.ajaxUI.loadContent('index.php?$location');</script>";
313 if(!empty($_POST['to_pdf']))
315 $location .= '&to_pdf='.$_POST['to_pdf'];
317 header("Location: index.php?$location");
323 if (!isset($_POST[$prefix.'email_opt_out'])) $focus->email_opt_out = 0;
324 if (!isset($_POST[$prefix.'do_not_call'])) $focus->do_not_call = 0;
327 if(!empty($GLOBALS['check_notify'])) {
328 $focus->save($GLOBALS['check_notify']);
335 $return_id = $focus->id;
337 if (isset($_POST[$prefix.'prospect_id']) && !empty($_POST[$prefix.'prospect_id'])) {
338 $prospect=new Prospect();
339 $prospect->retrieve($_POST[$prefix.'prospect_id']);
340 $prospect->lead_id=$focus->id;
341 // Set to keep email in target
342 $prospect->in_workflow = true;
345 //if prospect id exists, make sure we are coming from prospect detail
346 if(strtolower($_POST['return_module']) =='prospects' && strtolower($_POST['return_action']) == 'detailview'){
347 //create campaing_log entry
349 if(isset($focus->campaign_id) && $focus->campaign_id != null){
350 campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
355 ///////////////////////////////////////////////////////////////////////////////
356 //// INBOUND EMAIL HANDLING
357 ///////////////////////////////////////////////////////////////////////////////
358 if(isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) {
359 if(!isset($current_user)) {
360 global $current_user;
363 // fake this case like it's already saved.
365 $email = new Email();
366 $email->retrieve($_REQUEST['inbound_email_id']);
367 $email->parent_type = 'Leads';
368 $email->parent_id = $focus->id;
369 $email->assigned_user_id = $current_user->id;
370 $email->status = 'read';
372 $email->load_relationship('leads');
373 $email->leads->add($focus->id);
375 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']);
378 //// END INBOUND EMAIL HANDLING
379 ///////////////////////////////////////////////////////////////////////////////
381 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
383 handleRedirect($return_id, 'Leads');