Cheetah
member_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 ch_import('ChWsbMemberMenu');
21 
22  // Check if administrator is logged in. If not display login form.
23 $logged['admin'] = member_auth(1, true, true);
24 
25 $GLOBALS['oAdmTemplate']->addJsTranslation(array(
26  '_adm_mbuilder_Sorry_could_not_insert_object',
27  '_adm_mbuilder_This_items_are_non_editable'
28 ));
29 
31 
32 $aMenuSection = array (
33  'top' => array ('title' => '_top', 'href' => 'member_menu_compose.php?menu_position=top'),
34  'top_extra' => array ('title' => '_top_extra', 'href' => 'member_menu_compose.php?menu_position=top_extra'),
35 );
36 
37 $sResponce = null;
38 
39 // top is default position ;
40 $sMenuSection = 'top';
41 
42 if (isset($_GET['menu_position'])) {
43  foreach ($aMenuSection AS $sValue => $a) {
44  if ($sValue == $_GET['menu_position']) {
45  $sMenuSection = $sValue;
46  break;
47  }
48  }
49 }
51 
52 // ** FOR 'AJAX' REQUESTS ;
53 if(ch_get('action') !== false) {
54  switch(ch_get('action')) {
55  case 'edit_form':
56  $id = (int)ch_get('id');
57 
58  header('Content-Type: text/html; charset=utf-8');
59 
60  $aItem = db_assoc_arr( "SELECT * FROM `sys_menu_member` WHERE `ID` = $id", 0 );
61  $sResponce = ($aItem) ? showEditForm($aItem, $sMenuSection) : echoMenuEditMsg('Error', 'red');
62  break;
63  case 'create_item':
64  $newID = createNewElement($_POST['type'], (int)$_POST['source'], $sMenuSection);
65  $sResponce = $newID;
66  break;
67  case 'deactivate_item':
68  $res = db_res( "UPDATE `sys_menu_member` SET `Active`='0' WHERE `ID`=" . (int)ch_get('id') );
70  break;
71  case 'save_item':
72  $id = (int)$_POST['id'];
73  if( !$id ) {
74  $sResponce = echoMenuEditMsg( 'Error', 'red' );
75  } else {
76  $aItemFields = array( 'Name', 'Caption', 'Link', 'Target', 'Icon', 'Script' );
77  $aItem = array();
78 
79  foreach( $aItemFields as $field ) {
80  $aItem[$field] = ( isset($_POST[$field]) ) ? $_POST[$field] : null ;
81  }
82 
83  if ( !$aItem['Icon'] ) {
84  $aItem['Icon'] = 'member_menu_default.png';
85  }
86 
87  $res = saveItem( $id, $aItem, $sMenuSection );
88  updateLangFile( $_POST['Caption'], $_POST['LangCaption'] );
89 
90  $res['message'] = MsgBox($res['message']);
91  echo json_encode($res);
92  exit;
93  }
94  break;
95 
96  case 'delete_item':
97  $id = (int)$_POST['id'];
98  if( !$id ) {
99  $sResponce = 'Item ID is not specified';
100  } else {
101  $aItem = db_arr( "SELECT `Deletable` FROM `sys_menu_member` WHERE `ID` = $id" );
102  if( !$aItem ) {
103  $sResponce = 'Item not found';
104  } else if( !(int)$aItem['Deletable'] ) {
105  $sResponce = 'Item is non-deletable';
106  } else {
107  $res = db_res( "DELETE FROM `sys_menu_member` WHERE `ID` = $id" );
108  $sResponce = ( db_affected_rows($res) ) ? 'OK' : 'Couldn\'t delete the item';
109  }
110  }
111  break;
112 
113  case 'save_orders':
114  saveOrders( ch_get('top'), ch_get('custom'), $sMenuSection );
115  $sResponce = 'OK';
116  break;
117  }
118 
119  // return script's response and recompile the menu cache ;
120  $oMemeberMenu -> createMemberMenuCache();
121  echo $sResponce;
122  exit;
123 }
124 
125 // generate all active menu items ;
126 
127 $sTopQuery = "SELECT `ID`, `Name`, `Movable` FROM `sys_menu_member` WHERE `Active`='1' AND `Position`='{$sMenuSection}' AND `Type`<>'linked_item' ORDER BY `Order`";
129 
130 $sAllQuery = "SELECT `ID`, `Name` FROM `sys_menu_member` WHERE `Type`<>'linked_item' AND (`Clonable`='1' OR (`Clonable`='0' AND `Active`='0')) ORDER BY `Name`";
131 
133 
135  <script type=\"text/javascript\">
136  topParentID = 'menu_app_wrapper';
137  parserUrl = '" . $GLOBALS['site']['url_admin'] . "member_menu_compose.php?menu_position={$sMenuSection}';
138 
139  allowNewItem = true;
140  allowAddToTop = true;
141  allowAddToCustom = false;
142  iInactivePerRow = 5;
143  sendSystemOrder = false;
144 
145  aCoords = {};
146  aCoords['startX'] = 6;
147  aCoords['startY'] = 24;
148  aCoords['width'] = 117;
149  aCoords['height'] = 28;
150  aCoords['diffX'] = 122;
151  aCoords['diffY'] = 32;
152 
153  aTopItems = {};
154  aCustomItems = {};
155  aSystemItems = {};
156  aAllItems = {};
157 ";
158 
160 while(($aTopItem = $rTopItems->fetch()) !== false) {
161  $sComposerInit .= "
162 
163  aTopItems[$iIndex] = [{$aTopItem['ID']}, '" . ch_js_string( $aTopItem['Name'], CH_ESCAPE_STR_APOS ) . "', {$aTopItem['Movable']}];
164  aCustomItems[$iIndex] = {};";
165 
166  $iIndex++;
167 }
168 
169 $sComposerInit .= "\n";
170 while(($aAllItem = $rAllItems->fetch()) !== false) {
171  $sComposerInit .= "
172  aAllItems['{$aAllItem['ID']} '] = '" . ch_js_string( $aAllItem['Name'], CH_ESCAPE_STR_APOS ) . "';";
173 }
174  $sComposerInit .= "
175  </script>
176 ";
177 
179 $_page = array(
180  'name_index' => $iNameIndex,
181  'css_name' => array('menu_compose.css', 'forms_adv.css'),
182  'js_name' => array('menu_compose.js', 'ChWsbMenu.js'),
183  'header' => _t('_mmbuilder_page_title')
184 );
185 
186 $sContent = $GLOBALS['oAdmTemplate']->parseHtmlByName('menu_compose.html', array(
187  'extra_js' => $sComposerInit
188 ));
189 
190 $_page_cont[$iNameIndex]['page_main_code'] = DesignBoxAdmin(_t('_mmbuilder_box_title'), $sContent, $aMenuSection);
191 
192 
193 PageCodeAdmin();
194 
195 
196 function showEditForm( $aItem, $sMenuSection )
197 {
198  $aForm = array(
199  'form_attrs' => array(
200  'id' => 'formItemEdit',
201  'name' => 'formItemEdit',
202  'action' => $GLOBALS['site']['url_admin'] . 'member_menu_compose.php',
203  'method' => 'post',
204  'enctype' => 'multipart/form-data',
205  ),
206  'inputs' => array (
207  'Name' => array(
208  'type' => 'text',
209  'name' => 'Name',
210  'caption' => _t('_adm_mbuilder_System_Name'),
211  'value' => $aItem['Name'],
212  'attrs' => array()
213  ),
214  'Caption' => array(
215  'type' => 'text',
216  'name' => 'Caption',
217  'caption' => _t('_adm_mbuilder_Language_Key'),
218  'value' => $aItem['Caption'],
219  'attrs' => array()
220  ),
221  'LangCaption' => array(
222  'type' => 'text',
223  'name' => 'LangCaption',
224  'caption' => _t('_adm_mbuilder_Default_Name'),
225  'value' => _t( $aItem['Caption'] ),
226  'attrs' => array()
227  ),
228  'Link' => array(
229  'type' => 'text',
230  'name' => 'Link',
231  'caption' => _t('_URL'),
232  'value' => htmlspecialchars_adv( $aItem['Link'] ),
233  'attrs' => array()
234  ),
235  'Script' => array(
236  'type' => 'text',
237  'name' => 'Script',
238  'caption' => _t('_adm_mbuilder_script'),
239  'value' => htmlspecialchars_adv( $aItem['Script'] ),
240  'attrs' => array()
241  ),
242  'Icon' => array(
243  'type' => 'text',
244  'name' => 'Icon',
245  'caption' => _t('_adm_mbuilder_icon'),
246  'value' => htmlspecialchars_adv( $aItem['Icon'] ),
247  'attrs' => array()
248  ),
249  'Target' => array(
250  'type' => 'radio_set',
251  'name' => 'Target',
252  'caption' => _t('_adm_mbuilder_Target_Window'),
253  'value' => $aItem['Target'] == '_blank' ? '_blank' : '_self',
254  'values' => array(
255  '_self' => _t('_adm_mbuilder_Same'),
256  '_blank' => _t('_adm_mbuilder_New')
257  ),
258  'attrs' => array()
259  ),
260  'submit' => array(
261  'type' => 'input_set',
262  array(
263  'type' => 'button',
264  'name' => 'save',
265  'value' => _t('_Save Changes'), //if( $aItem['Editable'] )
266  'attrs' => array(
267  'onclick' => 'javascript:saveItem(' . $aItem['ID'] . ');'
268  )
269  ),
270  array(
271  'type' => 'button',
272  'name' => 'delete',
273  'value' => _t('_Delete'), //if( $aItem['Deletable'] )
274  'attrs' => array(
275  'onclick' => 'javascript:deleteItem(' . $aItem['ID'] . ');'
276  )
277  )
278  ),
279  )
280  );
281 
282  foreach($aForm['inputs'] as $sKey => $aInput)
283  if(in_array($aInput['type'], array('text', 'checkbox')) && !$aItem['Editable'])
284  $aForm['inputs'][$sKey]['attrs']['disabled'] = "disabled";
285 
286  if(strpos($aItem['Visible'], 'non') !== false)
287  $aForm['inputs']['Visible']['value'][] = 'non';
288  if(strpos($aItem['Visible'], 'memb') !== false)
289  $aForm['inputs']['Visible']['value'][] = 'memb';
290 
292  return PopupBox('mmc_edit_popup', _t('_adm_mbuilder_edit_item')
293  , $GLOBALS['oAdmTemplate']->parseHtmlByName('design_box_content.html'
294  , array('content' => $oForm->getCode() . LoadingBox('formItemEditLoading'))));
295 }
296 
303 function createNewElement( $type, $source, $sMenuSection = 'top' )
304 {
305  global $oMenu;
306 
307  if( $source ) {
308  $sourceActive = db_value( "SELECT `Active` FROM `sys_menu_member` WHERE `ID`=$source" );
309  if( !$sourceActive ) {
310  //convert to active
311  db_res( "UPDATE `sys_menu_member` SET `Active`='1', `Position`='$type' WHERE `ID`=$source" );
312  $newID = $source;
313  } else {
314  //create from source
315  db_res( "INSERT INTO `sys_menu_member`
316  (`Caption`, `Name`, `Icon`, `Link`, `Script`, `Eval`, `PopupMenu`, `Movable`, `Clonable`, `Editable`, `Deletable`, `Target`, `Position`, `Type`, `Bubble`, `Description`)
317  SELECT `Caption`, `Name`, `Icon`, `Link`, `Script`, `Eval`, `PopupMenu`, `Movable`, '0', `Editable`, '1', `Target`, `Position`, `Type`, `Bubble`, `Description`
318  FROM `sys_menu_member`
319  WHERE `ID`=$source" );
320  $newID = db_last_id();
321  }
322  } else {
323  //create new
324  db_res( "INSERT INTO `sys_menu_member` ( `Name`, `Position` ) VALUES ( 'NEW ITEM', '$type' )" );
325  $newID = db_last_id();
326  }
327 
328  return $newID;
329 }
330 
331 function echoMenuEditMsg( $text, $color = 'black' )
332 {
333  return <<<HTML
334  <div style="color:{$color};text-align:center;">{$text}</div>
335 HTML;
336 }
337 
345 function saveItem( $id, $aItem, $sMenuSection )
346 {
348 
349  $aOldItem = db_arr( "SELECT * FROM `sys_menu_member` WHERE `ID` = $id" );
350 
351  if(!$aOldItem) {
352  return array( 'code' => 2, 'message' => _t('_adm_mbuilder_Item_not_found') );
353  }
354 
355  if( (int) $aOldItem['Editable'] != 1 ) {
356  return array('code' => 3, 'message' => _t('_adm_mbuilder_Item_is_non_editable') );
357  }
358 
359  $sQuerySet = '';
360  foreach( $aItem as $field => $value )
361  $sQuerySet .= ", `$field`='" . process_db_input( $value ) ."'";
362 
363  $sQuerySet = substr( $sQuerySet, 1 );
364 
365  $sQuery = "UPDATE `sys_menu_member` SET $sQuerySet WHERE `ID` = $id";
366  db_res( $sQuery );
367 
368  // return script's response and recompile the menu cache ;
369  $oMemeberMenu -> createMemberMenuCache();
370 
371  return array('code' => 0, 'message' => _t('_Saved'), 'timer' => 3);
372 }
373 
374 function updateLangFile( $key, $string )
375 {
376  // clear from special chars ;
377  $key = preg_replace( '|\{([^\}]+)\}|', '', $key);
378 
379  if (!$key)
380  return;
381 
382  $langName = getParam( 'lang_default' );
383  $langID = db_value( "SELECT `ID` FROM `sys_localization_languages` WHERE `Name` = '" . process_db_input( $langName ) . "'" );
384 
385  $keyID = db_value( "SELECT `ID` FROM `sys_localization_keys` WHERE `Key` = '" . process_db_input( $key ) . "'" );
386  if( $keyID ) {
387  db_res( "UPDATE `sys_localization_strings` SET `String` = '" .process_db_input( $string ) . "' WHERE `IDKey`=$keyID AND `IDLanguage`=$langID" );
388  } else {
389  db_res( "INSERT INTO `sys_localization_keys` SET `IDCategory` = 2, `Key` = '" . process_db_input( $key ) . "'" );
390  db_res( "INSERT INTO `sys_localization_strings` SET `IDKey` = " . db_last_id() . ", `IDLanguage` = $langID, `String` = '" .process_db_input( $string ) . "'" );
391  }
392 
393  compileLanguage($langID);
394 }
395 
402 function saveOrders( $sTop, $aCustom, $sMenuSection )
403 {
404  db_res( "UPDATE `sys_menu_member` SET `Order` = 0 WHERE `Position` = '{$sMenuSection}' " );
405 
406  $sTop = trim( $sTop, ' ,' );
407  $aTopIDs = explode( ',', $sTop );
408 
409  foreach( $aTopIDs as $iOrd => $iID ) {
410  $iID = trim( $iID, ' ,' );
411  $iID = (int)$iID;
412 
413  if( !$iID )
414  continue;
415 
416  db_res( "UPDATE `sys_menu_member` SET `Order` = $iOrd, `Position` = '{$sMenuSection}' WHERE `ID` = $iID" );
417  }
418 }
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
saveItem
saveItem( $id, $aItem, $sMenuSection)
Definition: member_menu_compose.php:345
$aMenuSection
$aMenuSection
Definition: member_menu_compose.php:32
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
$oMenu
$oMenu
Definition: bottom_menu_compose.php:29
updateLangFile
updateLangFile( $key, $string)
Definition: member_menu_compose.php:374
$sTopQuery
if(ch_get('action') !==false) $sTopQuery
Definition: member_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
ChWsbMemberMenu
Definition: ChWsbMemberMenu.php:92
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
ch_get
ch_get($sName)
Definition: utils.inc.php:1664
$sContent
$sContent
Definition: member_menu_compose.php:186
createNewElement
createNewElement( $type, $source, $sMenuSection='top')
Definition: member_menu_compose.php:303
php
$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
$_GET
$_GET['debug']
Definition: index.php:67
$sComposerInit
$sComposerInit
Definition: member_menu_compose.php:134
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
$oMemeberMenu
$oMemeberMenu
Definition: member_menu_compose.php:30
$aChSecurityExceptions
$aChSecurityExceptions
Definition: member_menu_compose.php:9
$sResponce
$sResponce
Definition: member_menu_compose.php:37
htmlspecialchars_adv
htmlspecialchars_adv($string)
Definition: utils.inc.php:302
db_last_id
db_last_id()
Definition: db.inc.php:47
saveOrders
saveOrders( $sTop, $aCustom, $sMenuSection)
Definition: member_menu_compose.php:402
$rAllItems
$rAllItems
Definition: member_menu_compose.php:132
$iNameIndex
$iNameIndex
Definition: member_menu_compose.php:178
$iIndex
$iIndex
Definition: member_menu_compose.php:159
echoMenuEditMsg
echoMenuEditMsg( $text, $color='black')
Definition: member_menu_compose.php:331
showEditForm
showEditForm( $aItem, $sMenuSection)
Definition: member_menu_compose.php:196
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
$_page_cont
$_page_cont[$iNameIndex]['page_main_code']
Definition: member_menu_compose.php:190
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
$_page
$_page
Definition: member_menu_compose.php:179
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
$rTopItems
$rTopItems
Definition: member_menu_compose.php:128
$sMenuSection
$sMenuSection
Definition: member_menu_compose.php:40
$logged
$logged['admin']
Definition: member_menu_compose.php:23
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$sAllQuery
$sAllQuery
Definition: member_menu_compose.php:130
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10