]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/LanguageManager.php
Release 6.2.0
[Github/sugarcrm.git] / include / SugarObjects / LanguageManager.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 class LanguageManager{
38         
39         /**
40          * Called from VardefManager to allow for caching a lang file for a module
41          * @param module - the name of the module we are working with
42          * @param templates - an array of templates this module uses
43          */
44         function createLanguageFile($module , $templates=array('default'), $refresh = false){
45                 global $mod_strings, $current_language;
46                 if(!empty($GLOBALS['sugar_config']['developerMode']) || !empty($_SESSION['developerMode'])){
47                 $refresh = true;
48         }
49                 $temp_mod_strings = $mod_strings;
50                 $lang = $current_language;
51         if(empty($lang))
52             $lang = $GLOBALS['sugar_config']['default_language'];
53                 static $createdModules = array();
54                 if(empty($createdModules[$module]) && ($refresh || !file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'.$module.'/language/'.$lang.'.lang.php'))){
55                         $loaded_mod_strings = array();
56                         $loaded_mod_strings = LanguageManager::loadTemplateLanguage($module , $templates, $lang , $loaded_mod_strings);
57                         $createdModules[$module] = true;
58                         LanguageManager::refreshLanguage($module,$lang, $loaded_mod_strings);   
59                 }
60         }
61         
62         /**
63          * Load the module  tempalte lauguage files 
64          * @param module - the name of the module we are working with
65          * @param templates - an array of templates this module uses
66          * @param lang - current language this module use
67          * @param loaded_mod_strings - the string that we will add the module template language  into
68          */
69         function loadTemplateLanguage($module , $templates , $lang, $loaded_mod_strings){
70                 $templates = array_reverse($templates);
71                 foreach($templates as $template){
72                         $temp = LanguageManager::addTemplate($module,$lang, $template);
73                         $loaded_mod_strings = sugarArrayMerge($loaded_mod_strings, $temp);
74                 }
75                 return $loaded_mod_strings;
76         }
77         
78         function addTemplate($module, $lang, $template){
79                 if($template == 'default')$template = 'basic';
80                 $templates = array();
81                 $fields = array();
82                 if(empty($templates[$template])){
83                         $path = 'include/SugarObjects/templates/' . $template . '/language/'.$lang.'.lang.php';
84                         if(file_exists($path)){
85                                 require($path);
86                                 $templates[$template] = $mod_strings;
87                         }else{
88                                 $path = 'include/SugarObjects/implements/' . $template . '/language/'.$lang.'.lang.php';
89                                 if(file_exists($path)){
90                                         require($path);
91                                         $templates[$template] = $mod_strings;
92                                 }
93                         }
94                 }
95                 if(!empty($templates[$template])){
96                         return $templates[$template];   
97                 }
98         }
99         
100         function saveCache($module,$lang, $loaded_mod_strings, $additonal_objects= array()){
101                 if(empty($lang))
102                         $lang = $GLOBALS['sugar_config']['default_language'];
103                 
104                 $file = create_cache_directory('modules/' . $module . '/language/'.$lang.'.lang.php');
105                 write_array_to_file('mod_strings',$loaded_mod_strings, $file);
106                 include($file);
107                 
108                 // put the item in the sugar cache.
109                 $key = self::getLanguageCacheKey($module,$lang);
110                 sugar_cache_put($key,$loaded_mod_strings);
111         }
112         
113         /**
114          * clear out the language cache. 
115          * @param string module_dir the module_dir to clear, if not specified then clear
116          *                      clear language cache for all modules.
117          * @param string lang the name of the object we are clearing this is for sugar_cache
118          */
119         function clearLanguageCache($module_dir = '', $lang = ''){
120                 if(empty($lang)) {
121                         $languages = array_keys($GLOBALS['sugar_config']['languages']);
122                 } else {
123                         $languages = array($lang);
124                 }
125                 //if we have a module name specified then just remove that language file
126                 //otherwise go through each module and clean up the language
127                 if(!empty($module_dir)) {
128                         foreach($languages as $clean_lang) {
129                                 LanguageManager::_clearCache($module_dir, $clean_lang);
130                         }
131                 } else {
132                         $cache_dir = $GLOBALS['sugar_config']['cache_dir'].'modules/';
133                         if(file_exists($cache_dir) && $dir = @opendir($cache_dir)) {
134                                 while(($entry = readdir($dir)) !== false) {
135                                         if ($entry == "." || $entry == "..") continue;
136                                                 foreach($languages as $clean_lang) {
137                                                         LanguageManager::_clearCache($entry, $clean_lang);
138                                                 }
139                                 }
140                                 closedir($dir);
141                         }
142                 }
143         }
144         
145         /**
146          * PRIVATE function used within clearLanguageCache so we do not repeat logic
147          * @param string module_dir the module_dir to clear
148          * @param string lang the name of the language file we are clearing this is for sugar_cache
149          */
150         function _clearCache($module_dir = '', $lang){
151                 if(!empty($module_dir) && !empty($lang)){
152                         $file = $GLOBALS['sugar_config']['cache_dir'].'modules/'.$module_dir.'/language/'.$lang.'.lang.php';
153                         if(file_exists($file)){
154                                 unlink($file);
155                                 $key = self::getLanguageCacheKey($module_dir,$lang);
156                                 sugar_cache_clear($key);
157                         }
158                 }
159         }
160         
161         /**
162          * Given a module, search all of the specified locations, and any others as specified
163          * in order to refresh the cache file
164          * 
165          * @param string $module the given module we want to load the vardefs for
166          * @param string $lang the given language we wish to load
167          * @param array $additional_search_paths an array which allows a consumer to pass in additional vardef locations to search
168          */
169         function refreshLanguage($module, $lang, $loaded_mod_strings = array(), $additional_search_paths = null){
170                 // Some of the vardefs do not correctly define dictionary as global.  Declare it first.
171                 $lang_paths = array(
172                                         'modules/'.$module.'/language/'.$lang.'.lang.php',
173                                         'modules/'.$module.'/language/'.$lang.'.lang.override.php',
174                                         'custom/modules/'.$module.'/Ext/Language/'.$lang.'.lang.ext.php',
175                                         'custom/modules/'.$module.'/language/'.$lang.'.lang.php',
176                                  );
177
178                 #27023, if this module template language file was not attached , get the template from this module vardef cache file if exsits and load the template language files.
179                 static $createdModules;
180                 if(empty($createdModules[$module]) && isset($GLOBALS['beanList'][$module])){
181                                 $object = $GLOBALS['beanList'][$module];
182                                 
183                                 if ($object == 'aCase')
184                             $object = 'Case';
185                         
186                         if(!empty($GLOBALS["dictionary"]["$object"]["templates"])){
187                                 $templates = $GLOBALS["dictionary"]["$object"]["templates"];                                    
188                                         $loaded_mod_strings = LanguageManager::loadTemplateLanguage($module , $templates, $lang , $loaded_mod_strings);
189                                         $createdModules[$module] = true;                
190                         }       
191                 }
192                 //end of fix #27023
193                 
194                 // Add in additional search paths if they were provided.
195                 if(!empty($additional_search_paths) && is_array($additional_search_paths))
196                 {
197                         $lang_paths = array_merge($lang_paths, $additional_search_paths);
198                 }
199                 
200                 //search a predefined set of locations for the vardef files
201                 foreach($lang_paths as $path){
202                         if(file_exists($path)){
203                                 require($path);
204                                 if(!empty($mod_strings)){
205                                         if (function_exists('sugarArrayMergeRecursive')){
206                                                 $loaded_mod_strings = sugarArrayMergeRecursive($loaded_mod_strings, $mod_strings);
207                                         }
208                                         else{
209                                                 $loaded_mod_strings = sugarArrayMerge($loaded_mod_strings, $mod_strings);
210                                         }       
211                                 }
212                         }
213                 }
214                 
215                 //great! now that we have loaded all of our vardefs.
216                 //let's go save them to the cache file.
217                 if(!empty($loaded_mod_strings))
218                         LanguageManager::saveCache($module, $lang, $loaded_mod_strings);
219         }
220         
221         function loadModuleLanguage($module, $lang, $refresh=false){
222                 //here check if the cache file exists, if it does then load it, if it doesn't
223                 //then call refreshVardef
224                 //if either our session or the system is set to developerMode then refresh is set to true
225                 
226                 // Retrieve the vardefs from cache.
227                 $key = self::getLanguageCacheKey($module,$lang);
228                 
229                 if(!$refresh)
230                 {
231                         $return_result = sugar_cache_retrieve($key);            
232                         if(!empty($return_result)){
233                                 return $return_result;
234                         }
235                 }
236                         
237                 // Some of the vardefs do not correctly define dictionary as global.  Declare it first.
238                 if($refresh || !file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'.$module.'/language/'.$lang.'.lang.php')){
239                         LanguageManager::refreshLanguage($module, $lang);
240                 }
241                         
242                 //at this point we should have the cache/modules/... file
243                 //which was created from the refreshVardefs so let's try to load it.
244                 if(file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$lang.'.lang.php')){
245                         global $mod_strings;
246
247                         require($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$lang.'.lang.php');
248                                 
249                         // now that we hae loaded the data from disk, put it in the cache.
250                         if(!empty($mod_strings))
251                                 sugar_cache_put($key,$mod_strings);
252                         if(!empty($_SESSION['translation_mode'])){
253                                 $mod_strings = array_map('translated_prefix', $mod_strings);                    
254                         }
255                         return $mod_strings;
256                 }
257         }
258
259     /**
260      * Return the cache key for the module language definition
261      *
262      * @static
263      * @param  $module
264      * @param  $lang
265      * @return string
266      */
267     public static function getLanguageCacheKey($module, $lang)
268         {
269          return "LanguageManager.$module.$lang";
270         }
271 }
272
273 function translated_prefix($key){
274         return '[translated]' . $key;
275 }