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 ********************************************************************************/
40 function checkForDuplicates($prefix, $id){
41 require_once('include/formbase.php');
44 if(!checkRequired($prefix, array_keys($focus->required_fields))){
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'] ."')";
52 $query = $baseQuery ." last_name = '". $_POST[$prefix.'last_name'] ."'";
56 $result = $db->query($query);
57 while (($row = $db->fetchByAssoc($result)) != null) {
58 if(!isset($rows[$row['id']])) {
64 if(isset($_POST[$prefix.'email1']) && !empty($_POST[$prefix.'email1'])){
65 $emailStr="'". strtoupper($_POST[$prefix.'email1']) ."'";
67 if(isset($_POST[$prefix.'email2']) && !empty($_POST[$prefix.'email2'])){
68 if (!empty($emailStr)) $emailStr.=",";
69 $emailStr="'". strtoupper($_POST[$prefix.'email2']) ."'";
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);
90 return !empty($rows) ? $rows : null;
94 function buildTableForm($rows, $mod=''){
96 global $current_language;
97 $mod_strings = return_module_language($current_language, $mod);
98 }else global $mod_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>";
107 require_once('include/formbase.php');
108 $form .= getPostToForm();
111 foreach ($rows[0] as $key=>$value){
115 $form .= "<td scope='col' >". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
120 $rowColor = 'oddListRowS1';
121 foreach($rows as $row){
124 $form .= "<tr class='$rowColor'>";
125 $form .= "<td width='1%' nowrap='nowrap' align='center'><input type='checkbox' name='selectedLeads[]' value='{$row['id']}'></td>";
128 foreach ($row as $key=>$value){
132 $form .= "<td scope='row'><a target='_blank' href='index.php?module=Leads&action=DetailView&record=${row['id']}'>$value</a></td>";
135 $form .= "<td><a target='_blank' href='index.php?module=Leads&action=DetailView&record=${row['id']}'>$value</a></td>";
138 if($rowColor == 'evenListRowS1'){
139 $rowColor = 'oddListRowS1';
141 $rowColor = 'evenListRowS1';
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>";
154 function getWideFormBody($prefix, $mod='', $formname=''){
155 if(!ACLController::checkAccess('Leads', 'edit', true)){
159 $temp_strings = $mod_strings;
161 global $current_language;
162 $mod_strings = return_module_language($current_language, $mod);
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'];
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> $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>
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;
205 function getFormBody($prefix, $mod='', $formname=''){
206 if(!ACLController::checkAccess('Leads', 'edit', true)){
210 $temp_strings = $mod_strings;
212 global $current_language;
213 $mod_strings = return_module_language($current_language, $mod);
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'];
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>
233 <input name='${prefix}phone_work' type="text" value=""><br>
234 $lbl_email_address<br>
235 <input name='${prefix}email1' type="text" value=""></p>
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;
251 function getForm($prefix, $mod='Leads'){
252 if(!ACLController::checkAccess('Leads', 'edit', true)){
256 global $current_language;
257 $mod_strings = return_module_language($current_language, $mod);
258 }else global $mod_strings;
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'];
266 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
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">
273 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}LeadSave");
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>
279 $the_form .= get_left_form_footer();
280 $the_form .= get_validate_record_js();
288 function handleSave($prefix,$redirect=true, $useRequired=false, $do_save=true, $exist_lead=null){
290 require_once('modules/Campaigns/utils.php');
291 require_once('include/formbase.php');
295 if(empty($exist_lead)) {
299 $focus = $exist_lead;
302 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))){
305 $focus = populateFromPost($prefix, $focus);
306 if(!$focus->ACLAccess('Save')){
307 ACLController::displayNoAccess(true);
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;
314 if(!empty($GLOBALS['check_notify'])) {
315 $focus->save($GLOBALS['check_notify']);
322 $return_id = $focus->id;
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;
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
336 if(isset($focus->campaign_id) && $focus->campaign_id != null){
337 campaign_log_lead_entry($focus->campaign_id,$prospect, $focus,'lead');
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;
350 // fake this case like it's already saved.
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';
359 $email->load_relationship('leads');
360 $email->leads->add($focus->id);
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']);
365 //// END INBOUND EMAIL HANDLING
366 ///////////////////////////////////////////////////////////////////////////////
368 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
370 handleRedirect($return_id, 'Leads');