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 ********************************************************************************/
47 * This is the base class that all other SugarMerge objects extend
52 * The variable name that is used with the file for example in editviewdefs and detailviewdefs it is $viewdefs
56 protected $varName = 'viewdefs';
58 * Enter the name of the parameter used in the $varName for example in editviewdefs and detailviewdefs it is 'EditView' and 'DetailView' respectively - $viewdefs['EditView']
62 protected $viewDefs = 'EditView';
64 * this will store the meta data for the original file
68 protected $originalData = array();
70 * this will store the meta data for the new file
74 protected $newData = array();
76 * this will store the meta data for the custom file
80 protected $customData = array();
82 * this will store an associative array contianing all the fields that are used in the original meta data file
86 protected $originalFields = array();
88 * this will store an associative array contianing all the fields that are used in the new meta data file
92 protected $newFields = array();
94 * this will store an associative array contianing all the fields that are used in the custom meta data file
98 protected $customFields = array();
100 * this will store an associative array contianing all the merged fields
104 protected $mergedFields = array();
106 * the name of the module to be merged
110 protected $module = 'module';
112 * the max number of columns for this view
116 protected $maxCols = 2;
118 * If we should use the best match algorithim
122 protected $bestMatch = true;
124 * The default panel we place the fields in if we aren't using the best match algorithim
128 protected $defaultPanel = 'default';
130 * The name of the panels section in the meta data
134 protected $panelName = 'panels';
136 * The name of the templateMeta data secion in the meta data
138 protected $templateMetaName = 'templateMeta';
140 * The file pointer to log to if set to NULL it will use the GLOBALS['log'] if available and log to debug
144 protected $fp = NULL;
148 * Determines if getFields should analyze panels to determine if it is a MultiPanel
152 protected $scanForMultiPanel = true;
155 * If true then it works as though it's a multipanel
159 protected $isMultiPanel = true;
163 * The ids of the panels found in custom metadata fuke
166 protected $customPanelIds = array();
170 * The ids of the panels found in original metadata fuke
173 protected $originalPanelIds = array();
177 * The ids of the panels found in original metadata fuke
180 protected $newPanelIds = array();
184 * Special case conversion
187 protected $fieldConversionMapping = array(
188 'Campaigns' => array('created_by_name'=>'date_entered', 'modified_by_name'=>'date_modified'),
189 'Cases' => array('created_by_name'=>'date_entered', 'modified_by_name'=>'date_modified'),
190 'Contracts' => array('created_by_name'=>'date_entered', 'modified_by_name'=>'date_modified'),
191 'Leads' => array('created_by'=>'date_entered'),
192 'Meetings' => array('created_by_name'=>'date_entered', 'modified_by_name'=>'date_modified'),
193 'ProspectLists' => array('created_by_name'=>'date_entered', 'modified_by_name'=>'date_modified'),
194 'Prospects' => array('created_by_name'=>'date_entered', 'modified_by_name'=>'date_modified'),
198 * Clears out the values of the arrays so that the same object can be utilized
201 protected function clear(){
202 unset($this->newData);
203 $this->newData = array();
204 unset($this->customData);
205 $this->customData = array();
206 unset($this->originalData);
207 $this->originalData = array();
208 unset($this->newFields);
209 $this->newFields = array();
210 unset($this->customFields);
211 $this->customFields = array();
212 unset($this->originalFields);
213 $this->originalFields = array();
214 unset($this->mergedFields);
215 $this->mergedFields = array();
216 unset($this->mergeData);
217 $this->mergeData = array();
218 $this->defaultPanel = 'default';
222 * Allows the user to choose to use the best match algorithim or not
226 public function setBestMatch($on=true){
227 $this->bestMatch = $on;
232 * Allows users to set the name to use as the default panel in the meta data
234 * @param STRING $name - name of the default panel
236 public function setDefaultPanel($name = 'default'){
237 $this->defaultPanel = $name;
241 * Allows the user to set a filepointer that is already open to log to
243 * @param FILEPOINTER $fp
245 public function setLogFilePointer($fp){
250 * opens the file with the 'a' parameter and use it to log messages to
252 * @param STRING $file - path to file we wish to log to
254 public function setLogFile($file){
255 $this->fp = fopen($file, 'a');
262 * returns true if $val1 and $val2 match otherwise it returns false
264 * @param MULTI $val1 - a value to compare to val2
265 * @param MULTI $val2 - a value to compare to val1
266 * @return BOOLEAN - if $val1 and $val2 match
268 protected function areMatchingValues($val1, $val2){
269 if(!is_array($val1)){
270 //if val2 is an array and val1 isn't then it isn't a match
274 //otherwise both are not arrays so we can return a comparison between them
275 return $val1 == $val2;
277 //if val1 is an array and val2 isn't then it isn't a match
278 if(!is_array($val2)){
282 foreach($val1 as $k=>$v){
283 if(!isset($val2[$k]))return false;
284 if(!$this->areMatchingValues($val1[$k], $val2[$k])){
290 //this implies that there are still values left so the two must not match since we unset any matching values
298 * Recursiveley merges two arrays
300 * @param ARRAY $gimp - if keys match this arrays values are overriden
301 * @param ARRAY $dom - if keys match this arrays values will override the others
302 * @return ARRAY $merged - the merges array
304 function arrayMerge($gimp, $dom) {
305 if(is_array($gimp) && is_array($dom)) {
306 foreach($dom as $domKey => $domVal) {
307 if(isset($gimp[$domKey])) {
308 if(is_array($domVal)) {
309 $gimp[$domKey] = $this->arrayMerge($gimp[$domKey], $dom[$domKey]);
311 $gimp[$domKey] = $domVal;
314 $gimp[$domKey] = $domVal;
322 * Merges the meta data of a single field
324 * @param ARRAY $orig - the original meta-data for this field
325 * @param ARRAY $new - the new meta-data for this field
326 * @param ARRAY $custom - the custom meta-data for this field
327 * @return ARRAY $merged - the merged meta-data
329 protected function mergeField($orig, $new, $custom){
330 $orig_custom = $this->areMatchingValues($orig, $custom);
331 $new_custom = $this->areMatchingValues($new, $custom);
332 // if both are true then there is nothing to merge since all three fields match
333 if(!($orig_custom && $new_custom)){
334 $this->log('merging field');
335 $this->log('original meta-data');
337 $this->log('new meta-data');
339 $this->log('custom meta-data');
341 $this->log('merged meta-data');
346 //if orignal and custom match always take the new value or if new and custom match
347 if($orig_custom || $new_custom){
351 //if original and new match always take the custom
352 if($this->areMatchingValues($orig, $new)){
357 if(is_array($custom)) {
358 //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
360 $new = $this->arrayMerge($custom, $new);
364 //otherwise we know that new is not an array and custom has been 'customized' so let's keep those customizations.
369 //default to returning the New version of the field
375 * Merges the fields together and stores them in $this->mergedFields
378 protected function mergeFields() {
379 foreach($this->customFields as $field=>$data) {
380 //if we have this field in both the new fields and the original fields - it has existed since the last install/upgrade
381 if(isset($this->newFields[$field]) && isset($this->originalFields[$field])){
382 //if both the custom field and the original match then we take the location of the custom field since it hasn't moved
383 $loc = $this->customFields[$field]['loc'];
384 $loc['source'] = 'custom';
388 //Address fields present a special problem...
389 if(preg_match('/(alt_|primary_|billing_|shipping_)address_street/i', $field, $matches)) {
390 $prefix = $matches[1];
391 $city = $prefix . 'address_city';
392 $postal_code = $prefix . 'address_postalcode';
393 $state = $prefix . 'address_state';
394 $country = $prefix . 'address_country';
396 if(isset($this->customFields[$city]) ||
397 isset($this->customFields[$postal_code]) ||
398 isset($this->customFields[$state]) ||
399 isset($this->customFields[$country])) {
401 $this->mergedFields[$field] = array(
402 'data'=>$this->customFields[$field]['data'],
408 //but we still merge the meta data of the three
409 $this->mergedFields[$field] = array(
410 'data'=>$this->mergeField($this->originalFields[$field]['data'], $this->newFields[$field]['data'], $this->customFields[$field]['data']),
413 //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
414 } else if(!isset($this->newFields[$field])){
415 $this->mergedFields[$field] = $data;
416 $this->mergedFields[$field]['loc']['source'] = 'custom';
418 //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
419 $this->mergedFields[$field] = array(
420 'data'=>$this->mergeField('', $this->newFields[$field]['data'], $this->customFields[$field]['data']),
421 'loc'=>$this->customFields[$field]['loc']);
423 $this->mergedFields[$field]['loc']['source'] = 'custom';
424 //echo var_export($this->mergedFields[$field], true);
427 //then we clear out the field from
428 unset($this->originalFields[$field]);
429 unset($this->customFields[$field]);
430 unset($this->newFields[$field]);
435 * These are fields that were removed by the customer
437 foreach($this->originalFields as $field=>$data){
438 unset($this->originalFields[$field]);
439 unset($this->newFields[$field]);
443 * These are fields that were added by sugar
445 $new_field_panel = $this->defaultPanel;
446 foreach($this->customPanelIds as $custom_panel_ids=>$panels) {
447 $new_field_panel = $custom_panel_ids;
450 foreach($this->newFields as $field=>$data){
451 $data['loc']['source']= 'new';
452 $data['loc']['panel'] = $new_field_panel;
453 $this->mergedFields[$field] = array(
454 'data'=>$data['data'],
455 'loc'=>$data['loc']);
456 unset($this->newFields[$field]);
461 * Walks through the merged fields and places them in the appropriate place based on their location parameter as well as the choosen algorithim
463 * @return ARRAY $panels - the new panels section for the merged file
465 protected function buildPanels(){
468 $panel_keys = array_keys($this->customPanelIds);
469 $this->defaultPanel = end($panel_keys);
471 foreach($this->mergedFields as $field_id=>$field){
472 //If this field is in a panel not defined in the custom layout, set it to default panel
473 if(!isset($this->customPanelIds[$field['loc']['panel']])) {
474 $field['loc']['panel'] = $this->defaultPanel;
477 if($field['loc']['source'] == 'new') {
478 if($this->bestMatch){
479 //for best match as long as the column is filled let's keep walking down till we can fill it
480 $row = end(array_keys($this->customData[$this->module][$this->viewDefs][$this->panelName][$field['loc']['panel']]));
482 while(!empty($panels[$field['loc']['panel']][$row][$col])){
489 //row should be at a point that there is no field in this location
490 $panels[$field['loc']['panel']][$row][$col] = $field['data'];
492 //so for not best match we place it in the default panel at the first available column for the row
494 while(!empty($panels[$this->defaultPanel][$row][$field['loc']['col']])){
497 $panels[$field['loc']['panel']][$row][$field['loc']['col']] = $field['data'];
500 $panels[$field['loc']['panel']][$field['loc']['row']][$field['loc']['col']] = $field['data'];
506 foreach($panels as $k=>$panel){
507 foreach($panel as $r=>$row){
508 ksort($panels[$k][$r]);
517 * Merge the templateMeta entry for the view defs. Also assume that any changes made in the custom files should
518 * have precedence since they must be changed manually, even over new files that may be provided in the upgarde
522 protected function mergeTemplateMeta()
524 //this is to handle the situation in Calls/Meetings where we updated the templateMeta and will fail if we don't update this.
525 //long term we should not do this and should provide a way for calls/meetings to update themselves.
526 if( isset($this->customData[$this->module][$this->viewDefs][$this->templateMetaName]) && strcmp(strtolower($this->module), 'calls') != 0 && strcmp(strtolower($this->module), 'meetings') != 0 )
528 $this->newData[$this->module][$this->viewDefs][$this->templateMetaName] = $this->customData[$this->module][$this->viewDefs][$this->templateMetaName];
534 * Sets the panel section for the meta-data after it has been merged
537 protected function setPanels(){
538 $this->newData[$this->module][$this->viewDefs][$this->panelName] = $this->buildPanels();
540 if(!$this->isMultiPanel) {
541 $this->newData[$this->module][$this->viewDefs][$this->panelName] = $this->newData[$this->module][$this->viewDefs][$this->panelName][$this->defaultPanel];
547 * Parses out the fields for each files meta data and then calls on mergeFields and setPanels
550 protected function mergeMetaData(){
551 $this->originalFields = $this->getFields($this->originalData[$this->module][$this->viewDefs][$this->panelName]);
552 $this->originalPanelIds = $this->getPanelIds($this->originalData[$this->module][$this->viewDefs][$this->panelName]);
553 $this->customFields = $this->getFields($this->customData[$this->module][$this->viewDefs][$this->panelName]);
554 $this->customPanelIds = $this->getPanelIds($this->customData[$this->module][$this->viewDefs][$this->panelName]);
555 $this->newFields = $this->getFields($this->newData[$this->module][$this->viewDefs][$this->panelName]);
556 //echo var_export($this->newFields, true);
557 $this->newPanelIds = $this->getPanelIds($this->newData[$this->module][$this->viewDefs][$this->panelName]);
558 $this->mergeFields();
559 $this->mergeTemplateMeta();
563 * 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
565 * @param ARRAY $panels - this is the 'panel' section of the meta-data
566 * @return ARRAY $fields - an associate array of fields and their meta-data as well as their location
568 protected function getFields(&$panels){
572 $setDefaultPanel = false;
574 if(count($panels) == 1) {
575 $arrayKeys = array_keys($panels);
576 if(!empty($arrayKeys[0])) {
577 $this->defaultPanel = $arrayKeys[0];
578 $panels = $panels[$arrayKeys[0]];
580 $panels = $panels[''];
582 $setDefaultPanel = true;
585 if($this->scanForMultiPanel){
586 require_once('include/SugarFields/Parsers/MetaParser.php');
587 if($setDefaultPanel || !MetaParser::hasMultiplePanels($panels)) {
588 $panels = array($this->defaultPanel=>$panels);
589 $this->isMultiPanel = false;
593 //echo "---------------------------------------------------------\n";
594 //echo var_export($panels, true);
596 foreach($panels as $panel_id=>$panel){
597 foreach($panel as $row_id=>$rows){
598 foreach($rows as $col_id=>$col){
600 $field_name = 'BLANK_' . $blanks;
603 $field_name = is_array($col) && isset($col['name']) ? $col['name'] : $col;
605 if(!empty($col['name'])) {
606 $field_name = $col['name'];
613 if(is_string($field_name)) {
614 // We need to replace all instances of the fake uploadfile and filename field that has custom code with the real filename field
615 if(!empty($col['customCode']))
617 if($field_name == 'uploadfile')
619 $replaceField = false;
620 if ( !empty($col['customCode']) ) {
621 $replaceField = true;
622 unset($col['customCode']);
625 if( !empty($col['displayParams']) && !empty($col['displayParams']['link']) ) {
626 $replaceField = true;
629 if ( $replaceField ) {
630 $field_name = 'filename';
631 $col['name'] = 'filename';
633 } else if ($field_name == 'filename') {
638 $fields[$field_name] = array('data'=>$col, 'loc'=>array('panel'=>"{$panel_id}", 'row'=>"{$row_id}", 'col'=>"{$col_id}"));
644 //echo "---------------------------------------------------------\n";
645 //echo var_export($fields, true);
655 protected function getPanelIds($panels){
657 $panel_ids = array();
658 $setDefaultPanel = false;
660 if(count($panels) == 1) {
661 $arrayKeys = array_keys($panels);
662 if(!empty($arrayKeys[0])) {
663 $this->defaulPanel = $arrayKeys[0];
664 $panels = $panels[$arrayKeys[0]];
666 $panels = $panels[''];
668 $setDefaultPanel = true;
671 if($this->scanForMultiPanel){
672 require_once('include/SugarFields/Parsers/MetaParser.php');
673 if($setDefaultPanel || !MetaParser::hasMultiplePanels($panels)) {
674 $panels = array($this->defaultPanel=>$panels);
675 $this->isMultiPanel = false;
679 foreach($panels as $panel_id=>$panel){
680 $panel_ids[$panel_id] = $panel_id;
687 * Loads the meta data of the original, new, and custom file into the variables originalData, newData, and customData respectively
689 * @param STRING $module - name of the module's files that are to be merged
690 * @param STRING $original_file - path to the file that originally shipped with sugar
691 * @param STRING $new_file - path to the new file that is shipping with the patch
692 * @param STRING $custom_file - path to the custom file
694 protected function loadData($module, $original_file, $new_file, $custom_file){
695 $this->module = $module;
696 $varnmame = $this->varName;
697 require($original_file);
698 $this->originalData = $$varnmame;
700 $this->newData = $$varnmame;
701 if(file_exists($custom_file)){
702 require($custom_file);
703 $this->customData = $$varnmame;
705 $this->customData = $this->originalData;
710 * This will save the merged data to a file
712 * @param STRING $to - path of the file to save it to
713 * @return BOOLEAN - success or failure of the save
715 public function save($to){
716 return write_array_to_file("viewdefs['$this->module']['$this->viewDefs']", $this->newData[$this->module][$this->viewDefs], $to);
720 * This will return the meta data of the merged file
722 * @return ARRAY - the meta data of the merged file
724 public function getData(){
725 return $this->newData;
729 * public function that will merge meta data from an original sugar file that shipped with the product, a customized file, and a new file shipped with an upgrade
731 * @param STRING $module - name of the module's files that are to be merged
732 * @param STRING $original_file - path to the file that originally shipped with sugar
733 * @param STRING $new_file - path to the new file that is shipping with the patch
734 * @param STRING $custom_file - path to the custom file
735 * @param BOOLEAN $save - boolean on if it should save the results to the custom file or not
736 * @return BOOLEAN - if the merged file was saved if false is passed in for the save parameter it always returns true
738 public function merge($module, $original_file, $new_file, $custom_file=false, $save=true){
740 $this->log("\n\n". 'Starting a merge in ' . get_class($this));
741 $this->log('merging the following files');
742 $this->log('original file:' . $original_file);
743 $this->log('new file:' . $new_file);
744 $this->log('custom file:' . $custom_file);
745 if(empty($custom_file) && $save){
748 $this->loadData($module, $original_file, $new_file, $custom_file);
749 $this->mergeMetaData();
750 if($save && !empty($this->newData) && !empty($custom_file)){
752 copy($custom_file, $custom_file . '.suback.php');
753 return $this->save($custom_file);
756 if(!$save)return true;
760 * Logs the given message if the message is not a string it will export it first. If $this->fp is NULL then it will try to log to the $GLOBALS['log'] if it is available
762 * @param MULTI $message
764 protected function log($message){
765 if(!is_string($message)){
766 $message = var_export($message, true);
768 if(!empty($this->fp)){
769 fwrite($this->fp, $message. "\n");
771 if(!empty($GLOBALS['log'])){
772 $GLOBALS['log']->debug($message . "\n");