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