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.
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.
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
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
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.
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.
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 ********************************************************************************/
39 /*********************************************************************************
41 * Description: TODO: To be written.
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 * Contributor(s): ______________________________________..
45 ********************************************************************************/
49 class MergeRecord extends SugarBean {
50 var $object_name = 'MergeRecord';
51 var $module_dir = 'MergeRecords';
52 var $acl_display_only = true;
54 var $merge_bean_class;
55 var $merge_bean_file_path;
58 var $merge_bean_class2;
59 var $merge_bean_file_path2;
63 //these arrays store the fields and params to search on
64 var $field_search_params = Array ();
66 //this is a object for the bean you are merging on
70 //store a copy of the merge bean related strings
71 var $merge_bean_strings = Array ();
73 function MergeRecord($merge_module = '', $merge_id = '') {
75 //parent :: SugarBean();
77 if ($merge_module != '')
78 $this->load_merge_bean($merge_module, $merge_id);
81 function retrieve($id) {
82 if (isset ($_REQUEST['action']) && $_REQUEST['action'] == 'Step2')
83 $this->load_merge_bean($this->merge_bean, false, $id);
85 parent :: retrieve($id);
88 function load_merge_bean($merge_module, $load_module_strings = false, $merge_id = '') {
92 global $current_language;
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];
98 require_once ($this->merge_bean_file_path);
99 $this->merge_bean = new $this->merge_bean_class();
101 $this->merge_bean->retrieve($merge_id);
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();
109 //load master module strings
110 if ($load_module_strings)
111 $this->merge_bean_strings = return_module_language($current_language, $merge_module);
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 = '') {
119 global $current_language;
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];
125 require_once ($this->merge_bean_file_path2);
126 $this->merge_bean2 = new $this->merge_bean_class2();
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);
134 var $new_schema = true;
136 //-----------------------------------------------------------------------
137 //-------------Wrapping Necessary Merge Bean Calls-----------------------
138 //-----------------------------------------------------------------------
140 function fill_in_additional_list_fields() {
141 return $this->merge_bean->fill_in_additional_list_fields();
144 function fill_in_additional_detail_fields() {
145 return $this->merge_bean->fill_in_additional_detail_fields();
148 function get_summary_text() {
149 return $this->merge_bean->get_summary_text();
152 function get_list_view_data() {
153 return $this->merge_bean->get_list_view_data();
155 //-----------------------------------------------------------------------
156 //-----------------------------------------------------------------------
157 //-----------------------------------------------------------------------
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
163 function build_generic_where_clause($the_query_string) {
164 return $this->merge_bean->build_generic_where_clause($the_query_string);
167 //adding in 4.0+ acl function for possible acl stuff down the line
168 function bean_implements($interface) {
169 switch ($interface) {
176 function ACLAccess($view,$is_owner='not_set'){
177 global $current_user;
179 //if the module doesn't implement ACLS or is empty
180 if(empty($this->merge_bean) || !$this->merge_bean->bean_implements('ACL'))
185 if($is_owner == 'not_set'){
186 $is_owner = $this->merge_bean->isOwner($current_user->id);
188 return ACLController::checkAccess($this->merge_bean->module_dir,'edit', true);
192 //keep save function to handle anything special on merges
193 function save($check_notify = FALSE) {
195 return parent :: save($check_notify);
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';
203 if (isset($search_params[$searchFieldString]) ) {
205 if (isset($search_params[$searchFieldString]) == '') {
206 $this->field_search_params[$key]['value']='NULL';
208 $this->field_search_params[$key]['value']=$search_params[$searchFieldString];
210 if (isset ($search_params[$searchTypeString])) {
211 $this->field_search_params[$key]['search_type'] = $search_params[$searchTypeString];
213 $this->field_search_params[$key]['search_type'] = 'Exact';
215 //add field_def to the array.
216 $this->field_search_params[$key] = array_merge($value,$this->field_search_params[$key] );
221 function get_inputs_for_search_params($search_params)
224 foreach ($this->merge_bean->field_defs as $key=>$value) {
225 $searchFieldString=$key.'SearchField';
226 $searchTypeString=$key.'SearchType';
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";
234 return $returnString;
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";
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)."%'";
251 elseif($search_type=='start'){
252 $where = "releases.name LIKE '".$GLOBALS['db']->quote($value)."%'";
255 $where = "releases.name = '".$GLOBALS['db']->quote($value)."'";
257 $list=$this->merge_bean2->get_releases(false,'Active',$where);
258 foreach($list as $key => $value){
259 $list_to_join[]="'".$GLOBALS['db']->quote($key)."'";
261 $in=implode(', ', $list_to_join);
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";
271 $table_name = $this->merge_bean->table_name;
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'])."%'");
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)");
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;
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'])."%'");
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)");
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;
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'])."'");
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)");
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;
328 if($this->merge_bean->bean_implements('ACL') && ACLController::requireOwner($this->merge_bean->module_dir, 'delete') )
330 global $current_user;
331 $where_clauses[] = $this->merge_bean->getOwnerWhere($current_user->id);
333 array_push($where_clauses, $this->merge_bean->table_name.".id !='".$GLOBALS['db']->quote($this->merge_bean->id)."'");
334 return $where_clauses;
337 //duplicating utils function for now for possiblity of future or/and and
338 //other functionality
339 function generate_where_statement($where_clauses) {
342 foreach ($where_clauses as $clause) {