Cheetah
nav_menu_compose.php
Go to the documentation of this file.
1 <?php
2 
8 /*
9  * Page for displaying and editing profile fields.
10  */
11 
12 define ('CH_SECURITY_EXCEPTIONS', true);
14 $aChSecurityExceptions[] = 'POST.Link';
15 $aChSecurityExceptions[] = 'REQUEST.Link';
16 
17 require_once( '../inc/header.inc.php' );
18 require_once( CH_DIRECTORY_PATH_INC . 'profiles.inc.php' );
19 require_once( CH_DIRECTORY_PATH_INC . 'design.inc.php' );
20 require_once( CH_DIRECTORY_PATH_INC . 'admin_design.inc.php' );
21 require_once( CH_DIRECTORY_PATH_INC . 'utils.inc.php' );
22 require_once( CH_DIRECTORY_PATH_INC . 'languages.inc.php' );
23 ch_import('ChWsbMenu');
24 
25 // Check if administrator is logged in. If not display login form.
26 $logged['admin'] = member_auth(1, true, true);
27 
28 $GLOBALS['oAdmTemplate']->addJsTranslation(array(
29  '_adm_mbuilder_Sorry_could_not_insert_object',
30  '_adm_mbuilder_This_items_are_non_editable'
31 ));
32 
33 $oMenu = new ChWsbMenu();
34 
35 if(ch_get('action') !== false) {
36  switch(ch_get('action')) {
37  case 'edit_form':
38  $id = (int)ch_get('id');
39 
40  header('Content-Type: text/html; charset=utf-8');
41 
42  $aItem = db_assoc_arr( "SELECT * FROM `sys_menu_top` WHERE `ID` = '{$id}'", 0 );
43  if( $aItem )
44  echo showEditForm( $aItem );
45  else
46  echoMenuEditMsg( _t('_Error occured'), 'red' );
47  exit;
48  case 'create_item':
49  $newID = createNewElement($_POST['type'], (int)$_POST['source']);
50  echo $newID;
51  exit;
52  case 'deactivate_item':
53  $res = db_res( "UPDATE `sys_menu_top` SET `Active`=0 WHERE `ID`=" . (int)ch_get('id'));
54  echo db_affected_rows($res);
55  $oMenu -> compile();
56  exit;
57  case 'save_item':
58  $id = (int)$_POST['id'];
59  if(!$id)
60  $aResult = array('code' => 1, 'message' => _t('_Error occured'));
61  else {
62  $aItemFields = array('Name', 'Caption', 'Link', 'Picture', 'Icon');
63  $aItem = array();
64  foreach( $aItemFields as $field )
65  $aItem[$field] = $_POST[$field];
66 
67  $aVis = array();
68  if( (int)$_POST['Visible_non'] )
69  $aVis[] = 'non';
70  if( (int)$_POST['Visible_memb'] )
71  $aVis[] = 'memb';
72 
73  $aItem['Visible'] = implode( ',', $aVis );
74  $aItem['BQuickLink'] = (int)$_POST['BInQuickLink'] ? '1' : '0';
75  $aItem['Target'] = $_POST['Target'] == '_blank' ? '_blank' : '';
76 
77  $aResult = saveItem( $id, $aItem );
78  }
79 
80  $aResult['message'] = MsgBox($aResult['message']);
81 
82  echo json_encode($aResult);
83  exit;
84  case 'delete_item':
85  $id = (int)$_POST['id'];
86  if( !$id ) {
87  echo _t('_adm_mbuilder_Item_ID_not_specified');
88  exit;
89  }
90 
91  $aItem = db_arr( "SELECT `Deletable` FROM `sys_menu_top` WHERE `ID` = '{$id}'" );
92  if( !$aItem ) {
93  echo _t('_adm_mbuilder_Item_not_found');
94  exit;
95  }
96 
97  if( !(int)$aItem['Deletable'] ) {
98  echo _t('_adm_mbuilder_Item_is_non_deletable');
99  exit;
100  }
101 
102  $res = db_res( "DELETE FROM `sys_menu_top` WHERE `ID` = $id" );
103  if( db_affected_rows($res) )
104  echo 'OK';
105  else
106  echo _t('_adm_mbuilder_Could_not_delete_the_item');
107  $oMenu -> compile();
108  exit;
109  case 'save_orders':
110  saveOrders(ch_get('top'), ch_get('custom'));
111  echo 'OK';
112  exit;
113  }
114 }
115 
116 $sTopQuery = "SELECT `ID`, `Name`, `Movable` FROM `sys_menu_top` WHERE `Active`=1 AND `Type`='top' ORDER BY `Order`";
118 
119 $sSysQuery = "SELECT `ID`, `Name`, `Movable` FROM `sys_menu_top` WHERE `Active`=1 AND `Type`='system' ORDER BY `Order`";
121 
122 $sAllQuery = "SELECT `ID`, `Name` FROM `sys_menu_top` WHERE `Type`!='system' AND (`Clonable`='1' OR (`Clonable`='0' AND `Active`='0')) ORDER BY `Name`";
124 
125 $sAdminUrl = CH_WSB_URL_ADMIN;
126 
128  <script type=\"text/javascript\">
129  <!--
130  topParentID = 'menu_app_wrapper';
131  parserUrl = '" . $GLOBALS['site']['url_admin'] . "nav_menu_compose.php?';
132 
133  allowNewItem = true;
134  allowAddToTop = true;
135  allowAddToCustom = true;
136  iInactivePerRow = 7;
137  sendSystemOrder = false;
138 
139  aCoords = {};
140  aCoords['startX'] = 6;
141  aCoords['startY'] = 24;
142  aCoords['width'] = 117;
143  aCoords['height'] = 28;
144  aCoords['diffX'] = 122;
145  aCoords['diffY'] = 32;
146 
147  aTopItems = {};
148  aCustomItems = {};
149  aSystemItems = {};
150  aAllItems = {};
151 ";
152 
154 while(($aTopItem = $rTopItems->fetch()) !== false) {
155  $sComposerInit .= "
156 
157  aTopItems[$iIndex] = [{$aTopItem['ID']}, '" . ch_js_string( $aTopItem['Name'], CH_ESCAPE_STR_APOS ) . "', " . $aTopItem['Movable'] . "];
158  aCustomItems[$iIndex] = {};";
159  $sQuery = "SELECT `ID`, `Name`, `Movable` FROM `sys_menu_top` WHERE `Active`=1 AND `Type`='custom' AND `Parent`={$aTopItem['ID']} ORDER BY `Order`";
160 
161  $iSubIndex = 0;
162  $rCustomItems = db_res( $sQuery );
163  while(($aCustomItem = $rCustomItems->fetch()) !== false) {
164  $sComposerInit .= "
165  aCustomItems[$iIndex][" . ($iSubIndex++) . "] = [{$aCustomItem['ID']}, '" . ch_js_string( $aCustomItem['Name'], CH_ESCAPE_STR_APOS ) . "', " . $aCustomItem['Movable'] . "];";
166  }
167 
168  $iIndex++;
169 }
170 
171 while(($aSystemItem = $rSysItems->fetch()) !== false) {
172  $sComposerInit .= "
173 
174  aSystemItems[$iIndex] = [{$aSystemItem['ID']}, '" . ch_js_string( $aSystemItem['Name'], CH_ESCAPE_STR_APOS ) . "', " . $aSystemItem['Movable'] . "];
175  aCustomItems[$iIndex] = {};";
176  $sQuery = "SELECT `ID`, `Name`, `Movable` FROM `sys_menu_top` WHERE `Active`=1 AND `Type`='custom' AND `Parent`={$aSystemItem['ID']} ORDER BY `Order`";
177 
178  $iSubIndex = 0;
179  $rCustomItems = db_res( $sQuery );
180  while(($aCustomItem = $rCustomItems->fetch()) !== false) {
181  $sComposerInit .= "
182  aCustomItems[$iIndex][" . ($iSubIndex++) . "] = [{$aCustomItem['ID']}, '" . ch_js_string( $aCustomItem['Name'], CH_ESCAPE_STR_APOS ) . "', " . $aCustomItem['Movable'] . "];";
183  }
184 
185  $iIndex++;
186 }
187 
188 $sComposerInit .= "\n";
189 while(($aAllItem = $rAllItems->fetch()) !== false) {
190  $sComposerInit .= "
191  aAllItems['{$aAllItem['ID']} '] = '" . ch_js_string( $aAllItem['Name'], CH_ESCAPE_STR_APOS ) . "';";
192 }
193 $sComposerInit .= "
194  -->
195  </script>";
196 
198 $_page = array(
199  'name_index' => $iNameIndex,
200  'css_name' => array('menu_compose.css', 'forms_adv.css'),
201  'js_name' => array('menu_compose.js', 'ChWsbMenu.js'),
202  'header' => _t('_adm_mbuilder_title')
203 );
204 
205 $sContent = $GLOBALS['oAdmTemplate']->parseHtmlByName('menu_compose.html', array(
206  'extra_js' => $sComposerInit
207 ));
208 
209 $_page_cont[$iNameIndex]['controls'] = null;
210 $_page_cont[$iNameIndex]['page_main_code'] = DesignBoxAdmin(_t('_adm_mbuilder_title_box'), $sContent);
211 
212 PageCodeAdmin();
213 
214 // Functions
215 function showEditForm($aItem)
216 {
217  $aForm = array(
218  'form_attrs' => array(
219  'id' => 'formItemEdit',
220  'name' => 'formItemEdit',
221  'action' => $GLOBALS['site']['url_admin'] . 'nav_menu_compose.php',
222  'method' => 'post',
223  'enctype' => 'multipart/form-data',
224  ),
225  'inputs' => array (
226  'Name' => array(
227  'type' => 'text',
228  'name' => 'Name',
229  'caption' => _t('_adm_mbuilder_System_Name'),
230  'value' => $aItem['Name'],
231  'attrs' => array()
232  ),
233  'Caption' => array(
234  'type' => 'text',
235  'name' => 'Caption',
236  'caption' => _t('_adm_mbuilder_Language_Key'),
237  'value' => $aItem['Caption'],
238  'attrs' => array()
239  ),
240  'LangCaption' => array(
241  'type' => 'text',
242  'name' => 'LangCaption',
243  'caption' => _t('_adm_mbuilder_Default_Name'),
244  'value' => _t( $aItem['Caption'] ),
245  'attrs' => array()
246  ),
247  'Link' => array(
248  'type' => 'text',
249  'name' => 'Link',
250  'caption' => _t('_URL'),
251  'value' => $aItem['Link'],
252  'attrs' => array()
253  ),
254  'Picture' => array(
255  'type' => 'text',
256  'name' => 'Picture',
257  'caption' => _t('_Picture'),
258  'value' => $aItem['Picture'],
259  'attrs' => array()
260  ),
261  'Icon' => array(
262  'type' => 'text',
263  'name' => 'Icon',
264  'caption' => _t('_adm_mbuilder_icon'),
265  'value' => $aItem['Icon'],
266  'attrs' => array()
267  ),
268  'BInQuickLink' => array(
269  'type' => 'checkbox',
270  'name' => 'BInQuickLink',
271  'caption' => _t('_adm_mbuilder_Quick_Link'),
272  'value' => 'on',
273  'checked' => $aItem['BQuickLink'] != 0,
274  'attrs' => array()
275  ),
276  'Target' => array(
277  'type' => 'radio_set',
278  'name' => 'Target',
279  'caption' => _t('_adm_mbuilder_Target_Window'),
280  'value' => $aItem['Target'] == '_blank' ? '_blank' : '_self',
281  'values' => array(
282  '_self' => _t('_adm_mbuilder_Same'),
283  '_blank' => _t('_adm_mbuilder_New')
284  ),
285  'attrs' => array()
286  ),
287  'Visible' => array(
288  'type' => 'checkbox_set',
289  'name' => 'Visible',
290  'caption' => _t('_adm_mbuilder_Visible_for'),
291  'value' => array(),
292  'values' => array(
293  'non' => _t('_Guest'),
294  'memb' => _t('_Member')
295  ),
296  'attrs' => array()
297  ),
298  'submit' => array(
299  'type' => 'input_set',
300  array(
301  'type' => 'button',
302  'name' => 'save',
303  'value' => _t('_Save Changes'),
304  'attrs' => array(
305  'onclick' => 'javascript:saveItem(' . $aItem['ID'] . ');'
306  )
307  ),
308  array(
309  'type' => 'button',
310  'name' => 'delete',
311  'value' => _t('_Delete'),
312  'attrs' => array(
313  'onclick' => 'javascript:deleteItem(' . $aItem['ID'] . ');'
314  )
315  )
316  ),
317  )
318  );
319 
320  foreach($aForm['inputs'] as $sKey => $aInput)
321  if(in_array($aInput['type'], array('text', 'checkbox')) && !$aItem['Editable'])
322  $aForm['inputs'][$sKey]['attrs']['disabled'] = "disabled";
323 
324  if(strpos($aItem['Visible'], 'non') !== false)
325  $aForm['inputs']['Visible']['value'][] = 'non';
326  if(strpos($aItem['Visible'], 'memb') !== false)
327  $aForm['inputs']['Visible']['value'][] = 'memb';
328 
330  return PopupBox('tmc_edit_popup', _t('_adm_mbuilder_edit_item'), $GLOBALS['oAdmTemplate']->parseHtmlByName('design_box_content.html', array('content' => $oForm->getCode() . LoadingBox('formItemEditLoading'))));
331 }
332 
333 function createNewElement( $type, $source )
334 {
335  global $oMenu;
336 
337  if( $source ) {
338  $sourceActive = db_value( "SELECT `Active` FROM `sys_menu_top` WHERE `ID`='{$source}'" );
339  if( !$sourceActive ) {
340  //convert to active
341  db_res( "UPDATE `sys_menu_top` SET `Active`=1, `Type`='{$type}' WHERE `ID`='{$source}'" );
342  $newID = $source;
343  } else {
344  //create from source
345  db_res( "INSERT INTO `sys_menu_top`
346  (`Name`, `Caption`, `Link`, `Visible`, `Target`, `Onclick`, `Check`, `Movable`, `Clonable`, `Editable`, `Deletable`, `Type`, `Picture`, `Icon`, `BQuickLink`, `Statistics`)
347  SELECT
348  `Name`, `Caption`, `Link`, `Visible`, `Target`, `Onclick`, `Check`, `Movable`, '0', `Editable`, '1', '{$type}', `Picture`, `Icon`, `BQuickLink`, `Statistics`
349  FROM `sys_menu_top`
350  WHERE `ID`='{$source}'" );
351  $newID = db_last_id();
352  }
353  } else {
354  //create new
355  db_res( "INSERT INTO `sys_menu_top` ( `Name`, `Type` ) VALUES ( 'NEW ITEM', '{$type}' )" );
356  $newID = db_last_id();
357  }
358 
359  $oMenu -> compile();
360  return $newID;
361 }
362 
363 function echoMenuEditMsg( $text, $color = 'black' )
364 {
365  echo <<<EOF
366 <div onclick="hideEditForm();" style="color:{$color};text-align:center;">{$text}</div>
367 <script type="text/javascript">setTimeout( 'hideEditForm();', 1000 )</script>
368 EOF;
369 }
370 
371 function saveItem( $id, $aItem )
372 {
373  global $oMenu;
374 
375  $sSavedC = _t('_Saved');
376  $sItemNotFoundC = _t('_adm_mbuilder_Item_not_found');
377  $sItemNonEditableC = _t('_adm_mbuilder_Item_is_non_editable');
378 
379  $aOldItem = db_arr( "SELECT * FROM `sys_menu_top` WHERE `ID`='{$id}'" );
380 
381  if(!$aOldItem)
382  return array('code' => 2, 'message' => $sItemNotFoundC);
383 
384  if((int)$aOldItem['Editable'] != 1)
385  return array('code' => 3, 'message' => $sItemNonEditableC);
386 
387  $sQuerySet = '';
388  foreach( $aItem as $field => $value )
389  $sQuerySet .= ", `{$field}`='" . process_db_input( $value ) ."'";
390 
391  $sQuerySet = substr( $sQuerySet, 1 );
392 
393  $sQuery = "UPDATE `sys_menu_top` SET {$sQuerySet} WHERE `ID` = '{$id}'";
394 
395  db_res( $sQuery );
396  $oMenu -> compile();
397 
398  return array('code' => 0, 'message' => $sSavedC, 'timer' => 3);
399 }
400 
401 function updateLangFile( $key, $string )
402 {
403  if (!$key)
404  return;
405 
406  $langName = getParam( 'lang_default' );
407  $langID = db_value( "SELECT `ID` FROM `sys_localization_languages` WHERE `Name` = '" . process_db_input( $langName ) . "'" );
408 
409  $keyID = db_value( "SELECT `ID` FROM `sys_localization_keys` WHERE `Key` = '" . process_db_input( $key ) . "'" );
410  if( $keyID ) {
411  db_res( "UPDATE `sys_localization_strings` SET `String` = '" .process_db_input( $string ) . "' WHERE `IDKey`='{$keyID}' AND `IDLanguage`='{$langID}'" );
412  } else {
413  db_res( "INSERT INTO `sys_localization_keys` SET `IDCategory` = 2, `Key` = '" . process_db_input( $key ) . "'" );
414  db_res( "INSERT INTO `sys_localization_strings` SET `IDKey` = " . db_last_id() . ", `IDLanguage` = '{$langID}', `String` = '" .process_db_input( $string ) . "'" );
415  }
416 
417  compileLanguage($langID);
418 }
419 
420 function saveOrders( $sTop, $aCustom )
421 {
422  global $oMenu;
423 
424  db_res( "UPDATE `sys_menu_top` SET `Order` = 0, `Parent` = 0" );
425 
426  $sTop = trim( $sTop, ' ,' );
427  $aTopIDs = explode( ',', $sTop );
428  foreach( $aTopIDs as $iOrd => $iID ) {
429  $iID = trim( $iID, ' ,' );
430  $iID = (int)$iID;
431 
432  if( !$iID )
433  continue;
434 
435  db_res( "UPDATE `sys_menu_top` SET `Order` = '{$iOrd}', `Type` = 'top' WHERE `ID` = '{$iID}'" );
436  }
437 
438  foreach( $aCustom as $iParent => $sCustom ) {
439  $iParent = (int)$iParent;
440  $sCustom = trim( $sCustom, ' ,' );
441  $aCustomIDs = explode( ',', $sCustom );
442  foreach( $aCustomIDs as $iOrd => $iID ) {
443  $iID = trim( $iID, ' ,' );
444  $iID = (int)$iID;
445 
446  if( !$iID )
447  continue;
448 
449  db_res( "UPDATE `sys_menu_top` SET `Order` = '{$iOrd}', `Type` = 'custom', `Parent`='{$iParent}' WHERE `ID` = '{$iID}'" );
450  }
451  }
452  $oMenu -> compile();
453 }
$sTopQuery
if(ch_get('action') !==false) $sTopQuery
Definition: nav_menu_compose.php:116
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
$oMenu
$oMenu
Definition: nav_menu_compose.php:33
ChTemplFormView
Definition: ChTemplFormView.php:11
db_assoc_arr
db_assoc_arr($query, $bindings=[])
Definition: db.inc.php:86
LoadingBox
LoadingBox($sName)
Definition: design.inc.php:185
MsgBox
MsgBox($sText, $iTimer=0)
Definition: design.inc.php:175
$sComposerInit
$sComposerInit
Definition: nav_menu_compose.php:127
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
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
$sSysQuery
$sSysQuery
Definition: nav_menu_compose.php:119
php
$rTopItems
$rTopItems
Definition: nav_menu_compose.php:117
$iIndex
$iIndex
Definition: nav_menu_compose.php:153
saveItem
saveItem( $id, $aItem)
Definition: nav_menu_compose.php:371
$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
createNewElement
createNewElement( $type, $source)
Definition: nav_menu_compose.php:333
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
$rSysItems
$rSysItems
Definition: nav_menu_compose.php:120
db_last_id
db_last_id()
Definition: db.inc.php:47
type
if(!defined("USER_STATUS_TYPE")) define("USER_STATUS_TYPE" type
Definition: constants.inc.php:13
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
PageCodeAdmin
PageCodeAdmin($oTemplate=null)
Definition: admin_design.inc.php:45
_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
showEditForm
showEditForm($aItem)
Definition: nav_menu_compose.php:215
$_page_cont
$_page_cont[$iNameIndex]['controls']
Definition: nav_menu_compose.php:209
saveOrders
saveOrders( $sTop, $aCustom)
Definition: nav_menu_compose.php:420
$rAllItems
$rAllItems
Definition: nav_menu_compose.php:123
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
$sAllQuery
$sAllQuery
Definition: nav_menu_compose.php:122
PopupBox
PopupBox($sName, $sTitle, $sContent, $aActions=array())
Definition: design.inc.php:189
$aChSecurityExceptions
$aChSecurityExceptions
Definition: nav_menu_compose.php:13
db_value
db_value($query, $bindings=[], $error_checking=true, $index=0)
Definition: db.inc.php:98
$sContent
$sContent
Definition: nav_menu_compose.php:205
$iNameIndex
$iNameIndex
Definition: nav_menu_compose.php:197
$aForm
$aForm
Definition: forgot.php:43
echoMenuEditMsg
echoMenuEditMsg( $text, $color='black')
Definition: nav_menu_compose.php:363
$_page
$_page
Definition: nav_menu_compose.php:198
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$sAdminUrl
$sAdminUrl
Definition: nav_menu_compose.php:125
$logged
$logged['admin']
Definition: nav_menu_compose.php:26
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ChWsbMenu
Definition: ChWsbMenu.php:14
updateLangFile
updateLangFile( $key, $string)
Definition: nav_menu_compose.php:401