]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MergeRecords/SaveMerge.php
Release 6.5.16
[Github/sugarcrm.git] / modules / MergeRecords / SaveMerge.php
1 <?php
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-2013 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         if (!$mergesource->retrieve($id))
106         {
107             continue;
108         }               
109         //kbrill Bug #13826
110         foreach ($linked_fields as $name => $properties) {
111                 if ($properties['name']=='modified_user_link' || $properties['name']=='created_by_link' || in_array($properties['name'], $exclude))
112                 {
113                         continue;
114                 }
115             if (isset($properties['duplicate_merge'])) {
116                 if ($properties['duplicate_merge']=='disabled' or 
117                     $properties['duplicate_merge']=='false' or
118                     $properties['name']=='assigned_user_link') {
119                     continue;
120                 }
121             }
122             if ($name == 'accounts' && $focus->merge_bean->module_dir == 'Opportunities')
123                 continue;
124                 
125                 
126             if ($mergesource->load_relationship($name)) {
127                 //check to see if loaded relationship is with email address
128                 $relName=$mergesource->$name->getRelatedModuleName();
129                 if (!empty($relName) and strtolower($relName)=='emailaddresses'){
130                     //handle email address merge
131                     handleEmailMerge($focus,$name,$mergesource->$name->get());   
132                 }else{
133                     $data=$mergesource->$name->get();
134                     if (is_array($data)) {
135                         if ($focus->merge_bean->load_relationship($name) ) {
136                             foreach ($data as $related_id) {
137                                 //add to primary bean
138                                 $focus->merge_bean->$name->add($related_id);
139                             }   
140                         }
141                     }
142                 }
143             }
144         }
145         //END Bug #13826
146         //delete the child bean, this action will cascade into related data too.
147         $mergesource->mark_deleted($mergesource->id);
148     }
149 }
150 $GLOBALS['log']->debug("Merged record with id of ".$return_id);
151
152 //do not redirect if noRedirect flag is set.  This is mostly used by Unit tests
153 if(empty($_REQUEST['noRedirect'])){
154     header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
155 }
156
157 //This function will compare the email addresses to be merged and only add the email id's
158 //of the email addresses that are not duplicates.
159 //$focus - Merge Bean
160 //$name - name of relationship (email_addresses)
161 //$data - array of email id's that will be merged into existing bean.
162 function handleEmailMerge($focus,$name,$data){
163     $mrgArray = array();
164     //get the email id's to merge
165     $existingData=$data;
166
167     //make sure id's to merge exist and are in array format
168
169     //get the existing email id's 
170     $focus->merge_bean->load_relationship($name);
171     $exData=$focus->merge_bean->$name->get();
172
173     if (!is_array($existingData) || empty($existingData)) {
174         return ;
175     }
176         //query email and retrieve existing email address 
177         $exEmailQuery = 'Select id, email_address from email_addresses where id in (';
178             $first = true;
179             foreach($exData as $id){
180                 if($first){
181                     $exEmailQuery .= " '$id' ";
182                     $first = false;
183                 }else{
184                     $exEmailQuery .= ", '$id' ";
185                     $first = false;
186                 }
187             }
188         $exEmailQuery .= ')';
189
190         $exResult = $focus->merge_bean->db->query($exEmailQuery);
191         while(($row=$focus->merge_bean->db->fetchByAssoc($exResult))!= null) {
192             $existingEmails[$row['id']]=$row['email_address'];
193         }
194
195
196         //query email and retrieve email address to be linked.
197         $newEmailQuery = 'Select id, email_address from email_addresses where id in (';
198             $first = true;
199             foreach($existingData as $id){
200                 if($first){
201                     $newEmailQuery .= " '$id' ";
202                     $first = false;
203                 }else{
204                     $newEmailQuery .= ", '$id' ";
205                     $first = false;
206                 }        
207             }
208         $newEmailQuery .= ')';
209         
210         $newResult = $focus->merge_bean->db->query($newEmailQuery);
211         while(($row=$focus->merge_bean->db->fetchByAssoc($newResult))!= null) {
212             $newEmails[$row['id']]=$row['email_address'];
213         }
214
215         //compare the two arrays and remove duplicates
216         foreach($newEmails as $k=>$n){
217             if(!in_array($n,$existingEmails)){
218                 $mrgArray[$k] = $n;
219             }   
220         }
221
222         //add email id's.
223         foreach ($mrgArray as $related_id=>$related_val) {
224             //add to primary bean
225             $focus->merge_bean->$name->add($related_id);
226         }   
227 }
228 ?>