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.
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 /*********************************************************************************
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 ********************************************************************************/
46 require_once('modules/UpgradeWizard/SugarMerge/EditViewMerge.php');
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
51 class ListViewMerge extends EditViewMerge{
52 protected $varName = 'listViewDefs';
53 protected $viewDefs = 'ListView';
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
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
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]))));
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
77 protected function getFields(&$panels, $multiple = true){
80 if(!$multiple)$panels = array($panels);
82 foreach($panels as $panel_id=>$panel){
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));
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
103 protected function buildPanels() {
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]);
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'];
122 * Since all the meta-data is just a list of fields the panel section should be all the meta data
125 protected function setPanels(){
126 $this->newData = $this->buildPanels();
130 * This will save the merged data to a file
132 * @param STRING $to - path of the file to save it to
133 * @return BOOLEAN - success or failure of the save
135 public function save($to){
136 return write_array_to_file("$this->varName['$this->module']", $this->newData, $to);
141 * Merges the fields together and stores them in $this->mergedFields
144 protected function mergeFields() {
145 if ($this->sugarMerge instanceof SugarMerge && is_file($this->sugarMerge->getNewPath() . 'modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php')) {
146 require_once($this->sugarMerge->getNewPath() . 'modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php');
148 require_once('modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php');
150 $objectName = BeanFactory::getBeanName($this->module);
151 VardefManager::loadVardef($this->module, $objectName);
153 foreach($this->customFields as $field=>$data){
154 $fieldName = strtolower($data['loc']['row']);
155 if (!empty($GLOBALS['dictionary'][$objectName]['fields'][$fieldName])) {
156 $data['data'] = array_merge(ListLayoutMetaDataParser::createViewDefsByFieldDefs($GLOBALS['dictionary'][$objectName]['fields'][$fieldName]), $data['data']);
158 //if we have this field in both the new fields and the original fields - it has existed since the last install/upgrade
159 if(isset($this->newFields[$field]) && isset($this->originalFields[$field])){
160 //if both the custom field and the original match then we take the location of the custom field since it hasn't moved
161 $loc = $this->customFields[$field]['loc'];
162 $loc['source'] = 'custom';
164 //echo var_export($loc, true);
165 //but we still merge the meta data of the three
166 $this->mergedFields[$field] = array(
167 'data'=>$this->mergeField($this->originalFields[$field]['data'], $this->newFields[$field]['data'], $this->customFields[$field]['data']),
171 //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
172 } else if(!isset($this->newFields[$field])){
173 $this->mergedFields[$field] = $data;
174 $this->mergedFields[$field]['loc']['source'] = 'custom';
176 //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
177 $this->mergedFields[$field] = array(
178 'data'=>$this->mergeField('', $this->newFields[$field]['data'], $this->customFields[$field]['data']),
179 'loc'=>$this->customFields[$field]['loc']);
181 $this->mergedFields[$field]['loc']['source'] = 'custom';
182 //echo var_export($this->mergedFields[$field], true);
185 //then we clear out the field from
186 unset($this->originalFields[$field]);
187 unset($this->customFields[$field]);
188 unset($this->newFields[$field]);
193 * These are fields that were removed by the customer
195 foreach($this->originalFields as $field=>$data){
196 unset($this->originalFields[$field]);
197 unset($this->newFields[$field]);
200 foreach($this->newFields as $field=>$data){
201 $data['loc']['source']= 'new';
202 $this->mergedFields[$field] = array(
203 'data'=>$data['data'],
204 'loc'=>$data['loc']);
205 unset($this->newFields[$field]);
211 * Merges the meta data of a single field
213 * @param ARRAY $orig - the original meta-data for this field
214 * @param ARRAY $new - the new meta-data for this field
215 * @param ARRAY $custom - the custom meta-data for this field
216 * @return ARRAY $merged - the merged meta-data
218 protected function mergeField($orig, $new, $custom){
219 $orig_custom = $this->areMatchingValues($orig, $custom);
220 $new_custom = $this->areMatchingValues($new, $custom);
221 // if both are true then there is nothing to merge since all three fields match
222 if(!($orig_custom && $new_custom)){
223 $this->log('merging field');
224 $this->log('original meta-data');
226 $this->log('new meta-data');
228 $this->log('custom meta-data');
230 $this->log('merged meta-data');
235 //if orignal and custom match always take the new value or if new and custom match
236 if($orig_custom || $new_custom){
238 $new['default'] = isset($custom['default']) ? $custom['default'] : false;
241 //if original and new match always take the custom
242 if($this->areMatchingValues($orig, $new)){
247 if(is_array($custom)) {
248 //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
250 $new = $this->arrayMerge($custom, $new);
252 $new['default'] = $custom['default'];
255 //otherwise we know that new is not an array and custom has been 'customized' so let's keep those customizations.
261 //default to returning the New version of the field
262 $new['default'] = isset($custom['default']) ? $custom['default'] : false;