]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Leads/LeadFormBase.php
Release 6.2.3
[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 class LeadFormBase  {
39
40 function checkForDuplicates($prefix, $id){
41         require_once('include/formbase.php');
42         
43         $focus = new Lead();
44         if(!checkRequired($prefix, array_keys($focus->required_fields))){
45                 return null;
46         }
47         $query = '';
48         $baseQuery = "select id,first_name, last_name,account_name, title  from leads where deleted!=1 and id!='$id' and (status!='Converted' or status is NULL) and ";
49         if(isset($_POST[$prefix.'first_name']) && !empty($_POST[$prefix.'first_name']) && isset($_POST[$prefix.'last_name']) && !empty($_POST[$prefix.'last_name'])){
50                 $query = $baseQuery ." (first_name='". $_POST[$prefix.'first_name'] . "' and last_name = '". $_POST[$prefix.'last_name'] ."')";
51         }else{
52                         $query = $baseQuery ."  last_name = '". $_POST[$prefix.'last_name'] ."'";
53         }
54         $rows = array();
55     global $db;
56         $result = $db->query($query);
57         while (($row = $db->fetchByAssoc($result)) != null) {
58                 if(!isset($rows[$row['id']])) {
59                    $rows[]=$row;
60                 }
61         }
62
63         $emailStr="";
64         if(isset($_POST[$prefix.'email1']) && !empty($_POST[$prefix.'email1'])){
65                 $emailStr="'". strtoupper($_POST[$prefix.'email1']) ."'";
66         }
67         if(isset($_POST[$prefix.'email2']) && !empty($_POST[$prefix.'email2'])){
68                 if (!empty($emailStr)) $emailStr.=",";
69                 $emailStr="'". strtoupper($_POST[$prefix.'email2']) ."'";
70         }
71
72         if(!empty($emailStr) > 0) {
73                 $query = 'SELECT DISTINCT er.bean_id AS id FROM email_addr_bean_rel er, ' .
74                          'email_addresses ea WHERE ea.id = er.email_address_id ' .
75                          'AND ea.deleted = 0 AND er.deleted = 0 AND er.bean_module = \'Contacts\' ' .
76                      'AND email_address_caps IN (' . $emailStr . ')';
77                 $result = $db->query($query);
78                 while (($row= $db->fetchByAssoc($result)) != null) {
79                         if(!isset($rows[$row['id']])) {
80                            $query2 = "SELECT id, first_name, last_name, title FROM contacts WHERE deleted = 0 AND id = '" . $row['id'] . "'";
81                            $result2 = $db->query($query2);
82                            $r = $db->fetchByAssoc($result2);
83                            if(isset($r['id'])) {
84                                   $rows[]=$r;
85                            }
86                         } //if
87                 }
88         } //if
89         
90     return !empty($rows) ? $rows : null;
91 }
92
93
94 function buildTableForm($rows, $mod=''){
95         if(!empty($mod)){
96         global $current_language;
97         $mod_strings = return_module_language($current_language, $mod);
98         }else global $mod_strings;
99         global $app_strings;
100         $cols = sizeof($rows[0]) * 2 + 1;
101         $form = '<table width="100%"><tr><td>'.$mod_strings['MSG_DUPLICATE']. '</td></tr><tr><td height="20"></td></tr></table>';
102         $form .= "<form action='index.php' method='post' name='dupLeads'><input type='hidden' name='selectedLead' value=''>";
103          $form .= get_form_header($mod_strings['LBL_DUPLICATE'],"", '');
104         $form .= "<table width='100%' cellpadding='0' cellspacing='0'>  <tr >   <td ></td>";
105
106
107         require_once('include/formbase.php');
108         $form .= getPostToForm();
109
110         if(isset($rows[0])){
111                 foreach ($rows[0] as $key=>$value){
112                         if($key != 'id'){
113
114
115                                         $form .= "<td scope='col' >". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
116                         }
117                 }
118                 $form .= "</tr>";
119         }
120         $rowColor = 'oddListRowS1';
121         foreach($rows as $row){
122
123
124                 $form .= "<tr class='$rowColor'>";
125                 $form .= "<td width='1%' nowrap='nowrap' align='center'><input type='checkbox' name='selectedLeads[]' value='{$row['id']}'></td>";
126                 $wasSet = false;
127
128                 foreach ($row as $key=>$value){
129                                 if($key != 'id'){
130
131                                         if(!$wasSet){
132                                                 $form .= "<td scope='row'><a target='_blank' href='index.php?module=Leads&action=DetailView&record=${row['id']}'>$value</a></td>";
133                                                 $wasSet = true;
134                                         }else{
135                                         $form .= "<td><a target='_blank' href='index.php?module=Leads&action=DetailView&record=${row['id']}'>$value</a></td>";
136                 }}
137                 }
138                 if($rowColor == 'evenListRowS1'){
139                         $rowColor = 'oddListRowS1';
140                 }else{
141                          $rowColor = 'evenListRowS1';
142                 }
143                 $form .= "</tr>";
144         }
145                 $form .= "<tr ><td colspan='$cols' ></td></tr>";
146         $form .= "</table><br><input type='submit' class='button' name='ContinueLead' value='${app_strings['LBL_NEXT_BUTTON_LABEL']}'></form>";
147         return $form;
148
149
150
151
152
153 }
154 function getWideFormBody($prefix, $mod='', $formname=''){
155 if(!ACLController::checkAccess('Leads', 'edit', true)){
156                 return '';
157         }
158 global $mod_strings;
159 $temp_strings = $mod_strings;
160 if(!empty($mod)){
161         global $current_language;
162         $mod_strings = return_module_language($current_language, $mod);
163 }
164                 global $app_strings;
165                 global $current_user;
166                 global $app_list_strings;
167                 $primary_address_country_options = get_select_options_with_id($app_list_strings['countries_dom'], '');
168                 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
169                 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
170                 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
171                 $lbl_phone = $mod_strings['LBL_OFFICE_PHONE'];
172                 $lbl_address =  $mod_strings['LBL_PRIMARY_ADDRESS'];
173                 $user_id = $current_user->id;
174                 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
175                 $form = <<<EOQ
176                 <input type="hidden" name="${prefix}record" value="">
177                 <input type="hidden" name="${prefix}status" value="New">
178                 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
179                 <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>
180                 <tr><td colspan='4'><hr></td></tr>
181                 <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>
182                 <tr><td colspan='4'><hr></td></tr>
183                 <tr><td nowrap colspan='4'>$lbl_address<br><input type='text' name='${prefix}primary_address_street' size='80'></td></tr>
184                 <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>
185                 <tr><td colspan='4'><hr></td></tr>
186                 <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>
187                 <tr><td colspan='4'><hr></td></tr>
188                 <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>
189                 <tr><td nowrap colspan='4'>${mod_strings['LBL_DESCRIPTION']}<br><textarea cols='80' rows='4' name='${prefix}description' ></textarea></td></tr></table>
190
191 EOQ;
192
193
194 $javascript = new javascript();
195 $javascript->setFormName($formname);
196 $javascript->setSugarBean(new Lead());
197 $javascript->addField('email1','false',$prefix);
198 $javascript->addField('email2','false',$prefix);
199 $javascript->addRequiredFields($prefix);
200 $form .=$javascript->getScript();
201 $mod_strings = $temp_strings;
202 return $form;
203 }
204
205 function getFormBody($prefix, $mod='', $formname=''){
206         if(!ACLController::checkAccess('Leads', 'edit', true)){
207                 return '';
208         }
209 global $mod_strings;
210 $temp_strings = $mod_strings;
211 if(!empty($mod)){
212         global $current_language;
213         $mod_strings = return_module_language($current_language, $mod);
214 }
215                 global $app_strings;
216                 global $current_user;
217                 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
218                 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
219                 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
220                 $lbl_phone = $mod_strings['LBL_PHONE'];
221                 $user_id = $current_user->id;
222                 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
223                 $form = <<<EOQ
224                 <input type="hidden" name="${prefix}record" value="">
225                 <input type="hidden" name="${prefix}email2" value="">
226                 <input type="hidden" name="${prefix}status" value="New">
227                 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
228 <p>             $lbl_first_name<br>
229                 <input name="${prefix}first_name" type="text" value=""><br>
230                 $lbl_last_name <span class="required">$lbl_required_symbol</span><br>
231                 <input name='${prefix}last_name' type="text" value=""><br>
232                 $lbl_phone<br>
233                 <input name='${prefix}phone_work' type="text" value=""><br>
234                 $lbl_email_address<br>
235                 <input name='${prefix}email1' type="text" value=""></p>
236
237 EOQ;
238
239
240 $javascript = new javascript();
241 $javascript->setFormName($formname);
242 $javascript->setSugarBean(new Lead());
243 $javascript->addField('email1','false',$prefix);
244 $javascript->addField('email2','false',$prefix);
245 $javascript->addRequiredFields($prefix);
246 $form .=$javascript->getScript();
247 $mod_strings = $temp_strings;
248 return $form;
249
250 }
251 function getForm($prefix, $mod='Leads'){
252         if(!ACLController::checkAccess('Leads', 'edit', true)){
253                 return '';
254         }
255 if(!empty($mod)){
256         global $current_language;
257         $mod_strings = return_module_language($current_language, $mod);
258 }else global $mod_strings;
259 global $app_strings;
260
261 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
262 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
263 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
264
265
266 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
267 $the_form .= <<<EOQ
268
269                 <form name="${prefix}LeadSave" onSubmit="return check_form('${prefix}LeadSave')" method="POST" action="index.php">
270                         <input type="hidden" name="${prefix}module" value="Leads">
271                         <input type="hidden" name="${prefix}action" value="Save">
272 EOQ;
273 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
274 $the_form .= <<<EOQ
275                 <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>
276                 </form>
277
278 EOQ;
279 $the_form .= get_left_form_footer();
280 $the_form .= get_validate_record_js();
281
282 return $the_form;
283
284
285 }
286
287
288 function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
289         
290             require_once('modules/Campaigns/utils.php');        
291         require_once('include/formbase.php');
292
293         
294
295         if(empty($exist_lead)) {
296         $focus = new Lead();
297     }
298     else {
299         $focus = $exist_lead;
300     }
301
302         if($useRequired &&  !checkRequired($prefix, array_keys($focus->required_fields))){
303                 return null;
304         }
305         $focus = populateFromPost($prefix, $focus);
306         if(!$focus->ACLAccess('Save')){
307                 ACLController::displayNoAccess(true);
308                 sugar_cleanup(true);
309         }
310         if (!isset($_POST[$prefix.'email_opt_out'])) $focus->email_opt_out = 0;
311         if (!isset($_POST[$prefix.'do_not_call'])) $focus->do_not_call = 0;
312     
313     if($do_save) {
314         if(!empty($GLOBALS['check_notify'])) {
315                 $focus->save($GLOBALS['check_notify']);
316         }
317         else {
318                 $focus->save(FALSE);
319         }
320     }
321     
322     $return_id = $focus->id;
323     
324         if (isset($_POST[$prefix.'prospect_id']) &&  !empty($_POST[$prefix.'prospect_id'])) {
325                 $prospect=new Prospect();
326                 $prospect->retrieve($_POST[$prefix.'prospect_id']);
327                 $prospect->lead_id=$focus->id;
328                 // Set to keep email in target
329                 $prospect->in_workflow = true;
330                 $prospect->save();
331
332         //if prospect id exists, make sure we are coming from prospect detail
333         if(strtolower($_POST['return_module']) =='prospects' && strtolower($_POST['return_action']) == 'detailview'){
334             //create campaing_log entry
335
336             if(isset($focus->campaign_id) && $focus->campaign_id != null){
337                 campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
338             }
339         }
340         }
341
342         ///////////////////////////////////////////////////////////////////////////////
343         ////    INBOUND EMAIL HANDLING
344         ///////////////////////////////////////////////////////////////////////////////
345         if(isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) {
346                 if(!isset($current_user)) {
347                         global $current_user;
348                 } 
349                         
350                 // fake this case like it's already saved.
351                 
352                 $email = new Email();
353                 $email->retrieve($_REQUEST['inbound_email_id']);
354                 $email->parent_type = 'Leads';
355                 $email->parent_id = $focus->id;
356                 $email->assigned_user_id = $current_user->id;
357                 $email->status = 'read';
358                 $email->save();
359                 $email->load_relationship('leads');
360                 $email->leads->add($focus->id);
361                 
362                 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']);
363                 exit();
364         }
365         ////    END INBOUND EMAIL HANDLING
366         /////////////////////////////////////////////////////////////////////////////// 
367         
368         $GLOBALS['log']->debug("Saved record with id of ".$return_id);
369         if($redirect){
370                 handleRedirect($return_id, 'Leads');
371         }else{
372                 return $focus;
373         }
374 }
375
376
377
378 }
379
380
381 ?>