2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
39 * Description: TODO: To be written.
40 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
41 * All Rights Reserved.
42 * Contributor(s): ______________________________________..
43 ********************************************************************************/
46 $focus = new MergeRecord();
47 $focus->load_merge_bean($_REQUEST['merge_module'], true, $_REQUEST['record']);
49 foreach($focus->merge_bean->column_fields as $field)
51 if(isset($_POST[$field]))
53 $value = $_POST[$field];
54 if(is_array($value) && !empty($focus->merge_bean->field_defs[$field]['isMultiSelect'])) {
55 if(empty($value[0])) {
58 $value = encodeMultienumValue($value);
60 $focus->merge_bean->$field = $value;
61 }elseif (isset($focus->merge_bean->field_name_map[$field]['type']) && $focus->merge_bean->field_name_map[$field]['type'] == 'bool' ) {
62 $focus->merge_bean->$field = 0;
66 foreach($focus->merge_bean->additional_column_fields as $field)
68 if(isset($_POST[$field]))
70 $value = $_POST[$field];
71 if(is_array($value) && !empty($focus->merge_bean->field_defs[$field]->properties['isMultiSelect'])) {
72 if(empty($value[0])) {
75 $value = encodeMultienumValue($value);
77 $focus->merge_bean->$field = $value;
83 $_REQUEST['useEmailWidget'] = true;
84 $focus->merge_bean->save($check_notify);
85 unset($_REQUEST['useEmailWidget']);
87 $return_id = $focus->merge_bean->id;
88 $return_module = $focus->merge_module;
89 $return_action = 'DetailView';
91 //handle realated data.
93 $linked_fields=$focus->merge_bean->get_linked_fields();
95 $exclude = explode(',', $_REQUEST['merged_links']);
97 if (is_array($_POST['merged_ids'])) {
98 foreach ($_POST['merged_ids'] as $id) {
99 require_once ($focus->merge_bean_file_path);
100 $mergesource = new $focus->merge_bean_class();
101 $mergesource->retrieve($id);
103 foreach ($linked_fields as $name => $properties) {
104 if ($properties['name']=='modified_user_link' || in_array($properties['name'], $exclude))
108 if (isset($properties['duplicate_merge'])) {
109 if ($properties['duplicate_merge']=='disabled' or
110 $properties['duplicate_merge']=='false' or
111 $properties['name']=='assigned_user_link') {
115 if ($name == 'accounts' && $focus->merge_bean->module_dir == 'Opportunities')
119 if ($mergesource->load_relationship($name)) {
120 //check to see if loaded relationship is with email address
121 $relName=$mergesource->$name->getRelatedModuleName();
122 if (!empty($relName) and strtolower($relName)=='emailaddresses'){
123 //handle email address merge
124 handleEmailMerge($focus,$name,$mergesource->$name->get());
126 $data=$mergesource->$name->get();
127 if (is_array($data)) {
128 if ($focus->merge_bean->load_relationship($name) ) {
129 foreach ($data as $related_id) {
130 //add to primary bean
131 $focus->merge_bean->$name->add($related_id);
139 //delete the child bean, this action will cascade into related data too.
140 $mergesource->mark_deleted($mergesource->id);
143 $GLOBALS['log']->debug("Merged record with id of ".$return_id);
145 header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
148 //This function will compare the email addresses to be merged and only add the email id's
149 //of the email addresses that are not duplicates.
150 //$focus - Merge Bean
151 //$name - name of relationship (email_addresses)
152 //$data - array of email id's that will be merged into existing bean.
153 function handleEmailMerge($focus,$name,$data){
155 //get the email id's to merge
158 //make sure id's to merge exist and are in array format
160 //get the existing email id's
161 $focus->merge_bean->load_relationship($name);
162 $exData=$focus->merge_bean->$name->get();
164 if (!is_array($existingData) || empty($existingData)) {
167 //query email and retrieve existing email address
168 $exEmailQuery = 'Select id, email_address from email_addresses where id in (';
170 foreach($exData as $id){
172 $exEmailQuery .= " '$id' ";
175 $exEmailQuery .= ", '$id' ";
179 $exEmailQuery .= ')';
181 $exResult = $focus->merge_bean->db->query($exEmailQuery);
182 while(($row=$focus->merge_bean->db->fetchByAssoc($exResult))!= null) {
183 $existingEmails[$row['id']]=$row['email_address'];
187 //query email and retrieve email address to be linked.
188 $newEmailQuery = 'Select id, email_address from email_addresses where id in (';
190 foreach($existingData as $id){
192 $newEmailQuery .= " '$id' ";
195 $newEmailQuery .= ", '$id' ";
199 $newEmailQuery .= ')';
201 $newResult = $focus->merge_bean->db->query($newEmailQuery);
202 while(($row=$focus->merge_bean->db->fetchByAssoc($newResult))!= null) {
203 $newEmails[$row['id']]=$row['email_address'];
206 //compare the two arrays and remove duplicates
207 foreach($newEmails as $k=>$n){
208 if(!in_array($n,$existingEmails)){
214 foreach ($mrgArray as $related_id=>$related_val) {
215 //add to primary bean
216 $focus->merge_bean->$name->add($related_id);