]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/upgrade_custom_relationships.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Administration / upgrade_custom_relationships.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 /**
39  * Searches through the installed relationships to find broken self referencing one-to-many relationships 
40  * (wrong field used in the subpanel, and the left link not marked as left)
41  */
42 function upgrade_custom_relationships($modules = array())
43 {
44         global $current_user, $moduleList;
45         if (!is_admin($current_user)) sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']); 
46         
47         require_once("modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php");
48         require_once("modules/ModuleBuilder/parsers/relationships/OneToManyRelationship.php");
49         
50         if (empty($modules))
51                 $modules = $moduleList;
52         
53         foreach($modules as $module)
54         {
55                 $depRels = new DeployedRelationships($module);
56                 $relList = $depRels->getRelationshipList();
57                 foreach($relList as $relName)
58                 {
59                         $relObject = $depRels->get($relName);
60                         $def = $relObject->getDefinition();
61                         //We only need to fix self referencing one to many relationships
62                         if ($def['lhs_module'] == $def['rhs_module'] && $def['is_custom'] && $def['relationship_type'] == "one-to-many")
63                         {
64                                 $layout_defs = array();
65                                 if (!is_dir("custom/Extension/modules/$module/Ext/Layoutdefs") || !is_dir("custom/Extension/modules/$module/Ext/Vardefs"))
66                                         continue;
67                                 //Find the extension file containing the vardefs for this relationship
68                                 foreach(scandir("custom/Extension/modules/$module/Ext/Vardefs") as $file)
69                                 {
70                                         if (substr($file,0,1) != "." && strtolower(substr($file, -4)) == ".php")
71                                         {
72                                                 $dictionary = array($module => array("fields" => array()));
73                                                 $filePath = "custom/Extension/modules/$module/Ext/Vardefs/$file";
74                                                 include($filePath);
75                                                 if(isset($dictionary[$module]["fields"][$relName]))
76                                                 {
77                                                         $rhsDef = $dictionary[$module]["fields"][$relName];
78                                                         //Update the vardef for the left side link field
79                                                         if (!isset($rhsDef['side']) || $rhsDef['side'] != 'left')
80                                                         {
81                                                                 $rhsDef['side'] = 'left';
82                                                                 $fileContents = file_get_contents($filePath);
83                                                                 $out = preg_replace(
84                                                                         '/\$dictionary[\w"\'\[\]]*?' . $relName . '["\'\[\]]*?\s*?=\s*?array\s*?\(.*?\);/s',
85                                                                         '$dictionary["' . $module . '"]["fields"]["' . $relName . '"]=' . var_export_helper($rhsDef) . ";",
86                                                                         $fileContents
87                                                                 );
88                                                                 file_put_contents($filePath, $out);
89                                                         }
90                                                 }
91                                         }
92                                 }
93                                 //Find the extension file containing the subpanel definition for this relationship
94                                 foreach(scandir("custom/Extension/modules/$module/Ext/Layoutdefs") as $file)
95                                 {
96                                         if (substr($file,0,1) != "." && strtolower(substr($file, -4)) == ".php")
97                                         {
98                                                 $layout_defs = array($module => array("subpanel_setup" => array()));
99                                                 $filePath = "custom/Extension/modules/$module/Ext/Layoutdefs/$file";
100                                                 include($filePath);
101                                                 foreach($layout_defs[$module]["subpanel_setup"] as $key => $subDef)
102                                                 {
103                                                         if ($layout_defs[$module]["subpanel_setup"][$key]['get_subpanel_data'] == $relName)
104                                                         {
105                                                                 $fileContents = file_get_contents($filePath);
106                                                                 $out = preg_replace(
107                                                                         '/[\'"]get_subpanel_data[\'"]\s*=>\s*[\'"]' . $relName . '[\'"],/s',
108                                                                         "'get_subpanel_data' => '{$def["join_key_lhs"]}',",
109                                                                         $fileContents
110                                                                 );
111                                                                 file_put_contents($filePath, $out);
112                                                         }
113                                                 }
114                                         }
115                                 }
116                         }
117                 }
118         }
119 }
120
121 if (isset($_REQUEST['execute']) && $_REQUEST['execute'])
122         upgrade_custom_relationships();