alphanav.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Widget to display an alphabet menu
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Outputs a fancy alphabet letter navigation menu
  34. *
  35. * @category Widget
  36. * @package StatusNet
  37. * @author Zach Copley <zach@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. *
  41. * @see HTMLOutputter
  42. */
  43. class AlphaNav extends Widget
  44. {
  45. protected $action = null;
  46. protected $filters = array();
  47. /**
  48. * Prepare the widget for use
  49. *
  50. * @param Action $action the current action
  51. * @param boolean $numbers whether to output 0..9
  52. * @param Array $prepend array of filters to prepend
  53. * @param Array $append array of filters to append
  54. */
  55. function __construct(
  56. $action = null,
  57. $numbers = false,
  58. $prepend = false,
  59. $append = false
  60. )
  61. {
  62. parent::__construct($action);
  63. $this->action = $action;
  64. if ($prepend) {
  65. $this->filters = array_merge($prepend, $this->filters);
  66. }
  67. if ($numbers) {
  68. $this->filters = array_merge($this->filters, range(0, 9));
  69. }
  70. $this->filters = array_merge($this->filters, range('A', 'Z'));
  71. if ($append) {
  72. $this->filters = array_merge($this->filters, $append);
  73. }
  74. }
  75. /**
  76. * Show the widget
  77. *
  78. * Emit the HTML for the widget, using the configured outputter.
  79. *
  80. * @return void
  81. */
  82. function show()
  83. {
  84. $actionName = $this->action->trimmed('action');
  85. $this->action->elementStart('div', array('class' => 'alpha_nav'));
  86. for ($i = 0, $size = sizeof($this->filters); $i < $size; $i++) {
  87. $filter = $this->filters[$i];
  88. $classes = '';
  89. // Add some classes for styling
  90. if ($i == 0) {
  91. $classes .= 'first '; // first filter in the list
  92. } elseif ($i == $size - 1) {
  93. $classes .= 'last '; // last filter in the list
  94. }
  95. // hack to get around $m->connect(array('action' => 'all, 'nickname' => $nickname));
  96. if (strtolower($filter) == 'all') {
  97. $href = common_local_url($actionName);
  98. } else {
  99. $href = common_local_url(
  100. $actionName,
  101. array('filter' => strtolower($filter))
  102. );
  103. }
  104. $params = array('href' => $href);
  105. // sort column
  106. if (!empty($this->action->sort)) {
  107. $params['sort'] = $this->action->sort;
  108. }
  109. // sort order
  110. if ($this->action->reverse) {
  111. $params['reverse'] = 'true';
  112. }
  113. $current = $this->action->arg('filter');
  114. // Highlight the selected filter. If there is no selected
  115. // filter, highlight the last filter in the list (all)
  116. if (!isset($current) && $i == ($size - 1)
  117. || $current === strtolower($filter)) {
  118. $classes .= 'current ';
  119. }
  120. if (!empty($classes)) {
  121. $params['class'] = trim($classes);
  122. }
  123. $this->action->element('a', $params, $filter);
  124. }
  125. $this->action->elementEnd('div');
  126. }
  127. }