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-2013 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 ********************************************************************************/
39 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
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
49 require_once 'modules/ModuleBuilder/parsers/views/History.php' ;
51 abstract class AbstractMetaDataImplementation
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',
74 * Getters for the definitions loaded by the Constructor
76 function getViewdefs ()
78 $GLOBALS['log']->debug( get_class ( $this ) . '->getViewdefs:'.print_r($this->_viewdefs,true) ) ;
79 return $this->_viewdefs ;
82 function getOriginalViewdefs() {
83 return $this->_originalViewdefs;
86 function getFielddefs ()
88 return $this->_fielddefs ;
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
95 function getHistory ()
97 return $this->_history ;
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
106 protected function _loadFromFile ($filename)
109 if (! file_exists ( $filename ))
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
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
122 $moduleVariables = array ( 'module_name' , '_module_name' , 'OBJECT_NAME' , '_object_name' ) ;
124 $variables = array ( ) ;
125 foreach ( $moduleVariables as $name )
127 if (isset ( $$name ))
129 $variables [ $name ] = $$name ;
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 ;
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
140 if (isset ( $variables [ 'module_name' ] ))
142 $mbName = $variables [ 'module_name' ] ;
143 if ($mbName != $this->_moduleName)
145 $defs [ $this->_moduleName ] = $defs [ $mbName ] ;
146 unset ( $defs [ $mbName ] ) ;
149 $this->_variables = $variables ;
150 // now remove the modulename preamble from the loaded defs
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
159 protected function _loadFromPopupFile ($filename, $mod, $view, $forSave = false)
162 if (!file_exists ( $filename ))
167 $GLOBALS['log']->debug(get_class($this)."->_loadFromFile(): reading from ".$filename );
170 $oldModStrings = $GLOBALS['mod_strings'];
171 $GLOBALS['mod_strings'] = $mod;
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 ;
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])){
185 if(isset($defs[PopupMetaDataParser::$defsMap[$view]])){
186 $defs = $defs[PopupMetaDataParser::$defsMap[$view]];
188 //If there are no defs for this view, grab them from the non-popup view
189 if ($view == MB_POPUPLIST)
191 $this->_view = MB_LISTVIEW;
192 $defs = $this->_loadFromFile ( $this->getFileName ( MB_LISTVIEW, $this->_moduleName, MB_CUSTOMMETADATALOCATION ) ) ;
194 $defs = $this->_loadFromFile ( $this->getFileName ( MB_LISTVIEW, $this->_moduleName, MB_BASEMETADATALOCATION ) ) ;
195 $this->_view = $view;
197 else if ($view == MB_POPUPSEARCH)
199 $this->_view = MB_ADVANCEDSEARCH;
200 $defs = $this->_loadFromFile ( $this->getFileName ( MB_ADVANCEDSEARCH, $this->_moduleName, MB_CUSTOMMETADATALOCATION ) ) ;
202 $defs = $this->_loadFromFile ( $this->getFileName ( MB_ADVANCEDSEARCH, $this->_moduleName, MB_BASEMETADATALOCATION ) ) ;
204 if (isset($defs['layout']) && isset($defs['layout']['advanced_search']))
205 $defs = $defs['layout']['advanced_search'];
206 $this->_view = $view;
213 $this->_variables = array();
214 if(!empty($oldModStrings)){
215 $GLOBALS['mod_strings'] = $oldModStrings;
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
228 protected function _saveToFile ($filename , $defs , $useVariables = true, $forPopup = false )
230 if(file_exists($filename))
233 mkdir_recursive ( dirname ( $filename ) ) ;
235 $useVariables = (count ( $this->_variables ) > 0) && $useVariables ; // only makes sense to do the variable replace if we have variables to replace...
237 // create the new metadata file contents, and write it out
241 // write out the $<variable>=<modulename> lines
242 foreach ( $this->_variables as $key => $value )
244 $out .= "\$$key = '" . $value . "';\n" ;
248 $viewVariable = $this->_fileVariables [ $this->_view ] ;
250 $out .= "\$$viewVariable = \n" . var_export_helper ( $defs ) ;
252 $out .= "\$$viewVariable [".(($useVariables) ? '$module_name' : "'$this->_moduleName'")."] = \n" . var_export_helper ( $defs ) ;
257 if ( sugar_file_put_contents ( $filename, $out ) === false)
258 $GLOBALS [ 'log' ]->fatal ( get_class($this).": could not write new viewdef file " . $filename ) ;
262 * Fielddefs are obtained from two locations:
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
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...
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...
275 function _mergeFielddefs ( &$fielddefs , $layout )
277 foreach ( $layout as $key => $def )
280 if ( (string) $key == 'templateMeta' )
283 if ( is_array ( $def ) )
285 if ( isset ( $def [ 'name' ] ) && ! is_array ( $def [ 'name' ] ) ) // found a 'name' definition, that is not the definition of a field called name :)
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 ;
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
292 $key = strtolower ( $key ) ;
293 $fielddefs [ $key ] = ( isset ($fielddefs [ $key ] ) ) ? array_merge ( $fielddefs [ $key ], $def ) : $def ;
296 $this->_mergeFielddefs( $fielddefs , $def ) ;