]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.label.php
Release 6.5.0
[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-2012 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             return self::addLabels ( $language, $labels, $this->moduleName ) ;
74         }
75     }
76
77     /*
78      * Remove a label from the language pack for a module
79      * @param string $language      Language key, for example 'en_us'
80      * @param string $label         The label to remove
81      * @param string $labelvalue    The value of the label to remove
82      * @param string $moduleName    Name of the module to which to add these labels
83      * @param string $basepath      base path of the language file
84      * @param string $forRelationshipLabel      whether this is a relationship label
85      */
86     static function removeLabel($language, $label, $labelvalue, $moduleName, $basepath = null, $forRelationshipLabel = false) {
87         $GLOBALS [ 'log' ]->debug ( "ParserLabel->removeLabels($language, \$label, \$labelvalue, $moduleName, $basepath );" ) ;
88         if (is_null ( $basepath ))
89         {
90             $deployedModule = true ;
91             $basepath = "custom/modules/$moduleName/language" ;
92             if($forRelationshipLabel){
93                 $basepath = "custom/modules/$moduleName/Ext/Language" ;
94             }
95             if (! is_dir ( $basepath ))
96             {
97                 $GLOBALS ['log']->debug("$basepath is not a directory.");
98                 return false;
99             }
100         }
101
102         $filename = "$basepath/$language.lang.php" ;
103         if($forRelationshipLabel){
104                 $filename = "$basepath/$language.lang.ext.php" ;
105         }
106
107         $dir_exists = is_dir ( $basepath ) ;
108
109         $mod_strings = array ( ) ;
110
111         if ($dir_exists)
112         {
113             if (file_exists ($filename))
114             {
115                 // obtain $mod_strings
116                 include ($filename) ;
117             } else {
118                 $GLOBALS ['log']->debug("file $filename does not exist.");
119                 return false;
120             }
121         } else {
122             $GLOBALS ['log']->debug("directory $basepath does not exist.");
123             return false ;
124         }
125
126         $changed = false ;
127
128         if (isset($mod_strings[$label]) && $mod_strings[$label]==$labelvalue) {
129             unset($mod_strings[$label]);
130             $changed = true;
131         }
132
133         if ($changed)
134         {
135             if (! write_array_to_file ( "mod_strings", $mod_strings, $filename )) {
136                 $GLOBALS [ 'log' ]->fatal ( "Could not write $filename" ) ;
137             } else {
138                 // if we have a cache to worry about, then clear it now
139                 if ($deployedModule) {
140                     $GLOBALS ['log']->debug ( "PaserLabel->addLabels: clearing language cache" ) ;
141                     $cache_key = "module_language." . $language . $moduleName ;
142                     sugar_cache_clear ( $cache_key ) ;
143                     LanguageManager::clearLanguageCache ( $moduleName, $language ) ;
144                 }
145             }
146         }
147
148         return true ;
149     }
150
151     /*
152      * Add a set of labels to the language pack for a module, deployed or undeployed
153      * @param string $language      Language key, for example 'en_us'
154      * @param array $labels         The labels to add in the form of an array of System label => Display label pairs
155      * @param string $moduleName    Name of the module to which to add these labels
156      * @param string $packageName   If module is undeployed, name of the package to which it belongs
157      */
158     static function addLabels ($language , $labels , $moduleName , $basepath = null, $forRelationshipLabel = false)
159     {
160
161         $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels($language, \$labels, $moduleName, $basepath );" ) ;
162         $GLOBALS [ 'log' ]->debug ( "\$labels:" . print_r ( $labels, true ) ) ;
163
164         $deployedModule = false ;
165         if (is_null ( $basepath ))
166         {
167             $deployedModule = true ;
168             $basepath = "custom/modules/$moduleName/language" ;
169             if($forRelationshipLabel){
170                 $basepath = "custom/modules/$moduleName/Ext/Language" ;
171             }
172             if (! is_dir ( $basepath ))
173             {
174                 mkdir_recursive($basepath);
175             }
176         }
177
178         $filename = "$basepath/$language.lang.php" ;
179         if($forRelationshipLabel){
180                 $filename = "$basepath/$language.lang.ext.php" ;
181         }
182         $dir_exists = is_dir ( $basepath ) ;
183
184         $mod_strings = array ( ) ;
185
186         if ($dir_exists)
187         {
188             if (file_exists ( $filename ))
189             {
190                 // obtain $mod_strings
191                 include ($filename) ;
192             }else if($forRelationshipLabel){
193                 $fh = fopen ($filename, 'a');
194                 fclose($fh);
195             }
196         } else
197         {
198             return false ;
199         }
200
201                 $changed = false ;
202
203         //$charset = (isset($app_strings['LBL_CHARSET'])) ? $app_strings['LBL_CHARSET'] : $GLOBALS['sugar_config']['default_charset'] ;
204
205                 foreach ( $labels as $key => $value )
206                 {
207             if (! isset ( $mod_strings [ $key ] ) || strcmp ( $value, $mod_strings [ $key ] ) != 0)
208                     {
209                     $mod_strings [$key] = to_html(strip_tags(from_html($value))); // must match encoding used in view.labels.php
210                         $changed = true ;
211                     }
212                 }
213
214                 if ($changed)
215                 {
216             $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels: writing new mod_strings to $filename" ) ;
217                     $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels: mod_strings=".print_r($mod_strings,true) ) ;
218             if (! write_array_to_file ( "mod_strings", $mod_strings, $filename ))
219                     {
220                 $GLOBALS [ 'log' ]->fatal ( "Could not write $filename" ) ;
221                     } else
222                     {
223                         // if we have a cache to worry about, then clear it now
224                 if ($deployedModule)
225                         {
226                             SugarCache::cleanOpcodes();
227                             $GLOBALS [ 'log' ]->debug ( "PaserLabel->addLabels: clearing language cache" ) ;
228                             $cache_key = "module_language." . $language . $moduleName ;
229                             sugar_cache_clear ( $cache_key ) ;
230                             LanguageManager::clearLanguageCache ( $moduleName, $language ) ;
231                         }
232                     }
233                 }
234
235         return true ;
236     }
237
238     /**
239      * Takes in the request params from a save request and processes
240      * them for the save.
241      * @param $metadata
242      * @param string $language      Language key, for example 'en_us'
243      */
244     function handleSaveRelationshipLabels ($metadata , $language)
245         {
246         foreach ( $metadata as $definition )
247             {
248                 $labels = array();
249                 $labels[$definition [ 'system_label' ]] = $definition [ 'display_label' ];
250                 self::addLabels ( $language, $labels, $definition [ 'module' ],null,true );
251             }
252         }
253
254     function addLabelsToAllLanguages($labels)
255             {
256         $langs = get_languages();
257         foreach($langs as $lang_key => $lang_display)
258         {
259                 $this->addLabels($lang_key, $labels, $this->moduleName);
260         }
261     }
262 }
263
264 ?>