]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.dropdown.php
Release 6.2.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / parser.dropdown.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 require_once('modules/ModuleBuilder/parsers/ModuleBuilderParser.php');
40
41  class ParserDropDown extends ModuleBuilderParser {
42
43     /**
44      * Takes in the request params from a save request and processes
45      * them for the save.
46      *
47      * @param REQUEST params  $params
48      */
49     function saveDropDown($params){
50         require_once('modules/Administration/Common.php');
51                 $emptyMarker = translate('LBL_BLANK');
52                 $selected_lang = (!empty($params['dropdown_lang'])?$params['dropdown_lang']:$_SESSION['authenticated_user_language']);
53                 $type = $_REQUEST['view_package'];
54                 $dir = '';
55                 $dropdown_name = $params['dropdown_name'];
56                 $json = getJSONobj();
57
58                 $list_value = str_replace('&quot;&quot;:&quot;&quot;', '&quot;__empty__&quot;:&quot;&quot;', $params['list_value']);
59                 //Bug 21362 ENT_QUOTES- convert single quotes to escaped single quotes.
60                 $temp = $json->decode(html_entity_decode(rawurldecode($list_value), ENT_QUOTES) );
61                 $dropdown = array () ;
62                 // dropdown is received as an array of (name,value) pairs - now extract to name=>value format preserving order
63                 // we rely here on PHP to preserve the order of the received name=>value pairs - associative arrays in PHP are ordered
64                 foreach ( $temp as $item )
65                 {
66                         $dropdown[ $item [0] ] = $item [ 1 ] ;
67                 }
68
69                 if(array_key_exists($emptyMarker, $dropdown)){
70             $output=array();
71             foreach($dropdown as $key => $value){
72                 if($emptyMarker===$key)
73                     $output['']='';
74                 else
75                     $output[$key]=$value;
76                 }
77             $dropdown=$output;
78                 }
79
80                 if($type != 'studio'){
81                         $mb = new ModuleBuilder();
82                         $module =& $mb->getPackageModule($params['view_package'], $params['view_module']);
83                         $this->synchMBDropDown($dropdown_name, $dropdown, $selected_lang, $module);
84                         //Can't use synch on selected lang as we want to overwrite values, not just keys
85                         $module->mblanguage->appListStrings[$selected_lang.'.lang.php'][$dropdown_name] = $dropdown;
86                         $module->mblanguage->save($module->key_name); // tyoung - key is required parameter as of
87                 }else{
88                         $contents = return_custom_app_list_strings_file_contents($selected_lang);
89                         $my_list_strings = return_app_list_strings_language($selected_lang);
90                         if($selected_lang == $GLOBALS['current_language']){
91                    $GLOBALS['app_list_strings'][$dropdown_name] = $dropdown;
92                 }
93                         //write to contents
94                         $contents = str_replace("?>", '', $contents);
95                         if(empty($contents))$contents = "<?php";
96                 //add new drop down to the bottom
97                 if(!empty($params['use_push'])){
98                         //this is for handling moduleList and such where nothing should be deleted or anything but they can be renamed
99                         foreach($dropdown as $key=>$value){
100                                 //only if the value has changed or does not exist do we want to add it this way
101                                 if(!isset($my_list_strings[$dropdown_name][$key]) || strcmp($my_list_strings[$dropdown_name][$key], $value) != 0 ){
102                                         //clear out the old value
103                                         $pattern_match = '/\s*\$app_list_strings\s*\[\s*\''.$dropdown_name.'\'\s*\]\[\s*\''.$key.'\'\s*\]\s*=\s*[\'\"]{1}.*?[\'\"]{1};\s*/ism';
104                                         $contents = preg_replace($pattern_match, "\n", $contents);
105                                         //add the new ones
106                                         $contents .= "\n\$GLOBALS['app_list_strings']['$dropdown_name']['$key']=" . var_export_helper($value) . ";";
107                                 }
108                         }
109                 }else{
110                         //Now synch up the keys in other langauges to ensure that removed/added Drop down values work properly under all langs.
111                         $this->synchDropDown($dropdown_name, $dropdown, $selected_lang, $dir);
112                         $contents = $this->getNewCustomContents($dropdown_name, $dropdown, $selected_lang);
113                 }
114                     if(!empty($dir) && !is_dir($dir))
115                     {
116                         $continue = mkdir_recursive($dir);
117                     }
118                         save_custom_app_list_strings_contents($contents, $selected_lang, $dir);
119                 }
120                 sugar_cache_reset();
121                 clearAllJsAndJsLangFilesWithoutOutput();
122     }
123
124     /**
125          * function synchDropDown
126          *      Ensures that the set of dropdown keys is consistant accross all languages.
127          *
128          * @param $dropdown_name The name of the dropdown to be synched
129          * @param $dropdown array The dropdown currently being saved
130          * @param $selected_lang String the language currently selected in Studio/MB
131          * @param $saveLov String the path to the directory to save the new lang file in.
132          */
133     function synchDropDown($dropdown_name, $dropdown, $selected_lang, $saveLoc) {
134                 $allLanguages =  get_languages();
135         foreach ($allLanguages as $lang => $langName) {
136                 if ($lang != $selected_lang) {
137                         $listStrings = return_app_list_strings_language($lang);
138                         $langDropDown = array();
139                         if (isset($listStrings[$dropdown_name]) && is_array($listStrings[$dropdown_name]))
140                         {
141                                 $langDropDown = $this->synchDDKeys($dropdown, $listStrings[$dropdown_name]);
142                         } else
143                         {
144                                 //if the dropdown does not exist in the language, justt use what we have.
145                                 $langDropDown = $dropdown;
146                         }
147                         $contents = $this->getNewCustomContents($dropdown_name, $langDropDown, $lang);
148                         save_custom_app_list_strings_contents($contents, $lang, $saveLoc);
149                 }
150         }
151     }
152
153     /**
154          * function synchMBDropDown
155          *      Ensures that the set of dropdown keys is consistant accross all languages in a ModuleBuilder Module
156          *
157          * @param $dropdown_name The name of the dropdown to be synched
158          * @param $dropdown array The dropdown currently being saved
159          * @param $selected_lang String the language currently selected in Studio/MB
160          * @param $module MBModule the module to update the languages in
161          */
162     function synchMBDropDown($dropdown_name, $dropdown, $selected_lang, $module) {
163         $selected_lang  = $selected_lang . '.lang.php';
164                 foreach($module->mblanguage->appListStrings as $lang => $listStrings) {
165                         if ($lang != $selected_lang)
166                         {
167                                 $langDropDown = array();
168                                 if (isset($listStrings[$dropdown_name]) && is_array($listStrings[$dropdown_name]))
169                                 {
170                                         $langDropDown = $this->synchDDKeys($dropdown, $listStrings[$dropdown_name]);
171                                 } else
172                         {
173                                 $langDropDown = $dropdown;
174                         }
175                         $module->mblanguage->appListStrings[$lang][$dropdown_name] = $langDropDown;
176                                 $module->mblanguage->save($module->key_name);
177                         }
178                 }
179     }
180
181     private function synchDDKeys($dom, $sub) {
182         //check for extra keys
183         foreach($sub as $key=>$value) {
184                 if (!isset($dom[$key])) {
185                         unset ($sub[$key]);
186                 }
187         }
188         //check for missing keys
189         foreach($dom as $key=>$value) {
190                 if (!isset($sub[$key])) {
191                         $sub[$key] = $value;
192                 }
193         }
194         return $sub;
195     }
196
197     function getPatternMatch($dropdown_name) {
198         return '/\s*\$GLOBALS\s*\[\s*\'app_list_strings\s*\'\s*\]\[\s*\''
199                  . $dropdown_name.'\'\s*\]\s*=\s*array\s*\([^\)]*\)\s*;\s*/ism';
200     }
201
202     function getNewCustomContents($dropdown_name, $dropdown, $lang) {
203         $contents = return_custom_app_list_strings_file_contents($lang);
204         $contents = str_replace("?>", '', $contents);
205                 if(empty($contents))$contents = "<?php";
206         $contents = preg_replace($this->getPatternMatch($dropdown_name), "\n", $contents);
207             $contents .= "\n\$GLOBALS['app_list_strings']['$dropdown_name']=" . var_export_helper($dropdown) . ";";
208             return $contents;
209     }
210 }
211 ?>