2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition 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 if (isset($_POST['date_entered'])) {
85 // set this to true so we won't unset date_entered when saving
86 $focus->merge_bean->update_date_entered = true;
88 $focus->merge_bean->save($check_notify);
89 unset($_REQUEST['useEmailWidget']);
91 $return_id = $focus->merge_bean->id;
92 $return_module = $focus->merge_module;
93 $return_action = 'DetailView';
95 //handle realated data.
97 $linked_fields=$focus->merge_bean->get_linked_fields();
99 $exclude = explode(',', $_REQUEST['merged_links']);
101 if (is_array($_POST['merged_ids'])) {
102 foreach ($_POST['merged_ids'] as $id) {
103 require_once ($focus->merge_bean_file_path);
104 $mergesource = new $focus->merge_bean_class();
105 $mergesource->retrieve($id);
107 foreach ($linked_fields as $name => $properties) {
108 if ($properties['name']=='modified_user_link' || in_array($properties['name'], $exclude))
112 if (isset($properties['duplicate_merge'])) {
113 if ($properties['duplicate_merge']=='disabled' or
114 $properties['duplicate_merge']=='false' or
115 $properties['name']=='assigned_user_link') {
119 if ($name == 'accounts' && $focus->merge_bean->module_dir == 'Opportunities')
123 if ($mergesource->load_relationship($name)) {
124 //check to see if loaded relationship is with email address
125 $relName=$mergesource->$name->getRelatedModuleName();
126 if (!empty($relName) and strtolower($relName)=='emailaddresses'){
127 //handle email address merge
128 handleEmailMerge($focus,$name,$mergesource->$name->get());
130 $data=$mergesource->$name->get();
131 if (is_array($data)) {
132 if ($focus->merge_bean->load_relationship($name) ) {
133 foreach ($data as $related_id) {
134 //add to primary bean
135 $focus->merge_bean->$name->add($related_id);
143 //delete the child bean, this action will cascade into related data too.
144 $mergesource->mark_deleted($mergesource->id);
147 $GLOBALS['log']->debug("Merged record with id of ".$return_id);
149 header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
152 //This function will compare the email addresses to be merged and only add the email id's
153 //of the email addresses that are not duplicates.
154 //$focus - Merge Bean
155 //$name - name of relationship (email_addresses)
156 //$data - array of email id's that will be merged into existing bean.
157 function handleEmailMerge($focus,$name,$data){
159 //get the email id's to merge
162 //make sure id's to merge exist and are in array format
164 //get the existing email id's
165 $focus->merge_bean->load_relationship($name);
166 $exData=$focus->merge_bean->$name->get();
168 if (!is_array($existingData) || empty($existingData)) {
171 //query email and retrieve existing email address
172 $exEmailQuery = 'Select id, email_address from email_addresses where id in (';
174 foreach($exData as $id){
176 $exEmailQuery .= " '$id' ";
179 $exEmailQuery .= ", '$id' ";
183 $exEmailQuery .= ')';
185 $exResult = $focus->merge_bean->db->query($exEmailQuery);
186 while(($row=$focus->merge_bean->db->fetchByAssoc($exResult))!= null) {
187 $existingEmails[$row['id']]=$row['email_address'];
191 //query email and retrieve email address to be linked.
192 $newEmailQuery = 'Select id, email_address from email_addresses where id in (';
194 foreach($existingData as $id){
196 $newEmailQuery .= " '$id' ";
199 $newEmailQuery .= ", '$id' ";
203 $newEmailQuery .= ')';
205 $newResult = $focus->merge_bean->db->query($newEmailQuery);
206 while(($row=$focus->merge_bean->db->fetchByAssoc($newResult))!= null) {
207 $newEmails[$row['id']]=$row['email_address'];
210 //compare the two arrays and remove duplicates
211 foreach($newEmails as $k=>$n){
212 if(!in_array($n,$existingEmails)){
218 foreach ($mrgArray as $related_id=>$related_val) {
219 //add to primary bean
220 $focus->merge_bean->$name->add($related_id);