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 ********************************************************************************/
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
52 abstract class PersonFormBase {
60 * This function creates a table with form data. It is used by the form base code when checking for duplicates
62 * @param $rows Array of duplicate row data
63 * @return $form The HTML form data
65 function buildTableForm($rows)
71 $newLinkLabel = 'LNK_NEW_' . strtoupper($this->objectName);
73 $cols = sizeof($rows[0]) * 2 + 1;
75 if ($action != 'ShowDuplicates')
77 $duplicateLabel = string_format($app_strings['MSG_DUPLICATE'], array(strtolower($this->objectName), $this->moduleName));
78 $form = '<table width="100%"><tr><td>'.$duplicateLabel.'</td></tr><tr><td height="20"></td></tr></table>';
79 $form .= "<form action='index.php' method='post' name='dup{$this->moduleName}'><input type='hidden' name='selected{$this->objectName}' value=''>";
80 $form .= getPostToForm('/emailAddress(PrimaryFlag|OptOutFlag|InvalidFlag)?[0-9]*?$/', true);
82 $duplicateLabel = string_format($app_strings['MSG_SHOW_DUPLICATES'], array(strtolower($this->objectName), $this->moduleName));
83 $form = '<table width="100%"><tr><td>'.$duplicateLabel.'</td></tr><tr><td height="20"></td></tr></table>';
86 $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>";
87 if ($action == 'ShowDuplicates')
89 $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";
90 if (!empty($_REQUEST['return_module']) && !empty($_REQUEST['return_action']) && !empty($_REQUEST['return_id']))
92 $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']} '>";
93 } else if (!empty($_POST['return_module']) && !empty($_POST['return_action'])) {
94 $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']} '>";
96 $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']} '>";
99 $form .= "<input type='submit' class='button' name='Continue{$this->moduleName}' value='{$newLinkLabel}'>";
102 $form .= "</td></tr></table></td></tr><tr>";
104 if ($action != 'ShowDuplicates')
106 $form .= "<td scope='col'> </td>";
109 require_once('include/formbase.php');
111 if(isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') {
112 $_POST['return_action'] = 'DetailView';
115 if(isset($_POST['return_action']) && $_POST['return_action'] == 'DetailView' && empty($_REQUEST['return_id'])) {
116 unset($_POST['return_action']);
119 $form .= getPostToForm();
122 foreach ($rows[0] as $key=>$value){
124 $form .= "<td scope='col' >". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
130 $rowColor = 'oddListRowS1';
132 foreach($rows as $row){
134 $form .= "<tr class='$rowColor'>";
135 if ($action != 'ShowDuplicates')
137 $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";
141 foreach ($row as $key=>$value){
143 if(isset($_POST['popup']) && $_POST['popup']==true){
144 $form .= "<td scope='row'><a href='#' onclick=\"window.opener.location='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'\">$value</a></td>\n";
147 $form .= "<td scope='row'><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
150 $form .= "<td><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
155 if($rowColor == 'evenListRowS1'){
156 $rowColor = 'oddListRowS1';
158 $rowColor = 'evenListRowS1';
162 $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
163 if ($action == 'ShowDuplicates')
165 $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";
166 if (!empty($_REQUEST['return_module']) && !empty($_REQUEST['return_action']) && !empty($_REQUEST['return_id']))
167 $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']} '>";
168 else if (!empty($_POST['return_module']) && !empty($_POST['return_action']))
169 $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']} '>";
171 $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']} '>";
175 $form .= "<input type='submit' class='button' name='Continue{$this->objectName}' value='{$newLinkLabel}'></form>";
177 $form .= "</td></tr></table></td></tr></table>";
185 * 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
188 * @param $prefix String value of any prefix to the form input names
189 * @return $rows Array of matching Leads entries found; null if none found
191 function checkForDuplicates($prefix='')
193 require_once('include/formbase.php');
194 require_once('include/MVC/SugarModule.php');
195 $focus = SugarModule::get($this->moduleName)->loadBean();
197 $query = $this->getDuplicateQuery($prefix);
207 $result = $db->query($query);
209 //Loop through the results and store
210 while (($row = $db->fetchByAssoc($result)) != null)
212 if(!isset($rows[$row['id']])) {
217 //Now check for duplicates using email values supplied
221 while(isset($_POST["{$this->moduleName}{$count}emailAddress{$count}"]))
223 $emailStr .= ",'" . strtoupper(trim($_POST["{$this->moduleName}{$count}emailAddress" . $count++])) . "'";
226 if(!empty($emailStr))
228 $emailStr = substr($emailStr, 1);
229 $query = 'SELECT DISTINCT er.bean_id AS id FROM email_addr_bean_rel er, ' .
230 'email_addresses ea WHERE ea.id = er.email_address_id ' .
231 'AND ea.deleted = 0 AND er.deleted = 0 AND er.bean_module = \'' . $this->moduleName . '\' ' .
232 'AND email_address_caps IN (' . $emailStr . ')';
234 $result = $db->query($query);
235 while (($row = $db->fetchByAssoc($result)) != null)
237 if(!isset($rows[$row['id']])) {
238 $query2 = "SELECT id, first_name, last_name, title FROM {$focus->table_name} WHERE deleted=0 AND id = '" . $row['id'] . "'";
239 $result2 = $db->query($query2);
240 $r = $db->fetchByAssoc($result2);
241 if(isset($r['id']) && !array_key_exists('id', $r)) {
248 return !empty($rows) ? $rows : null;
255 * This is the function that subclasses should extend and return a SQL query used for the initial duplicate
258 * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
259 * @param $prefix String value of prefix that may be present in $_POST variables
260 * @return SQL String of the query that should be used for the initial duplicate lookup check
262 public function getDuplicateQuery($prefix='')