PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/abhishekmica/Garden
PHP | 259 lines | 163 code | 39 blank | 57 comment | 39 complexity | 237a7face577cc56eb92a3d0941d5c5c 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->Items[$Group] = array('Group' => $Group, 'Links' => array());
  55. if ($Text === FALSE) {
  56. // This link is the group heading.
  57. $this->Items[$Group]['Url'] = $Url;
  58. $this->Items[$Group]['Permission'] = $Permission;
  59. $this->Items[$Group]['Attributes'] = array_merge($this->Items[$Group]['Attributes'], $Attributes);
  60. } else {
  61. $Link = array('Text' => $Text, 'Url' => $Url, 'Permission' => $Permission, 'Attributes' => $Attributes, '_Sort' => count($this->Items[$Group]['Links']));
  62. if (isset($Attributes['After'])) {
  63. $Link['After'] = $Attributes['After'];
  64. unset($Attributes['After']);
  65. }
  66. $this->Items[$Group]['Links'][$Url] = $Link;
  67. }
  68. }
  69. public function AddItem($Group, $Text, $Permission = FALSE, $Attributes = array()) {
  70. if (!array_key_exists($Group, $this->Items))
  71. $Item = array('Group' => $Group, 'Links' => array(), '_Sort' => count($this->Items));
  72. else
  73. $Item = $this->Items[$Group];
  74. if (isset($Attributes['After'])) {
  75. $Item['After'] = $Attributes['After'];
  76. unset($Attributes['After']);
  77. }
  78. $Item['Text'] = $Text;
  79. $Item['Permission'] = $Permission;
  80. $Item['Attributes'] = $Attributes;
  81. $this->Items[$Group] = $Item;
  82. }
  83. public function AssetTarget() {
  84. return 'Menu';
  85. }
  86. public function CheckPermissions() {
  87. $Session = Gdn::Session();
  88. foreach ($this->Items as $Group => $Item) {
  89. if (GetValue('Permission', $Item) && !$Session->CheckPermission($Item['Permission'])) {
  90. unset($this->Items[$Group]);
  91. continue;
  92. }
  93. foreach ($Item['Links'] as $Key => $Link) {
  94. if (GetValue('Permission', $Link) && !$Session->CheckPermission($Link['Permission']))
  95. unset($this->Items[$Group]['Links'][$Key]);
  96. }
  97. // Remove the item if there are no more links.
  98. if (!GetValue('Url', $Item) && !count($this->Items[$Group]['Links']))
  99. unset($this->Items[$Group]);
  100. }
  101. }
  102. public function ClearGroups() {
  103. $this->Items = array();
  104. }
  105. protected function _Compare($A, $B = NULL) {
  106. static $Groups;
  107. if ($B === NULL) {
  108. $Groups = $A;
  109. return;
  110. }
  111. $SortA = $this->_CompareSort($A, $Groups);
  112. $SortB = $this->_CompareSort($B, $Groups);
  113. if ($SortA > $SortB)
  114. return 1;
  115. elseif ($SortA < $SortB)
  116. return -1;
  117. elseif ($A['_Sort'] > $B['_Sort']) // fall back to order added
  118. return 1;
  119. elseif ($A['_Sort'] < $B['_Sort'])
  120. return -1;
  121. else
  122. return 0;
  123. }
  124. /**
  125. * The sort is determined by looking at:
  126. * a) The item's sort.
  127. * b) Whether the item is after another.
  128. * c) The order the item was added.
  129. * @param array $A
  130. * @param array $All
  131. * @return int
  132. */
  133. protected function _CompareSort($A, $All) {
  134. if (isset($A['Sort']))
  135. return $A['Sort'];
  136. if (isset($A['After']) && isset($All[$A['After']])) {
  137. $After = $All[$A['After']];
  138. if (isset($After['Sort']))
  139. return $After['Sort'] + 0.1;
  140. return $After['_Sort'] + 0.1;
  141. }
  142. return $A['_Sort'];
  143. }
  144. public function HighlightRoute($Route) {
  145. $this->_HighlightRoute = $Route;
  146. }
  147. public function RemoveLink($Group, $Text) {
  148. if (array_key_exists($Group, $this->Items) && isset($this->Items[$Group]['Links'])) {
  149. $Links =& $this->Items[$Group]['Links'];
  150. if (isset($Links[$Text])) {
  151. unset($this->Items[$Group]['Links'][$Text]);
  152. return;
  153. }
  154. foreach ($Links as $Index => $Link) {
  155. if (GetValue('Text', $Link) == $Text) {
  156. unset($this->Items[$Group]['Links'][$Index]);
  157. return;
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * Removes all links from a specific group.
  164. */
  165. public function RemoveLinks($Group) {
  166. $this->Items[$Group] = array();
  167. }
  168. /**
  169. * Removes an entire group of links, and the group itself, from the menu.
  170. */
  171. public function RemoveGroup($Group) {
  172. if (array_key_exists($Group, $this->Items))
  173. unset($this->Items[$Group]);
  174. }
  175. public function ToString($HighlightRoute = '') {
  176. if ($HighlightRoute == '')
  177. $HighlightRoute = $this->_HighlightRoute;
  178. if ($HighlightRoute == '')
  179. $HighlightRoute = Gdn_Url::Request();
  180. $HighlightUrl = Url($HighlightRoute);
  181. // Apply a sort to the items if given.
  182. if (is_array($this->Sort)) {
  183. $Sort = array_flip($this->Sort);
  184. foreach ($this->Items as $Group => &$Item) {
  185. if (isset($Sort[$Group]))
  186. $Item['Sort'] = $Sort[$Group];
  187. else
  188. $Item['_Sort'] += count($Sort);
  189. foreach ($Item['Links'] as $Url => &$Link) {
  190. if (isset($Sort[$Url]))
  191. $Link['Sort'] = $Sort[$Url];
  192. elseif (isset($Sort[$Link['Text']]))
  193. $Link['Sort'] = $Sort[$Link['Text']];
  194. else
  195. $Link['_Sort'] += count($Sort);
  196. }
  197. }
  198. }
  199. // Sort the groups.
  200. $this->_Compare($this->Items);
  201. uasort($this->Items, array($this, '_Compare'));
  202. // Sort the items within the groups.
  203. foreach ($this->Items as &$Item) {
  204. $this->_Compare($Item['Links']);
  205. uasort($Item['Links'], array($this, '_Compare'));
  206. // Highlight the group.
  207. if (GetValue('Url', $Item) && Url($Item['Url']) == $HighlightUrl)
  208. $Item['Attributes']['class'] = ConcatSep(' ', GetValue('class', $Item['Attributes']), 'Active');
  209. // Hightlight the correct item in the group.
  210. foreach ($Item['Links'] as &$Link) {
  211. if (GetValue('Url', $Link) && Url($Link['Url']) == $HighlightUrl) {
  212. $Link['Attributes']['class'] = ConcatSep(' ', GetValue('class', $Link['Attributes']), 'Active');
  213. $Item['Attributes']['class'] = ConcatSep(' ', GetValue('class', $Item['Attributes']), 'Active');
  214. }
  215. }
  216. }
  217. return parent::ToString();
  218. }
  219. }
  220. }