2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 var $iTemplates = array();
39 var $templates = array();
40 function MBLanguage( $name, $path, $label, $key_name){
43 $this->key_name = $key_name;
44 $this->label = $label;
48 $this->generateModStrings();
49 $this->generateAppStrings();
52 function loadStrings($file)
54 $module = strtoupper($this->name);
55 $object_name = strtoupper($this->key_name);
56 $_object_name = strtolower($this->name);
57 if(!file_exists($file))return;
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])){
65 $this->strings[$e] = $mod_strings;
67 $this->strings[$e] = array_merge($this->strings[$e], $mod_strings);
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
79 $object_name = strtolower($this->key_name);
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])){
87 $this->appListStrings[$e] = $app_list_strings;
89 $this->appListStrings[$e] = array_merge($this->appListStrings[$e], $app_list_strings);
97 function generateModStrings(){
98 $this->strings = array();
99 $this->loadTemplates();
101 foreach($this->iTemplates as $template=>$val){
102 $file = MB_IMPLEMENTS . '/' . $template . '/language';
103 $this->loadStrings($file);
105 foreach($this->templates as $template=>$val){
106 $file = MB_TEMPLATES . '/' . $template . '/language';
107 $this->loadStrings($file);
109 $this->loadStrings($this->path . '/language');
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]);
117 if(!empty($this->strings['en_us.lang.php']))return $this->strings['en_us.lang.php'];
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]);
126 if(!empty($this->appListStrings['en_us.lang.php']))return $this->appListStrings['en_us.lang.php'];
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');
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);
143 foreach($this->templates as $template=>$val){
144 $file = MB_TEMPLATES . '/' . $template . '/language/application';
145 $this->loadAppListStrings($file);
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');
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,
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,
172 foreach($required as $k=>$v){
173 if(empty($values[$k]) || $renameLang){
177 write_array_to_file('mod_strings', $values, $save_path .'/'.$lang,'w', $header);
179 $app_save_path = $this->path . '/../../language/application';
180 mkdir_recursive($app_save_path);
181 $key_changed = ($this->key_name != $key_name);
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)) {
193 unset($values['moduleList'][$this->key_name]);
197 $values = sugarArrayMerge($values, $app_list_strings);
198 $values['moduleList'][$key_name]= $this->label;
201 $appFile = $header. "\n";
202 require_once('include/utils/array_utils.php');
203 $this->getGlobalAppListStringsForMB($values);
204 foreach($values as $key=>$array){
206 //keep the original when duplicating
207 $appFile .= override_value_to_string_recursive2 ('app_list_strings', $key, $array);
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);
218 $fp = sugar_fopen($app_save_path . '/'. $lang, 'w');
219 fwrite($fp, $appFile);
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 )
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']];
241 function build($path){
242 if(file_exists($this->path.'/language/'))
243 copy_recursive($this->path.'/language/', $path . '/language/');
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();
257 * Reset the templates and load the language files again. This is called from
258 * MBModule->save() once the config file has been written.
261 $this->templates = null;