2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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)
42 function upgrade_custom_relationships($modules = array())
44 global $current_user, $moduleList;
45 if (!is_admin($current_user)) sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
47 require_once("modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php");
48 require_once("modules/ModuleBuilder/parsers/relationships/OneToManyRelationship.php");
51 $modules = $moduleList;
53 foreach($modules as $module)
55 $depRels = new DeployedRelationships($module);
56 $relList = $depRels->getRelationshipList();
57 foreach($relList as $relName)
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")
64 $layout_defs = array();
65 if (!is_dir("custom/Extension/modules/$module/Ext/Layoutdefs") || !is_dir("custom/Extension/modules/$module/Ext/Vardefs"))
67 //Find the extension file containing the vardefs for this relationship
68 foreach(scandir("custom/Extension/modules/$module/Ext/Vardefs") as $file)
70 if (substr($file,0,1) != "." && strtolower(substr($file, -4)) == ".php")
72 $dictionary = array($module => array("fields" => array()));
73 $filePath = "custom/Extension/modules/$module/Ext/Vardefs/$file";
75 if(isset($dictionary[$module]["fields"][$relName]))
77 $rhsDef = $dictionary[$module]["fields"][$relName];
78 //Update the vardef for the left side link field
79 if (!isset($rhsDef['side']) || $rhsDef['side'] != 'left')
81 $rhsDef['side'] = 'left';
82 $fileContents = file_get_contents($filePath);
84 '/\$dictionary[\w"\'\[\]]*?' . $relName . '["\'\[\]]*?\s*?=\s*?array\s*?\(.*?\);/s',
85 '$dictionary["' . $module . '"]["fields"]["' . $relName . '"]=' . var_export_helper($rhsDef) . ";",
88 file_put_contents($filePath, $out);
93 //Find the extension file containing the subpanel definition for this relationship
94 foreach(scandir("custom/Extension/modules/$module/Ext/Layoutdefs") as $file)
96 if (substr($file,0,1) != "." && strtolower(substr($file, -4)) == ".php")
98 $layout_defs = array($module => array("subpanel_setup" => array()));
99 $filePath = "custom/Extension/modules/$module/Ext/Layoutdefs/$file";
101 foreach($layout_defs[$module]["subpanel_setup"] as $key => $subDef)
103 if ($layout_defs[$module]["subpanel_setup"][$key]['get_subpanel_data'] == $relName)
105 $fileContents = file_get_contents($filePath);
107 '/[\'"]get_subpanel_data[\'"]\s*=>\s*[\'"]' . $relName . '[\'"],/s',
108 "'get_subpanel_data' => '{$def["join_key_lhs"]}',",
111 file_put_contents($filePath, $out);
121 if (isset($_REQUEST['execute']) && $_REQUEST['execute'])
122 upgrade_custom_relationships();