menu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. var $action = null;
  48. var $actionName = null;
  49. var $actionArgs = null;
  50. /**
  51. * Construction
  52. *
  53. * @param Action $action current action, used for output
  54. */
  55. function __construct(Action $action=null)
  56. {
  57. parent::__construct($action);
  58. $this->action = $action;
  59. $this->actionName = $action->trimmed('action');
  60. $rtargs = $action->returnToArgs();
  61. $this->actionArgs = $rtargs[1];
  62. }
  63. function getItems()
  64. {
  65. return array();
  66. }
  67. function tag()
  68. {
  69. return null;
  70. }
  71. function show()
  72. {
  73. $items = $this->getItems();
  74. $tag = $this->tag();
  75. $attrs = array('class' => 'nav');
  76. if (!is_null($tag)) {
  77. $attrs['id'] = 'nav_' . $tag;
  78. }
  79. if (Event::handle('StartNav', array($this, &$tag, &$items))) {
  80. $this->out->elementStart('ul', $attrs);
  81. foreach ($items as $item) {
  82. list($actionName, $args, $label, $description, $id) = $item;
  83. $this->item($actionName, $args, $label, $description, $id);
  84. }
  85. $this->out->elementEnd('ul');
  86. Event::handle('EndNav', array($this, $tag, $items));
  87. }
  88. }
  89. function item($actionName, array $args, $label, $description, $id=null, $cls=null)
  90. {
  91. if (empty($id)) {
  92. $id = $this->menuItemID($actionName, $args);
  93. }
  94. $url = common_local_url($actionName, $args);
  95. $this->out->menuItem($url,
  96. $label,
  97. $description,
  98. $this->isCurrent($actionName, $args),
  99. $id,
  100. $cls);
  101. }
  102. function isCurrent($actionName, array $args)
  103. {
  104. if ($actionName != $this->actionName) {
  105. return false;
  106. }
  107. foreach ($this->actionArgs as $k => $v) {
  108. if (!array_key_exists($k, $args) || $args[$k] != $v) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. function menuItemID($actionName, $args = null)
  115. {
  116. $id = sprintf('nav_%s', $actionName);
  117. if (!is_null($args)) {
  118. foreach ($args as $key => $value) {
  119. $id .= '_' . $key . '_' . $value;
  120. }
  121. }
  122. return $id;
  123. }
  124. function submenu($label, $menu)
  125. {
  126. $this->action->elementStart('li');
  127. $this->action->element('h3', null, $label);
  128. $menu->show();
  129. $this->action->elementEnd('li');
  130. }
  131. }