]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/ModuleBuilderParser.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / ModuleBuilderParser.php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39
40 class ModuleBuilderParser
41 {
42         
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
45         
46         function ModuleBuilderParser()
47         {
48                 $this->_defMap = array('listview'=>'listViewDefs','searchview'=>'searchdefs','editview'=>'viewdefs','detailview'=>'viewdefs','quickcreate'=>'viewdefs');
49         }
50         /*
51          * Initialize this parser
52          */
53         function init ()
54         {
55         }
56         
57         /*
58          * Dummy function used to ease the transition to the new parser structure
59          */
60         function populateFromPost()
61         {
62         }
63         
64         function _loadFromFile($view,$file,$moduleName)
65         {
66                 
67                 $variables = array();
68             if (! file_exists($file))
69         {
70             $this->_fatalError("ModuleBuilderParser: required viewdef file {$file} does not exist");
71         }
72         $GLOBALS['log']->info('ModuleBuilderParser->_loadFromFile(): file='.$file);        
73         require ($file); // loads in a $viewdefs
74
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)
82         {
83             if (isset($$name)) {
84                 $variables[$name] = $$name;
85             }
86         }
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;
91
92         if (isset($variables['module_name']))
93         {
94                 $mbName = $variables['module_name'];
95                 if ($mbName != $moduleName)
96                 {
97                         $GLOBALS['log']->debug('ModuleBuilderParser->_loadFromFile(): tidying module names from '.$mbName.' to '.$moduleName);
98                         $defs[$moduleName] = $defs[$mbName];
99                         unset($defs[$mbName]);
100                 }
101         }
102 //          $GLOBALS['log']->debug('ModuleBuilderParser->_loadFromFile(): '.print_r($defs,true));
103         return (array('viewdefs' => $defs, 'variables' => $variables));
104         }
105         
106         function handleSave ($file,$view,$moduleName,$defs)
107         {
108         }
109         
110         
111         /*
112          * Save the new layout
113          */
114         function _writeToFile ($file,$view,$moduleName,$defs,$variables)
115         {
116                 if(file_exists($file))
117                     unlink($file);
118                 
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' ) )
123             {
124                 $out = "<?php\n";    
125                 if ($useVariables)
126                 {
127                     // write out the $<variable>=<modulename> lines
128                     foreach($variables as $key=>$value)
129                     {
130                         $out .= "\$$key = '".$value."';\n";
131                     }
132                 }
133                 
134                 // write out the defs array itself
135                 switch (strtolower($view))
136                 {
137                         case 'editview':
138                         case 'detailview':
139                         case 'quickcreate':
140                                 $defs = array($view => $defs);
141                                 break;
142                         default:
143                                 break;
144                 }
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) );
148                 
149                 // tidy up the parenthesis
150                 if ($useVariables)
151                 {
152                         $out .= "\n)"; 
153                 }
154                 $out .= ";\n?>\n";
155                 
156 //           $GLOBALS['log']->debug("parser.modifylayout.php->_writeFile(): out=".print_r($out,true));
157             fputs( $fh, $out);
158             fclose( $fh );
159             }
160             else
161             {
162                 $GLOBALS['log']->fatal("ModuleBuilderParser->_writeFile() Could not write new viewdef file ".$file);
163             }
164         }
165
166
167     function _fatalError ($msg)
168     {
169         $GLOBALS ['log']->fatal($msg);
170         echo $msg;
171         sugar_cleanup();
172         die();
173     }
174     
175 }
176
177 ?>