]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.dropdown.php
Release 6.5.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-2012 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         if(is_array($temp))
65         {
66             foreach ( $temp as $item )
67             {
68                 $dropdown[ SugarCleaner::stripTags(from_html($item [ 0 ]), false) ] = SugarCleaner::stripTags(from_html($item [ 1 ]), false) ;
69             }
70         }
71                 if(array_key_exists($emptyMarker, $dropdown)){
72             $output=array();
73             foreach($dropdown as $key => $value){
74                 if($emptyMarker===$key)
75                     $output['']='';
76                 else
77                     $output[$key]=$value;
78                 }
79             $dropdown=$output;
80                 }
81
82                 if($type != 'studio'){
83                         $mb = new ModuleBuilder();
84                         $module = $mb->getPackageModule($params['view_package'], $params['view_module']);
85                         $this->synchMBDropDown($dropdown_name, $dropdown, $selected_lang, $module);
86                         //Can't use synch on selected lang as we want to overwrite values, not just keys
87                         $module->mblanguage->appListStrings[$selected_lang.'.lang.php'][$dropdown_name] = $dropdown;
88                         $module->mblanguage->save($module->key_name); // tyoung - key is required parameter as of
89                 }else{
90                         $contents = return_custom_app_list_strings_file_contents($selected_lang);
91                         $my_list_strings = return_app_list_strings_language($selected_lang);
92                         if($selected_lang == $GLOBALS['current_language']){
93                    $GLOBALS['app_list_strings'][$dropdown_name] = $dropdown;
94                 }
95                         //write to contents
96                         $contents = str_replace("?>", '', $contents);
97                         if(empty($contents))$contents = "<?php";
98                 //add new drop down to the bottom
99                 if(!empty($params['use_push'])){
100                         //this is for handling moduleList and such where nothing should be deleted or anything but they can be renamed
101                         foreach($dropdown as $key=>$value){
102                                 //only if the value has changed or does not exist do we want to add it this way
103                                 if(!isset($my_list_strings[$dropdown_name][$key]) || strcmp($my_list_strings[$dropdown_name][$key], $value) != 0 ){
104                                         //clear out the old value
105                                         $pattern_match = '/\s*\$app_list_strings\s*\[\s*\''.$dropdown_name.'\'\s*\]\[\s*\''.$key.'\'\s*\]\s*=\s*[\'\"]{1}.*?[\'\"]{1};\s*/ism';
106                                         $contents = preg_replace($pattern_match, "\n", $contents);
107                                         //add the new ones
108                                         $contents .= "\n\$GLOBALS['app_list_strings']['$dropdown_name']['$key']=" . var_export_helper($value) . ";";
109                                 }
110                         }
111                 }else{
112                         //Now synch up the keys in other langauges to ensure that removed/added Drop down values work properly under all langs.
113                         $this->synchDropDown($dropdown_name, $dropdown, $selected_lang, $dir);
114                         $contents = $this->getNewCustomContents($dropdown_name, $dropdown, $selected_lang);
115                 }
116                     if(!empty($dir) && !is_dir($dir))
117                     {
118                         $continue = mkdir_recursive($dir);
119                     }
120                         save_custom_app_list_strings_contents($contents, $selected_lang, $dir);
121                 }
122                 sugar_cache_reset();
123                 clearAllJsAndJsLangFilesWithoutOutput();
124     }
125
126     /**
127          * function synchDropDown
128          *      Ensures that the set of dropdown keys is consistant accross all languages.
129          *
130          * @param $dropdown_name The name of the dropdown to be synched
131          * @param $dropdown array The dropdown currently being saved
132          * @param $selected_lang String the language currently selected in Studio/MB
133          * @param $saveLov String the path to the directory to save the new lang file in.
134          */
135     function synchDropDown($dropdown_name, $dropdown, $selected_lang, $saveLoc) {
136                 $allLanguages =  get_languages();
137         foreach ($allLanguages as $lang => $langName) {
138                 if ($lang != $selected_lang) {
139                         $listStrings = return_app_list_strings_language($lang);
140                         $langDropDown = array();
141                         if (isset($listStrings[$dropdown_name]) && is_array($listStrings[$dropdown_name]))
142                         {
143                                 $langDropDown = $this->synchDDKeys($dropdown, $listStrings[$dropdown_name]);
144                         } else
145                         {
146                                 //if the dropdown does not exist in the language, justt use what we have.
147                                 $langDropDown = $dropdown;
148                         }
149                         $contents = $this->getNewCustomContents($dropdown_name, $langDropDown, $lang);
150                         save_custom_app_list_strings_contents($contents, $lang, $saveLoc);
151                 }
152         }
153     }
154
155     /**
156          * function synchMBDropDown
157          *      Ensures that the set of dropdown keys is consistant accross all languages in a ModuleBuilder Module
158          *
159          * @param $dropdown_name The name of the dropdown to be synched
160          * @param $dropdown array The dropdown currently being saved
161          * @param $selected_lang String the language currently selected in Studio/MB
162          * @param $module MBModule the module to update the languages in
163          */
164     function synchMBDropDown($dropdown_name, $dropdown, $selected_lang, $module) {
165         $selected_lang  = $selected_lang . '.lang.php';
166                 foreach($module->mblanguage->appListStrings as $lang => $listStrings) {
167                         if ($lang != $selected_lang)
168                         {
169                                 $langDropDown = array();
170                                 if (isset($listStrings[$dropdown_name]) && is_array($listStrings[$dropdown_name]))
171                                 {
172                                         $langDropDown = $this->synchDDKeys($dropdown, $listStrings[$dropdown_name]);
173                                 } else
174                         {
175                                 $langDropDown = $dropdown;
176                         }
177                         $module->mblanguage->appListStrings[$lang][$dropdown_name] = $langDropDown;
178                                 $module->mblanguage->save($module->key_name);
179                         }
180                 }
181     }
182
183     private function synchDDKeys($dom, $sub) {
184         //check for extra keys
185         foreach($sub as $key=>$value) {
186                 if (!isset($dom[$key])) {
187                         unset ($sub[$key]);
188                 }
189         }
190         //check for missing keys
191         foreach($dom as $key=>$value) {
192                 if (!isset($sub[$key])) {
193                         $sub[$key] = $value;
194                 }
195         }
196         return $sub;
197     }
198
199     function getPatternMatch($dropdown_name) {
200         return '/\s*\$GLOBALS\s*\[\s*\'app_list_strings\s*\'\s*\]\[\s*\''
201                  . $dropdown_name.'\'\s*\]\s*=\s*array\s*\([^\)]*\)\s*;\s*/ism';
202     }
203
204     function getNewCustomContents($dropdown_name, $dropdown, $lang) {
205         $contents = return_custom_app_list_strings_file_contents($lang);
206         $contents = str_replace("?>", '', $contents);
207                 if(empty($contents))$contents = "<?php";
208         $contents = preg_replace($this->getPatternMatch($dropdown_name), "\n", $contents);
209             $contents .= "\n\$GLOBALS['app_list_strings']['$dropdown_name']=" . var_export_helper($dropdown) . ";";
210             return $contents;
211     }
212 }
213 ?>