/applications/dashboard/modules/class.sidemenumodule.php

https://github.com/Emaratilicious/Garden · PHP · 261 lines · 164 code · 40 blank · 57 comment · 39 complexity · fa030e76451c17a37ef5c352a524321a MD5 · raw file

  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. if (!class_exists('SideMenuModule', FALSE)) {
  11. /**
  12. * Manages the items in the page menu and eventually returns the menu as a
  13. * string with ToString();
  14. */
  15. class SideMenuModule extends Gdn_Module {
  16. /**
  17. * Should the group titles be autolinked to the first anchor in the group? Default TRUE;
  18. */
  19. public $AutoLinkGroups;
  20. /**
  21. * An array of menu items.
  22. */
  23. public $Items;
  24. protected $_Items;
  25. /**
  26. * The html id attribute to be applied to the root element of the menu.
  27. * Default is "Menu".
  28. */
  29. public $HtmlId;
  30. /**
  31. * The class attribute to be applied to the root element of the
  32. * breadcrumb. Default is none.
  33. */
  34. public $CssClass;
  35. /**
  36. * An array of menu group names arranged in the order that the menu
  37. * should be rendered.
  38. */
  39. public $Sort;
  40. /**
  41. * A route that, if found in the menu links, should cause that link to
  42. * have the Highlight class applied. This property is assigned with
  43. * $this->Highlight();
  44. */
  45. private $_HighlightRoute;
  46. public function __construct($Sender = '') {
  47. $this->HtmlId = 'SideMenu';
  48. $this->AutoLinkGroups = TRUE;
  49. $this->ClearGroups();
  50. parent::__construct($Sender);
  51. }
  52. public function AddLink($Group, $Text, $Url, $Permission = FALSE, $Attributes = array()) {
  53. if (!array_key_exists($Group, $this->Items)) {
  54. $this->AddItem($Group, T($Group));
  55. }
  56. if ($Text === FALSE) {
  57. // This link is the group heading.
  58. $this->Items[$Group]['Url'] = $Url;
  59. $this->Items[$Group]['Permission'] = $Permission;
  60. $this->Items[$Group]['Attributes'] = array_merge($this->Items[$Group]['Attributes'], $Attributes);
  61. } else {
  62. $Link = array('Text' => $Text, 'Url' => $Url, 'Permission' => $Permission, 'Attributes' => $Attributes, '_Sort' => count($this->Items[$Group]['Links']));
  63. if (isset($Attributes['After'])) {
  64. $Link['After'] = $Attributes['After'];
  65. unset($Attributes['After']);
  66. }
  67. $this->Items[$Group]['Links'][$Url] = $Link;
  68. }
  69. }
  70. public function AddItem($Group, $Text, $Permission = FALSE, $Attributes = array()) {
  71. if (!array_key_exists($Group, $this->Items))
  72. $Item = array('Group' => $Group, 'Links' => array(), '_Sort' => count($this->Items));
  73. else
  74. $Item = $this->Items[$Group];
  75. if (isset($Attributes['After'])) {
  76. $Item['After'] = $Attributes['After'];
  77. unset($Attributes['After']);
  78. }
  79. $Item['Text'] = $Text;
  80. $Item['Permission'] = $Permission;
  81. $Item['Attributes'] = $Attributes;
  82. $this->Items[$Group] = $Item;
  83. }
  84. public function AssetTarget() {
  85. return 'Menu';
  86. }
  87. public function CheckPermissions() {
  88. $Session = Gdn::Session();
  89. foreach ($this->Items as $Group => $Item) {
  90. if (GetValue('Permission', $Item) && !$Session->CheckPermission($Item['Permission'])) {
  91. unset($this->Items[$Group]);
  92. continue;
  93. }
  94. foreach ($Item['Links'] as $Key => $Link) {
  95. if (GetValue('Permission', $Link) && !$Session->CheckPermission($Link['Permission']))
  96. unset($this->Items[$Group]['Links'][$Key]);
  97. }
  98. // Remove the item if there are no more links.
  99. if (!GetValue('Url', $Item) && !count($this->Items[$Group]['Links']))
  100. unset($this->Items[$Group]);
  101. }
  102. }
  103. public function ClearGroups() {
  104. $this->Items = array();
  105. }
  106. protected function _Compare($A, $B = NULL) {
  107. static $Groups;
  108. if ($B === NULL) {
  109. $Groups = $A;
  110. return;
  111. }
  112. $SortA = $this->_CompareSort($A, $Groups);
  113. $SortB = $this->_CompareSort($B, $Groups);
  114. if ($SortA > $SortB)
  115. return 1;
  116. elseif ($SortA < $SortB)
  117. return -1;
  118. elseif ($A['_Sort'] > $B['_Sort']) // fall back to order added
  119. return 1;
  120. elseif ($A['_Sort'] < $B['_Sort'])
  121. return -1;
  122. else
  123. return 0;
  124. }
  125. /**
  126. * The sort is determined by looking at:
  127. * a) The item's sort.
  128. * b) Whether the item is after another.
  129. * c) The order the item was added.
  130. * @param array $A
  131. * @param array $All
  132. * @return int
  133. */
  134. protected function _CompareSort($A, $All) {
  135. if (isset($A['Sort']))
  136. return $A['Sort'];
  137. if (isset($A['After']) && isset($All[$A['After']])) {
  138. $After = $All[$A['After']];
  139. if (isset($After['Sort']))
  140. return $After['Sort'] + 0.1;
  141. return $After['_Sort'] + 0.1;
  142. }
  143. return $A['_Sort'];
  144. }
  145. public function HighlightRoute($Route) {
  146. $this->_HighlightRoute = $Route;
  147. }
  148. public function RemoveLink($Group, $Text) {
  149. if (array_key_exists($Group, $this->Items) && isset($this->Items[$Group]['Links'])) {
  150. $Links =& $this->Items[$Group]['Links'];
  151. if (isset($Links[$Text])) {
  152. unset($this->Items[$Group]['Links'][$Text]);
  153. return;
  154. }
  155. foreach ($Links as $Index => $Link) {
  156. if (GetValue('Text', $Link) == $Text) {
  157. unset($this->Items[$Group]['Links'][$Index]);
  158. return;
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * Removes all links from a specific group.
  165. */
  166. public function RemoveLinks($Group) {
  167. $this->Items[$Group] = array();
  168. }
  169. /**
  170. * Removes an entire group of links, and the group itself, from the menu.
  171. */
  172. public function RemoveGroup($Group) {
  173. if (array_key_exists($Group, $this->Items))
  174. unset($this->Items[$Group]);
  175. }
  176. public function ToString($HighlightRoute = '') {
  177. if ($HighlightRoute == '')
  178. $HighlightRoute = $this->_HighlightRoute;
  179. if ($HighlightRoute == '')
  180. $HighlightRoute = Gdn_Url::Request();
  181. $HighlightUrl = Url($HighlightRoute);
  182. // Apply a sort to the items if given.
  183. if (is_array($this->Sort)) {
  184. $Sort = array_flip($this->Sort);
  185. foreach ($this->Items as $Group => &$Item) {
  186. if (isset($Sort[$Group]))
  187. $Item['Sort'] = $Sort[$Group];
  188. else
  189. $Item['_Sort'] += count($Sort);
  190. foreach ($Item['Links'] as $Url => &$Link) {
  191. if (isset($Sort[$Url]))
  192. $Link['Sort'] = $Sort[$Url];
  193. elseif (isset($Sort[$Link['Text']]))
  194. $Link['Sort'] = $Sort[$Link['Text']];
  195. else
  196. $Link['_Sort'] += count($Sort);
  197. }
  198. }
  199. }
  200. // Sort the groups.
  201. $this->_Compare($this->Items);
  202. uasort($this->Items, array($this, '_Compare'));
  203. // Sort the items within the groups.
  204. foreach ($this->Items as &$Item) {
  205. $this->_Compare($Item['Links']);
  206. uasort($Item['Links'], array($this, '_Compare'));
  207. // Highlight the group.
  208. if (GetValue('Url', $Item) && Url($Item['Url']) == $HighlightUrl)
  209. $Item['Attributes']['class'] = ConcatSep(' ', GetValue('class', $Item['Attributes']), 'Active');
  210. // Hightlight the correct item in the group.
  211. foreach ($Item['Links'] as &$Link) {
  212. if (GetValue('Url', $Link) && Url($Link['Url']) == $HighlightUrl) {
  213. $Link['Attributes']['class'] = ConcatSep(' ', GetValue('class', $Link['Attributes']), 'Active');
  214. $Item['Attributes']['class'] = ConcatSep(' ', GetValue('class', $Item['Attributes']), 'Active');
  215. }
  216. }
  217. }
  218. return parent::ToString();
  219. }
  220. }
  221. }