SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/NavigationGroups.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. /**
  17. * Menu groups for front end main navigation
  18. **/
  19. class NavigationGroups
  20. {
  21. private static $fallback_names = array(
  22. 1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six', 7 => 'Seven'
  23. );
  24.  
  25. private static $names = array();
  26. private static $extras = array();
  27.  
  28.  
  29. private static function &loadGroupNames($subsite_id = null) {
  30. if (! $subsite_id) {
  31. $subsite_id = SubsiteSelector::$subsite_id;
  32. }
  33.  
  34. if (!isset(self::$names[$subsite_id])) {
  35. $q = "SELECT id, page_id, position, name
  36. FROM ~menu_groups
  37. WHERE subsite_id = ?";
  38. $res = Pdb::q($q, [$subsite_id], 'pdo');
  39.  
  40. self::$names[$subsite_id] = array();
  41. foreach ($res as $row) {
  42. self::$names[$subsite_id][$row['page_id'] . '_' . $row['position']] = $row;
  43. }
  44. $res->closeCursor();
  45. }
  46.  
  47. if (!isset(self::$extras[$subsite_id])) {
  48. $q = "SELECT extra.page_id, extra.text, image.filename AS image
  49. FROM ~menu_extras AS extra
  50. LEFT JOIN ~files AS image ON extra.image = image.id
  51. WHERE extra.subsite_id = ?";
  52. $res = Pdb::q($q, [$subsite_id], 'pdo');
  53.  
  54. self::$extras[$subsite_id] = array();
  55. foreach ($res as $row) {
  56. self::$extras[$subsite_id][$row['page_id']] = $row;
  57. }
  58. }
  59.  
  60. return self::$names[$subsite_id];
  61. }
  62.  
  63.  
  64. /**
  65.   * Return an array of group names to use for a given top parent
  66.   * ADMIN ONLY METHOD
  67.   *
  68.   * @param TreeNode $page_id The top-parent page id
  69.   * @return array Group IDs and names
  70.   **/
  71. public static function getGroupsAdmin($page_id)
  72. {
  73. $names = self::loadGroupNames($_SESSION['admin']['active_subsite']);
  74.  
  75. $groups = Subsites::getConfigAdmin('nav_groups');
  76. if (! $groups) {
  77. return array();
  78. }
  79.  
  80. // Determine number of groups to offer
  81. $num = $groups[$page_id];
  82. if (! $num) {
  83. return array();
  84. }
  85.  
  86. $out = array();
  87. for ($position = 1; $position <= $num; ++$position) {
  88. $key = $page_id . '_' . $position;
  89.  
  90. // If there isn't a db record, create one
  91. if (! isset($names[$key])) {
  92. $update_data = array();
  93. $update_data['subsite_id'] = (int)$_SESSION['admin']['active_subsite'];
  94. $update_data['page_id'] = (int)$page_id;
  95. $update_data['position'] = (int)$position;
  96. $update_data['date_added'] = Pdb::now();
  97. $update_data['date_modified'] = Pdb::now();
  98. $update_data['name'] = self::$fallback_names[$position];
  99.  
  100. $menu_group_id = Pdb::insert('menu_groups', $update_data);
  101. $names[$key] = [
  102. 'id' => $menu_group_id,
  103. 'name' => self::$fallback_names[$position],
  104. ];
  105. }
  106.  
  107. $out[$names[$key]['id']] = $names[$key];
  108. }
  109.  
  110. return $out;
  111. }
  112.  
  113.  
  114. /**
  115.   * Get all group details, grouped by top parent page id
  116.   * ADMIN ONLY METHOD
  117.   **/
  118. public static function getAllGroupsAdmin()
  119. {
  120. $groups = Subsites::getConfigAdmin('nav_groups');
  121.  
  122. $all_groups = array();
  123. foreach ($groups as $page_id => $num) {
  124. $all_groups[$page_id] = self::getGroupsAdmin($page_id);
  125. }
  126.  
  127. return $all_groups;
  128. }
  129.  
  130.  
  131. /**
  132.   * Get all group names, grouped by top parent page id
  133.   * ADMIN ONLY METHOD
  134.   **/
  135. public static function getAllNamesAdmin()
  136. {
  137. $groups = Subsites::getConfigAdmin('nav_groups');
  138.  
  139. $all_names = array();
  140. foreach ($groups as $page_id => $num) {
  141. $groups = self::getGroupsAdmin($page_id);
  142.  
  143. $names = [];
  144. foreach ($groups as $id => $row) {
  145. $names[$id] = $row['name'];
  146. }
  147.  
  148. $all_names[$page_id] = $names;
  149. }
  150.  
  151. return $all_names;
  152. }
  153.  
  154.  
  155. /**
  156.   * Returns an array of position -> name for all groups for a page
  157.   **/
  158. public static function getAllNames($page_id)
  159. {
  160. $names = self::loadGroupNames();
  161.  
  162. $out = array();
  163. foreach ($names as $key => $group) {
  164. preg_match('!^([0-9]+)_([0-9]+)$!', $key, $matches);
  165. if ($matches[1] == $page_id) {
  166. $out[$matches[2]] = $group['name'];
  167. }
  168. }
  169.  
  170. return $out;
  171. }
  172.  
  173. /**
  174.   * Return the group id for a given page and position
  175.   **/
  176. public static function getId($page_id, $position)
  177. {
  178. $names = self::loadGroupNames();
  179. return $names[$page_id . '_' . $position]['id'];
  180. }
  181.  
  182. /**
  183.   * Return the group name for a given page and position
  184.   **/
  185. public static function getName($page_id, $position)
  186. {
  187. $names = self::loadGroupNames();
  188. return $names[$page_id . '_' . $position]['name'];
  189. }
  190.  
  191. /**
  192.   * Return an array of Treenode instances for the items in this group
  193.   **/
  194. public static function getItems($page_id, $position, $limit = null)
  195. {
  196. $names = self::loadGroupNames();
  197.  
  198. $menu_group = $names[$page_id . '_' . $position]['id'];
  199. if ($menu_group == null) return array();
  200.  
  201. $root = Navigation::getRootNode();
  202. $root->filterChildren(new TreenodeInMenuMatcher());
  203.  
  204. $items = $root->findAllNodes(new TreenodeValueMatcher('menu_group', $menu_group));
  205.  
  206. if ($limit) {
  207. $items = array_slice($items, 0, $limit);
  208. }
  209.  
  210. $root->removeFilter();
  211.  
  212. return $items;
  213. }
  214.  
  215.  
  216. /**
  217.   * Get all extras, grouped by top parent page id
  218.   * ADMIN ONLY METHOD
  219.   **/
  220. public static function getAllExtrasAdmin() {
  221. self::loadGroupNames($_SESSION['admin']['active_subsite']);
  222. return self::$extras[$_SESSION['admin']['active_subsite']];
  223. }
  224.  
  225.  
  226. /**
  227.   * Return an array of extra menu details for a top-level nav items
  228.   *
  229.   * Return keys:
  230.   * text Description
  231.   * image Image filename
  232.   *
  233.   * @param int $page_id Top-level page ID
  234.   * @return array
  235.   **/
  236. public static function getExtras($page_id) {
  237. self::loadGroupNames();
  238. return self::$extras[SubsiteSelector::$subsite_id][$page_id];
  239. }
  240.  
  241. }
  242.