groupsearch.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Group search action class.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @author Robin Millette <millette@status.net>
  11. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  12. * @link http://status.net/
  13. *
  14. * StatusNet - the distributed open-source microblogging tool
  15. * Copyright (C) 2008, 2009, StatusNet, Inc.
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. require_once INSTALLDIR . '/lib/search/searchaction.php';
  34. require_once INSTALLDIR . '/lib/profile/profilelist.php';
  35. /**
  36. * Group search action class.
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Robin Millette <millette@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  43. * @link http://status.net/
  44. */
  45. class GroupsearchAction extends SearchAction
  46. {
  47. function getInstructions()
  48. {
  49. // TRANS: Instructions for page where groups can be searched. %%site.name%% is the name of the StatusNet site.
  50. return _('Search for groups on %%site.name%% by their name, location, or description. ' .
  51. 'Separate the terms by spaces; they must be 3 characters or more.');
  52. }
  53. function title()
  54. {
  55. // TRANS: Title for page where groups can be searched.
  56. return _('Group search');
  57. }
  58. function showResults($q, $page)
  59. {
  60. $user_group = new User_group;
  61. $user_group->limit((($page-1)*GROUPS_PER_PAGE), GROUPS_PER_PAGE + 1);
  62. $wheres = array('nickname', 'fullname', 'homepage', 'description', 'location');
  63. foreach ($wheres as $where) {
  64. $where_q = "$where like '%" . trim($user_group->escape($q), '\'') . '%\'';
  65. $user_group->whereAdd($where_q, 'OR');
  66. }
  67. $cnt = $user_group->find();
  68. if ($cnt > 0) {
  69. $terms = preg_split('/[\s,]+/', $q);
  70. $results = new GroupSearchResults($user_group, $terms, $this);
  71. $results->show();
  72. $user_group->free();
  73. $this->pagination($page > 1, $cnt > GROUPS_PER_PAGE,
  74. $page, 'groupsearch', array('q' => $q));
  75. } else {
  76. // TRANS: Text on page where groups can be searched if no results were found for a query.
  77. $this->element('p', 'error', _('No results.'));
  78. $this->searchSuggestions($q);
  79. if (common_logged_in()) {
  80. // TRANS: Additional text on page where groups can be searched if no results were found for a query for a logged in user.
  81. // TRANS: This message contains Markdown links in the form [link text](link).
  82. $message = _('If you cannot find the group you\'re looking for, you can [create it](%%action.newgroup%%) yourself.');
  83. }
  84. else {
  85. // TRANS: Additional text on page where groups can be searched if no results were found for a query for a not logged in user.
  86. // TRANS: This message contains Markdown links in the form [link text](link).
  87. $message = _('Why not [register an account](%%action.register%%) and [create the group](%%action.newgroup%%) yourself!');
  88. }
  89. $this->elementStart('div', 'guide');
  90. $this->raw(common_markup_to_html($message));
  91. $this->elementEnd('div');
  92. $user_group->free();
  93. }
  94. }
  95. function showScripts()
  96. {
  97. parent::showScripts();
  98. $this->autofocus('q');
  99. }
  100. }
  101. class GroupSearchResults extends GroupList
  102. {
  103. var $terms = null;
  104. var $pattern = null;
  105. function __construct($user_group, $terms, $action)
  106. {
  107. parent::__construct($user_group, null, $action);
  108. $this->terms = array_map('preg_quote',
  109. array_map('htmlspecialchars', $terms));
  110. $this->pattern = '/('.implode('|',$terms).')/i';
  111. }
  112. function highlight($text)
  113. {
  114. return preg_replace($this->pattern, '<strong>\\1</strong>', htmlspecialchars($text));
  115. }
  116. }