Passed
Push — scrutinizer-code-quality ( 09f5a1...c4c5fb )
by Adam
56:05 queued 14:08
created

SugarMerge::getNewPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
/*********************************************************************************
42
43
 * Description:  Defines the English language pack for the base application.
44
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
45
 * All Rights Reserved.
46
 * Contributor(s): ______________________________________..
47
 ********************************************************************************/
48
49
require_once('modules/UpgradeWizard/SugarMerge/EditViewMerge.php');
50
require_once('modules/UpgradeWizard/SugarMerge/DetailViewMerge.php');
51
require_once('modules/UpgradeWizard/SugarMerge/SearchMerge.php');
52
require_once('modules/UpgradeWizard/SugarMerge/ListViewMerge.php');
53
require_once('modules/UpgradeWizard/SugarMerge/QuickCreateMerge.php');
54
require_once('modules/ModuleBuilder/parsers/views/History.php');
55
56
/**
57
 * SugarMerge wraps around all the merge functionality of Sugar given a module name and the path to an unzipped patch
58
 *
59
 */
60
class SugarMerge {
61
	private $mergeMapping = array();
62
	private $new_path = '';
63
	private $custom_path = 'custom';
64
	private $original_path = '';
65
	private $merged = array();
66
	private $fp = NULL;
67
68
	function __construct($new_path='', $original_path='', $custom_path='custom') {
69
70
		$this->new_path = empty($new_path) || preg_match('/[\/]$/', $new_path) ? $new_path : $new_path . '/';
71
		$this->original_path = empty($original_path) || preg_match('/[\/]$/', $original_path) ? $original_path : $original_path . '/';
72
		$this->custom_path = empty($custom_path) || preg_match('/[\/]$/', $custom_path) ? $custom_path : $custom_path . '/';
73
74
		$this->mergeMapping = array(
75
			'editviewdefs.php'=> new EditViewMerge(),
76
			'detailviewdefs.php'=>new DetailViewMerge(),
77
			'listviewdefs.php'=>new ListViewMerge(),
78
			'searchdefs.php'=>new SearchMerge(),
79
			'quickcreatedefs.php'=>new QuickCreateMerge(),
80
		);
81
	}
82
83
    /**
84
     * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead
85
     */
86
    function SugarMerge($new_path='', $original_path='', $custom_path='custom'){
87
        $deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code';
88
        if(isset($GLOBALS['log'])) {
89
            $GLOBALS['log']->deprecated($deprecatedMessage);
90
        }
91
        else {
92
            trigger_error($deprecatedMessage, E_USER_DEPRECATED);
93
        }
94
        self::__construct($new_path, $original_path, $custom_path);
95
    }
96
97
98
	function setLogFilePointer($fp){
99
		$this->fp = $fp;
100
	}
101
102
103
104
	/**
105
	 * This will run through all the modules that may need merging and determine if there is anything to merge
106
	 * if $merge is set to true it will perform the merge
107
	 * if $merge and $save are set to true it will perform the merge and save the results in the custom directory
108
	 *
109
	 * @param BOOLEAN|ARRAY $merge - do we wish to perform the merge if false it will just return a list of files that can be merged.  If an array is passed, only those modules present in the array will be merged.
110
	 * @param BOOLEAN $save - do we wish to save the merged files to true - $merge must be true for this to apply - otherwise it will simulate merging so you can view the log files of the merge
111
	 * @param BOOLEAN $logHistory - do we wish to create history entries for any of the merges
112
	 * @return ARRAY - an associative array of module names to files that were either merged or have the potential to be merged depeneding if $merge and $save  are set to true
113
	 */
114
	function mergeAll($merge=true, $save=true, $logHistory=true){
115
		$this->merged = array();
116
		$searchDirectory = $this->custom_path;
117
		if(!preg_match('/[\/]modules$/si', $searchDirectory)) {
118
		   $searchDirectory .= preg_match('/[\/]$/', $this->custom_path) ? 'modules' : '/modules';
119
		}
120
121
		if(file_exists($searchDirectory)){
122
			$dir = dir($searchDirectory);
123
			while($e = $dir->read()){
124
				if(substr($e , 0, 1) != '.') {
125
					if(is_dir("{$searchDirectory}/{$e}/metadata")){
126
127
                        //lets make sure that the directory matches the case of the module before we pass it in
128
                        global $moduleList;
129
                        //lets populate an array with the available modules, and make the key's lowercase
130
                        $checkModList =  array_combine ($moduleList,$moduleList);
131
                        $checkModList = array_change_key_case($checkModList);
132
133
  						//now lets compare with the current directory.  This accounts for cases in which the directory was created in lowercase
134
                        if(!empty($checkModList[strtolower($e)])){
135
                            //directory was lowercase, let's use the right module value
136
							$e = $checkModList[strtolower($e)];
137
                        }
138
139
					    if( is_array($merge) )
140
					    {
141
					        if ( in_array($e,$merge) )
142
					        	$this->merged[$e] = $this->mergeModule($e, TRUE, $save,$logHistory );
143
					        else
144
					        {
145
					            $GLOBALS['log']->debug("SugarMerge is skipping $e module as filter array passed in but module not specified for merge.");
146
					            continue;
147
					        }
148
					    }
149
					    else
150
						  $this->merged[$e] = $this->mergeModule($e, $merge, $save,$logHistory );
151
					}
152
				}
153
			}
154
		}
155
		return $this->merged;
156
	}
157
158
159
160
161
	/**
162
	 * This will merge any files that need merging for a given module
163
	 * if $merge is set to true it will perform the merge
164
	 * if $merge and $save are set to true it will perform the merge and save the results in the custom directory
165
	 *
166
	 * @param STRING $module - the name of the module to merge files for
167
	 * @param BOOLEAN $merge - do we wish to perform the merge if false it will just return a list of files that can be merged
168
	 * @param BOOLEAN $save - do we wish to save the merged files to true - $merge must be true for this to apply - otherwise it will simulate merging so you can view the log files of the merge
169
	 * @param BOOLEAN $logHistory - do we wish to create history entries for any of the merges
170
	 * @return ARRAY - an associative array of files that were either merged or have the potential to be merged depeneding if $merge and $save  are set to true
171
	 */
172
	function mergeModule($module, $merge = true, $save=true,$logHistory=true){
173
		$merged = array();
174
		$path = $this->original_path . 'modules/' . $module . '/metadata/';
175
		$custom_path = $this->custom_path . 'modules/' . $module . '/metadata/';
176
		$new_path = $this->new_path . 'modules/' . $module . '/metadata/';
177
		foreach($this->mergeMapping as $file=>&$object){
178
			if(file_exists("{$custom_path}{$file}") && file_exists("{$new_path}{$file}")){
179
				if($merge){
180
					$merged[$file] = $this->mergeFile($module, $file, $save, $logHistory);
181
				}else{
182
					$merged[$file] = true;
183
				}
184
			}
185
		}
186
187
		return $merged;
188
189
	}
190
191
	/**
192
	 * This function will merge a single file for a module
193
	 *
194
	 * @param STRING $module - name of the module
195
	 * @param STRING $file - name of the file
196
	 * @param STRING $save - should the merged file be saved to the custom directory
197
	 * @return BOOLEAN - success or failure of the merge
198
	 */
199
	function mergeFile($module, $file, $save=true,$logHistory=true){
200
		$path = $this->original_path . 'modules/' . $module . '/metadata/';
201
		$custom_path = $this->custom_path . 'modules/' . $module . '/metadata/';
202
		$new_path = $this->new_path . 'modules/' . $module . '/metadata/';
203
		if($this->fp) $this->mergeMapping[$file]->setLogFilePointer($this->fp);
204
		if(isset($this->mergeMapping[$file]) && file_exists("{$path}{$file}") && file_exists("{$custom_path}{$file}") && file_exists("{$new_path}{$file}")){
205
		    //Create a log entry of the custom file before it is merged
206
		    if($logHistory && $save)
207
		          $this->createHistoryLog($module, "{$custom_path}{$file}",$file);
208
            $this->mergeMapping[$file]->sugarMerge = $this;
209
		    return $this->mergeMapping[$file]->merge($module, "{$path}{$file}", "{$new_path}{$file}", "{$custom_path}{$file}", $save);
210
		}
211
		return false;
212
213
	}
214
215
    /**
216
	 * Create a history copy of the custom file that will be merged so that it can be access through
217
	 * studio if admins wish to revert at a later date.
218
	 *
219
	 * @param STRING $module - name of the module
220
	 * @param STRING $file - name of the file
221
	 * @param STRING $customFile - Path to the custom file that will be merged
222
	 */
223
	protected function createHistoryLog($module,$customFile,$file)
224
	{
225
	    $historyPath = 'custom/' . MB_HISTORYMETADATALOCATION . "/modules/$module/metadata/$file";
226
	    $history = new History($historyPath);
227
	    $timeStamp = $history->append($customFile);
228
	    $GLOBALS['log']->debug("Created history file after merge with new file: " . $historyPath .'_'.$timeStamp);
229
	}
230
231
	/**
232
	 * Return the custom modules path
233
	 *
234
	 * @return STRING directory where custom module files are located
235
	 */
236
	function getCustomPath() {
237
		return $this->custom_path;
238
	}
239
240
241
	/**
242
	 * Return the new upgrade modules path
243
	 *
244
	 * @return STRING directory where new module files are located
245
	 */
246
	function getNewPath() {
247
		return $this->new_path;
248
	}
249
250
251
	/**
252
	 * Return the original modules path
253
	 *
254
	 * @return STRING directory where new module files are located
255
	 */
256
	function getOriginalPath() {
257
		return $this->original_path;
258
	}
259
260
}
261
?>