]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.label.php
Release 6.2.2
[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-2011 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 )) ] = remove_xss(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      * Add a set of labels to the language pack for a module, deployed or undeployed
79      * @param string $language      Language key, for example 'en_us'
80      * @param array $labels         The labels to add in the form of an array of System label => Display label pairs
81      * @param string $moduleName    Name of the module to which to add these labels
82      * @param string $packageName   If module is undeployed, name of the package to which it belongs
83      */
84     static function addLabels ($language , $labels , $moduleName , $basepath = null, $forRelationshipLabel = false)
85     {
86
87         $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels($language, \$labels, $moduleName, $basepath );" ) ;
88         $GLOBALS [ 'log' ]->debug ( "\$labels:" . print_r ( $labels, true ) ) ;
89         
90         $deployedModule = false ;
91         if (is_null ( $basepath ))
92         {
93             $deployedModule = true ;
94             $basepath = "custom/modules/$moduleName/language" ;
95             if($forRelationshipLabel){
96                 $basepath = "custom/modules/$moduleName/Ext/Language" ;
97             }
98             if (! is_dir ( $basepath ))
99             {
100                 mkdir_recursive($basepath);
101             }
102         }
103
104         $filename = "$basepath/$language.lang.php" ;
105         if($forRelationshipLabel){
106                 $filename = "$basepath/$language.lang.ext.php" ;
107         }
108         $dir_exists = is_dir ( $basepath ) ;
109
110         $mod_strings = array ( ) ;
111
112         if ($dir_exists)
113         {
114             if (file_exists ( $filename ))
115             {
116                 // obtain $mod_strings
117                 include ($filename) ;
118             }else if($forRelationshipLabel){
119                 $fh = fopen ($filename, 'a');
120                 fclose($fh);
121             }
122         } else
123         {
124             return false ;
125         }
126
127                 $changed = false ;
128
129         //$charset = (isset($app_strings['LBL_CHARSET'])) ? $app_strings['LBL_CHARSET'] : $GLOBALS['sugar_config']['default_charset'] ;
130
131                 foreach ( $labels as $key => $value )
132                 {
133             if (! isset ( $mod_strings [ $key ] ) || strcmp ( $value, $mod_strings [ $key ] ) != 0)
134                     {
135                 $mod_strings [$key] = html_entity_decode_utf8($value, ENT_QUOTES ); // must match encoding used in view.labels.php
136                         $changed = true ;
137                     }
138                 }
139
140                 if ($changed)
141                 {
142             $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels: writing new mod_strings to $filename" ) ;
143                     $GLOBALS [ 'log' ]->debug ( "ParserLabel->addLabels: mod_strings=".print_r($mod_strings,true) ) ;            
144             if (! write_array_to_file ( "mod_strings", $mod_strings, $filename ))
145                     {
146                 $GLOBALS [ 'log' ]->fatal ( "Could not write $filename" ) ;
147                     } else
148                     {
149                         // if we have a cache to worry about, then clear it now
150                 if ($deployedModule)
151                         {
152                             $GLOBALS [ 'log' ]->debug ( "PaserLabel->addLabels: clearing language cache" ) ;
153                             $cache_key = "module_language." . $language . $moduleName ;
154                             sugar_cache_clear ( $cache_key ) ;
155                             LanguageManager::clearLanguageCache ( $moduleName, $language ) ;
156                         }
157                     }
158                 }
159
160         return true ;
161     }
162     
163     /**
164      * Takes in the request params from a save request and processes
165      * them for the save.
166      * @param $metadata 
167      * @param string $language      Language key, for example 'en_us'
168      */
169     function handleSaveRelationshipLabels ($metadata , $language)
170         {
171         foreach ( $metadata as $definition )
172             {
173                 $labels = array();
174                 $labels[$definition [ 'system_label' ]] = $definition [ 'display_label' ];
175                 self::addLabels ( $language, $labels, $definition [ 'module' ],null,true );
176             }
177         }
178
179     function addLabelsToAllLanguages($labels)
180             {
181         $langs = get_languages();
182         foreach($langs as $lang_key => $lang_display)
183         {
184                 $this->addLabels($lang_key, $labels, $this->moduleName);
185         }
186     }
187 }
188
189 ?>