Cheetah
bottom_menu_compose.php
Go to the documentation of this file.
1 <?php
2 
8 define ('CH_SECURITY_EXCEPTIONS', true);
10 $aChSecurityExceptions[] = 'POST.Link';
11 $aChSecurityExceptions[] = 'REQUEST.Link';
12 
13 require_once( '../inc/header.inc.php' );
14 require_once( CH_DIRECTORY_PATH_INC . 'profiles.inc.php' );
15 require_once( CH_DIRECTORY_PATH_INC . 'design.inc.php' );
16 require_once( CH_DIRECTORY_PATH_INC . 'admin_design.inc.php' );
17 require_once( CH_DIRECTORY_PATH_INC . 'utils.inc.php' );
18 require_once( CH_DIRECTORY_PATH_INC . 'languages.inc.php' );
19 
20  // Check if administrator is logged in. If not display login form.
21 $logged['admin'] = member_auth(1, true, true);
22 
23 $GLOBALS['oAdmTemplate']->addJsTranslation(array(
24  '_adm_mbuilder_Sorry_could_not_insert_object',
25  '_adm_mbuilder_This_items_are_non_editable'
26 ));
27 
28 ch_import('ChWsbMenuBottom');
30 
31 $sResponce = null;
32 
33 // ** FOR 'AJAX' REQUESTS ;
34 if(ch_get('action') !== false) {
35  switch(ch_get('action')) {
36  case 'edit_form':
37  $id = (int)ch_get('id');
38 
39  header('Content-Type: text/html; charset=utf-8');
40 
41  $aItem = db_assoc_arr( "SELECT * FROM `" . $oMenu->sDbTable . "` WHERE `ID` = $id", 0 );
42  $sResponce = ($aItem) ? showEditForm($aItem) : echoMenuEditMsg('Error', 'red');
43  break;
44  case 'create_item':
45  $sResponce = createNewElement($_POST['type'], (int)$_POST['source']);
46  break;
47  case 'deactivate_item':
48  $res = db_res( "UPDATE `" . $oMenu->sDbTable . "` SET `Active`='0' WHERE `ID`=" . (int)ch_get('id') );
50  break;
51  case 'save_item':
52  $id = (int)$_POST['id'];
53  if(!$id) {
54  $sResponce = echoMenuEditMsg( 'Error', 'red' );
55  break;
56  }
57 
58  $aItemFields = array('Name', 'Caption', 'Icon', 'Link', 'Script', 'Target');
59 
60  $aItem = array();
61  foreach($aItemFields as $field)
62  $aItem[$field] = isset($_POST[$field]) ? $_POST[$field] : null;
63 
64  $aVis = array();
65  if( (int)$_POST['Visible_non'] )
66  $aVis[] = 'non';
67  if( (int)$_POST['Visible_memb'] )
68  $aVis[] = 'memb';
69 
70  $aItem['Visible'] = implode( ',', $aVis );
71  $aItem['Target'] = $_POST['Target'] == '_blank' ? '_blank' : '';
72 
73  $aResult = saveItem($id, $aItem);
74  updateLangFile( $_POST['Caption'], $_POST['LangCaption'] );
75 
76  $aResult['message'] = MsgBox($aResult['message']);
77  echo json_encode($aResult);
78  exit;
79 
80  break;
81 
82  case 'delete_item':
83  $id = (int)$_POST['id'];
84  if( !$id ) {
85  $sResponce = 'Item ID is not specified';
86  } else {
87  $aItem = db_arr( "SELECT `Deletable` FROM `" . $oMenu->sDbTable . "` WHERE `ID` = $id" );
88  if( !$aItem ) {
89  $sResponce = 'Item not found';
90  } else if( !(int)$aItem['Deletable'] ) {
91  $sResponce = 'Item is non-deletable';
92  } else {
93  $res = db_res( "DELETE FROM `" . $oMenu->sDbTable . "` WHERE `ID` = $id" );
94  $sResponce = ( db_affected_rows($res) ) ? 'OK' : 'Couldn\'t delete the item';
95  }
96  }
97  break;
98 
99  case 'save_orders':
100  saveOrders( ch_get('top'), ch_get('custom'));
101  $sResponce = 'OK';
102  break;
103  }
104 
105  $oMenu -> compile();
106  echo $sResponce;
107  exit;
108 }
109 
110 // generate all active menu items ;
111 $sTopQuery = "SELECT `ID`, `Name`, `Movable` FROM `" . $oMenu->sDbTable . "` WHERE `Active`='1' ORDER BY `Order`";
113 
114 $sAllQuery = "SELECT `ID`, `Name` FROM `" . $oMenu->sDbTable . "` WHERE `Clonable`='1' OR (`Clonable`='0' AND `Active`='0') ORDER BY `Name`";
116 
118  <script type=\"text/javascript\">
119  topParentID = 'menu_app_wrapper';
120  parserUrl = '" . $GLOBALS['site']['url_admin'] . "bottom_menu_compose.php?';
121 
122  allowNewItem = true;
123  allowAddToTop = true;
124  allowAddToCustom = false;
125  iInactivePerRow = 5;
126  sendSystemOrder = false;
127 
128  aCoords = {};
129  aCoords['startX'] = 6;
130  aCoords['startY'] = 24;
131  aCoords['width'] = 117;
132  aCoords['height'] = 28;
133  aCoords['diffX'] = 122;
134  aCoords['diffY'] = 32;
135 
136  aTopItems = {};
137  aCustomItems = {};
138  aSystemItems = {};
139  aAllItems = {};
140 ";
141 
143 while(($aTopItem = $rTopItems->fetch()) !== false) {
144  $sComposerInit .= "
145 
146  aTopItems[$iIndex] = [{$aTopItem['ID']}, '" . ch_js_string( $aTopItem['Name'], CH_ESCAPE_STR_APOS ) . "', {$aTopItem['Movable']}];
147  aCustomItems[$iIndex] = {};";
148 
149  $iIndex++;
150 }
151 
152 $sComposerInit .= "\n";
153 while(($aAllItem = $rAllItems->fetch()) !== false) {
154  $sComposerInit .= "
155  aAllItems['{$aAllItem['ID']} '] = '" . ch_js_string( $aAllItem['Name'], CH_ESCAPE_STR_APOS ) . "';";
156 }
157  $sComposerInit .= "
158  </script>
159 ";
160 
162 $_page = array(
163  'name_index' => $iNameIndex,
164  'css_name' => array('menu_compose.css', 'forms_adv.css'),
165  'js_name' => array('menu_compose.js', 'ChWsbMenu.js'),
166  'header' => _t('_adm_bmbuilder_page_title')
167 );
168 
169 $sContent = $GLOBALS['oAdmTemplate']->parseHtmlByName('menu_compose.html', array(
170  'extra_js' => $sComposerInit
171 ));
172 
173 $_page_cont[$iNameIndex]['controls'] = '';
174 $_page_cont[$iNameIndex]['page_main_code'] = DesignBoxAdmin(_t('_adm_bmbuilder_box_title'), $sContent);
175 
176 PageCodeAdmin();
177 
178 function showEditForm( $aItem )
179 {
180  $aForm = array(
181  'form_attrs' => array(
182  'id' => 'formItemEdit',
183  'name' => 'formItemEdit',
184  'action' => $GLOBALS['site']['url_admin'] . 'bottom_menu_compose.php',
185  'method' => 'post',
186  'enctype' => 'multipart/form-data',
187  ),
188  'inputs' => array (
189  'Name' => array(
190  'type' => 'text',
191  'name' => 'Name',
192  'caption' => _t('_adm_mbuilder_System_Name'),
193  'value' => $aItem['Name'],
194  'attrs' => array()
195  ),
196  'Caption' => array(
197  'type' => 'text',
198  'name' => 'Caption',
199  'caption' => _t('_adm_mbuilder_Language_Key'),
200  'value' => $aItem['Caption'],
201  'attrs' => array()
202  ),
203  'LangCaption' => array(
204  'type' => 'text',
205  'name' => 'LangCaption',
206  'caption' => _t('_adm_mbuilder_Default_Name'),
207  'value' => _t( $aItem['Caption'] ),
208  'attrs' => array()
209  ),
210  'Link' => array(
211  'type' => 'text',
212  'name' => 'Link',
213  'caption' => _t('_URL'),
214  'value' => htmlspecialchars_adv( $aItem['Link'] ),
215  'attrs' => array()
216  ),
217  'Script' => array(
218  'type' => 'text',
219  'name' => 'Script',
220  'caption' => _t('_adm_mbuilder_script'),
221  'value' => htmlspecialchars_adv( $aItem['Script'] ),
222  'attrs' => array()
223  ),
224  'Icon' => array(
225  'type' => 'text',
226  'name' => 'Icon',
227  'caption' => _t('_adm_mbuilder_icon'),
228  'value' => htmlspecialchars_adv( $aItem['Icon'] ),
229  'attrs' => array()
230  ),
231  'Target' => array(
232  'type' => 'radio_set',
233  'name' => 'Target',
234  'caption' => _t('_adm_mbuilder_Target_Window'),
235  'value' => $aItem['Target'] == '_blank' ? '_blank' : '_self',
236  'values' => array(
237  '_self' => _t('_adm_mbuilder_Same'),
238  '_blank' => _t('_adm_mbuilder_New')
239  ),
240  'attrs' => array()
241  ),
242  'Visible' => array(
243  'type' => 'checkbox_set',
244  'name' => 'Visible',
245  'caption' => _t('_adm_mbuilder_Visible_for'),
246  'value' => array(),
247  'values' => array(
248  'non' => _t('_Guest'),
249  'memb' => _t('_Member')
250  ),
251  'attrs' => array()
252  ),
253  'submit' => array(
254  'type' => 'input_set',
255  array(
256  'type' => 'button',
257  'name' => 'save',
258  'value' => _t('_Save Changes'), //if( $aItem['Editable'] )
259  'attrs' => array(
260  'onclick' => 'javascript:saveItem(' . $aItem['ID'] . ');'
261  )
262  ),
263  array(
264  'type' => 'button',
265  'name' => 'delete',
266  'value' => _t('_Delete'), //if( $aItem['Deletable'] )
267  'attrs' => array(
268  'onclick' => 'javascript:deleteItem(' . $aItem['ID'] . ');'
269  )
270  )
271  ),
272  )
273  );
274 
275  foreach($aForm['inputs'] as $sKey => $aInput)
276  if(in_array($aInput['type'], array('text', 'checkbox')) && !$aItem['Editable'])
277  $aForm['inputs'][$sKey]['attrs']['disabled'] = "disabled";
278 
279  if(strpos($aItem['Visible'], 'non') !== false)
280  $aForm['inputs']['Visible']['value'][] = 'non';
281  if(strpos($aItem['Visible'], 'memb') !== false)
282  $aForm['inputs']['Visible']['value'][] = 'memb';
283 
285  return PopupBox('mmc_edit_popup', _t('_adm_mbuilder_edit_item')
286  , $GLOBALS['oAdmTemplate']->parseHtmlByName('design_box_content.html'
287  , array('content' => $oForm->getCode() . LoadingBox('formItemEditLoading'))));
288 }
289 
296 function createNewElement( $type, $source)
297 {
298  global $oMenu;
299 
300  if( $source ) {
301  $sourceActive = db_value( "SELECT `Active` FROM `" . $oMenu->sDbTable . "` WHERE `ID`=$source" );
302  if( !$sourceActive ) {
303  //convert to active
304  db_res( "UPDATE `" . $oMenu->sDbTable . "` SET `Active`='1' WHERE `ID`=$source" );
305  $newID = $source;
306  } else {
307  //create from source
308  db_res( "INSERT INTO `" . $oMenu->sDbTable . "`(`Name`, `Caption`, `Icon`, `Link`, `Script`, `Target`, `Visible`, `Movable`, `Clonable`, `Editable`, `Deletable`)
309  SELECT `Caption`, `Name`, `Icon`, `Link`, `Script`, `Target`, `Visible`, `Movable`, '0', `Editable`, '1' FROM `" . $oMenu->sDbTable . "` WHERE `ID`=$source" );
310  $newID = db_last_id();
311  }
312  } else {
313  //create new
314  db_res( "INSERT INTO `" . $oMenu->sDbTable . "` (`Name`) VALUES ('NEW ITEM')" );
315  $newID = db_last_id();
316  }
317 
318  return $newID;
319 }
320 
321 function echoMenuEditMsg( $text, $color = 'black' )
322 {
323  return <<<HTML
324  <div style="color:{$color};text-align:center;">{$text}</div>
325 HTML;
326 }
327 
335 function saveItem( $id, $aItem)
336 {
338 
339  $aOldItem = db_arr( "SELECT * FROM `" . $oMenu->sDbTable . "` WHERE `ID` = $id" );
340 
341  if(!$aOldItem) {
342  return array( 'code' => 2, 'message' => _t('_adm_mbuilder_Item_not_found') );
343  }
344 
345  if( (int) $aOldItem['Editable'] != 1 ) {
346  return array('code' => 3, 'message' => _t('_adm_mbuilder_Item_is_non_editable') );
347  }
348 
349  $sQuerySet = '';
350  foreach( $aItem as $field => $value )
351  $sQuerySet .= ", `$field`='" . process_db_input( $value ) ."'";
352 
353  $sQuerySet = substr( $sQuerySet, 1 );
354 
355  $sQuery = "UPDATE `" . $oMenu->sDbTable . "` SET $sQuerySet WHERE `ID` = $id";
356  db_res( $sQuery );
357 
358  $oMenu -> compile();
359  return array('code' => 0, 'message' => _t('_Saved'), 'timer' => 3);
360 }
361 
362 function updateLangFile( $key, $string )
363 {
364  $key = preg_replace( '|\{([^\}]+)\}|', '', $key);
365  if (!$key)
366  return;
367 
368  $langName = getParam( 'lang_default' );
369  $langID = db_value( "SELECT `ID` FROM `sys_localization_languages` WHERE `Name` = '" . process_db_input( $langName ) . "'" );
370 
371  $keyID = db_value( "SELECT `ID` FROM `sys_localization_keys` WHERE `Key` = '" . process_db_input( $key ) . "'" );
372  if( $keyID ) {
373  db_res( "UPDATE `sys_localization_strings` SET `String` = '" .process_db_input( $string ) . "' WHERE `IDKey`=$keyID AND `IDLanguage`=$langID" );
374  } else {
375  db_res( "INSERT INTO `sys_localization_keys` SET `IDCategory` = 2, `Key` = '" . process_db_input( $key ) . "'" );
376  db_res( "INSERT INTO `sys_localization_strings` SET `IDKey` = " . db_last_id() . ", `IDLanguage` = $langID, `String` = '" .process_db_input( $string ) . "'" );
377  }
378 
379  compileLanguage($langID);
380 }
381 
388 function saveOrders( $sTop, $aCustom)
389 {
390  global $oMenu;
391 
392  db_res("UPDATE `" . $oMenu->sDbTable . "` SET `Order`='0' WHERE 1");
393 
394  $sTop = trim($sTop, ' ,');
395  $aTopIDs = explode(',', $sTop);
396 
397  foreach($aTopIDs as $iOrd => $iID) {
398  $iID = trim($iID, ' ,');
399  $iID = (int)$iID;
400 
401  if(!$iID)
402  continue;
403 
404  db_res("UPDATE `" . $oMenu->sDbTable . "` SET `Order`='" . $iOrd . "' WHERE `ID`='" . $iID ."'");
405  }
406 }
$sComposerInit
$sComposerInit
Definition: bottom_menu_compose.php:117
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
$sAllQuery
$sAllQuery
Definition: bottom_menu_compose.php:114
ChTemplFormView
Definition: ChTemplFormView.php:11
db_assoc_arr
db_assoc_arr($query, $bindings=[])
Definition: db.inc.php:86
createNewElement
createNewElement( $type, $source)
Definition: bottom_menu_compose.php:296
LoadingBox
LoadingBox($sName)
Definition: design.inc.php:185
$sResponce
$sResponce
Definition: bottom_menu_compose.php:31
MsgBox
MsgBox($sText, $iTimer=0)
Definition: design.inc.php:175
$oMenu
$oMenu
Definition: bottom_menu_compose.php:29
$sTopQuery
if(ch_get('action') !==false) $sTopQuery
Definition: bottom_menu_compose.php:111
ch_js_string
ch_js_string($mixedInput, $iQuoteType=CH_ESCAPE_STR_AUTO)
Definition: utils.inc.php:1294
compileLanguage
compileLanguage($langID=0)
Definition: languages.inc.php:301
$rTopItems
$rTopItems
Definition: bottom_menu_compose.php:112
$_page
$_page
Definition: bottom_menu_compose.php:162
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
$aResult
$aResult
Definition: index.php:19
ch_get
ch_get($sName)
Definition: utils.inc.php:1664
php
echoMenuEditMsg
echoMenuEditMsg( $text, $color='black')
Definition: bottom_menu_compose.php:321
$oForm
$oForm
Definition: host_tools.php:42
DesignBoxAdmin
DesignBoxAdmin($sTitle, $sContent, $mixedTopItems='', $sBottomItems='', $iIndex=1)
Definition: admin_design.inc.php:50
db_arr
db_arr($query, $bindings=[])
Definition: db.inc.php:76
exit
exit
Definition: cart.php:21
$iIndex
$iIndex
Definition: bottom_menu_compose.php:142
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
$logged
$logged['admin']
Definition: bottom_menu_compose.php:21
showEditForm
showEditForm( $aItem)
Definition: bottom_menu_compose.php:178
ChWsbMenuBottom
Definition: ChWsbMenuBottom.php:26
saveItem
saveItem( $id, $aItem)
Definition: bottom_menu_compose.php:335
htmlspecialchars_adv
htmlspecialchars_adv($string)
Definition: utils.inc.php:302
db_last_id
db_last_id()
Definition: db.inc.php:47
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
PageCodeAdmin
PageCodeAdmin($oTemplate=null)
Definition: admin_design.inc.php:45
$sContent
$sContent
Definition: bottom_menu_compose.php:169
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
member_auth
member_auth($member=0, $error_handle=true, $bAjx=false)
Definition: admin.inc.php:262
db_affected_rows
db_affected_rows($oStmt=null)
Definition: db.inc.php:56
$iNameIndex
$iNameIndex
Definition: bottom_menu_compose.php:161
updateLangFile
updateLangFile( $key, $string)
Definition: bottom_menu_compose.php:362
saveOrders
saveOrders( $sTop, $aCustom)
Definition: bottom_menu_compose.php:388
$aChSecurityExceptions
$aChSecurityExceptions
Definition: bottom_menu_compose.php:9
CH_ESCAPE_STR_APOS
const CH_ESCAPE_STR_APOS
escape apostrophes only, for js strings enclosed in apostrophes, for use in
Definition: utils.inc.php:33
db_res
db_res($query, $bindings=[])
Definition: db.inc.php:39
PopupBox
PopupBox($sName, $sTitle, $sContent, $aActions=array())
Definition: design.inc.php:189
HTML
while it may not seem that empty elements contain useful they can alter the layout of a document given appropriate styling This directive is most useful when you are processing machine generated HTML
Definition: AutoFormat.RemoveEmpty.txt:29
db_value
db_value($query, $bindings=[], $error_checking=true, $index=0)
Definition: db.inc.php:98
$aForm
$aForm
Definition: forgot.php:43
$rAllItems
$rAllItems
Definition: bottom_menu_compose.php:115
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$_page_cont
$_page_cont[$iNameIndex]['controls']
Definition: bottom_menu_compose.php:173
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10