]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/MB/MBLanguage.php
Release 6.1.4
[Github/sugarcrm.git] / modules / ModuleBuilder / MB / MBLanguage.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 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             $module = strtoupper($this->name);
54             $object_name = strtoupper($this->key_name);
55             $_object_name = strtolower($this->name);            
56                         if(!file_exists($file))return;
57                         
58                         $d = dir($file);
59                         while($e = $d->read()){
60                                 if(substr($e, 0, 1) != '.' && is_file($file . '/' . $e)){
61                                         include($file.'/'. $e);
62                                         if(empty($this->strings[$e])){
63                                                 
64                                                 $this->strings[$e] = $mod_strings;
65                                         }else{
66                                                 $this->strings[$e] = array_merge($this->strings[$e], $mod_strings);
67                                         }
68                                         
69                                         
70                                 }
71                         }
72                 }
73                 
74         function loadAppListStrings($file){
75                         
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 sugarArrayMerge($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 sugarArrayMerge($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) || $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                                 if(!$duplicate){
185                                         unset($values['moduleList'][$this->key_name]);
186                                 }
187                                 $values['moduleList'][$key_name]= $this->label;
188                                 $appFile = $header. "\n";
189                                 require_once('include/utils/array_utils.php');
190                                 $this->getGlobalAppListStringsForMB($values);
191                                 foreach($values as $key=>$array){
192                                         if($duplicate){
193                                                 //keep the original when duplicating
194                                                 $appFile .= override_value_to_string_recursive2 ('app_list_strings', $key, $array);
195                                         }
196                                         $okey = $key;
197                                         if($key_changed)$key = str_replace($this->key_name, $key_name, $key);
198                                         if($key_changed)$key = str_replace(strtolower($this->key_name), strtolower($key_name), $key);
199                                         // if we aren't duplicating or the key has changed let's add it
200                                         if(!$duplicate || $okey != $key){
201                                                 $appFile .= override_value_to_string_recursive2 ('app_list_strings', $key, $array);
202                                         }
203                                 }
204                         
205                                 $fp = sugar_fopen($app_save_path . '/'. $lang, 'w');
206                                 fwrite($fp, $appFile);
207                                 fclose($fp);
208                         }
209                 }
210                 
211                 /**
212                 *  If there is no this dropdown list  in  custom\modulebuilder\packages\$package\language\application\$lang.lang.php , 
213                 *  we will include it from global app_list_string array into custom\modulebuilder\packages\$package\language\application\$lang.lang.php 
214                 *  when we create a dropdown filed  and the value is created in MB.(#20728 )
215                 **/
216                 function getGlobalAppListStringsForMB(&$values){
217                         //Ensure it comes from MB
218                         if(!empty($_REQUEST['view_package']) && !empty($_REQUEST['type']) && $_REQUEST['type']='enum'  && !empty($_REQUEST['options'])){
219                                 if(!isset($values[$_REQUEST['options']])){
220                                         global $app_list_strings;
221                                         if(!empty($app_list_strings[$_REQUEST['options']])){
222                                                 $values[$_REQUEST['options']]  = $app_list_strings[$_REQUEST['options']];                                               
223                                         }
224                                 }
225                         }
226                 }
227                 
228                 function build($path){
229                         if(file_exists($this->path.'/language/'))
230                         copy_recursive($this->path.'/language/', $path . '/language/');
231                 }
232                 
233                 function loadTemplates() {
234                         if(empty($this->templates)){
235                                 if (file_exists("$this->path/config.php")) {
236                                         include "$this->path/config.php";
237                                         $this->templates = $config['templates'];
238                                         $this->iTemplates = array();
239                                 }
240                         }
241                 }
242                 
243                 /**
244                  * Reset the templates and load the language files again.  This is called from 
245                  * MBModule->save() once the config file has been written.
246                  */
247                 function reload(){
248                         $this->templates = null;
249                         $this->load();
250                 }
251                 
252         
253 }