]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MergeRecords/MergeRecord.php
Release 6.1.4
[Github/sugarcrm.git] / modules / MergeRecords / MergeRecord.php
1 <?php
2 if (!defined('sugarEntry') || !sugarEntry)
3     die('Not A Valid Entry Point');
4 /*********************************************************************************
5  * SugarCRM is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2011 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         //load master module strings
103         if ($load_module_strings)
104             $this->merge_bean_strings = return_module_language($current_language, $merge_module);
105     }
106
107     // Bug 22994, when the search key words are in other module, there needs to be another merge_bean.
108     function load_merge_bean2($merge_module, $load_module_strings = false, $merge_id = '') {
109         global $moduleList;
110         global $beanList;
111         global $beanFiles;
112         global $current_language;
113
114         $this->merge_module2 = $merge_module;
115         $this->merge_bean_class2 = $beanList[$this->merge_module2];
116         $this->merge_bean_file_path2 = $beanFiles[$this->merge_bean_class2];
117
118         require_once ($this->merge_bean_file_path2);
119         $this->merge_bean2 = new $this->merge_bean_class2();
120         if ($merge_id != '')
121             $this->merge_bean2->retrieve($merge_id);
122         //load master module strings
123         if ($load_module_strings)
124             $this->merge_bean_strings2 = return_module_language($current_language, $merge_module);
125     }
126
127     var $new_schema = true;
128
129     //-----------------------------------------------------------------------
130     //-------------Wrapping Necessary Merge Bean Calls-----------------------
131     //-----------------------------------------------------------------------
132     
133     function fill_in_additional_list_fields() {
134         return $this->merge_bean->fill_in_additional_list_fields();
135     }
136
137     function fill_in_additional_detail_fields() {
138         return $this->merge_bean->fill_in_additional_detail_fields();
139     }
140
141     function get_summary_text() {
142         return $this->merge_bean->get_summary_text();
143     }
144
145     function get_list_view_data() {
146         return $this->merge_bean->get_list_view_data();
147     }
148     //-----------------------------------------------------------------------
149     //-----------------------------------------------------------------------
150     //-----------------------------------------------------------------------
151
152     /**
153         builds a generic search based on the query string using or
154         do not include any $this-> because this is called on without having the class instantiated
155     */
156     function build_generic_where_clause($the_query_string) {
157         return $this->merge_bean->build_generic_where_clause($the_query_string);
158     }
159
160     //adding in 4.0+ acl function for possible acl stuff down the line
161     function bean_implements($interface) {
162         switch ($interface) {
163             case 'ACL' :
164                 return true;
165         }
166         return false;
167     }
168     
169     function ACLAccess($view,$is_owner='not_set'){
170         global $current_user;
171
172         //if the module doesn't implement ACLS or is empty  
173         if(empty($this->merge_bean) || !$this->merge_bean->bean_implements('ACL'))
174         {
175                 return true;
176         }
177         
178         if($is_owner == 'not_set'){
179             $is_owner = $this->merge_bean->isOwner($current_user->id);
180         }
181         return ACLController::checkAccess($this->merge_bean->module_dir,'edit', true);
182     }
183     
184
185     //keep save function to handle anything special on merges
186     function save($check_notify = FALSE) {
187             //something here
188     return parent :: save($check_notify);
189     }
190
191     function populate_search_params($search_params) {
192        foreach ($this->merge_bean->field_defs as $key=>$value) {
193             $searchFieldString=$key.'SearchField';
194             $searchTypeString=$key.'SearchType';
195              
196             if (isset($search_params[$searchFieldString]) ) {
197
198                 if (isset($search_params[$searchFieldString]) == '') {
199                     $this->field_search_params[$key]['value']='NULL';
200                 } else {
201                     $this->field_search_params[$key]['value']=$search_params[$searchFieldString];
202                 }
203                 if (isset ($search_params[$searchTypeString])) {
204                     $this->field_search_params[$key]['search_type'] = $search_params[$searchTypeString];
205                 } else {
206                     $this->field_search_params[$key]['search_type'] = 'Exact';
207                 }
208                 //add field_def to the array.
209                 $this->field_search_params[$key] = array_merge($value,$this->field_search_params[$key] );
210             }
211        }
212     }
213     
214     function get_inputs_for_search_params($search_params) 
215     {
216         $returnString = '';
217         foreach ($this->merge_bean->field_defs as $key=>$value) {
218             $searchFieldString=$key.'SearchField';
219             $searchTypeString=$key.'SearchType';
220             
221             if (isset($search_params[$searchFieldString]) ) {
222                 $returnString .= "<input type='hidden' name='$searchFieldString' value='{$search_params[$searchFieldString]}' />\n";
223                 $returnString .= "<input type='hidden' name='$searchTypeString' value='{$search_params[$searchTypeString]}' />\n";
224             }
225         }
226         
227         return $returnString;
228     }
229     
230     function email_addresses_query($table, $module, $bean_id) {
231         $query = $table.".id IN (SELECT ear.bean_id FROM email_addresses ea
232                                                                         LEFT JOIN email_addr_bean_rel ear ON ea.id = ear.email_address_id 
233                                                                         WHERE ear.bean_module = '{$module}'
234                                                                         AND ear.bean_id != '{$bean_id}' 
235                                                                         AND ear.deleted = 0";
236         return $query;
237     }
238     
239     function release_name_query($search_type, $value) {
240         $this->load_merge_bean2('Releases');
241         if($search_type=='like') {
242                 $where = "releases.name LIKE '%".$GLOBALS['db']->quote($value)."%'";
243         }
244         elseif($search_type=='start'){
245             $where = "releases.name LIKE '".$GLOBALS['db']->quote($value)."%'";
246         }
247         else {
248             $where = "releases.name = '".$GLOBALS['db']->quote($value)."'";
249         }
250         $list=$this->merge_bean2->get_releases(false,'Active',$where);
251         foreach($list as $key => $value){
252             $list_to_join[]="'".$GLOBALS['db']->quote($key)."'";
253         }
254         $in=implode(', ', $list_to_join);
255         return $in;
256     }
257
258     function create_where_statement() {
259         $where_clauses = array ();
260         foreach ($this->field_search_params as $merge_field => $vDefArray) {
261                 if (isset ($vDefArray['source']) && $vDefArray['source'] == 'custom_fields') {
262                         $table_name = $this->merge_bean->table_name."_cstm";
263             } else {
264                 $table_name = $this->merge_bean->table_name;
265             }
266
267             //Should move these if's into a central location for extensibility and addition for other search filters
268             //Must do the same for pulling values in js dropdown
269             if (isset ($vDefArray['search_type']) && $vDefArray['search_type'] == 'like') {
270                 if ($merge_field != "email1" && $merge_field != "email2" && $merge_field !="release_name") {
271                         array_push($where_clauses, $table_name.".".$merge_field." LIKE '%".$GLOBALS['db']->quote($vDefArray['value'])."%'");
272                 }
273                 elseif($merge_field =="release_name"){
274                     if(isset($vDefArray['value'])){
275                                                 $in = $this->release_name_query('like',$vDefArray['value']);
276                         array_push($where_clauses, $table_name.".found_in_release IN ($in)");
277                     }
278                 }
279                 else {
280                         $query = $this->email_addresses_query($table_name, $this->merge_module, $this->merge_bean->id);
281                         $query .= " AND ea.email_address LIKE '%".$GLOBALS['db']->quote($vDefArray['value'])."%')";
282                         $where_clauses[] = $query;
283                 }
284             }
285             elseif (isset ($vDefArray['search_type']) && $vDefArray['search_type'] == 'start') {
286                 if ($merge_field != "email1" && $merge_field != "email2" && $merge_field !="release_name") {
287                         array_push($where_clauses, $table_name.".".$merge_field." LIKE '".$GLOBALS['db']->quote($vDefArray['value'])."%'"); 
288                 }
289                 elseif($merge_field =="release_name"){
290                         if(isset($vDefArray['value'])){
291                                                 $in = $this->release_name_query('start',$vDefArray['value']);
292                         array_push($where_clauses, $table_name.".found_in_release IN ($in)");
293                     }
294                 }
295                 else {
296                         $query = $this->email_addresses_query($table_name, $this->merge_module, $this->merge_bean->id);
297                         $query .= " AND ea.email_address LIKE '".$GLOBALS['db']->quote($vDefArray['value'])."%')";
298                         $where_clauses[] = $query;
299                 }
300             }
301             else {
302                 if ($merge_field != "email1" && $merge_field != "email2" && $merge_field !="release_name") {
303                             array_push($where_clauses, $table_name.".".$merge_field."='".$GLOBALS['db']->quote($vDefArray['value'])."'");
304                 }
305                 elseif($merge_field =="release_name"){
306                     if(isset($vDefArray['value'])){
307                                                 $in = $this->release_name_query('exact',$vDefArray['value']);
308                         array_push($where_clauses, $table_name.".found_in_release IN ($in)");
309                     }
310                 }
311                 else {
312                         $query = $this->email_addresses_query($table_name, $this->merge_module, $this->merge_bean->id);
313                         $query .= " AND ea.email_address = '".$GLOBALS['db']->quote($vDefArray['value'])."')";
314                         $where_clauses[] = $query;
315                 }                
316             }
317         }
318         array_push($where_clauses, $this->merge_bean->table_name.".id !='".$GLOBALS['db']->quote($this->merge_bean->id)."'");
319         return $where_clauses;
320     }
321
322     //duplicating utils function for now for possiblity of future or/and and
323     //other functionality
324     function generate_where_statement($where_clauses) {
325         $where = '';
326
327         foreach ($where_clauses as $clause) {
328             if ($where != "")
329                 $where .= " AND ";
330             $where .= $clause;
331         }
332         return $where;
333     }
334 }
335 ?>