]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/forms/PersonFormBase.php
Release 6.4.0
[Github/sugarcrm.git] / include / SugarObjects / forms / PersonFormBase.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
39 /**
40  * PersonFormBase.php
41  *
42  * @author Collin Lee
43  *
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
47  * buildTableForm.
48  *
49  * @see LeadFormBase.php, ContactFormBase.php, ProspectFormBase.php
50  */
51 require_once('include/SugarObjects/forms/FormBase.php');
52
53 abstract class PersonFormBase extends FormBase {
54
55 var $moduleName;
56 var $objectName;
57     
58 /**
59  * buildTableForm
60  * 
61  * This function creates a table with form data.  It is used by the form base code when checking for duplicates
62  *
63  * @param $rows Array of duplicate row data
64  * @return $form The HTML form data
65  */
66 function buildTableForm($rows)
67 {
68         global $action;
69     global $mod_strings;
70         global $app_strings;
71
72     $newLinkLabel = 'LNK_NEW_' . strtoupper($this->objectName);
73     
74         $cols = sizeof($rows[0]) * 2 + 1;
75     
76         if ($action != 'ShowDuplicates')
77         {
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);
82         } else {
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>';
85         }
86     
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')
89         {
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']))
92         {
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']}  '>";
96         } else {
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']}  '>";
98         }
99     } else {
100                 $form .= "<input type='submit' class='button' name='Continue{$this->objectName}' value='{$mod_strings[$newLinkLabel]}'>";
101         }
102
103         $form .= "</td></tr></table></td></tr><tr>";
104
105     if ($action != 'ShowDuplicates')
106         {
107                 $form .= "<td scope='col'>&nbsp;</td>";
108         }
109
110         require_once('include/formbase.php');
111
112         if(isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') {
113                 $_POST['return_action'] = 'DetailView';
114         } 
115         
116         if(isset($_POST['return_action']) && $_POST['return_action'] == 'DetailView' && empty($_REQUEST['return_id'])) {
117                 unset($_POST['return_action']);
118         }
119                 
120    $form .= getPostToForm();
121         
122         if(isset($rows[0])){
123                 foreach ($rows[0] as $key=>$value){
124                         if($key != 'id'){
125                            $form .= "<td scope='col' >". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
126                         }
127                 }
128                 $form .= "</tr>";
129         }
130
131         $rowColor = 'oddListRowS1';
132
133     foreach($rows as $row){
134
135                 $form .= "<tr class='$rowColor'>";
136                 if ($action != 'ShowDuplicates')
137                 {
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>&nbsp;&nbsp;</td>\n";
139                 }
140                 $wasSet = false;
141
142                 foreach ($row as $key=>$value){
143                                 if($key != 'id'){
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";
146                                         }
147                                         else if(!$wasSet){
148                                                 $form .= "<td scope='row'><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
149                                                 $wasSet = true;
150                                         }else{
151                                             $form .= "<td><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
152                                         }
153                                 }
154                 }
155
156                 if($rowColor == 'evenListRowS1'){
157                         $rowColor = 'oddListRowS1';
158                 }else{
159                          $rowColor = 'evenListRowS1';
160                 }
161                 $form .= "</tr>";
162         }
163         $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
164         if ($action == 'ShowDuplicates')
165         {
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']}  '>";
171         else
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']}  '>";
173     }
174         else
175         {
176                 $form .= "<input type='submit' class='button' name='Continue{$this->objectName}' value='{$mod_strings[$newLinkLabel]}'></form>";
177         }
178     $form .= "</td></tr></table></td></tr></table>";
179         return $form;
180 }
181
182
183 /**
184  * checkForDuplicates
185  *
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
187  * of this class.
188  *
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
191  */
192 function checkForDuplicates($prefix='')
193 {
194         require_once('include/formbase.php');
195     require_once('include/MVC/SugarModule.php');
196     $focus = SugarModule::get($this->moduleName)->loadBean();
197
198         $query = $this->getDuplicateQuery($prefix);
199
200     if(empty($query))
201     {
202        return null;
203     }
204
205         $rows = array();
206
207     global $db;
208         $result = $db->query($query);
209
210     //Loop through the results and store
211         while (($row = $db->fetchByAssoc($result)) != null)
212     {
213                 if(!isset($rows[$row['id']])) {
214                    $rows[]=$row;
215                 }
216         }
217
218     //Now check for duplicates using email values supplied
219         $count = 0;
220         $emails = array();
221         $emailStr = '';
222         while(isset($_POST["{$this->moduleName}{$count}emailAddress{$count}"]))
223     {
224               $emailStr .= ",'" . strtoupper(trim($_POST["{$this->moduleName}{$count}emailAddress" . $count++])) . "'";
225         } //while
226
227         if(!empty($emailStr))
228     {
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 . ')';
234
235                 $result = $db->query($query);
236                 while (($row = $db->fetchByAssoc($result)) != null)
237         {
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'])) {
243                                   $rows[]=$r;
244                            }
245                         } //if
246                 }
247         } //if
248
249     return !empty($rows) ? $rows : null;
250 }
251
252
253 /**
254  * getDuplicateQuery
255  *
256  * This is the function that subclasses should extend and return a SQL query used for the initial duplicate
257  * check sequence.
258  *
259  * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
260  * @param $prefix String value of prefix that may be present in $_POST variables
261  * @return SQL String of the query that should be used for the initial duplicate lookup check
262  */
263 public function getDuplicateQuery($prefix='')
264 {
265     return null;
266 }
267
268
269 }