]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/forms/PersonFormBase.php
Release 6.5.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-2012 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         $form .= "</td></tr></table></td></tr><tr>";
103
104     if ($action != 'ShowDuplicates')
105         {
106                 $form .= "<td scope='col'>&nbsp;</td>";
107         }
108
109         require_once('include/formbase.php');
110
111         if(isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') {
112                 $_POST['return_action'] = 'DetailView';
113         } 
114         
115         if(isset($_POST['return_action']) && $_POST['return_action'] == 'DetailView' && empty($_REQUEST['return_id'])) {
116                 unset($_POST['return_action']);
117         }
118                 
119    $form .= getPostToForm();
120         
121         if(isset($rows[0])){
122                 foreach ($rows[0] as $key=>$value){
123                         if($key != 'id'){
124                            $form .= "<td scope='col' >". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
125                         }
126                 }
127                 $form .= "</tr>";
128         }
129
130         $rowColor = 'oddListRowS1';
131
132     foreach($rows as $row){
133
134                 $form .= "<tr class='$rowColor'>";
135                 if ($action != 'ShowDuplicates')
136                 {
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>&nbsp;&nbsp;</td>\n";
138                 }
139                 $wasSet = false;
140
141                 foreach ($row as $key=>$value){
142                                 if($key != 'id'){
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";
145                                         }
146                                         else if(!$wasSet){
147                                                 $form .= "<td scope='row'><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
148                                                 $wasSet = true;
149                                         }else{
150                                             $form .= "<td><a target='_blank' href='index.php?module={$this->moduleName}&action=DetailView&record=${row['id']}'>$value</a></td>\n";
151                                         }
152                                 }
153                 }
154
155                 if($rowColor == 'evenListRowS1'){
156                         $rowColor = 'oddListRowS1';
157                 }else{
158                          $rowColor = 'evenListRowS1';
159                 }
160                 $form .= "</tr>";
161         }
162         $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
163         if ($action == 'ShowDuplicates')
164         {
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']}  '>";
170         else
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']}  '>";
172     }
173         else
174         {
175                 $form .= "<input type='submit' class='button' name='Continue{$this->objectName}' value='{$mod_strings[$newLinkLabel]}'></form>";
176         }
177     $form .= "</td></tr></table></td></tr></table>";
178         return $form;
179 }
180
181
182 /**
183  * checkForDuplicates
184  *
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
186  * of this class.
187  *
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
190  */
191 function checkForDuplicates($prefix='')
192 {
193         require_once('include/formbase.php');
194     require_once('include/MVC/SugarModule.php');
195     $focus = SugarModule::get($this->moduleName)->loadBean();
196
197         $query = $this->getDuplicateQuery($focus, $prefix);
198
199     if(empty($query))
200     {
201        return null;
202     }
203
204         $rows = array();
205
206     global $db;
207         $result = $db->query($query);
208
209     //Loop through the results and store
210         while (($row = $db->fetchByAssoc($result)) != null)
211     {
212                 if(!isset($rows[$row['id']])) {
213                    $rows[]=$row;
214                 }
215         }
216
217     //Now check for duplicates using email values supplied
218         $count = 0;
219         $emails = array();
220         $emailStr = '';
221         while(isset($_POST["{$this->moduleName}{$count}emailAddress{$count}"]))
222     {
223               $emailStr .= ",'" . strtoupper(trim($_POST["{$this->moduleName}{$count}emailAddress" . $count++])) . "'";
224         } //while
225
226         if(!empty($emailStr))
227     {
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 . ')';
233
234                 $result = $db->query($query);
235                 while (($row = $db->fetchByAssoc($result)) != null)
236         {
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'])) {
242                                   $rows[]=$r;
243                            }
244                         } //if
245                 }
246         } //if
247
248     return !empty($rows) ? $rows : null;
249 }
250
251
252 /**
253  * getDuplicateQuery
254  *
255  * This is the function that subclasses should extend and return a SQL query used for the initial duplicate
256  * check sequence.
257  *
258  * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
259  * @param $focus sugarbean
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($focus, $prefix='')
264 {
265     return null;
266 }
267
268
269 }