]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Leads/LeadFormBase.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Leads / LeadFormBase.php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 require_once('include/SugarObjects/forms/PersonFormBase.php');
39
40 class LeadFormBase extends PersonFormBase {
41
42 var $moduleName = 'Leads';
43 var $objectName = 'Lead';
44
45 /**
46  * getDuplicateQuery
47  *
48  * This function returns the SQL String used for initial duplicate Leads check
49  *
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
54  */
55 public function getDuplicateQuery($focus, $prefix='')
56 {
57         $query = "SELECT id, first_name, last_name, account_name, title FROM leads ";
58
59     // Bug #46427 : Records from other Teams shown on Potential Duplicate Contacts screen during Lead Conversion
60     // add team security
61
62     $query .= " WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";
63     
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'] ."')";
67         } else {
68                 $query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
69         }
70     return $query;
71 }
72
73
74 function getWideFormBody($prefix, $mod='', $formname=''){
75 if(!ACLController::checkAccess('Leads', 'edit', true)){
76                 return '';
77         }
78 global $mod_strings;
79 $temp_strings = $mod_strings;
80 if(!empty($mod)){
81         global $current_language;
82         $mod_strings = return_module_language($current_language, $mod);
83 }
84                 global $app_strings;
85                 global $current_user;
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'];
95                 $form = <<<EOQ
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>&nbsp;$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>
110
111 EOQ;
112
113
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;
122 return $form;
123 }
124
125 function getFormBody($prefix, $mod='', $formname=''){
126         if(!ACLController::checkAccess('Leads', 'edit', true)){
127                 return '';
128         }
129 global $mod_strings;
130 $temp_strings = $mod_strings;
131 if(!empty($mod)){
132         global $current_language;
133         $mod_strings = return_module_language($current_language, $mod);
134 }
135                 global $app_strings;
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'];
143                 $form = <<<EOQ
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>
152                 $lbl_phone<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>
156
157 EOQ;
158
159
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;
168 return $form;
169
170 }
171 function getForm($prefix, $mod='Leads'){
172         if(!ACLController::checkAccess('Leads', 'edit', true)){
173                 return '';
174         }
175 if(!empty($mod)){
176         global $current_language;
177         $mod_strings = return_module_language($current_language, $mod);
178 }else global $mod_strings;
179 global $app_strings;
180
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'];
184
185
186 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
187 $the_form .= <<<EOQ
188
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">
192 EOQ;
193 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
194 $the_form .= <<<EOQ
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>
196                 </form>
197
198 EOQ;
199 $the_form .= get_left_form_footer();
200 $the_form .= get_validate_record_js();
201
202 return $the_form;
203
204
205 }
206
207
208 function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
209
210     require_once('modules/Campaigns/utils.php');
211         require_once('include/formbase.php');
212
213         if(empty($exist_lead)) {
214         $focus = new Lead();
215     }
216     else {
217         $focus = $exist_lead;
218     }
219
220         if($useRequired &&  !checkRequired($prefix, array_keys($focus->required_fields))){
221                 return null;
222         }
223         $focus = populateFromPost($prefix, $focus);
224         if(!$focus->ACLAccess('Save')){
225                 ACLController::displayNoAccess(true);
226                 sugar_cleanup(true);
227         }
228
229     //Check for duplicate Leads
230     if (empty($_POST['record']) && empty($_POST['dup_checked']))
231     {
232                 $duplicateLeads = $this->checkForDuplicates($prefix);
233
234         if(isset($duplicateLeads))
235         {
236             //Set the redirect location to call the ShowDuplicates action.  This will map to view.showduplicates.php
237                         $location='module=Leads&action=ShowDuplicates';
238
239                         $get = '';
240
241                         if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
242                                 $get .= '&inbound_email_id='.$_POST['inbound_email_id'];
243                         }
244
245                         if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
246                                 $get .= '&Leadsrelate_to='.$_POST['relate_to'];
247                         }
248                         if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
249                                 $get .= '&Leadsrelate_id='.$_POST['relate_id'];
250                         }
251
252                         //add all of the post fields to redirect get string
253                         foreach ($focus->column_fields as $field)
254                         {
255                                 if (!empty($focus->$field) && !is_object($focus->$field))
256                                 {
257                                         $get .= "&Leads$field=".urlencode($focus->$field);
258                                 }
259                         }
260
261                         foreach ($focus->additional_column_fields as $field)
262                         {
263                                 if (!empty($focus->$field))
264                                 {
265                                         $get .= "&Leads$field=".urlencode($focus->$field);
266                                 }
267                         }
268
269                         if($focus->hasCustomFields()) {
270                                 foreach($focus->field_defs as $name=>$field) {
271                                         if (!empty($field['source']) && $field['source'] == 'custom_fields')
272                                         {
273                                                 $get .= "&Leads$name=".urlencode($focus->$name);
274                                         }
275                                 }
276                         }
277
278
279                         $emailAddress = new SugarEmailAddress();
280                         $get .= $emailAddress->getFormBaseURL($focus);
281
282
283                         //create list of suspected duplicate lead ids in redirect get string
284                         $i=0;
285                         foreach ($duplicateLeads as $lead)
286                         {
287                                 $get .= "&duplicate[$i]=".$lead['id'];
288                                 $i++;
289                         }
290
291                         //add return_module, return_action, and return_id to redirect get string
292                         $get .= "&return_module=";
293                         if(!empty($_POST['return_module']))
294             {
295                 $get .= $_POST['return_module'];
296             } else {
297                             $get .= "Leads";
298             }
299
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'];
305
306                         // for InboundEmail flow
307                         if(!empty($_POST['start'])) $get .= '&start='.$_POST['start'];
308
309             $_SESSION['SHOW_DUPLICATES'] = $get;
310
311             if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
312             {
313                 ob_clean();
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>";
318             } else {
319                 if(!empty($_POST['to_pdf']))
320                 {
321                     $location .= '&to_pdf='.$_POST['to_pdf'];
322                 }
323                 header("Location: index.php?$location");
324             }
325             return null;
326                 }
327     }
328
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;
331
332     if($do_save) {
333         if(!empty($GLOBALS['check_notify'])) {
334                 $focus->save($GLOBALS['check_notify']);
335         }
336         else {
337                 $focus->save(FALSE);
338         }
339     }
340
341     $return_id = $focus->id;
342
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;
349                 $prospect->save();
350
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
354
355             if(isset($focus->campaign_id) && $focus->campaign_id != null){
356                 campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
357             }
358         }
359         }
360
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;
367                 }
368
369                 // fake this case like it's already saved.
370
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';
377                 $email->save();
378                 $email->load_relationship('leads');
379                 $email->leads->add($focus->id);
380
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']);
382                 exit();
383         }
384         ////    END INBOUND EMAIL HANDLING
385         ///////////////////////////////////////////////////////////////////////////////
386
387         $GLOBALS['log']->debug("Saved record with id of ".$return_id);
388         if($redirect){
389                 handleRedirect($return_id, 'Leads');
390         }else{
391                 return $focus;
392         }
393 }
394
395
396
397 }
398
399
400 ?>