]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/VardefManager.php
Release 6.1.4
[Github/sugarcrm.git] / include / SugarObjects / VardefManager.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
38   
39 class VardefManager{
40         static $custom_disabled_modules = array();
41
42         /**
43          * this method is called within a vardefs.php file which extends from a SugarObject.
44          * It is meant to load the vardefs from the SugarObject.
45          */
46    static function createVardef($module,$object, $templates=array('default'), $object_name=false){
47                         global $dictionary;
48
49                         //reverse the sort order so priority goes highest to lowest;
50                     $templates = array_reverse($templates);
51                         foreach($templates as $template){
52                                 VardefManager::addTemplate($module,$object,$template, $object_name);
53                         }
54                         LanguageManager::createLanguageFile($module, $templates);
55
56         if (isset(VardefManager::$custom_disabled_modules[$module])) {
57                         $vardef_paths = array(
58                                 'custom/modules/'.$module.'/Ext/Vardefs/vardefs.ext.php',
59                                 'custom/Extension/modules/'.$module.'/Ext/Vardefs/vardefs.php'
60                         );
61
62                         //search a predefined set of locations for the vardef files
63                         foreach($vardef_paths as $path){
64                                 if(file_exists($path)){
65                                         require($path);
66                                 }
67                         }
68         }
69     }
70         
71         /**
72          * Enables/Disables the loading of custom vardefs for a module.
73          * @param String $module Module to be enabled/disabled
74          * @param Boolean $enable true to enable, false to disable
75          * @return  null
76          */
77         public static function setCustomAllowedForModule($module, $enable) {
78                 if ($enable && isset($custom_disabled_modules[$module])) {
79                         unset($custom_disabled_modules[$module]);
80                 } else if (!$enable) {
81                         $custom_disabled_modules[$module] = true;
82                 }
83         }
84         
85         static function addTemplate($module, $object, $template, $object_name=false){
86                 if($template == 'default')$template = 'basic';
87                 $templates = array();
88                 $fields = array();
89                 if(empty($object_name))$object_name = $object;
90                 $_object_name = strtolower($object_name);
91                 if(!empty($GLOBALS['dictionary'][$object]['table'])){
92                         $table_name = $GLOBALS['dictionary'][$object]['table'];
93                 }else{
94                         $table_name = strtolower($module);
95                 }
96                 
97                 if(empty($templates[$template])){
98                         $path = 'include/SugarObjects/templates/' . $template . '/vardefs.php';
99                         if(file_exists($path)){
100                                 require($path);
101                                 $templates[$template] = $vardefs;
102                         }else{
103                                 $path = 'include/SugarObjects/implements/' . $template . '/vardefs.php';
104                                 if(file_exists($path)){
105                                         require($path);
106                                         $templates[$template] = $vardefs;
107                                 }
108                         }
109                 }
110
111                 if(!empty($templates[$template])){
112                         if(empty($GLOBALS['dictionary'][$object]['fields']))$GLOBALS['dictionary'][$object]['fields'] = array();
113                         if(empty($GLOBALS['dictionary'][$object]['relationships']))$GLOBALS['dictionary'][$object]['relationships'] = array();
114                         if(empty($GLOBALS['dictionary'][$object]['indices']))$GLOBALS['dictionary'][$object]['indices'] = array();
115                         $GLOBALS['dictionary'][$object]['fields'] = array_merge($templates[$template]['fields'], $GLOBALS['dictionary'][$object]['fields']);
116                         if(!empty($templates[$template]['relationships']))$GLOBALS['dictionary'][$object]['relationships'] = array_merge($templates[$template]['relationships'], $GLOBALS['dictionary'][$object]['relationships']);
117                         if(!empty($templates[$template]['indices']))$GLOBALS['dictionary'][$object]['indices'] = array_merge($templates[$template]['indices'], $GLOBALS['dictionary'][$object]['indices']);
118                         // maintain a record of this objects inheritance from the SugarObject templates...
119             $GLOBALS['dictionary'][$object]['templates'][ $template ] = $template ;
120                 }
121                 
122         }
123         
124         /**
125          * Save the dictionary object to the cache
126          * @param string $module the name of the module
127          * @param string $object the name of the object
128          */
129         static function saveCache($module,$object, $additonal_objects= array()){
130                 
131                 $file = create_cache_directory('modules/' . $module . '/' . $object . 'vardefs.php');
132                 write_array_to_file('GLOBALS["dictionary"]["'. $object . '"]',$GLOBALS['dictionary'][$object], $file);
133                 include($file);
134                 
135                 // put the item in the sugar cache.
136                 $key = "VardefManager.$module.$object";
137                 $data = $GLOBALS['dictionary'][$object];
138                 sugar_cache_put($key,$data);
139         }
140         
141         /**
142          * clear out the vardef cache. If we receive a module name then just clear the vardef cache for that module
143          * otherwise clear out the cache for every module
144          * @param string module_dir the module_dir to clear, if not specified then clear
145          *                      clear vardef cache for all modules.
146          * @param string object_name the name of the object we are clearing this is for sugar_cache
147          */
148         static function clearVardef($module_dir = '', $object_name = ''){
149                 //if we have a module name specified then just remove that vardef file
150                 //otherwise go through each module and remove the vardefs.php
151                 if(!empty($module_dir) && !empty($object_name)){
152                         VardefManager::_clearCache($module_dir, $object_name);
153                 }else{
154                         global $beanList;
155                         foreach($beanList as $module_dir => $object_name){
156                                 VardefManager::_clearCache($module_dir, $object_name);
157                         }
158                 }
159         }
160         
161         /**
162          * PRIVATE function used within clearVardefCache so we do not repeat logic
163          * @param string module_dir the module_dir to clear
164          * @param string object_name the name of the object we are clearing this is for sugar_cache
165          */
166         static function _clearCache($module_dir = '', $object_name = ''){
167                 if(!empty($module_dir) && !empty($object_name)){
168                         
169                         if($object_name == 'aCase') {
170                                 $object_name = 'Case';
171                         }
172                         
173                         $file = $GLOBALS['sugar_config']['cache_dir'].'modules/'.$module_dir.'/' . $object_name . 'vardefs.php';
174                         if(file_exists($file)){
175                                 unlink($file);
176                                 $key = "VardefManager.$module_dir.$object_name";
177                                 sugar_cache_clear($key);
178                         }
179                 }
180         }
181         
182         /**
183          * Given a module, search all of the specified locations, and any others as specified
184          * in order to refresh the cache file
185          * 
186          * @param string $module the given module we want to load the vardefs for
187          * @param string $object the given object we wish to load the vardefs for
188          * @param array $additional_search_paths an array which allows a consumer to pass in additional vardef locations to search
189          */
190         static function refreshVardefs($module, $object, $additional_search_paths = null, $cacheCustom = true){
191                 // Some of the vardefs do not correctly define dictionary as global.  Declare it first.
192                 global $dictionary;
193                 $vardef_paths = array(
194                                         'modules/'.$module.'/vardefs.php',
195                                         'custom/modules/'.$module.'/Ext/Vardefs/vardefs.ext.php',
196                                         'custom/Extension/modules/'.$module.'/Ext/Vardefs/vardefs.php'
197                                  );
198
199                 // Add in additional search paths if they were provided.
200                 if(!empty($additional_search_paths) && is_array($additional_search_paths))
201                 {
202                         $vardef_paths = array_merge($vardef_paths, $additional_search_paths);
203                 }
204                 //search a predefined set of locations for the vardef files
205                 foreach($vardef_paths as $path){
206                         if(file_exists($path)){
207                                 
208                                 require($path);
209                         }
210                 }
211
212                 //load custom fields into the vardef cache
213                 if($cacheCustom){
214                         require_once("modules/DynamicFields/DynamicField.php");
215                         $df = new DynamicField ($module) ;
216                 $df->buildCache($module);
217                 }
218                 
219                 //great! now that we have loaded all of our vardefs.
220                 //let's go save them to the cache file.
221                 if(!empty($GLOBALS['dictionary'][$object]))
222                         VardefManager::saveCache($module, $object);
223         }
224         
225         /**
226          * load the vardefs for a given module and object
227          * @param string $module the given module we want to load the vardefs for
228          * @param string $object the given object we wish to load the vardefs for
229          * @param bool   $refresh whether or not we wish to refresh the cache file.
230          */
231         static function loadVardef($module, $object, $refresh=false){
232                 //here check if the cache file exists, if it does then load it, if it doesn't
233                 //then call refreshVardef
234                 //if either our session or the system is set to developerMode then refresh is set to true
235                 if(!empty($GLOBALS['sugar_config']['developerMode']) || !empty($_SESSION['developerMode'])){
236                 $refresh = true;
237         }
238                 // Retrieve the vardefs from cache.
239                 $key = "VardefManager.$module.$object";
240                 
241                 if(!$refresh)
242                 {
243                         $return_result = sugar_cache_retrieve($key);
244                         if(!empty($return_result))
245                         {
246                                 $GLOBALS['dictionary'][$object] = $return_result;
247                                 return;
248                         }
249                 }
250                         
251                 // Some of the vardefs do not correctly define dictionary as global.  Declare it first.
252                 global $dictionary;
253                 if(empty($GLOBALS['dictionary'][$object]) || $refresh){
254                         //if the consumer has demanded a refresh or the cache/modules... file
255                         //does not exist, then we should do out and try to reload things
256                         if($refresh || !file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/' . $object . 'vardefs.php')){
257                                 VardefManager::refreshVardefs($module, $object);
258                         }
259                         
260                         //at this point we should have the cache/modules/... file
261                         //which was created from the refreshVardefs so let's try to load it.
262                         if(file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module .  '/' . $object . 'vardefs.php'))
263                         {
264                                 include_once($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module .  '/' . $object . 'vardefs.php');
265                                 
266                                 // now that we hae loaded the data from disk, put it in the cache.
267                                 if(!empty($GLOBALS['dictionary'][$object]))
268                                         sugar_cache_put($key,$GLOBALS['dictionary'][$object]);
269                         }
270                 }
271         }
272 }