YaWK  24.1
Yet another WebKit
menu.php
Go to the documentation of this file.
1 <?php
2 namespace YAWK {
3  /**
4  * @details <b>The default menu class. Serves all the menu functions.</b>
5  *
6  * This class serves all functions to create, edit, delete and modify menus and menu entries.
7  * <p><i>Class covers both, backend & frontend functionality.
8  * See Methods Summary for Details!</i></p>
9  *
10  * @author Daniel Retzl <[email protected]>
11  * @copyright 2009-2021 Daniel Retzl
12  * @license https://opensource.org/licenses/MIT
13  * @version 1.0.0
14  * @brief The default menu class. Serves all the menu functions.
15  */
16  class menu
17  {
18  /** * @param int the menu id */
19  public $id;
20 
21  /** * @param string the menu name */
22  public $name;
23 
24  /** * @param int the menu ID that should act as subMenu */
25  public $menuID;
26 
27  /** * @param int 0|1 published - yes or no */
28  public $published;
29 
30  /** * @param int the parent menu ID */
31  public $parent;
32 
33 
34 
35  /**
36  * @brief return true if menu is published, false if not. expects db object and menu ID to get the status from
37  * @copyright 2009-2016 Daniel Retzl
38  * @license https://opensource.org/licenses/MIT
39  * @version 1.0.0
40  * @param object $db database
41  * @param int $menuID the menuID to get data
42  */
43  public function isPublished($db, $menuID)
44  { /** @param db $db */
45  $res = $db->query("SELECT published FROM {menu_names}
46  WHERE published = 1
47  AND id = '".$menuID."'");
48  $status = mysqli_fetch_row($res);
49  if ($status['0'] == '1')
50  { // this menu ID is published
51  return true;
52  }
53  else
54  { // menu ID is not published
55  return false;
56  }
57  }
58 
59 
60  /**
61  * @brief display the global menu
62  * @copyright 2009-2016 Daniel Retzl
63  * @license https://opensource.org/licenses/MIT
64  * @version 1.0.0
65  * @param object $db database
66  * @param object $user current user object
67  * @param object $template template object
68  */
69  static function displayGlobalMenu($db, $user, $template)
70  { /** @param db $db */
71  $res = $db->query("SELECT value FROM {settings}
72  WHERE property = 'globalmenuid'");
73  if ($row = mysqli_fetch_row($res)) {
74  if ($published = self::getMenuStatus($db, $row[0]) != '0') {
75  self::display($db, $row[0], $user, $template);
76  }
77  }
78  }
79 
80  /**
81  * @brief create a new menu
82  * @copyright 2009-2016 Daniel Retzl
83  * @license https://opensource.org/licenses/MIT
84  * @version 1.0.0
85  * @param object $db the database object
86  * @param object $lang language
87  * @param string $name the name of the new menu
88  * @return bool
89  */
90  static function createMenu($db, $name, $lang)
91  { /** @param $db db */
92  // menu name not given
93  if (!$name) {
94  return false;
95  }
96  else
97  { // menu name is set, go ahead
98  if ($res = $db->query("SELECT MAX(id) FROM {menu_names}"))
99  { // get MAXid
100  $row = mysqli_fetch_row($res);
101  $menuID = $row[0] + 1;
103  if ($res = $db->query("INSERT INTO {menu_names} (id, name) VALUES ('" . $menuID . "', '" . $name . "')"))
104  { // data inserted
105  \YAWK\sys::setSyslog($db, 21, 0, "created menu $lang[ID]: $menuID <b>$name</b>", 0, 0, 0, 0);
106  return true;
107  }
108  else {
109  // q insert failed
110  \YAWK\sys::setSyslog($db, 24, 1, "failed to create menu: <b>$name</b>", 0, 0, 0, 0);
111  return false;
112  }
113  }
114  else {
115  // q get maxID failed
116  \YAWK\sys::setSyslog($db, 24, 1, "unable to retrieve max ID of menu <b>$name</b>", 0, 0, 0, 0);
117  return false;
118  }
119  }
120  }
121 
122  /**
123  * @brief Change the menu title
124  * @copyright 2009-2016 Daniel Retzl
125  * @license https://opensource.org/licenses/MIT
126  * @version 1.0.0
127  * @param object $db the database oject
128  * @param int $menu affected menu ID
129  * @param string $menutitle new menu title
130  * @return bool
131  */
132  static function changeTitle($db, $menu, $menutitle)
133  {
134  /** @param $db db $res */
135  if ($res = $db->query("UPDATE {menu_names} SET
136  name = '" . $menutitle . "'
137  WHERE id = '" . $menu . "'"))
138  {
139  \YAWK\sys::setSyslog($db, 21, 0,"updated menu title <b>$menutitle</b>", 0, 0, 0, 0);
140  return true;
141  }
142  else
143  {
144  \YAWK\sys::setSyslog($db, 24, 1,"failed to update menu title <b>$menutitle</b>", 0, 0, 0, 0);
145  return false;
146  }
147  }
148 
149  /**
150  * @brief Change the menu language
151  * @copyright 2009-2021 Daniel Retzl
152  * @license https://opensource.org/licenses/MIT
153  * @version 1.0.0
154  * @param object $db the database oject
155  * @param int $menu affected menu ID
156  * @param string $menuLanguage new menu language
157  * @return bool
158  */
159  static function changeLanguage($db, $menu, $menuLanguage)
160  {
161  $menuLanguage = mb_substr($menuLanguage, 0, 2);
162  if (empty($menuLanguage))
163  {
164  $menuLanguage = NULL;
165  }
166  /** @param $db db $res */
167  if ($res = $db->query("UPDATE {menu_names} SET
168  menuLanguage = '" . $menuLanguage . "'
169  WHERE id = '" . $menu . "'") &&
170  ($res = $db->query("UPDATE {menu} SET
171  menuLanguage = '" . $menuLanguage . "'
172  WHERE menuID = '" . $menu . "'")))
173 
174  {
175  \YAWK\sys::setSyslog($db, 21, 0,"updated menu language of menu ID $menu to <b>$menuLanguage</b>", 0, 0, 0, 0);
176  return true;
177  }
178  else
179  {
180  \YAWK\sys::setSyslog($db, 24, 1,"failed to update menu language of menu ID $menu to <b>$menuLanguage</b>", 0, 0, 0, 0);
181  return false;
182  }
183  }
184 
185  /**
186  * @brief Get menu ID by language
187  * @copyright 2009-2021 Daniel Retzl
188  * @license https://opensource.org/licenses/MIT
189  * @version 1.0.0
190  * @param object $db the database object
191  * @param string $menuLanguage menu language
192  * @return int|false $id return ID of the menu with given language or false if no ID was found
193  */
195  {
196  /** @param $db db $res */
197  if ($res = $db->query("SELECT id FROM {menu} WHERE menuLanguage='".$menuLanguage."'"))
198  {
199  $id = mysqli_fetch_row($res);
200  if (isset($id[0]) && (!empty($id[0])))
201  {
202  return $id[0];
203  }
204  else
205  {
206  return false;
207  }
208  }
209  else
210  {
211  return false;
212  }
213  }
214 
215  /**
216  * @brief add new entry to an existing menu
217  * @param $db object database object
218  * @copyright 2009-2016 Daniel Retzl
219  * @license https://opensource.org/licenses/MIT
220  * @version 1.0.0
221  * @param $menu int affected menu ID
222  * @param $text string new menu entry title
223  * @param $href string new menu link
224  * @return bool
225  */
226  static function addEntry($db, $menuID, $text, $href)
227  { /** @param $db db */
228 
229  // get menu name
231 
232  // ## select max ID from menu + add menu entry
233  $res = $db->query("SELECT MAX(sort) FROM {menu}
234  WHERE menuID = '".$menuID."'");
235 
236  $row = mysqli_fetch_row($res);
237  if (isset($row[0]))
238  { // add sort
239  $sort = $row[0] + 1;
240  }
241  else
242  { // sort
243  $sort = 1;
244  }
245 
246  // make sure that sort var is set
247  if (!isset($sort)) {
248  $sort = 1;
249  }
250 
251  // add menu entry
252  if ($res = $db->query("INSERT INTO {menu}
253  (sort, menuID, text, href)
254  VALUES (
255  '" . $sort . "',
256  '" . $menuID . "',
257  '" . $text . "',
258  '" . $href . "')"))
259  { // menu entry added
260  \YAWK\sys::setSyslog($db, 21, 0, "added menu entry <b>$text</b> to <b>$menuName</b>", 0, 0, 0, 0);
261  return true;
262  }
263  else
264  {
265  // add menu entry failed
266  \YAWK\sys::setSyslog($db, 23, 1, "failed to add menu entry <b>$text</b> to <b>$menuName</b>", 0, 0, 0, 0);
267  return false;
268  }
269  }
270 
271  /**
272  * @brief check if a whole menu is published or not
273  * @copyright 2009-2016 Daniel Retzl
274  * @license https://opensource.org/licenses/MIT
275  * @version 1.0.0
276  * @param object $db database object
277  * @param object $lang language object
278  * @param int $menuid affected menu ID
279  * @return bool
280  */
281  public static function getMenuStatus($db, $menuid)
282  {
283  /** @param $db db */
284  // get status from menu db
285  if ($res = $db->query("SELECT published FROM {menu_names} WHERE id = '" . $menuid . "'"))
286  { // fetch data
287  $row = mysqli_fetch_row($res);
288  return $row[0];
289  }
290  else
291  { // q failed
292  \YAWK\sys::setSyslog($db, 23, 1, "failed to get menu status of menu <b>ID: $menuid</b> (menu::getMenuStatus)", 0, 0, 0, 0);
293  return false;
294  }
295  }
296 
297  /**
298  * @brief check if a single menu entry is published or not
299  * @copyright 2009-2016 Daniel Retzl
300  * @license https://opensource.org/licenses/MIT
301  * @version 1.0.0
302  * @param $db object database object
303  * @param $menuid int affected menu id
304  * @return bool
305  */
306  public static function getMenuEntryStatus($db, $menuid)
307  {
308  /** @param $db db */
309  if ($res = $db->query("SELECT published FROM {menu} WHERE id = '" . $menuid . "'"))
310  {
311  $row = mysqli_fetch_row($res);
312  return $row[0];
313  }
314  else
315  {
316  \YAWK\sys::setSyslog($db, 23, 1, "failed to get status of menu entry <b>ID: $menuid</b> (menu::getMenuEntryStatus)", 0, 0, 0, 0);
317  return false;
318  }
319  }
320 
321  /**
322  * @brief toggle a whole menu offline
323  * @copyright 2009-2016 Daniel Retzl
324  * @license https://opensource.org/licenses/MIT
325  * @version 1.0.0
326  * @param object $db database
327  * @param object $lang language
328  * @param int $id affected menu id
329  * @param int $published menu status
330  * @return bool
331  */
333  {
334  /** @param $db db */
335 
336  // get name and status string
338  $status = \YAWK\sys::iStatusToString($published, "online", "offline");
339 
340  // TOGGLE PAGE STATUS
341  if ($res = $db->query("UPDATE {menu_names}
342  SET published = '" . $published . "'
343  WHERE id = '" . $id . "'"))
344  {
345  \YAWK\sys::setSyslog($db, 21, 0, "toggled <b>$menuName</b> to <b>$status</b>", 0, 0, 0, 0);
346  return true;
347  }
348  else
349  {
350  \YAWK\sys::setSyslog($db, 23, 1, "failed toggle <b>$menuName</b> to <b>$status</b>", 0, 0, 0, 0);
351  return false;
352  }
353  }
354 
355  /**
356  * @brief toggle a menu entry offline
357  * @copyright 2009-2016 Daniel Retzl
358  * @license https://opensource.org/licenses/MIT
359  * @version 1.0.0
360  * @param $db object database
361  * @param $id int affected menu entry id
362  * @param $published int menu status
363  * @param $menuID int affected menu id
364  * @return bool
365  */
367  {
368  /** @param $db db */
369 
370  // get name and status string
372  $status = \YAWK\sys::iStatusToString($published, "online", "offline");
373 
374  // TOGGLE PAGE STATUS
375  if (!$res = $db->query("UPDATE {menu}
376  SET published = '" . $published . "'
377  WHERE id = '" . $id . "'"))
378  { // throw error
379  \YAWK\sys::setSyslog($db, 23, 1, "failed to toggle <b>$menuItem</b> to <b>$status</b>", 0, 0, 0, 0);
380  \YAWK\alert::draw("warning", "Warning!", "Menu status could not be toggled.", "","4200");
381  return false;
382  }
383  else {
384  // all ok
385  \YAWK\sys::setSyslog($db, 21, 0, "toggled menu <b>$menuItem</b> to <b>$status</b>", 0, 0, 0, 0);
386  return true;
387  }
388  }
389 
390  /**
391  * @brief edit a single menu entry
392  * @copyright 2009-2016 Daniel Retzl
393  * @license https://opensource.org/licenses/MIT
394  * @version 1.0.0
395  * @param object $db database
396  * @param int $menu affected menu ID
397  * @param int $id affected menu entry ID
398  * @param string $title menu title
399  * @param string $href menu link
400  * @param int $sort sortation order
401  * @param int $gid group id
402  * @param int $published int 0|1 published status
403  * @param int $parentID int id of the parent menu entry
404  * @param string $target string link target (eg. _blank)
405  * @param string $icon string any fontAwesome icon
406  * @return bool
407  */
408  static function editEntry($db, $menu, $id, $text, $title, $href, $sort, $gid, $published, $parentID, $target, $icon)
409  { /** @param $db db */
411  $date_changed = date("Y-m-d G:i:s");
412  if (!empty($icon)) { $icon = "fa ".$icon; }
413  if ($res = $db->query("UPDATE {menu} SET
414  sort = '" . $sort . "',
415  href = '" . $href . "',
416  text = '" . $text . "',
417  title = '" . $title . "',
418  gid = '" . $gid . "',
419  published = '" . $published . "',
420  date_changed = '" . $date_changed . "',
421  parentID = '" . $parentID . "',
422  target = '" . $target . "',
423  icon = '" . $icon . "'
424  WHERE id = '" . $id . "'
425  AND menuID = '" . $menu . "'"))
426  {
427  \YAWK\sys::setSyslog($db, 21, 0, "edited <b>$title</b> in <b>$menuName</b>", 0, 0, 0, 0);
428  return true;
429  }
430  else
431  { // q failed
432  \YAWK\sys::setSyslog($db, 23, 1, "failed to edit <b>$title</b> in <b>$menuName</b>", 0, 0, 0, 0);
433  return false;
434  }
435  }
436 
437  /**
438  * @brief delete a single menu entry
439  * @copyright 2009-2016 Daniel Retzl
440  * @license https://opensource.org/licenses/MIT
441  * @version 1.0.0
442  * @param object $db database
443  * @param int $menu affected menu ID
444  * @param int $id affected menu entry ID
445  * @return bool
446  */
447  static function deleteEntry($db, $menu, $id)
448  { /** @param $db db */
450  $menuItem = \YAWK\menu::getMenuItemTitleByID($db, $id, $menu);
451  if (!$res = $db->query("DELETE FROM {menu} WHERE menuID = '" . $menu . "' AND id = '" . $id . "'"))
452  { // throw error
453  \YAWK\sys::setSyslog($db, 24, 1, "failed to delete <b>$menuItem</b> in <b>$menuName</b>", 0, 0, 0, 0);
454  return false;
455  }
456  else
457  { // menu deleted
458  if (!$res = $db->query("UPDATE {menu} SET id = id -1 WHERE id > '" . $id . "'"))
459  { // menu del not worked
460  \YAWK\sys::setSyslog($db, 23, 1, "failed to reset ID of menu <b>$menuName</b>", 0, 0, 0, 0);
461  return false;
462  }
463  else {
464  // all good, menu entry deleted
465  \YAWK\sys::setSyslog($db, 21, 0, "deleted <b>$menuItem</b> in <b>$menuName</b>", 0, 0, 0, 0);
466  return true;
467  }
468  }
469  }
470 
471  /**
472  * @brief delete a whole menu
473  * @copyright 2009-2016 Daniel Retzl
474  * @license https://opensource.org/licenses/MIT
475  * @version 1.0.0
476  * @param object $db database
477  * @param object $lang language
478  * @param int $id affected menu ID
479  * @return bool
480  */
481  static function delete($db, $id, $lang)
482  {
483  /** @param $db db */
485  // delete menu itself
486  if ($res = $db->query("DELETE FROM {menu_names} WHERE id = '" . $id . "'"))
487  { // delete according menu entries
488  if ($res = $db->query("DELETE FROM {menu} WHERE menuID = '" . $id . "'"))
489  {
490  // \YAWK\sys::setSyslog($db, 7, 0, "$lang[SYSLOG_MENU_DEL_ALLENTRIES_OK] <b>$menuName</b>", 0, 0, 0, 0);
491  \YAWK\sys::setSyslog($db, 21, 0, "deleted <b>$menuName</b> and all corresponding menu entries", 0, 0, 0, 0);
492  return true;
493  }
494  else
495  {
496  // q failed
497  \YAWK\sys::setSyslog($db, 24, 1, "failed to delete menu <b>$menuName</b>", 0, 0, 0, 0);
498  return false;
499  }
500  }
501  else
502  {
503  \YAWK\sys::setSyslog($db, 24, 1, "failed to delete menu <b>$menuName</b>", 0, 0, 0, 0);
504  }
505  return false;
506  }
507 
508 
509  /**
510  * @brief display menu entries for editing in backend
511  * @param object $db database
512  * @param int $id affected menuID
513  * @param array $lang language array
514  */
515  static function displayEditable($db, $id, $lang) /* SHOW EDITABLEN MENU ENTRIES IN BACKEND */
516  {
517  /** UPDATE: OPTIMIZATION NEEDED
518  * HERE SHOULD BE A SELECT JOIN user_groups + parent items
519  * instead of 3 different SELECTs - can anybody help here? */
520  /** @param $db db */
521  echo "
522 <table class=\"table table-striped table-hover table-responsive\" id=\"table-sort\">
523  <thead>
524  <tr>
525  <td><strong>&nbsp;</strong></td>
526  <td><strong>$lang[ID]</strong></td>
527  <td><strong>$lang[ICON] ".\YAWK\backend::printTooltip($lang['TT_MENU_ICON'])."</strong></td>
528  <td><strong>$lang[TEXT] ".\YAWK\backend::printTooltip($lang['TT_MENU_TEXT'])."</strong></td>
529  <td><strong>$lang[LINK] ".\YAWK\backend::printTooltip($lang['TT_MENU_ANCHOR'])."</strong></td>
530  <td><strong>$lang[TITLE] ".\YAWK\backend::printTooltip($lang['TT_MENU_TITLE'])."</strong></td>
531  <td><strong>$lang[GROUP] ".\YAWK\backend::printTooltip($lang['TT_MENU_GROUP'])."</strong></td>
532  <td><strong>$lang[TARGET] ".\YAWK\backend::printTooltip($lang['TT_MENU_TARGET'])."</strong></td>
533  <td><strong>$lang[SORTATION] ".\YAWK\backend::printTooltip($lang['TT_MENU_SORT'])."</strong></td>
534  <td><strong>$lang[PARENT_ELEMENT] ".\YAWK\backend::printTooltip($lang['TT_MENU_PARENT'])."</strong></td>
535  <td><strong>&nbsp;</td>
536  </tr>
537  </thead>
538  <tbody>";
539  // get menu entries from database
540  if ($res = $db->query("SELECT id, text, title, href, gid, target, sort, parentID, published, icon
541  FROM {menu}
542  WHERE menuID = '".$id."'
543  ORDER BY sort, parentID, title"))
544  {
545  while ($row = mysqli_fetch_assoc($res))
546  {
547  // get published status
548  if ($row['published'] === '1') {
549  $pub = "success";
550  // $pubtext = "On";
551  $pubtext = $lang['ON_'];
552  } else {
553  $pub = "danger";
554  // $pubtext = "Off";
555  $pubtext = $lang['OFF_'];
556  }
557  // get user groups
558  if ($group_res = $db->query("SELECT id, value FROM {user_groups} ORDER BY id")){
559  $groupArray = array();
560  $gidSelect = '';
561  while ($group_row = $group_res->fetch_assoc()){
562  $groupArray[] = $group_row;
563  }
564  foreach ($groupArray AS $group){
565  $gidSelect .= "
566  <option value=\"$group[id]\">$group[value]</option>";
567  }
568  }
569  // get name for group id
570  if (isset($row['gid'])){
571  $gid2name = $db->query("SELECT id, value FROM {user_groups} WHERE id = '".$row['gid']."'");
572  $groupName = mysqli_fetch_row($gid2name);
573  }
574 
575  // prepare parentID select field
576  // get menu entry name for <option....> from menuID
577  if($entries_res = $db->query("SELECT id, text, title, parentID FROM {menu} WHERE menuID = $id ORDER BY sort, parentID, title"))
578  {
579  $menuSelect = '';
580  $menuSelected = '';
581  $menuSelectAddon = '';
582  while ($entries_row = mysqli_fetch_assoc($entries_res))
583  { // only show data for
584  if ($row['id'] !== $entries_row['id']){
585  $menuSelect .= "
586  <option value=\"" . $entries_row['id'] . "\">" . $entries_row['text'] . "</option>";
587  $menuSelectAddon = "<option value=\"0\">$lang[NO_PARENT]</option>";
588 
589  if ($row['parentID'] === '0')
590  {
591  $menuSelected = "<option value=\"0\" selected>$lang[NO_PARENT]</option>";
592  }
593  else {
594  $parentID2name = $db->query("SELECT text FROM {menu} WHERE menuID = $id AND id=$row[parentID]");
595  $parentName = mysqli_fetch_row($parentID2name);
596  $menuSelected = "<option value=\"" . isset($row['parentID']) . "\" selected>" . isset($parentName[0]) . "</option>";
597  }
598  }
599  }
600  }
601 
602 
603  echo "
604  <tr>
605  <td><a href=\"index.php?page=menu-edit&toggleItem=1&menu=$id&id=$row[id]&published=$row[published]\">
606  <span class=\"label label-$pub\">$pubtext</span></a></td>
607  <td>
608  <input type=\"text\" class=\"form-control pull-left\" name=\"" . $row['id'] . "_id\" readonly value=\"" . $row['id'] . "\" size=\"1\">
609  </td>
610  <td>
611  <input type=\"text\" class=\"form-control pull-left icp icp-auto iconpicker-element iconpicker-input\" role=\"iconpicker\" name=\"" . $row['id'] . "_icon\" value=\"" . $row['icon'] . "\" size=\"2\">
612  </td>
613  <td>
614  <input type=\"text\" class=\"form-control pull-left\" name=\"" . $row['id'] . "_text\" value=\"" . $row['text'] . "\" size=\"12\">
615  </td>
616  <td>
617  <input type=\"text\" class=\"form-control pull-left\" name=\"" . $row['id'] . "_href\" value=\"" . $row['href'] . "\" size=\"45\">
618  </td>
619  <td>
620  <input type=\"text\" class=\"form-control pull-left\" name=\"" . $row['id'] . "_title\" value=\"" . $row['title'] . "\" size=\"12\">
621  </td>
622 
623  <td>
624  <select class=\"form-control\" name=\"" . $row['id'] . "_gid\">
625  <option value=\"$groupName[0]\">$groupName[1]</option>
626  $gidSelect
627  </select>
628  </td>
629 
630  <td>
631  <select class=\"form-control\" name=\"" . $row['id'] . "_target\">
632  <option value=\"" . $row['target'] . "\" selected>" . $row['target'] . "</option>
633  <option value=\"_self\">$lang[SELF]</option>
634  <option value=\"_blank\">$lang[BLANK]</option>
635  <option value=\"_parent\">$lang[PARENT]</option>
636  <option value=\"_top\">$lang[TOP]</option>
637  </select>
638  </td>
639 
640  <td>
641  <input type=\"text\" class=\"form-control pull-left\" name=\"" . $row['id'] . "_sort\" value=\"" . $row['sort'] . "\" size=\"1\" maxlength=\"3\">
642  </td>
643 
644  <td>
645  <select class=\"form-control\" name=\"" . $row['id'] . "_parentID\">
646  ".$menuSelected."
647  ".$menuSelect."
648  ".$menuSelectAddon."
649  </select>
650  </td>
651 
652  <td>
653  <!-- <a href=\"index.php?page=menu-edit&menu=" . $id . "&entry=" . $row['id'] . "&deleteitem=1\"><i class=\"fa fa-trash-o\" alt=\"delete\"></i></a> -->
654 
655  <a class=\"fa fa-trash-o\" role=\"dialog\" data-confirm=\"$lang[DELETE] $lang[ENTRY] &laquo; $row[text] / $row[href] &raquo; $lang[FOR_SURE]?\"
656  title=\"$lang[DELETE]\" href=\"index.php?page=menu-edit&menu=" . $id . "&entry=" . $row['id'] . "&del=1&deleteitem=1delete=true\">
657  </a>
658 
659  <input type=\"hidden\" name=\"" . $row['id'] ."_published\" value=\"".$row['published']."\">
660  </td>
661  </tr>";
662  }
663  echo "</tbody>
664 </table>";
665  }
666  }
667 
668  /**
669  * @brief get menu from database, build and draw it
670  * @copyright 2009-2016 Daniel Retzl
671  * @license https://opensource.org/licenses/MIT
672  * @version 1.0.0
673  * @param object $db database obj
674  * @param int $id affected menu ID
675  * @param object $user current user obj
676  * @param object $template template obj
677  */
678  static function display($db, $id, $user, $template)
679  { /** @param db $db */
680  $divider = '';
681  if (isset($_SESSION['gid'])) {
682  $currentRole = $_SESSION['gid'];
683  } else $currentRole = 2;
684 
685  // Language Stuff
686  // Check, if user has selected a language
687  if (isset($_COOKIE['userSelectedLanguage']) && (!empty($_COOKIE['userSelectedLanguage'])))
688  { // get this language from menu db - TODO: language need to be checked - what happens, if lang is not there?
689  $searchstring = "WHERE menuLanguage = '".$_COOKIE['userSelectedLanguage']."'";
690  }
691  else
692  { // if no cookie was set (no language selected) load the defined global menu
693  $searchstring = "WHERE menuID = '" . $id . "'";
694  }
695 
696  // Select entries from the menu table
697  $res = $db->query("SELECT id, text, title, href, target, parentID, divider, icon
698  FROM {menu}
699  ".$searchstring."
700  and gid <= '" . $currentRole . "'
701  AND published = 1
702  AND (date_publish <= NOW() or date_publish <=> NULL)
703  AND (date_unpublish >= NOW() or date_unpublish <=> NULL)
704  ORDER BY parentid, sort, title");
705 
706  // Create a multidimensional array to conatin a list of items and parents
707  $menu = array(
708  'items' => array(),
709  'parents' => array()
710  );
711  // Builds the array lists with data from the menu table
712  while ($items = mysqli_fetch_assoc($res)) {
713  // Creates entry into items array with current menu item id ie. $menu['items'][1]
714  $menu['items'][$items['id']] = $items;
715  // Creates entry into parents array. Parents array contains a list of all items with children
716  $menu['parents'][$items['parentID']][] = $items['id'];
717  }
718 
719  // Menu builder function, parentId 0 is the root
720 
721  function buildMenu($db, $parent, $menu, $id, $currentRole, $divider, $user, $template)
722  { /** @param db $db */
723 
724  // check if template ID is set
725  if (isset($template) && (!empty($template)))
726  {
727  if (isset($template->id) && (!empty($template->id)))
728  {
729  $bootstrapVersion = \YAWK\template::returnCurrentBootstrapVersion($db, $template->id);
730  }
731  else
732  { // get tpl ID
733  $template->id = \YAWK\template::getCurrentTemplateId($db);
734  $bootstrapVersion = \YAWK\template::returnCurrentBootstrapVersion($db, $template->id);
735  }
736  }
737  else
738  {
741  }
742 
743  // echo "<pre>";print_r($menu);echo"</pre>"; exit;
744  $navBarBrand = '';
745  $navbar_center = template::getTemplateSetting($db, "value", "navbar-center", $user, $template);
746  $navbar_brand = template::getTemplateSetting($db, "value", "navbar-brand", $user, $template);
747  $frontendSwitch = template::getTemplateSetting($db, "value", "frontendSwitch", $user, $template);
748 
749  if ($navbar_center == "1") { $navbar_center = " w-100 justify-content-center"; }
750  else { $navbar_center = ""; }
751 
752  if (!empty($navbar_brand) && ($navbar_brand == 1))
753  {
754  // get menu title
755  $res = $db->query("SELECT name FROM {menu_names} WHERE id='" . $id . "'");
756  $row = mysqli_fetch_row($res);
757  $menuName = $row[0];
758  if (!empty($menuName))
759  {
760  $navBarBrand = "<a class=\"navbar-brand\" id=\"navbar-brand\" href=\"index.html\">" . $menuName . "</a>";
761  }
762  else
763  {
764  $navBarBrand = "";
765  }
766  }
767  else
768  {
769  $navBarBrand = "";
770  }
771 
772  // User is able to switch template from frontend (typically to use a dark/lightmode).
773  if (!empty($frontendSwitch) && ($frontendSwitch == 1))
774  {
775  // get template IDs for light/dark theme (will be set on admin/template-redesign)
776  $darkThemeID = template::getTemplateSetting($db, "value", "darkThemeID", $user, $template);
777  $lightThemeID = template::getTemplateSetting($db, "value", "lightThemeID", $user, $template);
778  if (!empty($darkThemeID) && !empty($lightThemeID))
779  { // html markup that draws our darkmode switch
780  $templateSwitchMarkup = "<div id=\"frontendSwitch\" class=\"pull-right\">
781  <a href=\"index.php?templateID=".$darkThemeID."\" class=\"text-muted\"><i id=\"darkMode\" data-id=\"".$darkThemeID."\" class=\"fa fa-moon-o\"></i></a>
782  <span style=\"color:#ccc; margin-left:5px; margin-right:5px;\">|</span>
783  <a href=\"index.php?templateID=".$lightThemeID."\" class=\"text-muted\"><i id=\"lightMode\" data-id=\"".$lightThemeID."\" class=\"fa fa-sun-o\"></i></a></div>";
784  }
785  }
786 
787  // DRAW BOOTSTRAP 4 MENU
788  if ($bootstrapVersion == "4")
789  {
790  $html = "";
791  $html .= "
792 
793 <nav id=\"navbar\" class=\"navbar navbar-expand-lg navbar-light navbar-bg-custom\" style=\"z-index: 9999;\">
794 ".$navBarBrand."
795  <button class=\"navbar-toggler custom-toggler\" type=\"button\" data-toggle=\"collapse\" data-target=\"#navbarSupportedContent\" aria-controls=\"navbarSupportedContent\" aria-expanded=\"false\" aria-label=\"Toggle navigation\">
796  <span class=\"navbar-toggler-icon\"></span>
797  </button>
798 
799  <div class=\"collapse navbar-collapse\" id=\"navbarSupportedContent\">";
800  // echo "<pre>"; print_r($menu); echo "</pre>";
801 
802  $html .="
803  <ul class=\"navbar-nav ".$navbar_center."\">";
804  foreach ($menu['parents'][$parent] as $itemId) {
805 
806  // check if menu icon is set
807  if (!empty($menu['items'][$itemId]['icon'])){
808  // set markup for icon
809  $icon = "<i class=\"".$menu['items'][$itemId]['icon']." text-muted\"></i> ";
810  }
811  else
812  { // leave icon empty
813  $icon = "";
814  }
815 
816  // set parent w/o child items
817  if (!isset($menu['parents'][$itemId])) {
818 
819  if (!isset($menu['items'][$itemId]['title']) || (empty($menu['items'][$itemId]['title']))) {
820  $title = "";
821  } else {
822  $title = "title=\"" . $menu['items'][$itemId]['title'] . "\"";
823  }
824  $html .= "
825  <li class=\"nav-item\">
826  <a class=\"nav-link\" href=\"".$menu['items'][$itemId]['href']."\" target=\"".$menu['items'][$itemId]['target'] . "\" $title>".$icon." &nbsp;" . $menu['items'][$itemId]['text'] . "</a>
827  </li>";
828  }
829 
830  // menu item got at least 1 child item and should react as dropdown toggle
831  else
832  {
833  $html .= "
834  <li class=\"nav-item dropdown\">
835  <a class=\"nav-link dropdown-toggle\" href=\"" . $menu['items'][$itemId]['href'] . "\" id=\"" . $menu['items'][$itemId]['text'] . "\" role=\"button\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">".$icon." &nbsp;" . $menu['items'][$itemId]['text'] . "</a>
836 ";
837  }
838 
839  // set parents w child items (dropdown lists)
840  if (isset($menu['parents'][$itemId])) {
841  $html .= "
842  <div class=\"dropdown-menu\" aria-labelledby=\"navbarDropdown\">";
843 
844  // select child items from db
845  foreach ($menu['parents'][$itemId] as $child)
846  { // walk through child items
847  // check if menu icon is set
848  if (!empty($menu['items'][$itemId]['icon'])){
849  // set markup for icon
850  $icon = "<i class=\"".$menu['items'][$child]['icon']." text-muted\"></i> ";
851  }
852  else
853  { // leave icon empty
854  $icon = "";
855  }
856  // check if title is set
857  if (!isset($menu['items'][$itemId]['title']) || (empty($menu['items'][$itemId]['title'])))
858  {
859  $title = "";
860  }
861  else
862  {
863  $title = "title=\"$menu[items][$itemId][title]\"";
864  }
865  $html .= "
866  <a class=\"dropdown-item\" href=\"" . $menu['items'][$child]['href']."\" target=\"".$menu['items'][$itemId]['target']."\" $title>".$icon." &nbsp;".$menu['items'][$child]['text']."</a>";
867  }
868  // dropdown navi ends here
869  $html .= "
870  </div>
871  </li>";
872 
873  }
874  }
875 
876  $html.="
877 
878  </ul>";
879 
880  // logout menu link - display only if user is logged in
881  if (isset($_SESSION['username']) && isset($_SESSION['logged_in'])){
882  if ($_SESSION['logged_in'] == true){
883  // display only if logoutmenu is enabled
884  if (\YAWK\settings::getSetting($db, 'userpage_logoutmenu') === '1'){
886  }
887  }
888  }
889  else {$html .= "</ul>
890  <ul class=\"nav navbar-nav navbar-collapse navbar-right\">
891  <li>";
892  // check if userlogin is allowed
893  if (\YAWK\settings::getSetting($db, 'userlogin') === '1')
894  { // load loginbox into navbar
895  $html .= \YAWK\user::drawMenuLoginBox("","", "light");
896  }
897  $html .= "</li></ul>";
898  }
899 
900  $html .= "
901 </div>
902 ".$templateSwitchMarkup."
903 </nav>
904 ";
905 
906  return $html;
907  }
908 
909  // DRAW BOOTSTRAP 3 MENU
910  else if ($bootstrapVersion == "3")
911  {
912  $html = "";
913  $html .= "
914  <nav class=\"navbar navbar-default\" role=\"navigation\" id=\"topnavbar\">
915  <!-- <nav class=\"navbar navbar-default\" role=\"navigation\" id=\"topnavbar\"> -->
916  <div class=\"container\">
917  <div class=\"navbar-header\">
918  <button type=\"button\" class=\"navbar-toggle\" data-toggle=\"collapse\" data-target=\".navbar-collapse\">
919  <span class=\"sr-only\">Toggle navigation</span>
920  <span class=\"icon-bar\"></span>
921  <span class=\"icon-bar\"></span>
922  <span class=\"icon-bar\"></span>
923  </button>
924  $navBarBrand
925  </div> <!-- end nav header -->
926  <div id=\"navbar\" class=\"navbar-collapse collapse\">";
927  if (isset($menu['parents'][$parent])) {
928  // Start Bootstrap menu markup
929  $html .= "<ul class=\"nav navbar-nav\">";
930  // repeat foreach menu entry
931  foreach ($menu['parents'][$parent] as $itemId)
932  {
933  // set parent w/o child items
934  if (!isset($menu['parents'][$itemId])) {
935  if (!isset($menu['items'][$itemId]['title']) || (empty($menu['items'][$itemId]['title'])))
936  {
937  $title = "";
938  }
939  else
940  {
941  $title = "title=\"".$menu['items'][$itemId]['title']."\"";
942  }
943  $html .= "<li><a href=\"".$menu['items'][$itemId]['href']."\" target=\"".$menu['items'][$itemId]['target']."\" $title>" . $menu['items'][$itemId]['text'] . "</a></li>";
944  // vertical spacer
945  // $html .= "".$divider_html."";
946 
947  }
948 
949  // set parents w child items (dropdown lists)
950  if (isset($menu['parents'][$itemId])) {
951  $html .= "<li class=\"dropdown\">
952  <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">" . $menu['items'][$itemId]['text'] . " <b class=\"caret\"></b></a>
953  <ul class=\"dropdown-menu\">";
954 
955  // select child items from db
956  foreach ($menu['parents'][$itemId] as $child) {
957  if (!isset($menu['items'][$itemId]['title']) || (empty($menu['items'][$itemId]['title'])))
958  {
959  $title = "";
960  }
961  else
962  {
963  $title = "title=\"$menu[items][$itemId][title]\"";
964  }
965  $html .= "<li><a href=\"" . $menu['items'][$child]['href']."\" target=\"".$menu['items'][$itemId]['target']."\" $title>".$menu['items'][$child]['text']."</a></li>\n";
966  }
967  // boostrap navi ends here
968  $html .= "</ul>
969  </li>";
970 
971  }
972  } // end html markup of nav area
973  // logout menu link - display only if user is logged in
974  if (isset($_SESSION['username']) && isset($_SESSION['logged_in'])){
975  if ($_SESSION['logged_in'] == true){
976  // display only if logoutmenu is enabled
977  if (\YAWK\settings::getSetting($db, 'userpage_logoutmenu') === '1'){
979  }
980  }
981  }
982  else {$html .= "</ul>
983  <ul class=\"nav navbar-nav navbar-collapse navbar-right\">
984  <li>";
985  // check if userlogin is allowed
986  if (\YAWK\settings::getSetting($db, 'userlogin') === '1')
987  { // load loginbox into navbar
988  $html .= \YAWK\user::drawMenuLoginBox("","", "light");
989  }
990  $html .= "</li></ul>";
991  }
992  $html .= "<!-- /.nav-collapse -->
993  </div><!-- /navbar-inn -->
994  </div> <!-- /container -->
995 </nav><!-- navbar -->
996 ";
997 
998  }
999  return $html;
1000  }
1001  else
1002  {
1003  "Unable to load Bootstrap Menu";
1004  }
1005  return null;
1006  }
1007 
1008  echo buildMenu($db, 0, $menu, $id, $currentRole, $divider, $user, $template);
1009  }
1010 
1011  /**
1012  * @brief draw the logout menu (if user is logged in...)
1013  * @copyright 2009-2016 Daniel Retzl
1014  * @license https://opensource.org/licenses/MIT
1015  * @version 1.0.0
1016  * @param object $db database
1017  * @return bool|string
1018  */
1019  static function drawLogoutMenu($db){
1020  if (isset($_SESSION['username']) && $_SESSION['logged_in']) {
1021  if ($_SESSION['logged_in'] == true) {
1022  $html = "</ul>
1023  <ul id=\"logoutMenu\" class=\"nav navbar-nav navbar-collapse navbar-expand float-right pull-right\">
1024  <li class=\"dropdown\">&nbsp;&nbsp;
1025  <a id=\"logoutLink\" href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">";
1026  $html .= \YAWK\user::getUserImage($db, "frontend", \YAWK\sys::getCurrentUserName(), 22, 22);
1027  $html .= "</a>&nbsp;&nbsp;
1028  <ul id=\"dropdown-menu\" class=\"dropdown-menu\">
1029  <li><a href=\"logout.html\"><i class=\"glyphicon glyphicon-log-out\"></i> &nbsp;Logout</a></li></li>
1030  </ul>";
1031  return $html;
1032  }
1033  }
1034  return false;
1035  }
1036 
1037  /**
1038  * @brief get menu name for given id
1039  * @copyright 2009-2016 Daniel Retzl
1040  * @license https://opensource.org/licenses/MIT
1041  * @version 1.0.0
1042  * @param object $db database
1043  * @param array $lang language
1044  * @param int $id affected menu id
1045  * @return string
1046  */
1047  static function getMenuNameByID($db, $id)
1048  { /* @param $db db */
1049  $menu = '';
1050  if ($res = $db->query("SELECT name from {menu_names} WHERE id = $id"))
1051  {
1052  if ($row = mysqli_fetch_row($res))
1053  {
1054  $menu = $row[0];
1055  }
1056  }
1057  else
1058  {
1059  $menu = "MENU SELECT ID: $id failed";
1060  }
1061  return $menu;
1062  }
1063 
1064  /**
1065  * @brief returns the item title for given item and menu ID
1066  * @param object $db database
1067  * @param int $itemID the menu entry ID
1068  * @param int $menuID the menu ID
1069  * @return string title of the menu entry
1070  */
1072  { /* @param $db db */
1073  $menu = '';
1074  if ($res = $db->query("SELECT title from {menu} WHERE id = $itemID AND menuID = $menuID"))
1075  {
1076  if ($row = mysqli_fetch_row($res))
1077  {
1078  $menu = $row[0];
1079  }
1080  }
1081  else
1082  {
1083  $menu = "could not select menu item";
1084  }
1085  return $menu;
1086  }
1087 
1088  } // ./ class
1089 } // ./ namespace
$itemID
Definition: actions.php:11
print $lang['FILEMAN_UPLOAD']
$templateID
Definition: blog.php:9
static draw($type, $title, $text, $redirect, $delay)
Definition: alert.php:30
static printTooltip($toolTipText)
Draw a small question mark, enabling a tooltip on hover. toolTipText must be a string and will usuall...
Definition: backend.php:603
The default menu class. Serves all the menu functions.
Definition: menu.php:17
$parent
Definition: menu.php:31
static changeLanguage($db, $menu, $menuLanguage)
Change the menu language.
Definition: menu.php:159
static displayGlobalMenu($db, $user, $template)
display the global menu
Definition: menu.php:69
isPublished($db, $menuID)
return true if menu is published, false if not. expects db object and menu ID to get the status from
Definition: menu.php:43
static deleteEntry($db, $menu, $id)
delete a single menu entry
Definition: menu.php:447
toggleOffline($db, $id, $published, $lang)
toggle a whole menu offline
Definition: menu.php:332
static createMenu($db, $name, $lang)
create a new menu
Definition: menu.php:90
$published
Definition: menu.php:28
static getMenuNameByID($db, $id)
get menu name for given id
Definition: menu.php:1047
$menuID
Definition: menu.php:25
static drawLogoutMenu($db)
draw the logout menu (if user is logged in...)
Definition: menu.php:1019
static changeTitle($db, $menu, $menutitle)
Change the menu title.
Definition: menu.php:132
static addEntry($db, $menuID, $text, $href)
add new entry to an existing menu
Definition: menu.php:226
static getMenuStatus($db, $menuid)
check if a whole menu is published or not
Definition: menu.php:281
static displayEditable($db, $id, $lang)
display menu entries for editing in backend
Definition: menu.php:515
static getMenuIdFromLanguage($db, $menuLanguage)
Get menu ID by language.
Definition: menu.php:194
static getMenuEntryStatus($db, $menuid)
check if a single menu entry is published or not
Definition: menu.php:306
toggleItemOffline($db, $id, $published, $menuID)
toggle a menu entry offline
Definition: menu.php:366
static display($db, $id, $user, $template)
get menu from database, build and draw it
Definition: menu.php:678
static editEntry($db, $menu, $id, $text, $title, $href, $sort, $gid, $published, $parentID, $target, $icon)
edit a single menu entry
Definition: menu.php:408
static getMenuItemTitleByID($db, $itemID, $menuID)
returns the item title for given item and menu ID
Definition: menu.php:1071
static getSetting($db, $property)
Get and return value for property from settings database.
Definition: settings.php:470
static iStatusToString($i, $on, $off)
convert a integer status to string variable (0|1) to online / offline
Definition: sys.php:729
static getCurrentUserName()
returns the current user name, if set
Definition: sys.php:1284
static encodeChars($string)
convert german special chars and vowels into legal html
Definition: sys.php:1089
static getCurrentTemplateId(object $db)
return ID of current (active) template
Definition: template.php:73
static returnCurrentBootstrapVersion($db, $templateID)
Return which Bootstrap version is currently loaded in given template.
Definition: template.php:3158
static getUserImage($location, $user, $cssClass, $w, $h)
return and output user image
Definition: user.php:1079
static drawMenuLoginBox($username, $password, $style)
return the html for a menu login box
Definition: user.php:2021
if(!isset($page)) if(!isset($db)) if(!isset($lang)) if(isset($_GET['toggleItem'])) if(isset($_GET['del']) &&($_GET['del']==="1")) if(isset($_POST['changetitle'])) if(isset($_POST['changeLanguage'])) if(isset($_POST['add'])) else if(isset($_POST['save'])) else $menuName
Definition: menu-edit.php:217
$menuLanguage
Definition: menu-edit.php:309
This class serves methods to create backup from files.
Definition: AdminLTE.php:2
print $tourdates date
$gid
Definition: user-new.php:104