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.
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 ********************************************************************************/
44 * This is an abstract class to handle separating some of the common logic used between the form base code.
45 * One of the main common function shared was the checking of duplicate records. This is now handled in the
46 * checkForDuplicates method of this function. When duplicates were found, we created an HTML block using
49 * @see LeadFormBase.php, ContactFormBase.php, ProspectFormBase.php
51 require_once('include/SugarObjects/forms/FormBase.php');
53 abstract class PersonFormBase extends FormBase {
61 * This function creates a table with form data. It is used by the form base code when checking for duplicates
63 * @param $rows Array of duplicate row data
64 * @return $form The HTML form data
66 function buildTableForm($rows)
72 $newLinkLabel = 'LNK_NEW_' . strtoupper($this->objectName);
74 $cols = sizeof($rows[0]) * 2 + 1;
76 if ($action != 'ShowDuplicates')
78 $duplicateLabel = string_format($app_strings['MSG_DUPLICATE'], array(strtolower($this->objectName), $this->moduleName));
79 $form = '<table width="100%"><tr><td>'.$duplicateLabel.'</td></tr><tr><td height="20"></td></tr></table>';
80 $form .= "<form action='index.php' method='post' name='dup{$this->moduleName}'><input type='hidden' name='selected{$this->objectName}' value=''>";
81 $form .= getPostToForm('/emailAddress(PrimaryFlag|OptOutFlag|InvalidFlag)?[0-9]*?$/', true);
83 $duplicateLabel = string_format($app_strings['MSG_SHOW_DUPLICATES'], array(strtolower($this->objectName), $this->moduleName));
84 $form = '<table width="100%"><tr><td>'.$duplicateLabel.'</td></tr><tr><td height="20"></td></tr></table>';
87 $form .= "<table width='100%' cellpadding='0' cellspacing='0' class='list view' border='0'><tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
88 if ($action == 'ShowDuplicates')
90 $form .= "<input title='${app_strings['LBL_SAVE_BUTTON_TITLE']}' accessKey='${app_strings['LBL_SAVE_BUTTON_KEY']}' class='button' onclick=\"this.form.action.value='Save';\" type='submit' name='button' value=' ${app_strings['LBL_SAVE_BUTTON_LABEL']} '>\n";
91 if (!empty($_REQUEST['return_module']) && !empty($_REQUEST['return_action']) && !empty($_REQUEST['return_id']))
93 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.module.value=".$_REQUEST['return_module'].";this.form.action.value=".$_REQUEST['return_action'].";this.form.record.value=".$_REQUEST['return_id']."'\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
94 } else if (!empty($_POST['return_module']) && !empty($_POST['return_action'])) {
95 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.module.value=".$_POST['return_module'].";this.form.action.value=". $_POST['return_action'].";'\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
97 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.action.value='ListView';\" type='submit' type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
100 $form .= "<input type='submit' class='button' name='Continue{$this->objectName}' value='{$mod_strings[$newLinkLabel]}'>";
103 $form .= "</td></tr></table></td></tr><tr>";
105 if ($action != 'ShowDuplicates')
107 $form .= "<td scope='col'> </td>";
110 require_once('include/formbase.php');
112 if(isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') {
113 $_POST['return_action'] = 'DetailView';
116 if(isset($_POST['return_action']) && $_POST['return_action'] == 'DetailView' && empty($_REQUEST['return_id'])) {
117 unset($_POST['return_action']);
120 $form .= getPostToForm();
123 foreach ($rows[0] as $key=>$value){
125 $form .= "<td scope='col' >". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
131 $rowColor = 'oddListRowS1';
133 foreach($rows as $row){
135 $form .= "<tr class='$rowColor'>";
136 if ($action != 'ShowDuplicates')
138 $form .= "<td width='1%' nowrap='nowrap'><a href='#' onClick=\"document.forms['dup{$this->moduleName}'].selected{$this->objectName}.value='${row['id']}';document.forms['dup{$this->moduleName}'].submit() \">[{$app_strings['LBL_SELECT_BUTTON_LABEL']}]</a> </td>\n";
142 foreach ($row as $key=>$value){
144 if(isset($_POST['popup']) && $_POST['popup']==true){
145 $form .= "<td scope='row'><a href='#' onclick=\"window.opener.location='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'\">$value</a></td>\n";
148 $form .= "<td scope='row'><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
151 $form .= "<td><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
156 if($rowColor == 'evenListRowS1'){
157 $rowColor = 'oddListRowS1';
159 $rowColor = 'evenListRowS1';
163 $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
164 if ($action == 'ShowDuplicates')
166 $form .= "<input title='${app_strings['LBL_SAVE_BUTTON_TITLE']}' accessKey='${app_strings['LBL_SAVE_BUTTON_KEY']}' class='button' onclick=\"this.form.action.value='Save';\" type='submit' name='button' value=' ${app_strings['LBL_SAVE_BUTTON_LABEL']} '>\n";
167 if (!empty($_REQUEST['return_module']) && !empty($_REQUEST['return_action']) && !empty($_REQUEST['return_id']))
168 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.module.value=".$_REQUEST['return_module'].";this.form.action.value=".$_REQUEST['return_action'].";this.form.record.value=".$_REQUEST['return_id']."'\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
169 else if (!empty($_POST['return_module']) && !empty($_POST['return_action']))
170 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.module.value=".$_POST['return_module'].";this.form.action.value=". $_POST['return_action'].";'\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
172 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.action.value='ListView';\" type='submit' type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
176 $form .= "<input type='submit' class='button' name='Continue{$this->objectName}' value='{$mod_strings[$newLinkLabel]}'></form>";
178 $form .= "</td></tr></table></td></tr></table>";
186 * This function is used to locate any duplicate Leads that may be created when a new Lead is saved. It is called from the handleSave method
189 * @param $prefix String value of any prefix to the form input names
190 * @return $rows Array of matching Leads entries found; null if none found
192 function checkForDuplicates($prefix='')
194 require_once('include/formbase.php');
195 require_once('include/MVC/SugarModule.php');
196 $focus = SugarModule::get($this->moduleName)->loadBean();
198 $query = $this->getDuplicateQuery($focus, $prefix);
208 $result = $db->query($query);
210 //Loop through the results and store
211 while (($row = $db->fetchByAssoc($result)) != null)
213 if(!isset($rows[$row['id']])) {
218 //Now check for duplicates using email values supplied
222 while(isset($_POST["{$this->moduleName}{$count}emailAddress{$count}"]))
224 $emailStr .= ",'" . strtoupper(trim($_POST["{$this->moduleName}{$count}emailAddress" . $count++])) . "'";
227 if(!empty($emailStr))
229 $emailStr = substr($emailStr, 1);
230 $query = 'SELECT DISTINCT er.bean_id AS id FROM email_addr_bean_rel er, ' .
231 'email_addresses ea WHERE ea.id = er.email_address_id ' .
232 'AND ea.deleted = 0 AND er.deleted = 0 AND er.bean_module = \'' . $this->moduleName . '\' ' .
233 'AND email_address_caps IN (' . $emailStr . ')';
235 $result = $db->query($query);
236 while (($row = $db->fetchByAssoc($result)) != null)
238 if(!isset($rows[$row['id']])) {
239 $query2 = "SELECT id, first_name, last_name, title FROM {$focus->table_name} WHERE deleted=0 AND id = '" . $row['id'] . "'";
240 $result2 = $db->query($query2);
241 $r = $db->fetchByAssoc($result2);
242 if(isset($r['id'])) {
249 return !empty($rows) ? $rows : null;
256 * This is the function that subclasses should extend and return a SQL query used for the initial duplicate
259 * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
260 * @param $focus sugarbean
261 * @param $prefix String value of prefix that may be present in $_POST variables
262 * @return SQL String of the query that should be used for the initial duplicate lookup check
264 public function getDuplicateQuery($focus, $prefix='')