gorenest.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2024 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. /**
  21. * the menu class. Provides the menu based on user
  22. */
  23. class GoreNest {
  24. /**
  25. * the global DB object
  26. *
  27. * @var mysqli
  28. */
  29. private mysqli $_DB;
  30. /**
  31. * the current loaded user
  32. *
  33. * @var Doomguy
  34. */
  35. private Doomguy $_User;
  36. /**
  37. * the already loaded menu information
  38. * to avoid multiple calls to the DB
  39. *
  40. * @var array
  41. */
  42. private array $_menuData = array();
  43. /**
  44. * Array for faster check which call is allowed
  45. *
  46. * @var array
  47. */
  48. private array $_allowedPageRequests = array();
  49. /**
  50. * GoreNest constructor.
  51. *
  52. * @param mysqli $db
  53. * @param Doomguy $user
  54. */
  55. public function __construct(mysqli $db, Doomguy $user) {
  56. $this->_DB = $db;
  57. $this->_User = $user;
  58. $this->_loadMenu();
  59. }
  60. /**
  61. * Get the menu data for given area and category.
  62. * This shows only entries which have a category set.
  63. * No category can be used for hidden entries.
  64. *
  65. * @param string $category
  66. * @param bool $reload
  67. * @param array $_contextActions
  68. * @return array
  69. */
  70. public function get(string $category, bool $reload = false, array $_contextActions = array()): array {
  71. $ret = array();
  72. if(empty($category)) return $ret;
  73. if($reload === false && isset($this->_menuData[$category])) {
  74. return $this->_updateContextActions($this->_menuData[$category], $_contextActions);
  75. }
  76. $this->_loadMenu();
  77. if(isset($this->_menuData[$category])) {
  78. $ret = $this->_menuData[$category];
  79. }
  80. return $this->_updateContextActions($ret, $_contextActions);
  81. }
  82. /**
  83. * Load the complete menu
  84. *
  85. * @return void
  86. */
  87. private function _loadMenu(): void {
  88. # reset the menu
  89. $this->_menuData = array();
  90. $queryStr = "SELECT `id`, `text`, `action`, `icon`, `category`, `contextaction`
  91. FROM `".DB_PREFIX."_menu`
  92. WHERE ".$this->_User->getSQLRightsString()."
  93. ORDER BY position";
  94. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  95. try {
  96. $query = $this->_DB->query($queryStr);
  97. if($query !== false && $query->num_rows > 0) {
  98. while(($result = $query->fetch_assoc()) != false) {
  99. $this->_allowedPageRequests[$result['action']] = $result['action'];
  100. $this->_menuData[$result['category']][$result['id']] = $result;
  101. }
  102. }
  103. }
  104. catch (Exception $e) {
  105. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  106. }
  107. }
  108. /**
  109. * Return allowed page requests
  110. *
  111. * @return array
  112. */
  113. public function allowedPageRequests(): array {
  114. return $this->_allowedPageRequests;
  115. }
  116. /**
  117. * Check if there is the need to modify the action value based on contextaction column
  118. * and $_contextActions array
  119. *
  120. * @param array $_menuData
  121. * @param array $_contextActions
  122. * @return array
  123. */
  124. private function _updateContextActions(array $_menuData, array $_contextActions): array {
  125. if(!empty($_contextActions)) {
  126. foreach($_menuData as $id=>$data) {
  127. if(isset($_contextActions[$data['contextaction']])) {
  128. $_menuData[$id]['action'] = $data['action'].'&'.$data['contextaction'].'='.$_contextActions[$data['contextaction']];
  129. }
  130. }
  131. }
  132. return $_menuData;
  133. }
  134. }