]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MergeRecords/SaveMerge.php
Release 6.1.5
[Github/sugarcrm.git] / modules / MergeRecords / SaveMerge.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
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  ********************************************************************************/
44
45
46 $focus = new MergeRecord();
47 $focus->load_merge_bean($_REQUEST['merge_module'], true, $_REQUEST['record']);
48
49 foreach($focus->merge_bean->column_fields as $field)
50 {
51         if(isset($_POST[$field]))
52         {
53                 $value = $_POST[$field];
54                 if(is_array($value) && !empty($focus->merge_bean->field_defs[$field]['isMultiSelect'])) {
55             if(empty($value[0])) {
56                 unset($value[0]);
57             }
58             $value = encodeMultienumValue($value);
59         }
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;
63         }
64 }
65
66 foreach($focus->merge_bean->additional_column_fields as $field)
67 {
68         if(isset($_POST[$field]))
69         {
70                 $value = $_POST[$field];
71                 if(is_array($value) && !empty($focus->merge_bean->field_defs[$field]->properties['isMultiSelect'])) {
72             if(empty($value[0])) {
73                 unset($value[0]);
74             }
75             $value = encodeMultienumValue($value);
76         }
77                 $focus->merge_bean->$field = $value;
78         }
79 }
80
81 global $check_notify;
82
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;
87 }
88 $focus->merge_bean->save($check_notify);
89 unset($_REQUEST['useEmailWidget']);
90
91 $return_id = $focus->merge_bean->id;
92 $return_module = $focus->merge_module;
93 $return_action = 'DetailView';
94
95 //handle realated data.
96
97 $linked_fields=$focus->merge_bean->get_linked_fields();
98
99 $exclude = explode(',', $_REQUEST['merged_links']);
100
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);        
106         //kbrill Bug #13826
107         foreach ($linked_fields as $name => $properties) {
108                 if ($properties['name']=='modified_user_link' || in_array($properties['name'], $exclude))
109                 {
110                         continue;
111                 }
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') {
116                     continue;
117                 }
118             }
119             if ($name == 'accounts' && $focus->merge_bean->module_dir == 'Opportunities')
120                 continue;
121                 
122                 
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());   
129                 }else{
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);
136                             }   
137                         }
138                     }
139                 }
140             }
141         }
142         //END Bug #13826
143         //delete the child bean, this action will cascade into related data too.
144         $mergesource->mark_deleted($mergesource->id);
145     }
146 }
147 $GLOBALS['log']->debug("Merged record with id of ".$return_id);
148
149 header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
150
151
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){
158     $mrgArray = array();
159     //get the email id's to merge
160     $existingData=$data;
161
162     //make sure id's to merge exist and are in array format
163
164     //get the existing email id's 
165     $focus->merge_bean->load_relationship($name);
166     $exData=$focus->merge_bean->$name->get();
167
168     if (!is_array($existingData) || empty($existingData)) {
169         return ;
170     }
171         //query email and retrieve existing email address 
172         $exEmailQuery = 'Select id, email_address from email_addresses where id in (';
173             $first = true;
174             foreach($exData as $id){
175                 if($first){
176                     $exEmailQuery .= " '$id' ";
177                     $first = false;
178                 }else{
179                     $exEmailQuery .= ", '$id' ";
180                     $first = false;
181                 }
182             }
183         $exEmailQuery .= ')';
184
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'];
188         }
189
190
191         //query email and retrieve email address to be linked.
192         $newEmailQuery = 'Select id, email_address from email_addresses where id in (';
193             $first = true;
194             foreach($existingData as $id){
195                 if($first){
196                     $newEmailQuery .= " '$id' ";
197                     $first = false;
198                 }else{
199                     $newEmailQuery .= ", '$id' ";
200                     $first = false;
201                 }        
202             }
203         $newEmailQuery .= ')';
204         
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'];
208         }
209
210         //compare the two arrays and remove duplicates
211         foreach($newEmails as $k=>$n){
212             if(!in_array($n,$existingEmails)){
213                 $mrgArray[$k] = $n;
214             }   
215         }
216
217         //add email id's.
218         foreach ($mrgArray as $related_id=>$related_val) {
219             //add to primary bean
220             $focus->merge_bean->$name->add($related_id);
221         }   
222 }
223 ?>