menu.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Menu widget
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Widget
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Superclass for menus
  37. *
  38. * @category General
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class Menu extends Widget
  46. {
  47. public $widgetOpts;
  48. public $scoped;
  49. var $action = null;
  50. var $actionName = null;
  51. var $actionArgs = null;
  52. /**
  53. * Construction
  54. *
  55. * @param Action $action current action, used for output
  56. */
  57. function __construct(Action $action=null)
  58. {
  59. parent::__construct($action);
  60. $this->action = $action;
  61. $this->actionName = $action->trimmed('action');
  62. $rtargs = $action->returnToArgs();
  63. $this->actionArgs = $rtargs[1];
  64. }
  65. function getItems()
  66. {
  67. return array();
  68. }
  69. function tag()
  70. {
  71. return null;
  72. }
  73. function show()
  74. {
  75. $items = $this->getItems();
  76. $tag = $this->tag();
  77. $attrs = array('class' => 'nav');
  78. if (!is_null($tag)) {
  79. $attrs['id'] = 'nav_' . $tag;
  80. }
  81. if (Event::handle('StartNav', array($this, &$tag, &$items))) {
  82. $this->out->elementStart('ul', $attrs);
  83. foreach ($items as $item) {
  84. list($actionName, $args, $label, $description, $id) = $item;
  85. $this->item($actionName, $args, $label, $description, $id);
  86. }
  87. $this->out->elementEnd('ul');
  88. Event::handle('EndNav', array($this, $tag, $items));
  89. }
  90. }
  91. function item($actionName, array $args, $label, $description, $id=null, $cls=null)
  92. {
  93. if (empty($id)) {
  94. $id = $this->menuItemID($actionName, $args);
  95. }
  96. $url = common_local_url($actionName, $args);
  97. $this->out->menuItem($url,
  98. $label,
  99. $description,
  100. $this->isCurrent($actionName, $args),
  101. $id,
  102. $cls);
  103. }
  104. function isCurrent($actionName, array $args)
  105. {
  106. if ($actionName != $this->actionName) {
  107. return false;
  108. }
  109. foreach ($this->actionArgs as $k => $v) {
  110. if (!array_key_exists($k, $args) || $args[$k] != $v) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. function menuItemID($actionName, $args = null)
  117. {
  118. $id = sprintf('nav_%s', $actionName);
  119. if (!is_null($args)) {
  120. foreach ($args as $key => $value) {
  121. $id .= '_' . $key . '_' . $value;
  122. }
  123. }
  124. return $id;
  125. }
  126. function submenu($label, $menu)
  127. {
  128. if (Event::handle('StartSubMenu', [$this->action, $menu, $label])) {
  129. $this->action->elementStart('li');
  130. $this->action->element('h3', null, $label);
  131. $menu->show();
  132. $this->action->elementEnd('li');
  133. Event::handle('EndSubMenu', [$this->action, $menu, $label]);
  134. }
  135. }
  136. }