]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/MB/MBLanguage.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / MB / MBLanguage.php
1 <?php   
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 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 MBLanguage{
38                 var $iTemplates = array();
39                 var $templates = array();
40                 function MBLanguage( $name, $path, $label, $key_name){
41                         $this->path = $path;
42                         $this->name = $name;
43                         $this->key_name = $key_name;
44                         $this->label = $label;
45                 }
46                 
47                 function load(){
48                         $this->generateModStrings();
49                         $this->generateAppStrings();
50                 }
51                 
52                 function loadStrings($file)
53         {
54             $module = strtoupper($this->name);
55             $object_name = strtoupper($this->key_name);
56             $_object_name = strtolower($this->name);            
57                         if(!file_exists($file))return;
58                         
59                         $d = dir($file);
60                         while($e = $d->read()){
61                                 if(substr($e, 0, 1) != '.' && is_file($file . '/' . $e)){
62                                         include($file.'/'. $e);
63                                         if(empty($this->strings[$e])){
64                                                 
65                                                 $this->strings[$e] = $mod_strings;
66                                         }else{
67                                                 $this->strings[$e] = array_merge($this->strings[$e], $mod_strings);
68                                         }
69                                         
70                                         
71                                 }
72                         }
73                 }
74                 
75             function loadAppListStrings($file){
76             if(!file_exists($file))return;
77                         //we may not need this when loading in the app strings, but there is no harm
78                         //in setting it.
79                         $object_name = strtolower($this->key_name);
80
81                         $d = dir($file);
82                         while($e = $d->read()){
83                                 if(substr($e, 0, 1) != '.' && is_file($file . '/' . $e)){
84                                         include($file.'/'. $e);
85                                         if(empty($this->appListStrings[$e])){
86                                                 
87                                                 $this->appListStrings[$e] = $app_list_strings;
88                                         }else{
89                                                 $this->appListStrings[$e] = array_merge($this->appListStrings[$e], $app_list_strings);
90                                         }
91                                         
92                                         
93                                 }
94                         }
95                 }
96                 
97                 function generateModStrings(){
98                         $this->strings = array();
99                         $this->loadTemplates();
100                         
101                         foreach($this->iTemplates as $template=>$val){
102                                 $file = MB_IMPLEMENTS . '/' . $template . '/language';
103                                 $this->loadStrings($file);
104                         }
105                         foreach($this->templates as $template=>$val){
106                                 $file = MB_TEMPLATES . '/' . $template . '/language';
107                                 $this->loadStrings($file);
108                         }
109                         $this->loadStrings($this->path . '/language');
110                 }
111                 
112                 function getModStrings($language='en_us'){
113                         $language .= '.lang.php';
114                         if(!empty($this->strings[$language]) && $language != 'en_us.lang.php'){
115                             return sugarLangArrayMerge($this->strings['en_us.lang.php'], $this->strings[$language]);
116                         }
117                         if(!empty($this->strings['en_us.lang.php']))return $this->strings['en_us.lang.php'];
118                         $empty = array();
119                         return $empty;
120                 }
121                 function getAppListStrings($language='en_us'){
122                         $language .= '.lang.php';
123                         if(!empty($this->appListStrings[$language]) && $language != 'en_us.lang.php'){
124                             return sugarLangArrayMerge($this->appListStrings['en_us.lang.php'], $this->appListStrings[$language]);
125                         }
126                         if(!empty($this->appListStrings['en_us.lang.php']))return $this->appListStrings['en_us.lang.php'];
127                         $empty = array();
128                         return $empty;
129                 }
130                 
131                 function generateAppStrings($buildFromTemplate = true){
132                         $this->appListStrings = array('en_us.lang.php'=>array());
133                         //By default, generate app strings for the current language as well.
134                         $this->appListStrings[$GLOBALS [ 'current_language' ] . ".lang.php"] = array();
135                         $this->loadAppListStrings($this->path . '/../../language/application');
136                         
137                         if($buildFromTemplate){
138                                 //go through the templates application strings and load anything that is needed
139                                 foreach($this->iTemplates as $template=>$val){
140                                         $file = MB_IMPLEMENTS . '/' . $template . '/language/application';
141                                         $this->loadAppListStrings($file);
142                                 }
143                                 foreach($this->templates as $template=>$val){
144                                         $file = MB_TEMPLATES . '/' . $template . '/language/application';
145                                         $this->loadAppListStrings($file);
146                                 }
147                         }
148                 }
149                 function save($key_name, $duplicate=false, $rename=false){
150                         $header = file_get_contents('modules/ModuleBuilder/MB/header.php');
151                         $save_path = $this->path . '/language';
152                         mkdir_recursive($save_path);
153                         foreach($this->strings as $lang=>$values){
154                                 //Check if the module Label has changed.
155                                 $renameLang = $rename || empty($values) || (isset($values['LBL_MODULE_NAME']) && $this->label != $values['LBL_MODULE_NAME']);
156                                 $mod_strings = return_module_language(str_replace('.lang.php','',$lang), 'ModuleBuilder');
157                 $required = array(
158                 'LBL_LIST_FORM_TITLE'=>$this->label . " " . $mod_strings['LBL_LIST'],
159                 'LBL_MODULE_NAME'=>$this->label,
160                 'LBL_MODULE_TITLE'=>$this->label,
161                 'LBL_HOMEPAGE_TITLE'=>$mod_strings['LBL_HOMEPAGE_PREFIX'] . " " . $this->label,
162                 //FOR GENERIC MENU
163                 'LNK_NEW_RECORD'=>$mod_strings['LBL_CREATE'] ." ". $this->label,
164                 'LNK_LIST'=>$mod_strings['LBL_VIEW'] ." ". $this->label,
165                 'LNK_IMPORT_'.strtoupper($this->key_name)=>translate('LBL_IMPORT') ." ". $this->label,
166                 'LBL_SEARCH_FORM_TITLE'=>$mod_strings['LBL_SEARCH'] ." ". $this->label, 
167                 'LBL_HISTORY_SUBPANEL_TITLE'=>$mod_strings['LBL_HISTORY'],
168                 'LBL_ACTIVITIES_SUBPANEL_TITLE'=>$mod_strings['LBL_ACTIVITIES'],
169                 'LBL_'.strtoupper($this->key_name).'_SUBPANEL_TITLE'=>$this->label, 
170                 'LBL_NEW_FORM_TITLE' => $mod_strings['LBL_NEW'] ." ". $this->label,
171                 );
172                                 foreach($required as $k=>$v){
173                                         if(empty($values[$k]) || $renameLang){
174                                                 $values[$k] = $v;                       
175                                         }
176                                 }
177                                 write_array_to_file('mod_strings', $values, $save_path .'/'.$lang,'w', $header);
178                         }
179                         $app_save_path = $this->path . '/../../language/application';
180                         mkdir_recursive($app_save_path);
181                         $key_changed = ($this->key_name != $key_name);
182                         
183                         foreach($this->appListStrings as $lang=>$values){
184                                 // Load previously created modules data
185                                 $app_list_strings = array ();
186                                 $neededFile = $app_save_path . '/'. $lang;
187                                 if (file_exists($neededFile)) {
188                                         include $neededFile;
189                                 }
190
191                                 
192                                 if(!$duplicate){
193                                         unset($values['moduleList'][$this->key_name]);
194                                 }
195                                 
196
197                                 $values = sugarLangArrayMerge($values, $app_list_strings);
198                                 $values['moduleList'][$key_name]= $this->label;
199                                 
200                                 
201                                 $appFile = $header. "\n";
202                                 require_once('include/utils/array_utils.php');
203                                 $this->getGlobalAppListStringsForMB($values);
204                                 foreach($values as $key=>$array){
205                                         if($duplicate){
206                                                 //keep the original when duplicating
207                                                 $appFile .= override_value_to_string_recursive2 ('app_list_strings', $key, $array);
208                                         }
209                                         $okey = $key;
210                                         if($key_changed)$key = str_replace($this->key_name, $key_name, $key);
211                                         if($key_changed)$key = str_replace(strtolower($this->key_name), strtolower($key_name), $key);
212                                         // if we aren't duplicating or the key has changed let's add it
213                                         if(!$duplicate || $okey != $key){
214                                                 $appFile .= override_value_to_string_recursive2 ('app_list_strings', $key, $array);
215                                         }
216                                 }
217                         
218                                 $fp = sugar_fopen($app_save_path . '/'. $lang, 'w');
219                                 fwrite($fp, $appFile);
220                                 fclose($fp);
221                         }
222                 }
223                 
224                 /**
225                 *  If there is no this dropdown list  in  custom\modulebuilder\packages\$package\language\application\$lang.lang.php , 
226                 *  we will include it from global app_list_string array into custom\modulebuilder\packages\$package\language\application\$lang.lang.php 
227                 *  when we create a dropdown filed  and the value is created in MB.(#20728 )
228                 **/
229                 function getGlobalAppListStringsForMB(&$values){
230                         //Ensure it comes from MB
231                         if(!empty($_REQUEST['view_package']) && !empty($_REQUEST['type']) && $_REQUEST['type'] == 'enum'  && !empty($_REQUEST['options'])){
232                                 if(!isset($values[$_REQUEST['options']])){
233                                         global $app_list_strings;
234                                         if(!empty($app_list_strings[$_REQUEST['options']])){
235                                                 $values[$_REQUEST['options']]  = $app_list_strings[$_REQUEST['options']];                                               
236                                         }
237                                 }
238                         }
239                 }
240                 
241                 function build($path){
242                         if(file_exists($this->path.'/language/'))
243                         copy_recursive($this->path.'/language/', $path . '/language/');
244                 }
245                 
246                 function loadTemplates() {
247                         if(empty($this->templates)){
248                                 if (file_exists("$this->path/config.php")) {
249                                         include "$this->path/config.php";
250                                         $this->templates = $config['templates'];
251                                         $this->iTemplates = array();
252                                 }
253                         }
254                 }
255                 
256                 /**
257                  * Reset the templates and load the language files again.  This is called from 
258                  * MBModule->save() once the config file has been written.
259                  */
260                 function reload(){
261                         $this->templates = null;
262                         $this->load();
263                 }
264
265     /**
266      * Attempts to translate the given label if it is contained in this
267      * undeployed module's language strings
268      *
269      * @param string $label Label to translate
270      * @param string $language Language to use to translate the label
271      * @return string
272      */
273     public function translate($label, $language = "en_us"){
274             $language = $language . ".lang.php";
275             if (isset($this->strings[$language][$label]))
276                 return $this->strings[$language][$label];
277
278             if (isset($this->appListStrings[$language][$label]))
279                 return $this->appListStrings[$language][$label];
280
281             return $label;
282         }
283                 
284         
285 }