]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.label.php
Release 6.5.16
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / parser.label.php
1 <?php
2 if (! defined ( 'sugarEntry' ) || ! sugarEntry)
3     die ( 'Not A Valid Entry Point' ) ;
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
7  * 
8  * This program is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Affero General Public License version 3 as published by the
10  * Free Software Foundation with the addition of the following permission added
11  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14  * 
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
18  * details.
19  * 
20  * You should have received a copy of the GNU Affero General Public License along with
21  * this program; if not, see http://www.gnu.org/licenses or write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301 USA.
24  * 
25  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27  * 
28  * The interactive user interfaces in modified source and object code versions
29  * of this program must display Appropriate Legal Notices, as required under
30  * Section 5 of the GNU Affero General Public License version 3.
31  * 
32  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33  * these Appropriate Legal Notices must retain the display of the "Powered by
34  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35  * technical reasons, the Appropriate Legal Notices must display the words
36  * "Powered by SugarCRM".
37  ********************************************************************************/
38
39
40 require_once ('modules/ModuleBuilder/parsers/ModuleBuilderParser.php') ;
41
42 class ParserLabel extends ModuleBuilderParser
43 {
44
45     function ParserLabel ($moduleName, $packageName = '' )
46     {
47         $this->moduleName = $moduleName;
48         if (!empty($packageName))
49             $this->packageName = $packageName ;
50     }
51
52     /**
53      * Takes in the request params from a save request and processes
54      * them for the save.
55      * @param REQUEST $params       Labels as "label_".System label => Display label pairs
56      * @param string $language      Language key, for example 'en_us'
57      */
58     function handleSave ($params , $language)
59     {
60         $labels = array ( ) ;
61         foreach ( $params as $key => $value )
62         {
63             if (preg_match ( '/^label_/', $key ) && strcmp ( $value, 'no_change' ) != 0)
64             {
65                 $labels [ strtoupper(substr ( $key, 6 )) ] = SugarCleaner::cleanHtml(from_html($value),false);
66             }
67         }
68         if (!empty($this->packageName)) //we are in Module builder
69         {
70             return self::addLabels ( $language, $labels, $this->moduleName, "custom/modulebuilder/packages/{$this->packageName}/modules/{$this->moduleName}/language" ) ;
71         } else
72         {
73             $addLabelsResult = true;
74             $addExtLabelsResult = true;
75             $extLabels = array();
76             $extFile = "custom/modules/".$this->moduleName."/Ext/Language/".$language.".lang.ext.php";
77             if (is_file($extFile)) {
78                 include($extFile);
79                 foreach ($labels as $key=>$value) {
80                     if (isset($mod_strings[$key])) {
81                         $extLabels[$key] = $value;
82                         unset($labels[$key]);
83                     }
84                 }
85             }
86             if (!empty($labels)) {
87                 $addLabelsResult =  self::addLabels($language, $labels, $this->moduleName);
88             }
89             if (!empty($extLabels)) {
90                 $addExtLabelsResult =  self::addLabels($language, $extLabels, $this->moduleName, null, true);
91             }
92             return $addLabelsResult && $addExtLabelsResult;
93         }
94     }
95
96     /*
97      * Remove a label from the language pack for a module
98      * @param string $language      Language key, for example 'en_us'
99      * @param string $label         The label to remove
100      * @param string $labelvalue    The value of the label to remove
101      * @param string $moduleName    Name of the module to which to add these labels
102      * @param string $basepath      base path of the language file
103      * @param string $forRelationshipLabel      whether this is a relationship label
104      */
105     static function removeLabel($language, $label, $labelvalue, $moduleName, $basepath = null, $forRelationshipLabel = false) {
106         $GLOBALS [ 'log' ]->debug ( "ParserLabel->removeLabels($language, \$label, \$labelvalue, $moduleName, $basepath );" ) ;
107         if (is_null ( $basepath ))
108         {
109             $deployedModule = true ;
110             $basepath = "custom/modules/$moduleName/language" ;
111             if($forRelationshipLabel){
112                 $basepath = "custom/modules/$moduleName/Ext/Language" ;
113             }
114             if (! is_dir ( $basepath ))
115             {
116                 $GLOBALS ['log']->debug("$basepath is not a directory.");
117                 return false;
118             }
119         }
120
121         $filename = "$basepath/$language.lang.php" ;
122         if($forRelationshipLabel){
123                 $filename = "$basepath/$language.lang.ext.php" ;
124         }
125
126         $dir_exists = is_dir ( $basepath ) ;
127
128         $mod_strings = array ( ) ;
129
130         if ($dir_exists)
131         {
132             if (file_exists ($filename))
133             {
134                 // obtain $mod_strings
135                 include ($filename) ;
136             } else {
137                 $GLOBALS ['log']->debug("file $filename does not exist.");
138                 return false;
139             }
140         } else {
141             $GLOBALS ['log']->debug("directory $basepath does not exist.");
142             return false ;
143         }
144
145         $changed = false ;
146
147         if (isset($mod_strings[$label]) && $mod_strings[$label]==$labelvalue) {
148             unset($mod_strings[$label]);
149             $changed = true;
150         }
151
152         if ($changed)
153         {
154             if (! write_array_to_file ( "mod_strings", $mod_strings, $filename )) {
155                 $GLOBALS [ 'log' ]->fatal ( "Could not write $filename" ) ;
156             } else {
157                 // if we have a cache to worry about, then clear it now
158                 if ($deployedModule) {
159                     $GLOBALS ['log']->debug ( "PaserLabel->addLabels: clearing language cache" ) ;
160                     $cache_key = "module_language." . $language . $moduleName ;
161                     sugar_cache_clear ( $cache_key ) ;
162                     LanguageManager::clearLanguageCache ( $moduleName, $language ) ;
163                 }
164             }
165         }
166
167         return true ;
168     }
169
170     /*
171      * Add a set of labels to the language pack for a module, deployed or undeployed
172      * @param string $language      Language key, for example 'en_us'
173      * @param array $labels         The labels to add in the form of an array of System label => Display label pairs
174      * @param string $moduleName    Name of the module to which to add these labels
175      * @param string $packageName   If module is undeployed, name of the package to which it belongs
176      */
177     static function addLabels ($language , $labels , $moduleName , $basepath = null, $forRelationshipLabel = false)
178     {
179
180         $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels($language, \$labels, $moduleName, $basepath );" ) ;
181         $GLOBALS [ 'log' ]->debug ( "\$labels:" . print_r ( $labels, true ) ) ;
182
183         $deployedModule = false ;
184         if (is_null ( $basepath ))
185         {
186             $deployedModule = true ;
187             $basepath = "custom/modules/$moduleName/language" ;
188             if($forRelationshipLabel){
189                 $basepath = "custom/modules/$moduleName/Ext/Language" ;
190             }
191             if (! is_dir ( $basepath ))
192             {
193                 mkdir_recursive($basepath);
194             }
195         }
196
197         $filename = "$basepath/$language.lang.php" ;
198         if($forRelationshipLabel){
199                 $filename = "$basepath/$language.lang.ext.php" ;
200         }
201         $dir_exists = is_dir ( $basepath ) ;
202
203         $mod_strings = array ( ) ;
204
205         if ($dir_exists)
206         {
207             if (file_exists ( $filename ))
208             {
209                 // obtain $mod_strings
210                 include ($filename) ;
211             }else if($forRelationshipLabel){
212                 $fh = fopen ($filename, 'a');
213                 fclose($fh);
214             }
215         } else
216         {
217             return false ;
218         }
219
220                 $changed = false ;
221
222         //$charset = (isset($app_strings['LBL_CHARSET'])) ? $app_strings['LBL_CHARSET'] : $GLOBALS['sugar_config']['default_charset'] ;
223
224                 foreach ( $labels as $key => $value )
225                 {
226             if (! isset ( $mod_strings [ $key ] ) || strcmp ( $value, $mod_strings [ $key ] ) != 0)
227                     {
228                     $mod_strings [$key] = to_html(strip_tags(from_html($value))); // must match encoding used in view.labels.php
229                         $changed = true ;
230                     }
231                 }
232
233                 if ($changed)
234                 {
235             $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels: writing new mod_strings to $filename" ) ;
236                     $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels: mod_strings=".print_r($mod_strings,true) ) ;
237             if (! write_array_to_file ( "mod_strings", $mod_strings, $filename ))
238                     {
239                 $GLOBALS [ 'log' ]->fatal ( "Could not write $filename" ) ;
240                     } else
241                     {
242                         // if we have a cache to worry about, then clear it now
243                 if ($deployedModule)
244                         {
245                             SugarCache::cleanOpcodes();
246                             $GLOBALS [ 'log' ]->debug ( "PaserLabel->addLabels: clearing language cache" ) ;
247                             $cache_key = "module_language." . $language . $moduleName ;
248                             sugar_cache_clear ( $cache_key ) ;
249                             LanguageManager::clearLanguageCache ( $moduleName, $language ) ;
250                         }
251                     }
252                 }
253
254         return true ;
255     }
256
257     /**
258      * Takes in the request params from a save request and processes
259      * them for the save.
260      * @param $metadata
261      * @param string $language      Language key, for example 'en_us'
262      */
263     function handleSaveRelationshipLabels ($metadata , $language)
264         {
265         foreach ( $metadata as $definition )
266             {
267                 $labels = array();
268                 $labels[$definition [ 'system_label' ]] = $definition [ 'display_label' ];
269                 self::addLabels ( $language, $labels, $definition [ 'module' ],null,true );
270             }
271         }
272
273     function addLabelsToAllLanguages($labels)
274             {
275         $langs = get_languages();
276         foreach($langs as $lang_key => $lang_display)
277         {
278                 $this->addLabels($lang_key, $labels, $this->moduleName);
279         }
280     }
281 }
282
283 ?>