]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MergeRecords/MergeRecord.php
Release 6.5.0
[Github/sugarcrm.git] / modules / MergeRecords / MergeRecord.php
1 <?php
2 if (!defined('sugarEntry') || !sugarEntry)
3     die('Not A Valid Entry Point');
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7  * 
8  * This program is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Affero General Public License version 3 as published by the
10  * Free Software Foundation with the addition of the following permission added
11  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14  * 
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
18  * details.
19  * 
20  * You should have received a copy of the GNU Affero General Public License along with
21  * this program; if not, see http://www.gnu.org/licenses or write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301 USA.
24  * 
25  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27  * 
28  * The interactive user interfaces in modified source and object code versions
29  * of this program must display Appropriate Legal Notices, as required under
30  * Section 5 of the GNU Affero General Public License version 3.
31  * 
32  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33  * these Appropriate Legal Notices must retain the display of the "Powered by
34  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35  * technical reasons, the Appropriate Legal Notices must display the words
36  * "Powered by SugarCRM".
37  ********************************************************************************/
38
39 /*********************************************************************************
40
41  * Description:  TODO: To be written.
42  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43  * All Rights Reserved.
44  * Contributor(s): ______________________________________..
45  ********************************************************************************/
46
47
48
49 class MergeRecord extends SugarBean {
50     var $object_name = 'MergeRecord';
51     var $module_dir = 'MergeRecords';
52         var $acl_display_only = true;
53     var $merge_module;
54     var $merge_bean_class;
55     var $merge_bean_file_path;
56
57     var $merge_module2;
58     var $merge_bean_class2;
59     var $merge_bean_file_path2;
60
61     var $master_id;
62
63     //these arrays store the fields and params to search on
64     var $field_search_params = Array ();
65
66     //this is a object for the bean you are merging on
67     var $merge_bean;
68     var $merge_bean2;
69
70     //store a copy of the merge bean related strings
71     var $merge_bean_strings = Array ();
72
73     function MergeRecord($merge_module = '', $merge_id = '') {
74         global $sugar_config;
75        //parent :: SugarBean();
76
77         if ($merge_module != '')
78             $this->load_merge_bean($merge_module, $merge_id);
79     }
80
81     function retrieve($id) {
82         if (isset ($_REQUEST['action']) && $_REQUEST['action'] == 'Step2')
83             $this->load_merge_bean($this->merge_bean, false, $id);
84         else
85             parent :: retrieve($id);
86     }
87
88     function load_merge_bean($merge_module, $load_module_strings = false, $merge_id = '') {
89         global $moduleList;
90         global $beanList;
91         global $beanFiles;
92         global $current_language;
93
94         $this->merge_module = $merge_module;
95         $this->merge_bean_class = $beanList[$this->merge_module];
96         $this->merge_bean_file_path = $beanFiles[$this->merge_bean_class];
97
98         require_once ($this->merge_bean_file_path);
99         $this->merge_bean = new $this->merge_bean_class();
100         if ($merge_id != '')
101             $this->merge_bean->retrieve($merge_id);
102         
103         // Bug 18853 - Disable this view if the user doesn't have edit and delete permissions
104         if ( !$this->merge_bean->ACLAccess('edit') || !$this->merge_bean->ACLAccess('delete') ) {
105             ACLController::displayNoAccess();
106             sugar_die('');
107         }
108         
109         //load master module strings
110         if ($load_module_strings)
111             $this->merge_bean_strings = return_module_language($current_language, $merge_module);
112     }
113
114     // Bug 22994, when the search key words are in other module, there needs to be another merge_bean.
115     function load_merge_bean2($merge_module, $load_module_strings = false, $merge_id = '') {
116         global $moduleList;
117         global $beanList;
118         global $beanFiles;
119         global $current_language;
120
121         $this->merge_module2 = $merge_module;
122         $this->merge_bean_class2 = $beanList[$this->merge_module2];
123         $this->merge_bean_file_path2 = $beanFiles[$this->merge_bean_class2];
124
125         require_once ($this->merge_bean_file_path2);
126         $this->merge_bean2 = new $this->merge_bean_class2();
127         if ($merge_id != '')
128             $this->merge_bean2->retrieve($merge_id);
129         //load master module strings
130         if ($load_module_strings)
131             $this->merge_bean_strings2 = return_module_language($current_language, $merge_module);
132     }
133
134     var $new_schema = true;
135
136     //-----------------------------------------------------------------------
137     //-------------Wrapping Necessary Merge Bean Calls-----------------------
138     //-----------------------------------------------------------------------
139     
140     function fill_in_additional_list_fields() {
141         return $this->merge_bean->fill_in_additional_list_fields();
142     }
143
144     function fill_in_additional_detail_fields() {
145         return $this->merge_bean->fill_in_additional_detail_fields();
146     }
147
148     function get_summary_text() {
149         return $this->merge_bean->get_summary_text();
150     }
151
152     function get_list_view_data() {
153         return $this->merge_bean->get_list_view_data();
154     }
155     //-----------------------------------------------------------------------
156     //-----------------------------------------------------------------------
157     //-----------------------------------------------------------------------
158
159     /**
160         builds a generic search based on the query string using or
161         do not include any $this-> because this is called on without having the class instantiated
162     */
163     function build_generic_where_clause($the_query_string) {
164         return $this->merge_bean->build_generic_where_clause($the_query_string);
165     }
166
167     //adding in 4.0+ acl function for possible acl stuff down the line
168     function bean_implements($interface) {
169         switch ($interface) {
170             case 'ACL' :
171                 return true;
172         }
173         return false;
174     }
175     
176     function ACLAccess($view,$is_owner='not_set'){
177         global $current_user;
178
179         //if the module doesn't implement ACLS or is empty  
180         if(empty($this->merge_bean) || !$this->merge_bean->bean_implements('ACL'))
181         {
182                 return true;
183         }
184         
185         if($is_owner == 'not_set'){
186             $is_owner = $this->merge_bean->isOwner($current_user->id);
187         }
188         return ACLController::checkAccess($this->merge_bean->module_dir,'edit', true);
189     }
190     
191
192     //keep save function to handle anything special on merges
193     function save($check_notify = FALSE) {
194             //something here
195     return parent :: save($check_notify);
196     }
197
198     function populate_search_params($search_params) {
199        foreach ($this->merge_bean->field_defs as $key=>$value) {
200             $searchFieldString=$key.'SearchField';
201             $searchTypeString=$key.'SearchType';
202              
203             if (isset($search_params[$searchFieldString]) ) {
204
205                 if (isset($search_params[$searchFieldString]) == '') {
206                     $this->field_search_params[$key]['value']='NULL';
207                 } else {
208                     $this->field_search_params[$key]['value']=$search_params[$searchFieldString];
209                 }
210                 if (isset ($search_params[$searchTypeString])) {
211                     $this->field_search_params[$key]['search_type'] = $search_params[$searchTypeString];
212                 } else {
213                     $this->field_search_params[$key]['search_type'] = 'Exact';
214                 }
215                 //add field_def to the array.
216                 $this->field_search_params[$key] = array_merge($value,$this->field_search_params[$key] );
217             }
218        }
219     }
220     
221     function get_inputs_for_search_params($search_params) 
222     {
223         $returnString = '';
224         foreach ($this->merge_bean->field_defs as $key=>$value) {
225             $searchFieldString=$key.'SearchField';
226             $searchTypeString=$key.'SearchType';
227             
228             if (isset($search_params[$searchFieldString]) ) {
229                 $returnString .= "<input type='hidden' name='$searchFieldString' value='{$search_params[$searchFieldString]}' />\n";
230                 $returnString .= "<input type='hidden' name='$searchTypeString' value='{$search_params[$searchTypeString]}' />\n";
231             }
232         }
233         
234         return $returnString;
235     }
236     
237     function email_addresses_query($table, $module, $bean_id) {
238         $query = $table.".id IN (SELECT ear.bean_id FROM email_addresses ea
239                                                                         LEFT JOIN email_addr_bean_rel ear ON ea.id = ear.email_address_id 
240                                                                         WHERE ear.bean_module = '{$module}'
241                                                                         AND ear.bean_id != '{$bean_id}' 
242                                                                         AND ear.deleted = 0";
243         return $query;
244     }
245     
246     function release_name_query($search_type, $value) {
247         $this->load_merge_bean2('Releases');
248         if($search_type=='like') {
249                 $where = "releases.name LIKE '%".$GLOBALS['db']->quote($value)."%'";
250         }
251         elseif($search_type=='start'){
252             $where = "releases.name LIKE '".$GLOBALS['db']->quote($value)."%'";
253         }
254         else {
255             $where = "releases.name = '".$GLOBALS['db']->quote($value)."'";
256         }
257         $list=$this->merge_bean2->get_releases(false,'Active',$where);
258         foreach($list as $key => $value){
259             $list_to_join[]="'".$GLOBALS['db']->quote($key)."'";
260         }
261         $in=implode(', ', $list_to_join);
262         return $in;
263     }
264
265     function create_where_statement() {
266         $where_clauses = array ();
267         foreach ($this->field_search_params as $merge_field => $vDefArray) {
268                 if (isset ($vDefArray['source']) && $vDefArray['source'] == 'custom_fields') {
269                         $table_name = $this->merge_bean->table_name."_cstm";
270             } else {
271                 $table_name = $this->merge_bean->table_name;
272             }
273
274             //Should move these if's into a central location for extensibility and addition for other search filters
275             //Must do the same for pulling values in js dropdown
276             if (isset ($vDefArray['search_type']) && $vDefArray['search_type'] == 'like') {
277                 if ($merge_field != "email1" && $merge_field != "email2" && $merge_field !="release_name") {
278                         if ($vDefArray['value'] != '') {
279                                 array_push($where_clauses, $table_name.".".$merge_field." LIKE '%".$GLOBALS['db']->quote($vDefArray['value'])."%'");
280                         }
281                 }
282                 elseif($merge_field =="release_name"){
283                     if(isset($vDefArray['value'])){
284                                                 $in = $this->release_name_query('like',$vDefArray['value']);
285                         array_push($where_clauses, $table_name.".found_in_release IN ($in)");
286                     }
287                 }
288                 else {
289                         $query = $this->email_addresses_query($table_name, $this->merge_module, $this->merge_bean->id);
290                         $query .= " AND ea.email_address LIKE '%".$GLOBALS['db']->quote($vDefArray['value'])."%')";
291                         $where_clauses[] = $query;
292                 }
293             }
294             elseif (isset ($vDefArray['search_type']) && $vDefArray['search_type'] == 'start') {
295                 if ($merge_field != "email1" && $merge_field != "email2" && $merge_field !="release_name") {
296                         array_push($where_clauses, $table_name.".".$merge_field." LIKE '".$GLOBALS['db']->quote($vDefArray['value'])."%'"); 
297                 }
298                 elseif($merge_field =="release_name"){
299                         if(isset($vDefArray['value'])){
300                                                 $in = $this->release_name_query('start',$vDefArray['value']);
301                         array_push($where_clauses, $table_name.".found_in_release IN ($in)");
302                     }
303                 }
304                 else {
305                         $query = $this->email_addresses_query($table_name, $this->merge_module, $this->merge_bean->id);
306                         $query .= " AND ea.email_address LIKE '".$GLOBALS['db']->quote($vDefArray['value'])."%')";
307                         $where_clauses[] = $query;
308                 }
309             }
310             else {
311                 if ($merge_field != "email1" && $merge_field != "email2" && $merge_field !="release_name") {
312                             array_push($where_clauses, $table_name.".".$merge_field."='".$GLOBALS['db']->quote($vDefArray['value'])."'");
313                 }
314                 elseif($merge_field =="release_name"){
315                     if(isset($vDefArray['value'])){
316                                                 $in = $this->release_name_query('exact',$vDefArray['value']);
317                         array_push($where_clauses, $table_name.".found_in_release IN ($in)");
318                     }
319                 }
320                 else {
321                         $query = $this->email_addresses_query($table_name, $this->merge_module, $this->merge_bean->id);
322                         $query .= " AND ea.email_address = '".$GLOBALS['db']->quote($vDefArray['value'])."')";
323                         $where_clauses[] = $query;
324                 }                
325             }
326         }
327         // Add ACL Check
328         if($this->merge_bean->bean_implements('ACL') && ACLController::requireOwner($this->merge_bean->module_dir, 'delete') )
329         {
330             global $current_user;
331             $where_clauses[] = $this->merge_bean->getOwnerWhere($current_user->id);
332         }
333         array_push($where_clauses, $this->merge_bean->table_name.".id !='".$GLOBALS['db']->quote($this->merge_bean->id)."'");
334         return $where_clauses;
335     }
336
337     //duplicating utils function for now for possiblity of future or/and and
338     //other functionality
339     function generate_where_statement($where_clauses) {
340         $where = '';
341
342         foreach ($where_clauses as $clause) {
343             if ($where != "")
344                 $where .= " AND ";
345             $where .= $clause;
346         }
347         return $where;
348     }
349 }
350 ?>