menu.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Copyright (C) 2010 - 2019
  17. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. /**
  22. * menu class
  23. *
  24. * @method null delete
  25. * @method null toggle
  26. * @method null copy
  27. */
  28. if (!class_exists('menu')) {
  29. class menu {
  30. /**
  31. * declare the variables
  32. */
  33. private $app_name;
  34. private $app_uuid;
  35. private $name;
  36. private $table;
  37. private $toggle_field;
  38. private $toggle_values;
  39. private $description_field;
  40. private $location;
  41. public $menu_uuid;
  42. public $menu_language;
  43. /**
  44. * called when the object is created
  45. */
  46. public function __construct() {
  47. //assign the variables
  48. $this->app_name = 'menus';
  49. $this->app_uuid = 'f4b3b3d2-6287-489c-2a00-64529e46f2d7';
  50. $this->location = 'menus.php';
  51. }
  52. /**
  53. * called when there are no references to a particular object
  54. * unset the variables used in the class
  55. */
  56. public function __destruct() {
  57. foreach ($this as $key => $value) {
  58. unset($this->$key);
  59. }
  60. }
  61. /**
  62. * delete rows from the database
  63. */
  64. public function delete($records) {
  65. //assign the variables
  66. $this->name = 'menu';
  67. $this->table = 'menus';
  68. if (permission_exists($this->name.'_delete')) {
  69. //add multi-lingual support
  70. $language = new text;
  71. $text = $language->get();
  72. //validate the token
  73. $token = new token;
  74. if (!$token->validate($_SERVER['PHP_SELF'])) {
  75. message::add($text['message-invalid_token'],'negative');
  76. header('Location: '.$this->location);
  77. exit;
  78. }
  79. //delete multiple records
  80. if (is_array($records) && @sizeof($records) != 0) {
  81. //build the delete array
  82. $x = 0;
  83. foreach ($records as $record) {
  84. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  85. //remove menu languages
  86. $array['menu_languages'][$x][$this->name.'_uuid'] = $record['uuid'];
  87. //remove menu item groups
  88. $array['menu_item_groups'][$x][$this->name.'_uuid'] = $record['uuid'];
  89. //remove menu items
  90. $array['menu_items'][$x][$this->name.'_uuid'] = $record['uuid'];
  91. //build array to remove the menu
  92. $array[$this->table][$x][$this->name.'_uuid'] = $record['uuid'];
  93. //increment
  94. $x++;
  95. }
  96. }
  97. //delete the checked rows
  98. if (is_array($array) && @sizeof($array) != 0) {
  99. //grant temporary permissions
  100. $p = new permissions;
  101. $p->add('menu_item_delete', 'temp');
  102. $p->add('menu_item_group_delete', 'temp');
  103. $p->add('menu_language_delete', 'temp');
  104. //execute delete
  105. $database = new database;
  106. $database->app_name = $this->app_name;
  107. $database->app_uuid = $this->app_uuid;
  108. $database->delete($array);
  109. unset($array);
  110. //revoke temporary permissions
  111. $p->delete('menu_item_delete', 'temp');
  112. $p->delete('menu_item_group_delete', 'temp');
  113. $p->delete('menu_language_delete', 'temp');
  114. //set message
  115. message::add($text['message-delete']);
  116. }
  117. unset($records);
  118. }
  119. }
  120. }
  121. public function delete_items($records) {
  122. //assign the variables
  123. $this->name = 'menu_item';
  124. $this->table = 'menu_items';
  125. if (permission_exists($this->name.'_delete')) {
  126. //add multi-lingual support
  127. $language = new text;
  128. $text = $language->get();
  129. //validate the token
  130. $token = new token;
  131. if (!$token->validate('/core/menu/menu_item_list.php')) {
  132. message::add($text['message-invalid_token'],'negative');
  133. header('Location: '.$this->location);
  134. exit;
  135. }
  136. //delete multiple records
  137. if (is_array($records) && @sizeof($records) != 0) {
  138. //build the delete array
  139. $x = 0;
  140. foreach ($records as $record) {
  141. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  142. //build array
  143. $uuids[] = "'".$record['uuid']."'";
  144. //remove menu languages
  145. $array['menu_languages'][$x][$this->name.'_uuid'] = $record['uuid'];
  146. //remove menu item groups
  147. $array['menu_item_groups'][$x][$this->name.'_uuid'] = $record['uuid'];
  148. //remove menu items
  149. $array[$this->table][$x][$this->name.'_uuid'] = $record['uuid'];
  150. //increment
  151. $x++;
  152. }
  153. }
  154. //include child menu items
  155. if (is_array($uuids) && @sizeof($uuids) != 0) {
  156. $sql = "select menu_item_uuid as uuid from v_".$this->table." ";
  157. $sql .= "where menu_item_parent_uuid in (".implode(', ', $uuids).") ";
  158. $database = new database;
  159. $rows = $database->select($sql, $parameters, 'all');
  160. if (is_array($rows) && @sizeof($rows) != 0) {
  161. foreach ($rows as $row) {
  162. //remove menu languages
  163. $array['menu_languages'][$x][$this->name.'_uuid'] = $row['uuid'];
  164. //remove menu item groups
  165. $array['menu_item_groups'][$x][$this->name.'_uuid'] = $row['uuid'];
  166. //remove menu items
  167. $array[$this->table][$x][$this->name.'_uuid'] = $row['uuid'];
  168. //increment
  169. $x++;
  170. }
  171. }
  172. }
  173. //delete the checked rows
  174. if (is_array($array) && @sizeof($array) != 0) {
  175. //grant temporary permissions
  176. $p = new permissions;
  177. $p->add('menu_language_delete', 'temp');
  178. $p->add('menu_item_group_delete', 'temp');
  179. //execute delete
  180. $database = new database;
  181. $database->app_name = $this->app_name;
  182. $database->app_uuid = $this->app_uuid;
  183. $database->delete($array);
  184. unset($array);
  185. //revoke temporary permissions
  186. $p->delete('menu_language_delete', 'temp');
  187. $p->delete('menu_item_group_delete', 'temp');
  188. //set message
  189. message::add($text['message-delete']);
  190. }
  191. unset($records);
  192. }
  193. }
  194. }
  195. /**
  196. * toggle a field between two values
  197. */
  198. public function toggle_items($records) {
  199. //assign the variables
  200. $this->name = 'menu_item';
  201. $this->table = 'menu_items';
  202. $this->toggle_field = 'menu_item_protected';
  203. $this->toggle_values = ['true','false'];
  204. if (permission_exists($this->name.'_edit')) {
  205. //add multi-lingual support
  206. $language = new text;
  207. $text = $language->get();
  208. //validate the token
  209. $token = new token;
  210. if (!$token->validate('/core/menu/menu_item_list.php')) {
  211. message::add($text['message-invalid_token'],'negative');
  212. header('Location: '.$this->location);
  213. exit;
  214. }
  215. //toggle the checked records
  216. if (is_array($records) && @sizeof($records) != 0) {
  217. //get current toggle state
  218. foreach ($records as $record) {
  219. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  220. $uuids[] = "'".$record['uuid']."'";
  221. }
  222. }
  223. if (is_array($uuids) && @sizeof($uuids) != 0) {
  224. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  225. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  226. $database = new database;
  227. $rows = $database->select($sql, $parameters, 'all');
  228. if (is_array($rows) && @sizeof($rows) != 0) {
  229. foreach ($rows as $row) {
  230. $states[$row['uuid']] = $row['toggle'] == '' ? $this->toggle_values[1] : $row['toggle'];
  231. }
  232. }
  233. unset($sql, $parameters, $rows, $row);
  234. }
  235. //build update array
  236. $x = 0;
  237. foreach ($states as $uuid => $state) {
  238. //create the array
  239. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  240. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  241. //increment
  242. $x++;
  243. }
  244. //save the changes
  245. if (is_array($array) && @sizeof($array) != 0) {
  246. //save the array
  247. $database = new database;
  248. $database->app_name = $this->app_name;
  249. $database->app_uuid = $this->app_uuid;
  250. $database->save($array);
  251. unset($array);
  252. //set message
  253. message::add($text['message-toggle']);
  254. }
  255. unset($records, $states);
  256. }
  257. }
  258. }
  259. /**
  260. * delete items in the menu that are not protected
  261. */
  262. public function delete_unprotected() {
  263. //remove existing menu languages
  264. $sql = "delete from v_menu_languages ";
  265. $sql .= "where menu_uuid = :menu_uuid ";
  266. $sql .= "and menu_item_uuid in ( ";
  267. $sql .= " select menu_item_uuid ";
  268. $sql .= " from v_menu_items ";
  269. $sql .= " where menu_uuid = :menu_uuid ";
  270. $sql .= " and ( ";
  271. $sql .= " menu_item_protected <> 'true' ";
  272. $sql .= " or menu_item_protected is null ";
  273. $sql .= " ) ";
  274. $sql .= ") ";
  275. $parameters['menu_uuid'] = $this->menu_uuid;
  276. $database = new database;
  277. $database->execute($sql, $parameters);
  278. unset($sql, $parameters);
  279. //remove existing unprotected menu item groups
  280. $sql = "delete from v_menu_item_groups ";
  281. $sql .= "where menu_uuid = :menu_uuid ";
  282. $sql .= "and menu_item_uuid in ( ";
  283. $sql .= " select menu_item_uuid ";
  284. $sql .= " from v_menu_items ";
  285. $sql .= " where menu_uuid = :menu_uuid ";
  286. $sql .= " and ( ";
  287. $sql .= " menu_item_protected <> 'true' ";
  288. $sql .= " or menu_item_protected is null ";
  289. $sql .= " ) ";
  290. $sql .= ") ";
  291. $parameters['menu_uuid'] = $this->menu_uuid;
  292. $database = new database;
  293. $database->execute($sql, $parameters);
  294. unset($sql, $parameters);
  295. //remove existing unprotected menu items
  296. $sql = "delete from v_menu_items ";
  297. $sql .= "where menu_uuid = :menu_uuid ";
  298. $sql .= "and ( ";
  299. $sql .= " menu_item_protected <> 'true' ";
  300. $sql .= " or menu_item_protected is null ";
  301. $sql .= ") ";
  302. $parameters['menu_uuid'] = $this->menu_uuid;
  303. $database = new database;
  304. $database->execute($sql, $parameters);
  305. unset($sql, $parameters);
  306. }
  307. /**
  308. * restore the menu
  309. */
  310. public function restore() {
  311. //get the $apps array from the installed apps from the core and mod directories
  312. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_menu.php");
  313. $x = 0;
  314. if (is_array($config_list)) {
  315. foreach ($config_list as &$config_path) {
  316. $app_path = dirname($config_path);
  317. $app_path = preg_replace('/\A.*(\/.*\/.*)\z/', '$1', $app_path);
  318. $y = 0;
  319. try {
  320. //echo "[".$x ."] ".$config_path."\n";
  321. include($config_path);
  322. $x++;
  323. }
  324. catch (Exception $e) {
  325. echo 'exception caught: ' . $e->getMessage() . "\n";
  326. exit;
  327. }
  328. }
  329. }
  330. //get the list of languages
  331. $language = new text;
  332. //create a uuid array of the original uuid used as the key and new uuid as the value
  333. if (is_array($apps)) {
  334. $x = 0;
  335. foreach ($apps as $row) {
  336. if (is_array($row['menu'])) {
  337. foreach ($row['menu'] as $menu) {
  338. $uuid_array[$menu['uuid']] = uuid();
  339. }
  340. }
  341. }
  342. }
  343. //if the item uuid is not currently in the db then add it
  344. $sql = "select * from v_menu_items ";
  345. $sql .= "where menu_uuid = :menu_uuid ";
  346. $parameters['menu_uuid'] = $this->menu_uuid;
  347. $database = new database;
  348. $menu_items = $database->select($sql, $parameters, 'all');
  349. //use the app array to restore the default menu
  350. if (is_array($apps)) {
  351. $x = 0;
  352. foreach ($apps as $row) {
  353. if (is_array($row['menu'])) {
  354. foreach ($row['menu'] as $menu) {
  355. //set the variables
  356. if (strlen($menu['title'][$this->menu_language]) > 0) {
  357. $menu_item_title = $menu['title'][$this->menu_language];
  358. }
  359. else {
  360. $menu_item_title = $menu['title']['en-us'];
  361. }
  362. $uuid = $menu['uuid'];
  363. $menu_item_uuid = $uuid_array[$menu['uuid']];
  364. $menu_item_parent_uuid = $uuid_array[$menu['parent_uuid']];
  365. $menu_item_category = $menu['category'];
  366. $menu_item_icon = $menu['icon'];
  367. $menu_item_path = $menu['path'];
  368. $menu_item_order = $menu['order'];
  369. $menu_item_description = $menu['desc'];
  370. //check if the menu item exists and if it does set the row array
  371. $menu_item_exists = false;
  372. foreach ($menu_items as $item) {
  373. if ($item['uuid'] == $menu['uuid']) {
  374. $menu_item_exists = true;
  375. $row = $item;
  376. }
  377. }
  378. //item exists in the database
  379. if ($menu_item_exists) {
  380. //get parent_menu_item_protected
  381. foreach ($menu_items as $item) {
  382. if ($item['uuid'] == $menu['parent_uuid']) {
  383. $parent_menu_item_protected = $item['menu_item_protected'];
  384. }
  385. }
  386. //parent is not protected so the parent uuid needs to be updated
  387. if (is_uuid($menu_item_parent_uuid) && $menu_item_parent_uuid != $row['menu_item_parent_uuid'] && $parent_menu_item_protected != 'true') {
  388. $array['menu_items'][$x]['menu_item_uuid'] = $row['menu_item_uuid'];
  389. $array['menu_items'][$x]['menu_item_parent_uuid'] = $menu_item_parent_uuid;
  390. $x++;
  391. }
  392. }
  393. //item does not exist in the database
  394. if (!$menu_item_exists) {
  395. if ($menu_item_uuid != $menu_item_parent_uuid) {
  396. $array['menu_items'][$x]['menu_item_uuid'] = $menu_item_uuid;
  397. $array['menu_items'][$x]['menu_uuid'] = $this->menu_uuid;
  398. $array['menu_items'][$x]['uuid'] = $uuid;
  399. $array['menu_items'][$x]['menu_item_title'] = $menu_item_title;
  400. $array['menu_items'][$x]['menu_item_link'] = $menu_item_path;
  401. $array['menu_items'][$x]['menu_item_category'] = $menu_item_category;
  402. $array['menu_items'][$x]['menu_item_icon'] = $menu_item_icon;
  403. if (strlen($menu_item_order) > 0) {
  404. $array['menu_items'][$x]['menu_item_order'] = $menu_item_order;
  405. }
  406. if (is_uuid($menu_item_parent_uuid)) {
  407. $array['menu_items'][$x]['menu_item_parent_uuid'] = $menu_item_parent_uuid;
  408. }
  409. $array['menu_items'][$x]['menu_item_description'] = $menu_item_description;
  410. $x++;
  411. }
  412. }
  413. unset($field, $parameters, $num_rows);
  414. //set the menu languages
  415. if (!$menu_item_exists && is_array($language->languages)) {
  416. foreach ($language->languages as $menu_language) {
  417. //set the menu item title
  418. $menu_item_title = $menu["title"][$menu_language];
  419. if (strlen($menu_item_title) == 0) {
  420. $menu_item_title = $menu["title"]['en-us'];
  421. }
  422. //build insert array
  423. $array['menu_languages'][$x]['menu_language_uuid'] = uuid();
  424. $array['menu_languages'][$x]['menu_item_uuid'] = $menu_item_uuid;
  425. $array['menu_languages'][$x]['menu_uuid'] = $this->menu_uuid;
  426. $array['menu_languages'][$x]['menu_language'] = $menu_language;
  427. $array['menu_languages'][$x]['menu_item_title'] = $menu_item_title;
  428. $x++;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. if (is_array($array) && @sizeof($array) != 0) {
  435. //grant temporary permissions
  436. $p = new permissions;
  437. $p->add('menu_item_add', 'temp');
  438. $p->add('menu_language_add', 'temp');
  439. //execute insert
  440. $database = new database;
  441. $database->app_name = 'menu';
  442. $database->app_uuid = 'f4b3b3d2-6287-489c-2a00-64529e46f2d7';
  443. $database->save($array);
  444. unset($array);
  445. //revoke temporary permissions
  446. $p->delete('menu_item_add', 'temp');
  447. $p->delete('menu_language_add', 'temp');
  448. }
  449. }
  450. //make sure the default user groups exist
  451. $group = new groups;
  452. $group->defaults();
  453. //get default global group_uuids
  454. $sql = "select group_uuid, group_name from v_groups ";
  455. $sql .= "where domain_uuid is null ";
  456. $database = new database;
  457. $result = $database->select($sql, null, 'all');
  458. if (is_array($result) && @sizeof($result) != 0) {
  459. foreach ($result as $row) {
  460. $group_uuids[$row['group_name']] = $row['group_uuid'];
  461. }
  462. }
  463. unset($sql, $result, $row);
  464. //if there are no groups listed in v_menu_item_groups under menu_item_uuid then add the default groups
  465. if (is_array($apps)) {
  466. $x = 0;
  467. foreach($apps as $app) {
  468. if (is_array($apps)) {
  469. foreach ($app['menu'] as $sub_row) {
  470. if (isset($sub_row['groups'])) {
  471. foreach ($sub_row['groups'] as $group) {
  472. $sql = "select count(*) from v_menu_item_groups ";
  473. $sql .= "where menu_item_uuid = :menu_item_uuid ";
  474. $sql .= "and menu_uuid = :menu_uuid ";
  475. $sql .= "and group_name = :group_name ";
  476. $sql .= "and group_uuid = :group_uuid ";
  477. $parameters['menu_item_uuid'] = $uuid_array[$sub_row['uuid']];
  478. $parameters['menu_uuid'] = $this->menu_uuid;
  479. $parameters['group_name'] = $group;
  480. $parameters['group_uuid'] = $group_uuids[$group];
  481. $database = new database;
  482. $num_rows = $database->select($sql, $parameters, 'column');
  483. if ($num_rows == 0) {
  484. //no menu item groups found, build insert array for defaults
  485. $array['menu_item_groups'][$x]['menu_item_group_uuid'] = uuid();
  486. $array['menu_item_groups'][$x]['menu_uuid'] = $this->menu_uuid;
  487. $array['menu_item_groups'][$x]['menu_item_uuid'] = $uuid_array[$sub_row['uuid']];
  488. $array['menu_item_groups'][$x]['group_name'] = $group;
  489. $array['menu_item_groups'][$x]['group_uuid'] = $group_uuids[$group];
  490. $x++;
  491. }
  492. unset($sql, $parameters, $num_rows);
  493. }
  494. }
  495. }
  496. }
  497. }
  498. if (is_array($array) && @sizeof($array) != 0) {
  499. //grant temporary permissions
  500. $p = new permissions;
  501. $p->add('menu_item_group_add', 'temp');
  502. //execute insert
  503. $database = new database;
  504. $database->app_name = 'menu';
  505. $database->app_uuid = 'f4b3b3d2-6287-489c-2a00-64529e46f2d7';
  506. $database->save($array);
  507. unset($array);
  508. //revoke temporary permissions
  509. $p->delete('menu_item_group_add', 'temp');
  510. }
  511. }
  512. }
  513. /**
  514. * create the menu
  515. */
  516. public function build_html($menu_item_level = 0) {
  517. $menu_html_full = '';
  518. $menu_array = $this->menu_array();
  519. if (!isset($_SESSION['groups'])) {
  520. $_SESSION['groups'][0]['group_name'] = 'public';
  521. }
  522. if (is_array($menu_array)) {
  523. foreach($menu_array as $menu_field) {
  524. //set the variables
  525. $menu_item_link = $menu_field['menu_item_link'];
  526. $menu_item_category = $menu_field['menu_item_category'];
  527. $menu_items = $menu_field['menu_items'];
  528. //prepare the protected menus
  529. $menu_item_title = ($menu_field['menu_item_protected'] == "true") ? $menu_field['menu_item_title'] : $menu_field['menu_language_title'];
  530. //prepare the menu_tags according to the category
  531. $menu_tags = '';
  532. switch ($menu_item_category) {
  533. case "internal":
  534. $menu_tags = "href='".PROJECT_PATH.$submenu_item_link."'";
  535. break;
  536. case "external":
  537. if (substr($submenu_item_link, 0,1) == "/") {
  538. $submenu_item_link = PROJECT_PATH.$submenu_item_link;
  539. }
  540. $menu_tags = "href='".$submenu_item_link."' target='_blank'";
  541. break;
  542. case "email":
  543. $menu_tags = "href='mailto:".$submenu_item_link."'";
  544. break;
  545. }
  546. if ($menu_item_level == 0) {
  547. $menu_html = "<ul class='menu_main'>\n";
  548. $menu_html .= "<li>\n";
  549. if (!isset($_SESSION["username"])) {
  550. $_SESSION["username"] = '';
  551. }
  552. if (strlen($_SESSION["username"]) == 0) {
  553. $menu_html .= "<a $menu_tags style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n";
  554. }
  555. else {
  556. if ($submenu_item_link == "/login.php" || $submenu_item_link == "/users/signup.php") {
  557. //hide login and sign-up when the user is logged in
  558. }
  559. else {
  560. if (strlen($submenu_item_link) == 0) {
  561. $menu_html .= "<h2 align='center' style=''>".$menu_item_title."</h2>\n";
  562. }
  563. else {
  564. $menu_html .= "<a ".$menu_tags." style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n";
  565. }
  566. }
  567. }
  568. }
  569. if (is_array($menu_field['menu_items']) && count($menu_field['menu_items']) > 0) {
  570. $menu_html .= $this->build_child_html($menu_item_level, $menu_field['menu_items']);
  571. }
  572. if ($menu_item_level == 0) {
  573. $menu_html .= "</li>\n";
  574. $menu_html .= "</ul>\n\n";
  575. }
  576. $menu_html_full .= $menu_html;
  577. } //end for each
  578. }
  579. return $menu_html_full;
  580. }
  581. /**
  582. * create the sub menus
  583. */
  584. private function build_child_html($menu_item_level, $submenu_array) {
  585. $menu_item_level = $menu_item_level+1;
  586. if (count($_SESSION['groups']) == 0) {
  587. $_SESSION['groups'][0]['group_name'] = 'public';
  588. }
  589. if (is_array($submenu_array)) {
  590. //child menu found
  591. $submenu_html = "<ul class='menu_sub'>\n";
  592. foreach($submenu_array as $submenu_field) {
  593. //set the variables
  594. $menu_item_link = $submenu_field['menu_item_link'];
  595. $menu_item_category = $submenu_field['menu_item_category'];
  596. $menu_items = $submenu_field['menu_items'];
  597. //prepare the protected menus
  598. $menu_item_title = ($submenu_field['menu_item_protected'] == "true") ? $submenu_field['menu_item_title'] : $submenu_field['menu_language_title'];
  599. //prepare the menu_tags according to the category
  600. switch ($menu_item_category) {
  601. case "internal":
  602. $menu_tags = "href='".PROJECT_PATH.$menu_item_link."'";
  603. break;
  604. case "external":
  605. if (substr($menu_item_link, 0,1) == "/") {
  606. $menu_item_link = PROJECT_PATH.$menu_item_link;
  607. }
  608. $menu_tags = "href='".$menu_item_link."' target='_blank'";
  609. break;
  610. case "email":
  611. $menu_tags = "href='mailto:".$menu_item_link."'";
  612. break;
  613. }
  614. $submenu_html .= "<li>";
  615. //get sub menu for children
  616. if (is_array($menu_items) && count($menu_items) > 0) {
  617. $str_child_menu = $this->build_child_html($menu_item_level, $menu_items);
  618. }
  619. if (strlen($str_child_menu) > 1) {
  620. $submenu_html .= "<a ".$menu_tags.">".$menu_item_title."</a>";
  621. $submenu_html .= $str_child_menu;
  622. unset($str_child_menu);
  623. }
  624. else {
  625. $submenu_html .= "<a ".$menu_tags.">".$menu_item_title."</a>";
  626. }
  627. $submenu_html .= "</li>\n";
  628. }
  629. unset($submenu_array);
  630. $submenu_html .="</ul>\n";
  631. return $submenu_html;
  632. }
  633. }
  634. /**
  635. * create the menu array
  636. */
  637. public function menu_array($menu_item_level = 0) {
  638. //if there are no groups then set the public group
  639. if (!isset($_SESSION['groups'][0]['group_name'])) {
  640. $_SESSION['groups'][0]['group_name'] = 'public';
  641. }
  642. //get the menu from the database
  643. $sql = "select i.menu_item_link, l.menu_item_title as menu_language_title, ";
  644. $sql .= "i.menu_item_title, i.menu_item_protected, i.menu_item_category, ";
  645. $sql .= "i.menu_item_icon, i.menu_item_uuid, i.menu_item_parent_uuid ";
  646. $sql .= "from v_menu_items as i, v_menu_languages as l ";
  647. $sql .= "where i.menu_item_uuid = l.menu_item_uuid ";
  648. $sql .= "and l.menu_language = :menu_language ";
  649. $sql .= "and l.menu_uuid = :menu_uuid ";
  650. $sql .= "and i.menu_uuid = :menu_uuid ";
  651. $sql .= "and i.menu_item_parent_uuid is null ";
  652. $sql .= "and i.menu_item_uuid in ";
  653. $sql .= "( ";
  654. $sql .= "select menu_item_uuid ";
  655. $sql .= "from v_menu_item_groups ";
  656. $sql .= "where menu_uuid = :menu_uuid ";
  657. $x = 0;
  658. foreach($_SESSION['groups'] as $row) {
  659. $sql_where_or[] = "group_name = :group_name_".$x;
  660. $parameters['group_name_'.$x] = $row['group_name'];
  661. $x++;
  662. }
  663. if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) {
  664. $sql .= "and ( ";
  665. $sql .= implode(' or ', $sql_where_or);
  666. $sql .= ") ";
  667. }
  668. $sql .= "and menu_item_uuid is not null ";
  669. $sql .= ") ";
  670. $sql .= "order by i.menu_item_order asc ";
  671. $parameters['menu_language'] = $_SESSION['domain']['language']['code'];
  672. $parameters['menu_uuid'] = $this->menu_uuid;
  673. $database = new database;
  674. $result = $database->select($sql, $parameters, 'all');
  675. unset($sql, $parameters);
  676. //save the menu into an array
  677. $x = 0;
  678. $a = Array();
  679. if (is_array($result) && @sizeof($result) != 0) {
  680. foreach($result as $row) {
  681. //add the row to the array
  682. $a[$x] = $row;
  683. //add the sub menus to the array
  684. $menu_item_level = 0;
  685. if (strlen($row['menu_item_uuid']) > 0) {
  686. $a[$x]['menu_items'] = $this->menu_child_array($menu_item_level, $row['menu_item_uuid']);
  687. }
  688. //increment the row number
  689. $x++;
  690. }
  691. }
  692. unset($result, $row);
  693. //return the array
  694. return $a;
  695. }
  696. /**
  697. * create the sub menus
  698. */
  699. private function menu_child_array($menu_item_level, $menu_item_uuid) {
  700. //set the level
  701. $menu_item_level = $menu_item_level + 1;
  702. //if there are no groups then set the public group
  703. if (!isset($_SESSION['groups'][0]['group_name'])) {
  704. $_SESSION['groups'][0]['group_name'] = 'public';
  705. }
  706. //get the child menu from the database
  707. $sql = "select i.menu_item_link, l.menu_item_title as menu_language_title, i.menu_item_title, i.menu_item_protected, i.menu_item_category, i.menu_item_icon, i.menu_item_uuid, i.menu_item_parent_uuid ";
  708. $sql .= "from v_menu_items as i, v_menu_languages as l ";
  709. $sql .= "where i.menu_item_uuid = l.menu_item_uuid ";
  710. $sql .= "and l.menu_language = :menu_language ";
  711. $sql .= "and l.menu_uuid = :menu_uuid ";
  712. $sql .= "and i.menu_uuid = :menu_uuid ";
  713. $sql .= "and i.menu_item_parent_uuid = :menu_item_parent_uuid ";
  714. $sql .= "and i.menu_item_uuid in ";
  715. $sql .= "( ";
  716. $sql .= "select menu_item_uuid ";
  717. $sql .= "from v_menu_item_groups ";
  718. $sql .= "where menu_uuid = :menu_uuid ";
  719. $x = 0;
  720. foreach($_SESSION['groups'] as $row) {
  721. $sql_where_or[] = "group_name = :group_name_".$x;
  722. $parameters['group_name_'.$x] = $row['group_name'];
  723. $x++;
  724. }
  725. if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) {
  726. $sql .= "and ( ";
  727. $sql .= implode(' or ', $sql_where_or);
  728. $sql .= ") ";
  729. }
  730. $sql .= ") ";
  731. $sql .= "order by l.menu_item_title, i.menu_item_order asc ";
  732. $parameters['menu_language'] = $_SESSION['domain']['language']['code'];
  733. $parameters['menu_uuid'] = $this->menu_uuid;
  734. $parameters['menu_item_parent_uuid'] = $menu_item_uuid;
  735. $database = new database;
  736. $sub_result = $database->select($sql, $parameters, 'all');
  737. unset($sql, $parameters);
  738. //save the child menu into an array
  739. $x = 0;
  740. $a = Array();
  741. if (is_array($sub_result) && @sizeof($sub_result) != 0) {
  742. foreach($sub_result as $row) {
  743. //set the variables
  744. $menu_item_link = $row['menu_item_link'];
  745. $menu_item_category = $row['menu_item_category'];
  746. $menu_item_icon = $row['menu_item_icon'];
  747. $menu_item_uuid = $row['menu_item_uuid'];
  748. $menu_item_parent_uuid = $row['menu_item_parent_uuid'];
  749. //add the row to the array
  750. $a[$x] = $row;
  751. //prepare the protected menus
  752. if ($row['menu_item_protected'] == "true") {
  753. $a[$x]['menu_item_title'] = $row['menu_item_title'];
  754. }
  755. else {
  756. $a[$x]['menu_item_title'] = $row['menu_language_title'];
  757. }
  758. //get sub menu for children
  759. if (strlen($menu_item_uuid) > 0) {
  760. $a[$x]['menu_items'] = $this->menu_child_array($menu_item_level, $menu_item_uuid);
  761. }
  762. //increment the row
  763. $x++;
  764. }
  765. }
  766. unset($sub_result, $row);
  767. //return the array
  768. return $a;
  769. }
  770. /**
  771. * add the default menu when no menu exists
  772. */
  773. public function menu_default() {
  774. //set the default menu_uuid
  775. $this->menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286';
  776. //check to see if any menu exists
  777. $sql = "select count(*) as count from v_menus ";
  778. $sql .= "where menu_uuid = :menu_uuid ";
  779. $parameters['menu_uuid'] = $this->menu_uuid;
  780. $database = new database;
  781. $num_rows = $database->select($sql, $parameters, 'column');
  782. if ($num_rows == 0) {
  783. //built insert array
  784. $array['menus'][0]['menu_uuid'] = $this->menu_uuid;
  785. $array['menus'][0]['menu_name'] = 'default';
  786. $array['menus'][0]['menu_language'] = 'en-us';
  787. $array['menus'][0]['menu_description'] = 'Default Menu';
  788. //grant temporary permissions
  789. $p = new permissions;
  790. $p->add('menu_add', 'temp');
  791. //execute insert
  792. $database = new database;
  793. $database->app_name = 'menu';
  794. $database->app_uuid = 'f4b3b3d2-6287-489c-2a00-64529e46f2d7';
  795. $database->save($array);
  796. unset($array);
  797. //revoke temporary permissions
  798. $p->delete('menu_add', 'temp');
  799. //add the menu items
  800. $this->restore();
  801. }
  802. unset($sql, $parameters, $result, $row);
  803. }
  804. }
  805. }
  806. ?>