]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MergeRecords/SaveMerge.php
Release 6.2.0beta4
[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 $focus->merge_bean->save($check_notify);
85 unset($_REQUEST['useEmailWidget']);
86
87 $return_id = $focus->merge_bean->id;
88 $return_module = $focus->merge_module;
89 $return_action = 'DetailView';
90
91 //handle realated data.
92
93 $linked_fields=$focus->merge_bean->get_linked_fields();
94
95 $exclude = explode(',', $_REQUEST['merged_links']);
96
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);        
102         //kbrill Bug #13826
103         foreach ($linked_fields as $name => $properties) {
104                 if ($properties['name']=='modified_user_link' || in_array($properties['name'], $exclude))
105                 {
106                         continue;
107                 }
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') {
112                     continue;
113                 }
114             }
115             if ($name == 'accounts' && $focus->merge_bean->module_dir == 'Opportunities')
116                 continue;
117                 
118                 
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());   
125                 }else{
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);
132                             }   
133                         }
134                     }
135                 }
136             }
137         }
138         //END Bug #13826
139         //delete the child bean, this action will cascade into related data too.
140         $mergesource->mark_deleted($mergesource->id);
141     }
142 }
143 $GLOBALS['log']->debug("Merged record with id of ".$return_id);
144
145 header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
146
147
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){
154     $mrgArray = array();
155     //get the email id's to merge
156     $existingData=$data;
157
158     //make sure id's to merge exist and are in array format
159
160     //get the existing email id's 
161     $focus->merge_bean->load_relationship($name);
162     $exData=$focus->merge_bean->$name->get();
163
164     if (!is_array($existingData) || empty($existingData)) {
165         return ;
166     }
167         //query email and retrieve existing email address 
168         $exEmailQuery = 'Select id, email_address from email_addresses where id in (';
169             $first = true;
170             foreach($exData as $id){
171                 if($first){
172                     $exEmailQuery .= " '$id' ";
173                     $first = false;
174                 }else{
175                     $exEmailQuery .= ", '$id' ";
176                     $first = false;
177                 }
178             }
179         $exEmailQuery .= ')';
180
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'];
184         }
185
186
187         //query email and retrieve email address to be linked.
188         $newEmailQuery = 'Select id, email_address from email_addresses where id in (';
189             $first = true;
190             foreach($existingData as $id){
191                 if($first){
192                     $newEmailQuery .= " '$id' ";
193                     $first = false;
194                 }else{
195                     $newEmailQuery .= ", '$id' ";
196                     $first = false;
197                 }        
198             }
199         $newEmailQuery .= ')';
200         
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'];
204         }
205
206         //compare the two arrays and remove duplicates
207         foreach($newEmails as $k=>$n){
208             if(!in_array($n,$existingEmails)){
209                 $mrgArray[$k] = $n;
210             }   
211         }
212
213         //add email id's.
214         foreach ($mrgArray as $related_id=>$related_val) {
215             //add to primary bean
216             $focus->merge_bean->$name->add($related_id);
217         }   
218 }
219 ?>