]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/forms/PersonFormBase.php
Release 6.4.0beta3
[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
52 abstract class PersonFormBase {
53
54 var $moduleName;
55 var $objectName;
56     
57 /**
58  * buildTableForm
59  * 
60  * This function creates a table with form data.  It is used by the form base code when checking for duplicates
61  *
62  * @param $rows Array of duplicate row data
63  * @return $form The HTML form data
64  */
65 function buildTableForm($rows)
66 {
67         global $action;
68     global $mod_strings;
69         global $app_strings;
70
71     $newLinkLabel = 'LNK_NEW_' . strtoupper($this->objectName);
72     
73         $cols = sizeof($rows[0]) * 2 + 1;
74     
75         if ($action != 'ShowDuplicates')
76         {
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);
81         } else {
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>';
84         }
85     
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')
88         {
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']))
91         {
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']}  '>";
95         } else {
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']}  '>";
97         }
98     } else {
99                 $form .= "<input type='submit' class='button' name='Continue{$this->moduleName}' value='{$newLinkLabel}'>";
100         }
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='{$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($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']) && !array_key_exists('id', $r)) {
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 $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
261  */
262 public function getDuplicateQuery($prefix='')
263 {
264     return null;
265 }
266
267
268 }