]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/UpgradeWizard/SugarMerge/ListViewMerge.php
Release 6.5.0
[Github/sugarcrm.git] / modules / UpgradeWizard / SugarMerge / ListViewMerge.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-2012 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
40  * Description:  Defines the English language pack for the base application.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 require_once('modules/UpgradeWizard/SugarMerge/EditViewMerge.php');
47 /**
48  * This class is used to merge list view meta data. It subclasses EditView merge and transforms listview meta data into EditView meta data  for the merge and then transforms it back into list view meta data
49  *
50  */
51 class ListViewMerge extends EditViewMerge{
52         protected $varName = 'listViewDefs';
53         protected $viewDefs = 'ListView';
54         
55 /**
56          * Loads the meta data of the original, new, and custom file into the variables originalData, newData, and customData respectively it then transforms them into a structure that EditView Merge would understand
57          *
58          * @param STRING $module - name of the module's files that are to be merged
59          * @param STRING $original_file - path to the file that originally shipped with sugar
60          * @param STRING $new_file - path to the new file that is shipping with the patch 
61          * @param STRING $custom_file - path to the custom file
62          */
63         protected function loadData($module, $original_file, $new_file, $custom_file){
64                 parent::loadData($module, $original_file, $new_file, $custom_file);
65                 $this->originalData = array($module=>array( $this->viewDefs=>array($this->panelName=>array('DEFAULT'=>$this->originalData[$module]))));
66                 $this->customData = array($module=>array( $this->viewDefs=>array($this->panelName=>array('DEFAULT'=>$this->customData[$module]))));
67                 $this->newData = array($module=>array( $this->viewDefs=>array($this->panelName=>array('DEFAULT'=>$this->newData[$module]))));
68         
69         }
70         
71         /**
72          * This takes in a  list of panels and returns an associative array of field names to the meta-data of the field as well as the locations of that field
73          * Since ListViews don't have the concept of rows and columns it takes the panel and the row to be the field name
74          * @param ARRAY $panels - this is the 'panel' section of the meta-data for list views all the meta data is one panel since it is just a list of fields
75          * @return ARRAY $fields - an associate array of fields and their meta-data as well as their location
76          */
77         protected function getFields(&$panels, $multiple = true){
78                 $fields = array();
79                 $blanks = 0;
80                 if(!$multiple)$panels = array($panels);
81                 
82                 foreach($panels as $panel_id=>$panel){
83                                 
84                                 foreach($panel as $col_id=>$col){
85                                         $field_name = $col_id;
86                                         $fields[$field_name. $panel_id] = array('data'=>$col, 'loc'=>array('row'=>$col_id, 'panel'=>$col_id));
87                                 }
88         
89                                 
90                                 
91                         }
92                         return $fields;
93                 }
94                 
95         /**
96          * This builds the array of fields from the merged fields in the appropriate order
97          * when building the panels for a list view the most important thing is order 
98          * so we ensure the fields that came from the custom file keep 
99          * their order then we add any new fields at the end
100          *
101          * @return ARRAY
102          */
103         protected function buildPanels() {
104                 $panels  = array();
105                 //first only deal with ones that have their location coming from the custom source
106                 foreach($this->mergedFields as $id =>$field){
107                         if($field['loc']['source'] == 'custom'){
108                                 $panels[$field['loc']['panel']] = $field['data'];
109                                 unset($this->mergedFields[$id]);
110                         }
111                 }
112                 //now deal with the rest 
113                 foreach($this->mergedFields as $id =>$field){
114                             //Set the default attribute to false for all the rest of these fields since they're not from custom source
115                             $field['data']['default'] = false;
116                                 $panels[$field['loc']['panel']] = $field['data'];
117                 }
118                 return $panels;
119         }
120         
121         /**
122          * Since all the meta-data is just a list of fields the panel section should be all the meta data
123          *
124          */
125         protected function setPanels(){
126                 $this->newData = $this->buildPanels();
127         }
128         
129         /**
130          * This will save the merged data to a file
131          *
132          * @param STRING $to - path of the file to save it to 
133          * @return BOOLEAN - success or failure of the save
134          */
135         public function save($to){
136                 return write_array_to_file("$this->varName['$this->module']", $this->newData, $to);
137         }
138         
139         
140         /**
141          * Merges the fields together and stores them in $this->mergedFields
142          *
143          */
144         protected function mergeFields() {
145                 foreach($this->customFields as $field=>$data){
146                         //if we have this field in both the new fields and the original fields - it has existed since the last install/upgrade
147                         if(isset($this->newFields[$field]) && isset($this->originalFields[$field])){
148                                 //if both the custom field and the original match then we take the location of the custom field since it hasn't moved
149                                 $loc = $this->customFields[$field]['loc'];
150                                 $loc['source'] = 'custom';      
151
152                                 //echo var_export($loc, true);
153                                 //but we still merge the meta data of the three
154                                 $this->mergedFields[$field] = array(
155                                         'data'=>$this->mergeField($this->originalFields[$field]['data'], $this->newFields[$field]['data'], $this->customFields[$field]['data']), 
156                                         'loc'=>$loc);
157                                 
158                                 
159                         //if it's not set in the new fields then it was a custom field or an original field so we take the custom fields data and set the location source to custom
160                         } else if(!isset($this->newFields[$field])){
161                                 $this->mergedFields[$field] = $data;
162                                 $this->mergedFields[$field]['loc']['source'] = 'custom';
163                         } else {        
164                                 //otherwise  the field is in both new and custom but not in the orignal so we merge the new and custom data together and take the location from the custom
165                                 $this->mergedFields[$field] = array(
166                                         'data'=>$this->mergeField('', $this->newFields[$field]['data'], $this->customFields[$field]['data']), 
167                                         'loc'=>$this->customFields[$field]['loc']);
168                                 
169                                 $this->mergedFields[$field]['loc']['source'] = 'custom';
170                                 //echo var_export($this->mergedFields[$field], true);
171                         }
172                         
173                         //then we clear out the field from 
174                         unset($this->originalFields[$field]);
175                         unset($this->customFields[$field]);
176                         unset($this->newFields[$field]);
177                 }
178                 
179                 
180                 /**
181                  * These are fields that were removed by the customer
182                  */
183                 foreach($this->originalFields as $field=>$data){
184                         unset($this->originalFields[$field]);
185                         unset($this->newFields[$field]);
186                 }
187                         
188                 foreach($this->newFields as $field=>$data){
189                         $data['loc']['source']= 'new';
190                         $this->mergedFields[$field] = array(
191                                         'data'=>$data['data'], 
192                                         'loc'=>$data['loc']);
193                         unset($this->newFields[$field]);
194                 }
195         }
196                 
197         
198         /**
199          * Merges the meta data of a single field
200          *
201          * @param ARRAY $orig - the original meta-data for this field
202          * @param ARRAY $new - the new meta-data for this field
203          * @param ARRAY $custom - the custom meta-data for this field
204          * @return ARRAY $merged - the merged meta-data
205          */
206         protected function mergeField($orig, $new, $custom){
207                 $orig_custom = $this->areMatchingValues($orig, $custom);
208                 $new_custom = $this->areMatchingValues($new, $custom);
209                 // if both are true then there is nothing to merge since all three fields match
210                 if(!($orig_custom && $new_custom)){
211                         $this->log('merging field');
212                         $this->log('original meta-data');
213                         $this->log($orig);
214                         $this->log('new meta-data');
215                         $this->log($new);
216                         $this->log('custom meta-data');
217                         $this->log($custom);
218                         $this->log('merged meta-data');
219                         $log = true;
220                 }else{
221                         return $new;
222                 }
223                 //if orignal and custom match always take the new value or if new and custom match
224                 if($orig_custom || $new_custom){
225                         $this->log($new);
226                         $new['default'] = isset($custom['default']) ? $custom['default'] : false;
227                         return $new;
228                 }
229                 //if original and new match always take the custom
230                 if($this->areMatchingValues($orig, $new)){
231                         $this->log($custom);
232                         return $custom;
233                 }
234                 
235                 if(is_array($custom)) {
236                         //if both new and custom are arrays then at this point new != custom and orig != custom and orig != new  so let's merge the custom and the new and return that
237                         if(is_array($new)){
238                                 $new = $this->arrayMerge($custom, $new);
239                                 $this->log($new);
240                                 $new['default'] = $custom['default'];
241                                 return $new;
242                         }else{
243                                 //otherwise we know that new is not an array and custom has been 'customized' so let's keep those customizations.
244                                 $this->log($custom);
245                                 return $custom;
246                         }
247                 }
248                 
249                 //default to returning the New version of the field 
250                 $new['default'] = isset($custom['default']) ? $custom['default'] : false;
251                 return $new; 
252         }       
253 }
254
255 ?>