menu.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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) 2013
  17. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //define the menu class
  22. class menu {
  23. public $menu_uuid;
  24. //delete items in the menu that are not protected
  25. function delete() {
  26. //set the variable
  27. $db = $this->db;
  28. //remove the menu languages
  29. $sql = "delete from v_menu_languages ";
  30. $sql .= "where menu_uuid = '".$this->menu_uuid."' ";
  31. $db->exec(check_sql($sql));
  32. //remove the old menu
  33. $sql = "delete from v_menu_items ";
  34. $sql .= "where menu_uuid = '".$this->menu_uuid."' ";
  35. $sql .= "and (menu_item_protected <> 'true' ";
  36. $sql .= "or menu_item_protected is null); ";
  37. $db->exec(check_sql($sql));
  38. }
  39. //restore the menu
  40. function restore() {
  41. //set the variables
  42. $db = $this->db;
  43. //get the $apps array from the installed apps from the core and mod directories
  44. $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
  45. $x=0;
  46. foreach ($config_list as &$config_path) {
  47. include($config_path);
  48. $x++;
  49. }
  50. //use the app array to restore the default menu
  51. //$db->beginTransaction();
  52. foreach ($apps as $row) {
  53. foreach ($row['menu'] as $menu) {
  54. //set the variables
  55. $menu_item_title = $menu['title']['en-us'];
  56. $menu_item_uuid = $menu['uuid'];
  57. $menu_item_parent_uuid = $menu['parent_uuid'];
  58. $menu_item_category = $menu['category'];
  59. $menu_item_path = $menu['path'];
  60. $menu_item_order = $menu['order'];
  61. $menu_item_description = $menu['desc'];
  62. //if the item uuid is not currently in the db then add it
  63. $sql = "select * from v_menu_items ";
  64. $sql .= "where menu_uuid = '".$this->menu_uuid."' ";
  65. $sql .= "and menu_item_uuid = '$menu_item_uuid' ";
  66. $prep_statement = $db->prepare(check_sql($sql));
  67. if ($prep_statement) {
  68. $prep_statement->execute();
  69. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  70. if (count($result) == 0) {
  71. //insert the default menu into the database
  72. $sql = "insert into v_menu_items ";
  73. $sql .= "(";
  74. $sql .= "menu_item_uuid, ";
  75. $sql .= "menu_uuid, ";
  76. //$sql .= "menu_item_language, ";
  77. $sql .= "menu_item_title, ";
  78. $sql .= "menu_item_link, ";
  79. $sql .= "menu_item_category, ";
  80. if (strlen($menu_item_order) > 0) {
  81. $sql .= "menu_item_order, ";
  82. }
  83. if (strlen($menu_item_parent_uuid) > 0) {
  84. $sql .= "menu_item_parent_uuid, ";
  85. }
  86. $sql .= "menu_item_description ";
  87. $sql .= ") ";
  88. $sql .= "values ";
  89. $sql .= "(";
  90. $sql .= "'".$menu_item_uuid."', ";
  91. $sql .= "'".$this->menu_uuid."', ";
  92. //$sql .= "'$menu_item_language', ";
  93. $sql .= "'$menu_item_title', ";
  94. $sql .= "'$menu_item_path', ";
  95. $sql .= "'$menu_item_category', ";
  96. if (strlen($menu_item_order) > 0) {
  97. $sql .= "'$menu_item_order', ";
  98. }
  99. if (strlen($menu_item_parent_uuid) > 0) {
  100. $sql .= "'$menu_item_parent_uuid', ";
  101. }
  102. $sql .= "'$menu_item_description' ";
  103. $sql .= ")";
  104. if ($menu_item_uuid == $menu_item_parent_uuid) {
  105. //echo $sql."<br />\n";
  106. }
  107. else {
  108. $db->exec(check_sql($sql));
  109. }
  110. unset($sql);
  111. //set the menu languages
  112. foreach ($menu["title"] as $menu_language => $menu_item_title) {
  113. $menu_language_uuid = uuid();
  114. $sql = "insert into v_menu_languages ";
  115. $sql .= "(";
  116. $sql .= "menu_language_uuid, ";
  117. $sql .= "menu_item_uuid, ";
  118. $sql .= "menu_uuid, ";
  119. $sql .= "menu_language, ";
  120. $sql .= "menu_item_title ";
  121. $sql .= ") ";
  122. $sql .= "values ";
  123. $sql .= "(";
  124. $sql .= "'".$menu_language_uuid."', ";
  125. $sql .= "'".$menu_item_uuid."', ";
  126. $sql .= "'".$this->menu_uuid."', ";
  127. $sql .= "'$menu_language', ";
  128. $sql .= "'$menu_item_title' ";
  129. $sql .= ")";
  130. $db->exec(check_sql($sql));
  131. unset($sql);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. //if there are no groups listed in v_menu_item_groups under menu_uuid then add the default groups
  138. foreach($apps as $app) {
  139. foreach ($app['menu'] as $sub_row) {
  140. foreach ($sub_row['groups'] as $group) {
  141. $sql = "select count(*) as count from v_menu_item_groups ";
  142. $sql .= "where menu_item_uuid = '".$sub_row['uuid']."' ";
  143. $sql .= "and group_name = '$group' ";
  144. $prep_statement = $db->prepare($sql);
  145. $prep_statement->execute();
  146. $sub_result = $prep_statement->fetch(PDO::FETCH_ASSOC);
  147. unset ($prep_statement);
  148. if ($sub_result['count'] == 0) {
  149. //no menu item groups found add the defaults
  150. //add the record
  151. $sql = "insert into v_menu_item_groups ";
  152. $sql .= "(";
  153. $sql .= "menu_uuid, ";
  154. $sql .= "menu_item_uuid, ";
  155. $sql .= "group_name ";
  156. $sql .= ")";
  157. $sql .= "values ";
  158. $sql .= "(";
  159. $sql .= "'".$this->menu_uuid."', ";
  160. $sql .= "'".$sub_row['uuid']."', ";
  161. $sql .= "'".$group."' ";
  162. $sql .= ")";
  163. $db->exec($sql);
  164. unset($sql);
  165. }
  166. }
  167. }
  168. }
  169. } //end function
  170. //create the menu
  171. function build_html($sql, $menu_item_level) {
  172. $db = $this->db;
  173. $db_menu_full = '';
  174. if (!isset($_SESSION['groups'])) {
  175. $_SESSION['groups'][0]['group_name'] = 'public';
  176. }
  177. if (strlen($sql) == 0) { //default sql for base of the menu
  178. $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_uuid, i.menu_item_parent_uuid from v_menu_items as i, v_menu_languages as l ";
  179. $sql .= "where i.menu_item_uuid = l.menu_item_uuid ";
  180. $sql .= "and l.menu_language = '".$_SESSION['domain']['language']['code']."' ";
  181. $sql .= "and l.menu_uuid = '".$this->menu_uuid."' ";
  182. $sql .= "and i.menu_uuid = '".$this->menu_uuid."' ";
  183. $sql .= "and i.menu_item_parent_uuid is null ";
  184. $sql .= "and i.menu_item_uuid in ";
  185. $sql .= "(select menu_item_uuid from v_menu_item_groups where menu_uuid = '".$this->menu_uuid."' ";
  186. $sql .= "and ( ";
  187. if (!isset($_SESSION['groups'])) {
  188. $sql .= "group_name = 'public' ";
  189. }
  190. else {
  191. $x = 0;
  192. foreach($_SESSION['groups'] as $row) {
  193. if ($x == 0) {
  194. $sql .= "group_name = '".$row['group_name']."' ";
  195. }
  196. else {
  197. $sql .= "or group_name = '".$row['group_name']."' ";
  198. }
  199. $x++;
  200. }
  201. }
  202. $sql .= ") ";
  203. $sql .= "and menu_item_uuid is not null ";
  204. $sql .= ") ";
  205. $sql .= "order by i.menu_item_order asc ";
  206. }
  207. $prep_statement = $db->prepare(check_sql($sql));
  208. $prep_statement->execute();
  209. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  210. foreach($result as $field) {
  211. unset($prep_statement2, $sql2, $result2);
  212. $menu_tags = '';
  213. switch ($field['menu_item_category']) {
  214. case "internal":
  215. $menu_tags = "href='".PROJECT_PATH.$field['menu_item_link']."'";
  216. break;
  217. case "external":
  218. if (substr($field['menu_item_link'], 0,1) == "/") {
  219. $field['menu_item_link'] = PROJECT_PATH . $field['menu_item_link'];
  220. }
  221. $menu_tags = "href='".$field['menu_item_link']."' target='_blank'";
  222. break;
  223. case "email":
  224. $menu_tags = "href='mailto:".$field['menu_item_link']."'";
  225. break;
  226. }
  227. //prepare the protected menus
  228. if ($field['menu_item_protected'] == "true") {
  229. $menu_item_title = $field['menu_item_title'];
  230. }
  231. else {
  232. $menu_item_title = $field['menu_language_title'];
  233. }
  234. if ($menu_item_level == "main") {
  235. $db_menu = "<ul class='menu_main'>\n";
  236. $db_menu .= "<li>\n";
  237. if (!isset($_SESSION["username"])) {
  238. $_SESSION["username"] = '';
  239. }
  240. if (strlen($_SESSION["username"]) == 0) {
  241. $db_menu .= "<a $menu_tags style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n";
  242. }
  243. else {
  244. if ($field['menu_item_link'] == "/login.php" || $field['menu_item_link'] == "/users/signup.php") {
  245. //hide login and sign-up when the user is logged in
  246. }
  247. else {
  248. $db_menu .= "<a ".$menu_tags." style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n";
  249. }
  250. }
  251. }
  252. $menu_item_level = 0;
  253. if (strlen($field['menu_item_uuid']) > 0) {
  254. $db_menu .= $this->build_child_html($menu_item_level, $field['menu_item_uuid']);
  255. }
  256. if ($menu_item_level == "main") {
  257. $db_menu .= "</li>\n";
  258. $db_menu .= "</ul>\n\n";
  259. }
  260. $db_menu_full .= $db_menu;
  261. } //end for each
  262. unset($prep_statement, $sql, $result);
  263. return $db_menu_full;
  264. }
  265. //create the sub menus
  266. function build_child_html($menu_item_level, $menu_item_uuid) {
  267. $db = $this->db;
  268. $menu_item_level = $menu_item_level+1;
  269. if (count($_SESSION['groups']) == 0) {
  270. $_SESSION['groups'][0]['group_name'] = 'public';
  271. }
  272. $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_uuid, i.menu_item_parent_uuid ";
  273. $sql .= "from v_menu_items as i, v_menu_languages as l ";
  274. $sql .= "where i.menu_item_uuid = l.menu_item_uuid ";
  275. $sql .= "and l.menu_language = '".$_SESSION['domain']['language']['code']."' ";
  276. $sql .= "and l.menu_uuid = '".$this->menu_uuid."' ";
  277. $sql .= "and i.menu_uuid = '".$this->menu_uuid."' ";
  278. $sql .= "and i.menu_item_parent_uuid = '$menu_item_uuid' ";
  279. $sql .= "and i.menu_item_uuid in ";
  280. $sql .= "(select menu_item_uuid from v_menu_item_groups where menu_uuid = '".$this->menu_uuid."' ";
  281. $sql .= "and ( ";
  282. if (count($_SESSION['groups']) == 0) {
  283. $sql .= "group_name = 'public' ";
  284. }
  285. else {
  286. $x = 0;
  287. foreach($_SESSION['groups'] as $row) {
  288. if ($x == 0) {
  289. $sql .= "group_name = '".$row['group_name']."' ";
  290. }
  291. else {
  292. $sql .= "or group_name = '".$row['group_name']."' ";
  293. }
  294. $x++;
  295. }
  296. }
  297. $sql .= ") ";
  298. $sql .= ") ";
  299. $sql .= "order by l.menu_item_title, i.menu_item_order asc ";
  300. $prep_statement_2 = $db->prepare($sql);
  301. $prep_statement_2->execute();
  302. $result_2 = $prep_statement_2->fetchAll(PDO::FETCH_NAMED);
  303. if (count($result_2) > 0) {
  304. //child menu found
  305. $db_menu_sub = "<ul class='menu_sub'>\n";
  306. foreach($result_2 as $row) {
  307. $menu_item_link = $row['menu_item_link'];
  308. $menu_item_category = $row['menu_item_category'];
  309. $menu_item_uuid = $row['menu_item_uuid'];
  310. $menu_item_parent_uuid = $row['menu_item_parent_uuid'];
  311. //prepare the protected menus
  312. if ($row['menu_item_protected'] == "true") {
  313. $menu_item_title = $row['menu_item_title'];
  314. }
  315. else {
  316. $menu_item_title = $row['menu_language_title'];
  317. }
  318. //prepare the menu_tags according to the category
  319. switch ($menu_item_category) {
  320. case "internal":
  321. $menu_tags = "href='".PROJECT_PATH.$menu_item_link."'";
  322. break;
  323. case "external":
  324. if (substr($menu_item_link, 0,1) == "/") {
  325. $menu_item_link = PROJECT_PATH . $menu_item_link;
  326. }
  327. $menu_tags = "href='".$menu_item_link."' target='_blank'";
  328. break;
  329. case "email":
  330. $menu_tags = "href='mailto:".$menu_item_link."'";
  331. break;
  332. }
  333. $db_menu_sub .= "<li>";
  334. //get sub menu for children
  335. if (strlen($menu_item_uuid) > 0) {
  336. $str_child_menu = $this->build_child_html($menu_item_level, $menu_item_uuid);
  337. }
  338. if (strlen($str_child_menu) > 1) {
  339. $db_menu_sub .= "<a ".$menu_tags.">".$menu_item_title."</a>";
  340. $db_menu_sub .= $str_child_menu;
  341. unset($str_child_menu);
  342. }
  343. else {
  344. $db_menu_sub .= "<a ".$menu_tags.">".$menu_item_title."</a>";
  345. }
  346. $db_menu_sub .= "</li>\n";
  347. }
  348. unset($sql, $result_2);
  349. $db_menu_sub .="</ul>\n";
  350. return $db_menu_sub;
  351. }
  352. unset($prep_statement_2, $sql);
  353. }
  354. }
  355. ?>