]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Leads/LeadFormBase.php
Release 6.4.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-2011 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 $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
53  */
54 public function getDuplicateQuery($prefix='')
55 {
56         $query = "SELECT id, first_name, last_name, account_name, title FROM leads WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";
57
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'] ."')";
61         } else {
62                 $query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
63         }
64     return $query;
65 }
66
67
68 function getWideFormBody($prefix, $mod='', $formname=''){
69 if(!ACLController::checkAccess('Leads', 'edit', true)){
70                 return '';
71         }
72 global $mod_strings;
73 $temp_strings = $mod_strings;
74 if(!empty($mod)){
75         global $current_language;
76         $mod_strings = return_module_language($current_language, $mod);
77 }
78                 global $app_strings;
79                 global $current_user;
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'];
89                 $form = <<<EOQ
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>&nbsp;$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>
104
105 EOQ;
106
107
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;
116 return $form;
117 }
118
119 function getFormBody($prefix, $mod='', $formname=''){
120         if(!ACLController::checkAccess('Leads', 'edit', true)){
121                 return '';
122         }
123 global $mod_strings;
124 $temp_strings = $mod_strings;
125 if(!empty($mod)){
126         global $current_language;
127         $mod_strings = return_module_language($current_language, $mod);
128 }
129                 global $app_strings;
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'];
137                 $form = <<<EOQ
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>
146                 $lbl_phone<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>
150
151 EOQ;
152
153
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;
162 return $form;
163
164 }
165 function getForm($prefix, $mod='Leads'){
166         if(!ACLController::checkAccess('Leads', 'edit', true)){
167                 return '';
168         }
169 if(!empty($mod)){
170         global $current_language;
171         $mod_strings = return_module_language($current_language, $mod);
172 }else global $mod_strings;
173 global $app_strings;
174
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'];
178
179
180 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
181 $the_form .= <<<EOQ
182
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">
186 EOQ;
187 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
188 $the_form .= <<<EOQ
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>
190                 </form>
191
192 EOQ;
193 $the_form .= get_left_form_footer();
194 $the_form .= get_validate_record_js();
195
196 return $the_form;
197
198
199 }
200
201
202 function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
203
204     require_once('modules/Campaigns/utils.php');
205         require_once('include/formbase.php');
206
207         if(empty($exist_lead)) {
208         $focus = new Lead();
209     }
210     else {
211         $focus = $exist_lead;
212     }
213
214         if($useRequired &&  !checkRequired($prefix, array_keys($focus->required_fields))){
215                 return null;
216         }
217         $focus = populateFromPost($prefix, $focus);
218         if(!$focus->ACLAccess('Save')){
219                 ACLController::displayNoAccess(true);
220                 sugar_cleanup(true);
221         }
222
223     //Check for duplicate Leads
224     if (empty($_POST['record']) && empty($_POST['dup_checked']))
225     {
226                 $duplicateLeads = $this->checkForDuplicates($prefix);
227
228         if(isset($duplicateLeads))
229         {
230             //Set the redirect location to call the ShowDuplicates action.  This will map to view.showduplicates.php
231                         $location='module=Leads&action=ShowDuplicates';
232
233                         $get = '';
234
235                         if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
236                                 $get .= '&inbound_email_id='.$_POST['inbound_email_id'];
237                         }
238
239                         if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
240                                 $get .= '&Leadsrelate_to='.$_POST['relate_to'];
241                         }
242                         if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
243                                 $get .= '&Leadsrelate_id='.$_POST['relate_id'];
244                         }
245
246                         //add all of the post fields to redirect get string
247                         foreach ($focus->column_fields as $field)
248                         {
249                                 if (!empty($focus->$field) && !is_object($focus->$field))
250                                 {
251                                         $get .= "&Leads$field=".urlencode($focus->$field);
252                                 }
253                         }
254
255                         foreach ($focus->additional_column_fields as $field)
256                         {
257                                 if (!empty($focus->$field))
258                                 {
259                                         $get .= "&Leads$field=".urlencode($focus->$field);
260                                 }
261                         }
262
263                         if($focus->hasCustomFields()) {
264                                 foreach($focus->field_defs as $name=>$field) {
265                                         if (!empty($field['source']) && $field['source'] == 'custom_fields')
266                                         {
267                                                 $get .= "&Leads$name=".urlencode($focus->$name);
268                                         }
269                                 }
270                         }
271
272
273                         $emailAddress = new SugarEmailAddress();
274                         $get .= $emailAddress->getFormBaseURL($focus);
275
276
277                         //create list of suspected duplicate lead ids in redirect get string
278                         $i=0;
279                         foreach ($duplicateLeads as $lead)
280                         {
281                                 $get .= "&duplicate[$i]=".$lead['id'];
282                                 $i++;
283                         }
284
285                         //add return_module, return_action, and return_id to redirect get string
286                         $get .= "&return_module=";
287                         if(!empty($_POST['return_module']))
288             {
289                 $get .= $_POST['return_module'];
290             } else {
291                             $get .= "Leads";
292             }
293
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'];
299
300                         // for InboundEmail flow
301                         if(!empty($_POST['start'])) $get .= '&start='.$_POST['start'];
302
303             $_SESSION['SHOW_DUPLICATES'] = $get;
304
305             if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
306             {
307                 ob_clean();
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>";
312             } else {
313                 if(!empty($_POST['to_pdf']))
314                 {
315                     $location .= '&to_pdf='.$_POST['to_pdf'];
316                 }
317                 header("Location: index.php?$location");
318             }
319             return null;
320                 }
321     }
322
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;
325
326     if($do_save) {
327         if(!empty($GLOBALS['check_notify'])) {
328                 $focus->save($GLOBALS['check_notify']);
329         }
330         else {
331                 $focus->save(FALSE);
332         }
333     }
334
335     $return_id = $focus->id;
336
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;
343                 $prospect->save();
344
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
348
349             if(isset($focus->campaign_id) && $focus->campaign_id != null){
350                 campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
351             }
352         }
353         }
354
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;
361                 }
362
363                 // fake this case like it's already saved.
364
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';
371                 $email->save();
372                 $email->load_relationship('leads');
373                 $email->leads->add($focus->id);
374
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']);
376                 exit();
377         }
378         ////    END INBOUND EMAIL HANDLING
379         ///////////////////////////////////////////////////////////////////////////////
380
381         $GLOBALS['log']->debug("Saved record with id of ".$return_id);
382         if($redirect){
383                 handleRedirect($return_id, 'Leads');
384         }else{
385                 return $focus;
386         }
387 }
388
389
390
391 }
392
393
394 ?>