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.
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);
102 //load master module strings
103 if ($load_module_strings)
104 $this->merge_bean_strings = return_module_language($current_language, $merge_module);
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 = '') {
112 global $current_language;
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];
118 require_once ($this->merge_bean_file_path2);
119 $this->merge_bean2 = new $this->merge_bean_class2();
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);
127 var $new_schema = true;
129 //-----------------------------------------------------------------------
130 //-------------Wrapping Necessary Merge Bean Calls-----------------------
131 //-----------------------------------------------------------------------
133 function fill_in_additional_list_fields() {
134 return $this->merge_bean->fill_in_additional_list_fields();
137 function fill_in_additional_detail_fields() {
138 return $this->merge_bean->fill_in_additional_detail_fields();
141 function get_summary_text() {
142 return $this->merge_bean->get_summary_text();
145 function get_list_view_data() {
146 return $this->merge_bean->get_list_view_data();
148 //-----------------------------------------------------------------------
149 //-----------------------------------------------------------------------
150 //-----------------------------------------------------------------------
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
156 function build_generic_where_clause($the_query_string) {
157 return $this->merge_bean->build_generic_where_clause($the_query_string);
160 //adding in 4.0+ acl function for possible acl stuff down the line
161 function bean_implements($interface) {
162 switch ($interface) {
169 function ACLAccess($view,$is_owner='not_set'){
170 global $current_user;
172 //if the module doesn't implement ACLS or is empty
173 if(empty($this->merge_bean) || !$this->merge_bean->bean_implements('ACL'))
178 if($is_owner == 'not_set'){
179 $is_owner = $this->merge_bean->isOwner($current_user->id);
181 return ACLController::checkAccess($this->merge_bean->module_dir,'edit', true);
185 //keep save function to handle anything special on merges
186 function save($check_notify = FALSE) {
188 return parent :: save($check_notify);
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';
196 if (isset($search_params[$searchFieldString]) ) {
198 if (isset($search_params[$searchFieldString]) == '') {
199 $this->field_search_params[$key]['value']='NULL';
201 $this->field_search_params[$key]['value']=$search_params[$searchFieldString];
203 if (isset ($search_params[$searchTypeString])) {
204 $this->field_search_params[$key]['search_type'] = $search_params[$searchTypeString];
206 $this->field_search_params[$key]['search_type'] = 'Exact';
208 //add field_def to the array.
209 $this->field_search_params[$key] = array_merge($value,$this->field_search_params[$key] );
214 function get_inputs_for_search_params($search_params)
217 foreach ($this->merge_bean->field_defs as $key=>$value) {
218 $searchFieldString=$key.'SearchField';
219 $searchTypeString=$key.'SearchType';
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";
227 return $returnString;
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";
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)."%'";
244 elseif($search_type=='start'){
245 $where = "releases.name LIKE '".$GLOBALS['db']->quote($value)."%'";
248 $where = "releases.name = '".$GLOBALS['db']->quote($value)."'";
250 $list=$this->merge_bean2->get_releases(false,'Active',$where);
251 foreach($list as $key => $value){
252 $list_to_join[]="'".$GLOBALS['db']->quote($key)."'";
254 $in=implode(', ', $list_to_join);
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";
264 $table_name = $this->merge_bean->table_name;
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'])."%'");
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)");
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;
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'])."%'");
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)");
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;
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'])."'");
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)");
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;
318 array_push($where_clauses, $this->merge_bean->table_name.".id !='".$GLOBALS['db']->quote($this->merge_bean->id)."'");
319 return $where_clauses;
322 //duplicating utils function for now for possiblity of future or/and and
323 //other functionality
324 function generate_where_statement($where_clauses) {
327 foreach ($where_clauses as $clause) {