]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/AbstractMetaDataImplementation.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / views / AbstractMetaDataImplementation.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 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
40
41 /*
42  * Abstract base clase for Parser Implementations (using a Bridge Pattern)
43  * The Implementations hide the differences between :
44  * - Deployed modules (such as OOB modules and deployed ModuleBuilder modules) that are located in the /modules directory and have metadata in modules/<name>/metadata and in the custom directory
45  * - WIP modules which are being worked on in ModuleBuilder and that are located in custom
46  */
47
48
49 require_once 'modules/ModuleBuilder/parsers/views/History.php' ;
50
51 abstract class AbstractMetaDataImplementation
52 {
53         protected $_view ;
54         protected $_moduleName ;
55         protected $_viewdefs ;
56         protected $_originalViewdefs = array();
57         protected $_fielddefs ;
58         protected $_sourceFilename = '' ; // the name of the file from which we loaded the definition we're working on - needed when we come to write out the historical record
59         // would like this to be a constant, but alas, constants cannot contain arrays...
60         protected $_fileVariables = array (
61         MB_DASHLETSEARCH                        => 'dashletData',
62         MB_DASHLET                                      => 'dashletData',
63         MB_POPUPSEARCH                      => 'popupMeta',
64         MB_POPUPLIST                            => 'popupMeta',
65         MB_LISTVIEW                             => 'listViewDefs',
66         MB_BASICSEARCH                          => 'searchdefs',
67         MB_ADVANCEDSEARCH                       => 'searchdefs',
68         MB_EDITVIEW                             => 'viewdefs',
69         MB_DETAILVIEW                           => 'viewdefs',
70         MB_QUICKCREATE                          => 'viewdefs',
71         ) ;
72
73         /*
74          * Getters for the definitions loaded by the Constructor
75          */
76         function getViewdefs ()
77         {
78                 $GLOBALS['log']->debug( get_class ( $this ) . '->getViewdefs:'.print_r($this->_viewdefs,true) ) ;
79                 return $this->_viewdefs ;
80         }
81
82         function getOriginalViewdefs() {
83                 return $this->_originalViewdefs;
84         }
85
86         function getFielddefs ()
87         {
88                 return $this->_fielddefs ;
89         }
90
91         /*
92          * Obtain a new accessor for the history of this layout
93          * Ideally the History object would be a singleton; however given the use case (modulebuilder/studio) it's unlikely to be an issue
94          */
95         function getHistory ()
96         {
97                 return $this->_history ;
98         }
99
100         /*
101          * Load a layout from a file, given a filename
102          * Doesn't do any preprocessing on the viewdefs - just returns them as found for other classes to make sense of
103          * @param string filename       The full path to the file containing the layout
104          * @return array                The layout, null if the file does not exist
105          */
106         protected function _loadFromFile ($filename)
107         {
108                 // BEGIN ASSERTIONS
109                 if (! file_exists ( $filename ))
110                 {
111                         return null ;
112                 }
113                 // END ASSERTIONS
114                 $GLOBALS['log']->debug(get_class($this)."->_loadFromFile(): reading from ".$filename );
115                 require $filename ; // loads the viewdef - must be a require not require_once to ensure can reload if called twice in succession
116
117                 // Check to see if we have the module name set as a variable rather than embedded in the $viewdef array
118                 // If we do, then we have to preserve the module variable when we write the file back out
119                 // This is a format used by ModuleBuilder templated modules to speed the renaming of modules
120                 // OOB Sugar modules don't use this format
121
122                 $moduleVariables = array ( 'module_name' , '_module_name' , 'OBJECT_NAME' , '_object_name' ) ;
123
124                 $variables = array ( ) ;
125                 foreach ( $moduleVariables as $name )
126                 {
127                         if (isset ( $$name ))
128                         {
129                                 $variables [ $name ] = $$name ;
130                         }
131                 }
132
133                 // Extract the layout definition from the loaded file - the layout definition is held under a variable name that varies between the various layout types (e.g., listviews hold it in listViewDefs, editviews in viewdefs)
134                 $viewVariable = $this->_fileVariables [ $this->_view ] ;
135                 $defs = $$viewVariable ;
136
137                 // Now tidy up the module name in the viewdef array
138                 // MB created definitions store the defs under packagename_modulename and later methods that expect to find them under modulename will fail
139
140                 if (isset ( $variables [ 'module_name' ] ))
141                 {
142                         $mbName = $variables [ 'module_name' ] ;
143                         if ($mbName != $this->_moduleName)
144                         {
145                                 $defs [ $this->_moduleName ] = $defs [ $mbName ] ;
146                                 unset ( $defs [ $mbName ] ) ;
147                         }
148                 }
149                 $this->_variables = $variables ;
150                 // now remove the modulename preamble from the loaded defs
151                 reset($defs);
152                 $temp = each($defs);
153
154                 $GLOBALS['log']->debug( get_class ( $this ) . "->_loadFromFile: returning ".print_r($temp['value'],true)) ;
155                 return $temp['value']; // 'value' contains the value part of 'key'=>'value' part
156         }
157
158         
159         protected function _loadFromPopupFile ($filename, $mod, $view, $forSave = false)
160         {
161                 // BEGIN ASSERTIONS
162                 if (!file_exists ( $filename ))
163                 {
164                         return null ;
165                 }
166                 // END ASSERTIONS
167                 $GLOBALS['log']->debug(get_class($this)."->_loadFromFile(): reading from ".$filename );
168                 
169                 if(!empty($mod)){
170                         $oldModStrings = $GLOBALS['mod_strings'];
171                         $GLOBALS['mod_strings'] = $mod;
172                 }
173                 
174                 require $filename ; // loads the viewdef - must be a require not require_once to ensure can reload if called twice in succession
175                 $viewVariable = $this->_fileVariables [ $this->_view ] ;
176                 $defs = $$viewVariable ;
177                 if(!$forSave){
178                         //Now we will unset the reserve field in pop definition file.
179                         $limitFields = PopupMetaDataParser::$reserveProperties;
180                         foreach($limitFields as $v){
181                                 if(isset($defs[$v])){
182                                         unset($defs[$v]);
183                                 }
184                         }       
185                         if(isset($defs[PopupMetaDataParser::$defsMap[$view]])){
186                                 $defs = $defs[PopupMetaDataParser::$defsMap[$view]];
187                         }else{
188                                 //If there are no defs for this view, grab them from the non-popup view
189                                 if ($view == MB_POPUPLIST)
190                                 {
191                                         $this->_view = MB_LISTVIEW;
192                                 $defs = $this->_loadFromFile ( $this->getFileName ( MB_LISTVIEW, $this->_moduleName, MB_CUSTOMMETADATALOCATION ) ) ;
193                                 if ($defs == null)
194                                         $defs = $this->_loadFromFile ( $this->getFileName ( MB_LISTVIEW, $this->_moduleName, MB_BASEMETADATALOCATION ) ) ;
195                                 $this->_view = $view;
196                                 } 
197                                 else if ($view == MB_POPUPSEARCH)
198                                 {
199                                         $this->_view = MB_ADVANCEDSEARCH;
200                                 $defs = $this->_loadFromFile ( $this->getFileName ( MB_ADVANCEDSEARCH, $this->_moduleName, MB_CUSTOMMETADATALOCATION ) ) ;
201                                 if ($defs == null)
202                                         $defs = $this->_loadFromFile ( $this->getFileName ( MB_ADVANCEDSEARCH, $this->_moduleName, MB_BASEMETADATALOCATION ) ) ;
203                                 
204                                 if (isset($defs['layout']) && isset($defs['layout']['advanced_search']))
205                                         $defs = $defs['layout']['advanced_search'];
206                                 $this->_view = $view;
207                                 }
208                                 if ($defs == null)
209                                         $defs = array();
210                         }
211                 }
212                 
213                 $this->_variables = array();
214                 if(!empty($oldModStrings)){
215                         $GLOBALS['mod_strings'] = $oldModStrings;
216                 }
217                 return $defs; 
218         }
219
220         /*
221          * Save a layout to a file
222          * Must be the exact inverse of _loadFromFile
223          * Obtains the additional variables, such as module_name, to include in beginning of the file (as required by ModuleBuilder) from the internal variable _variables, set in the Constructor
224          * @param string filename       The full path to the file to contain the layout
225          * @param array defs            Array containing the layout definition; the top level should be the definition itself; not the modulename or viewdef= preambles found in the file definitions
226          * @param boolean useVariables  Write out with placeholder entries for module name and object name - used by ModuleBuilder modules
227          */
228         protected function _saveToFile ($filename , $defs , $useVariables = true, $forPopup = false )
229         {
230             if(file_exists($filename))
231                 unlink($filename);
232             
233             mkdir_recursive ( dirname ( $filename ) ) ;
234
235                 $useVariables = (count ( $this->_variables ) > 0) && $useVariables ; // only makes sense to do the variable replace if we have variables to replace...
236
237                 // create the new metadata file contents, and write it out
238                 $out = "<?php\n" ;
239                 if ($useVariables)
240                 {
241                         // write out the $<variable>=<modulename> lines
242                         foreach ( $this->_variables as $key => $value )
243                         {
244                                 $out .= "\$$key = '" . $value . "';\n" ;
245                         }
246                 }
247
248                 $viewVariable = $this->_fileVariables [ $this->_view ] ;
249                 if($forPopup){
250                         $out .= "\$$viewVariable = \n" . var_export_helper ( $defs ) ;
251                 }else{
252                 $out .= "\$$viewVariable [".(($useVariables) ? '$module_name' : "'$this->_moduleName'")."] = \n" . var_export_helper ( $defs ) ;
253                 }
254                 
255                 $out .= ";\n?>\n" ;
256
257                 if ( sugar_file_put_contents ( $filename, $out ) === false)
258                         $GLOBALS [ 'log' ]->fatal ( get_class($this).": could not write new viewdef file " . $filename ) ;
259         }
260
261         /*
262          * Fielddefs are obtained from two locations:
263          *
264          * 1. The starting point is the module's fielddefs, sourced from the Bean
265          * 2. Second comes any overrides from the layouts themselves. Note though that only visible fields are included in a layoutdef, which
266          *        means fields that aren't present in the current layout may have a layout defined in a lower-priority layoutdef, for example, the base layoutdef
267          *
268          * Thus to determine the current fielddef for any given field, we take the fielddef defined in the module's Bean and then override with first the base layout,
269          * then the customlayout, then finally the working layout...
270          *
271          * The complication is that although generating these merged fielddefs is naturally a method of the implementation, not the parser,
272          * we therefore lack knowledge as to which type of layout we are merging - EditView or ListView. So we can't use internal knowledge of the
273          * layout to locate the field definitions. Instead, we need to look for sections of the layout that match the template for a field definition...
274          */
275         function _mergeFielddefs ( &$fielddefs , $layout )
276         {
277                 foreach ( $layout as $key => $def )
278                 {
279
280                         if ( (string) $key == 'templateMeta' )
281                         continue ;
282
283                         if ( is_array ( $def ) )
284                         {
285                                 if ( isset ( $def [ 'name' ] ) && ! is_array ( $def [ 'name' ] ) ) // found a 'name' definition, that is not the definition of a field called name :)
286                                 {
287                                         // if this is a module field, then merge in the definition, otherwise this is a new field defined in the layout, so just take the definition
288                                         $fielddefs [ $def [ 'name'] ] = ( isset ($fielddefs [ $def [ 'name' ] ] ) ) ? array_merge ( $fielddefs [ $def [ 'name' ] ], $def ) : $def ;
289                                 }
290                                 else if ( isset ( $def [ 'label' ] ) || isset ( $def [ 'vname' ] ) || isset($def ['widget_class']) ) // dealing with a listlayout which lacks 'name' keys, but which does have 'label' keys
291                                 {
292                                         $key = strtolower ( $key ) ;
293                                         $fielddefs [ $key ] = ( isset ($fielddefs [ $key ] ) ) ? array_merge ( $fielddefs [ $key ], $def ) : $def ;
294                                 }
295                                 else
296                                 $this->_mergeFielddefs( $fielddefs , $def ) ;
297                         }
298                 }
299
300         }
301
302 }
303
304 ?>