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-2012 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 ********************************************************************************/
40 class ModuleBuilderParser
43 var $_defMap; // private - mapping from view to variable name inside the viewdef file
44 var $_variables = array(); // private - set of additional variables (other than the viewdefs) found in the viewdef file that need to be added to the file again when it is saved - used by ModuleBuilder
46 function ModuleBuilderParser()
48 $this->_defMap = array('listview'=>'listViewDefs','searchview'=>'searchdefs','editview'=>'viewdefs','detailview'=>'viewdefs','quickcreate'=>'viewdefs');
51 * Initialize this parser
58 * Dummy function used to ease the transition to the new parser structure
60 function populateFromPost()
64 function _loadFromFile($view,$file,$moduleName)
68 if (! file_exists($file))
70 $this->_fatalError("ModuleBuilderParser: required viewdef file {$file} does not exist");
72 $GLOBALS['log']->info('ModuleBuilderParser->_loadFromFile(): file='.$file);
73 require ($file); // loads in a $viewdefs
75 // Check to see if we have the module name set as a variable rather than embedded in the $viewdef array
76 // If we do, then we have to preserve the module variable when we write the file back out
77 // This is a format used by ModuleBuilder templated modules to speed the renaming of modules
78 // Traditional Sugar modules don't use this format
79 // We must do this in ParserModifyLayout (rather than just in ParserBuildLayout) because we might be editing the layout of a MB created module in Studio after it has been deployed
80 $moduleVariables = array('module_name','_module_name', 'OBJECT_NAME', '_object_name');
81 foreach ($moduleVariables as $name)
84 $variables[$name] = $$name;
87 $viewVariable = $this->_defMap[strtolower($view)];
88 // Now tidy up the module name in the viewdef array
89 // MB created definitions store the defs under packagename_modulename and later methods that expect to find them under modulename will fail
90 $defs = $$viewVariable;
92 if (isset($variables['module_name']))
94 $mbName = $variables['module_name'];
95 if ($mbName != $moduleName)
97 $GLOBALS['log']->debug('ModuleBuilderParser->_loadFromFile(): tidying module names from '.$mbName.' to '.$moduleName);
98 $defs[$moduleName] = $defs[$mbName];
99 unset($defs[$mbName]);
102 // $GLOBALS['log']->debug('ModuleBuilderParser->_loadFromFile(): '.print_r($defs,true));
103 return (array('viewdefs' => $defs, 'variables' => $variables));
106 function handleSave ($file,$view,$moduleName,$defs)
112 * Save the new layout
114 function _writeToFile ($file,$view,$moduleName,$defs,$variables)
116 if(file_exists($file))
119 mkdir_recursive ( dirname ( $file ) ) ;
120 $GLOBALS['log']->debug("ModuleBuilderParser->_writeFile(): file=".$file);
121 $useVariables = (count($variables)>0);
122 if( $fh = @sugar_fopen( $file, 'w' ) )
127 // write out the $<variable>=<modulename> lines
128 foreach($variables as $key=>$value)
130 $out .= "\$$key = '".$value."';\n";
134 // write out the defs array itself
135 switch (strtolower($view))
140 $defs = array($view => $defs);
145 $viewVariable = $this->_defMap[strtolower($view)];
146 $out .= "\$$viewVariable = ";
147 $out .= ($useVariables) ? "array (\n\$module_name =>\n".var_export_helper($defs) : var_export_helper( array($moduleName => $defs) );
149 // tidy up the parenthesis
156 // $GLOBALS['log']->debug("parser.modifylayout.php->_writeFile(): out=".print_r($out,true));
162 $GLOBALS['log']->fatal("ModuleBuilderParser->_writeFile() Could not write new viewdef file ".$file);
167 function _fatalError ($msg)
169 $GLOBALS ['log']->fatal($msg);