]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/VardefManager.php
Release 6.4.0beta1
[Github/sugarcrm.git] / include / SugarObjects / VardefManager.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition 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     static $linkFields;
42
43     /**
44      * this method is called within a vardefs.php file which extends from a SugarObject.
45      * It is meant to load the vardefs from the SugarObject.
46      */
47     static function createVardef($module, $object, $templates = array('default'), $object_name = false)
48     {
49         global $dictionary;
50
51         include_once('modules/TableDictionary.php');
52
53         //reverse the sort order so priority goes highest to lowest;
54         $templates = array_reverse($templates);
55         foreach ($templates as $template)
56         {
57             VardefManager::addTemplate($module, $object, $template, $object_name);
58         }
59         LanguageManager::createLanguageFile($module, $templates);
60
61         if (isset(VardefManager::$custom_disabled_modules[$module]))
62         {
63             $vardef_paths = array(
64                 'custom/modules/' . $module . '/Ext/Vardefs/vardefs.ext.php',
65                 'custom/Extension/modules/' . $module . '/Ext/Vardefs/vardefs.php'
66             );
67
68             //search a predefined set of locations for the vardef files
69             foreach ($vardef_paths as $path)
70             {
71                 if (file_exists($path)) {
72                     require($path);
73                 }
74             }
75         }
76     }
77
78     /**
79      * Enables/Disables the loading of custom vardefs for a module.
80      * @param String $module Module to be enabled/disabled
81      * @param Boolean $enable true to enable, false to disable
82      * @return  null
83      */
84     public static function setCustomAllowedForModule($module, $enable) {
85         if ($enable && isset($custom_disabled_modules[$module])) {
86               unset($custom_disabled_modules[$module]);
87         } else if (!$enable) {
88               $custom_disabled_modules[$module] = true;
89         }
90     }
91     
92     static function addTemplate($module, $object, $template, $object_name=false){
93         if($template == 'default')$template = 'basic';
94         $templates = array();
95         $fields = array();
96         if(empty($object_name))$object_name = $object;
97         $_object_name = strtolower($object_name);
98         if(!empty($GLOBALS['dictionary'][$object]['table'])){
99             $table_name = $GLOBALS['dictionary'][$object]['table'];
100         }else{
101             $table_name = strtolower($module);
102         }
103         
104         if(empty($templates[$template])){
105             $path = 'include/SugarObjects/templates/' . $template . '/vardefs.php';
106             if(file_exists($path)){
107                 require($path);
108                 $templates[$template] = $vardefs;
109             }else{
110                 $path = 'include/SugarObjects/implements/' . $template . '/vardefs.php';
111                 if(file_exists($path)){
112                     require($path);
113                     $templates[$template] = $vardefs;
114                 }
115             }
116         }
117
118         if(!empty($templates[$template])){
119             if(empty($GLOBALS['dictionary'][$object]['fields']))$GLOBALS['dictionary'][$object]['fields'] = array();
120             if(empty($GLOBALS['dictionary'][$object]['relationships']))$GLOBALS['dictionary'][$object]['relationships'] = array();
121             if(empty($GLOBALS['dictionary'][$object]['indices']))$GLOBALS['dictionary'][$object]['indices'] = array();
122             $GLOBALS['dictionary'][$object]['fields'] = array_merge($templates[$template]['fields'], $GLOBALS['dictionary'][$object]['fields']);
123             if(!empty($templates[$template]['relationships']))$GLOBALS['dictionary'][$object]['relationships'] = array_merge($templates[$template]['relationships'], $GLOBALS['dictionary'][$object]['relationships']);
124             if(!empty($templates[$template]['indices']))$GLOBALS['dictionary'][$object]['indices'] = array_merge($templates[$template]['indices'], $GLOBALS['dictionary'][$object]['indices']);
125             // maintain a record of this objects inheritance from the SugarObject templates...
126             $GLOBALS['dictionary'][$object]['templates'][ $template ] = $template ;
127         }
128         
129     }
130
131
132     /**
133      * Remove invalid field definitions
134      * @static
135      * @param Array $fieldDefs
136      * @return  Array
137      */
138     static function cleanVardefs($fieldDefs)
139     {
140         foreach($fieldDefs as $field => $defs)
141         {
142             if (empty($def['name']) || empty($def['type']))
143             {
144                 unset($fieldDefs[$field]);
145             }
146         }
147
148         return $fieldDefs;
149     }
150     
151     /**
152      * Save the dictionary object to the cache
153      * @param string $module the name of the module
154      * @param string $object the name of the object
155      */
156     static function saveCache($module,$object, $additonal_objects= array()){
157         
158         $file = create_cache_directory('modules/' . $module . '/' . $object . 'vardefs.php');
159         write_array_to_file('GLOBALS["dictionary"]["'. $object . '"]',$GLOBALS['dictionary'][$object], $file);
160         if ( sugar_is_file($file) && is_readable($file)) {
161             include($file);
162         }
163         
164         // put the item in the sugar cache.
165         $key = "VardefManager.$module.$object";
166         //Sometimes bad definitions can get in from left over extensions or file system lag(caching). We need to clean those.
167         $data = self::cleanVardefs($GLOBALS['dictionary'][$object]);
168         sugar_cache_put($key,$data);
169     }
170     
171     /**
172      * clear out the vardef cache. If we receive a module name then just clear the vardef cache for that module
173      * otherwise clear out the cache for every module
174      * @param string module_dir the module_dir to clear, if not specified then clear
175      *                      clear vardef cache for all modules.
176      * @param string object_name the name of the object we are clearing this is for sugar_cache
177      */
178     static function clearVardef($module_dir = '', $object_name = ''){
179         //if we have a module name specified then just remove that vardef file
180         //otherwise go through each module and remove the vardefs.php
181         if(!empty($module_dir) && !empty($object_name)){
182             VardefManager::_clearCache($module_dir, $object_name);
183         }else{
184             global $beanList;
185             foreach($beanList as $module_dir => $object_name){
186                 VardefManager::_clearCache($module_dir, $object_name);
187             }
188         }
189     }
190     
191     /**
192      * PRIVATE function used within clearVardefCache so we do not repeat logic
193      * @param string module_dir the module_dir to clear
194      * @param string object_name the name of the object we are clearing this is for sugar_cache
195      */
196     static function _clearCache($module_dir = '', $object_name = ''){
197         if(!empty($module_dir) && !empty($object_name)){
198             
199             if($object_name == 'aCase') {
200                 $object_name = 'Case';
201             }
202             
203             $file = sugar_cached('modules/').$module_dir.'/' . $object_name . 'vardefs.php';
204             if(file_exists($file)){
205                 unlink($file);
206                 $key = "VardefManager.$module_dir.$object_name";
207                 sugar_cache_clear($key);
208             }
209         }
210     }
211     
212     /**
213      * Given a module, search all of the specified locations, and any others as specified
214      * in order to refresh the cache file
215      * 
216      * @param string $module the given module we want to load the vardefs for
217      * @param string $object the given object we wish to load the vardefs for
218      * @param array $additional_search_paths an array which allows a consumer to pass in additional vardef locations to search
219      */
220     static function refreshVardefs($module, $object, $additional_search_paths = null, $cacheCustom = true, $params = array()){
221         // Some of the vardefs do not correctly define dictionary as global.  Declare it first.
222         global $dictionary, $beanList;
223         $vardef_paths = array(
224                     'modules/'.$module.'/vardefs.php',
225                     'custom/modules/'.$module.'/Ext/Vardefs/vardefs.ext.php',
226                     'custom/Extension/modules/'.$module.'/Ext/Vardefs/vardefs.php'
227                  );
228
229         // Add in additional search paths if they were provided.
230         if(!empty($additional_search_paths) && is_array($additional_search_paths))
231         {
232             $vardef_paths = array_merge($vardef_paths, $additional_search_paths);
233         }
234         $found = false;
235         //search a predefined set of locations for the vardef files
236         foreach($vardef_paths as $path){
237             if(file_exists($path)){
238                 require($path);
239                 $found = true;
240             }
241         }
242         //Some modules have multiple beans, we need to see if this object has a module_dir that is different from its module_name
243         if(!$found){
244             $temp = BeanFactory::newBean($module);
245             if ($temp && $temp->module_dir != $temp->module_name && !empty($beanList[$temp->module_dir]))
246             {
247                 self::refreshVardefs($temp->module_dir, $beanList[$temp->module_dir], $additional_search_paths, $cacheCustom);
248             }
249         }
250
251         //load custom fields into the vardef cache
252         if($cacheCustom){
253             require_once("modules/DynamicFields/DynamicField.php");
254             $df = new DynamicField ($module) ;
255             $df->buildCache($module);
256         }
257
258         //great! now that we have loaded all of our vardefs.
259         //let's go save them to the cache file.
260         if(!empty($GLOBALS['dictionary'][$object])) {
261             VardefManager::saveCache($module, $object);
262         }
263     }
264
265     /**
266      * @static
267      * @param  $module
268      * @param  $object
269      * @return array|bool  returns a list of all fields in the module of type 'link'.
270      */
271     protected static function getLinkFieldsForModule($module, $object)
272     {
273         global $dictionary;
274         if($object == 'aCase') {
275             $object = 'Case';
276         }
277         if (empty($dictionary[$object])) {
278             self::loadVardef($module, $object, false, array('ignore_rel_calc_fields' => true));
279         }
280         if (empty($dictionary[$object]))
281         {
282             $GLOBALS['log']->debug("Failed to load vardefs for $module:$object in linkFieldsForModule<br/>");
283             return false;
284         }
285
286         //Cache link fields for this call in a static variable
287         if (!isset(self::$linkFields))
288             self::$linkFields = array();
289
290         if (isset(self::$linkFields[$object]))
291             return self::$linkFields[$object];
292
293         $vardef = $dictionary[$object];
294         $links = array();
295         foreach($vardef['fields'] as $name => $def)
296         {
297             //Look through all link fields for related modules that have calculated fields that use that relationship
298             if(!empty($def['type']) && $def['type'] == 'link' && !empty($def['relationship']))
299             {
300                 $links[$name] = $def;
301             }
302         }
303         return $links;
304     }
305
306
307     public static function getLinkFieldForRelationship($module, $object, $relName)
308     {
309         $relLinkFields = self::getLinkFieldsForModule($module, $object);
310         $matches = array();
311         if (!empty($relLinkFields))
312         {
313             foreach($relLinkFields as $rfName => $rfDef)
314             {
315                 if ($rfDef['relationship'] == $relName)
316                 {
317                     $matches[] = $rfDef;
318                 }
319             }
320         }
321         if (empty($matches))
322             return false;
323         if (sizeof($matches) == 1)
324             return $matches[0];
325         //For relationships where both sides are the same module, more than one link will be returned
326         return $matches;
327     }
328
329     
330     /**
331      * apply global "account_required" setting if possible
332      * @param array    $vardef
333      * @return array   updated $vardef
334      */
335     static function applyGlobalAccountRequirements($vardef)
336     {
337         if (isset($GLOBALS['sugar_config']['require_accounts'])) {
338             if (isset($vardef['fields']) &&
339                 isset($vardef['fields']['account_name']) &&
340                 isset($vardef['fields']['account_name']['required']))
341             {
342                 $vardef['fields']['account_name']['required'] = $GLOBALS['sugar_config']['require_accounts'];
343             }
344
345         }
346
347         return $vardef;
348     }
349
350
351     /**
352      * load the vardefs for a given module and object
353      * @param string $module the given module we want to load the vardefs for
354      * @param string $object the given object we wish to load the vardefs for
355      * @param bool   $refresh whether or not we wish to refresh the cache file.
356      */
357     static function loadVardef($module, $object, $refresh=false, $params = array()){
358         //here check if the cache file exists, if it does then load it, if it doesn't
359         //then call refreshVardef
360         //if either our session or the system is set to developerMode then refresh is set to true
361         if(!empty($GLOBALS['sugar_config']['developerMode']) || !empty($_SESSION['developerMode'])){
362             $refresh = true;
363         }
364         // Retrieve the vardefs from cache.
365         $key = "VardefManager.$module.$object";
366         
367         if(!$refresh)
368         {
369             $return_result = sugar_cache_retrieve($key);
370             $return_result = self::applyGlobalAccountRequirements($return_result);
371
372             if(!empty($return_result))
373             {
374                 $GLOBALS['dictionary'][$object] = $return_result;
375                 return;
376             }
377         }
378                 
379         // Some of the vardefs do not correctly define dictionary as global.  Declare it first.
380         global $dictionary;
381         if(empty($GLOBALS['dictionary'][$object]) || $refresh){
382             //if the consumer has demanded a refresh or the cache/modules... file
383             //does not exist, then we should do out and try to reload things
384
385                         $cachedfile = sugar_cached('modules/'). $module . '/' . $object . 'vardefs.php';
386                         if($refresh || !file_exists($cachedfile)){
387                                 VardefManager::refreshVardefs($module, $object, null, true, $params);
388                         }
389             
390             //at this point we should have the cache/modules/... file
391             //which was created from the refreshVardefs so let's try to load it.
392             if(file_exists($cachedfile))
393             {
394                 if (is_readable($cachedfile))
395                 {
396                     include_once($cachedfile);
397                 }
398                 // now that we hae loaded the data from disk, put it in the cache.
399                 if(!empty($GLOBALS['dictionary'][$object]))
400                 {
401                     $GLOBALS['dictionary'][$object] = self::applyGlobalAccountRequirements($GLOBALS['dictionary'][$object]);
402                     sugar_cache_put($key,$GLOBALS['dictionary'][$object]);
403                 }
404             }
405         }
406     }
407
408 }