userdirectory.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Output a user directory
  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 Public
  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('GNUSOCIAL')) {
  30. exit(1);
  31. }
  32. /**
  33. * User directory
  34. *
  35. * @category Personal
  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. class UserdirectoryAction extends ManagedAction
  42. {
  43. protected $redirectAfterLogin = true;
  44. /**
  45. * The page we're on
  46. *
  47. * @var integer
  48. */
  49. public $page;
  50. /**
  51. * What to filter the search results by
  52. *
  53. * @var string
  54. */
  55. public $filter;
  56. /**
  57. * Column to sort by
  58. *
  59. * @var string
  60. */
  61. public $sort;
  62. /**
  63. * How to order search results, ascending or descending
  64. *
  65. * @var string
  66. */
  67. public $reverse;
  68. /**
  69. * Query
  70. *
  71. * @var string
  72. */
  73. public $q;
  74. /**
  75. * Title of the page
  76. *
  77. * @return string Title of the page
  78. */
  79. public function title()
  80. {
  81. // @todo fixme: This looks kinda gross
  82. if ($this->filter == 'all') {
  83. if ($this->page != 1) {
  84. // TRANS: Page title for user directory. %d is a page number.
  85. return(sprintf(_m('User Directory, page %d'), $this->page));
  86. }
  87. // TRANS: Page title for user directory.
  88. return _m('User directory');
  89. } elseif ($this->page == 1) {
  90. return sprintf(
  91. // TRANS: Page title for user directory. %s is the applied filter.
  92. _m('User directory - %s'),
  93. strtoupper($this->filter)
  94. );
  95. } else {
  96. return sprintf(
  97. // TRANS: Page title for user directory.
  98. // TRANS: %1$s is the applied filter, %2$d is a page number.
  99. _m('User directory - %1$s, page %2$d'),
  100. strtoupper($this->filter),
  101. $this->page
  102. );
  103. }
  104. }
  105. /**
  106. * Instructions for use
  107. *
  108. * @return instructions for use
  109. */
  110. public function getInstructions()
  111. {
  112. // TRANS: %%site.name%% is the name of the StatusNet site.
  113. return _m('Search for people on %%site.name%% by their name, '
  114. . 'location, or interests. Separate the terms by spaces; '
  115. . ' they must be 3 characters or more.');
  116. }
  117. /**
  118. * Is this page read-only?
  119. *
  120. * @return boolean true
  121. */
  122. public function isReadOnly($args)
  123. {
  124. return true;
  125. }
  126. protected function doPreparation()
  127. {
  128. $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
  129. $this->filter = $this->arg('filter', 'all');
  130. $this->reverse = $this->boolean('reverse');
  131. $this->q = $this->trimmed('q');
  132. $this->sort = $this->arg('sort', 'nickname');
  133. }
  134. /**
  135. * Show the page notice
  136. *
  137. * Shows instructions for the page
  138. *
  139. * @return void
  140. */
  141. public function showPageNotice()
  142. {
  143. $instr = $this->getInstructions();
  144. $output = common_markup_to_html($instr);
  145. $this->elementStart('div', 'instructions');
  146. $this->raw($output);
  147. $this->elementEnd('div');
  148. }
  149. /**
  150. * Content area
  151. *
  152. * Shows the list of popular notices
  153. *
  154. * @return void
  155. */
  156. public function showContent()
  157. {
  158. $this->showForm();
  159. $this->elementStart('div', ['id' => 'profile_directory']);
  160. $alphaNav = new AlphaNav($this, false, false, ['0-9', 'All']);
  161. $alphaNav->show();
  162. $profile = null;
  163. $profile = $this->getUsers();
  164. $cnt = 0;
  165. if (!empty($profile)) {
  166. $profileList = new SortableSubscriptionList(
  167. $profile,
  168. common_current_user(),
  169. $this
  170. );
  171. $cnt = $profileList->show();
  172. $profile->free();
  173. if (0 == $cnt) {
  174. $this->showEmptyListMessage();
  175. }
  176. }
  177. $args = [];
  178. if (isset($this->q)) {
  179. $args['q'] = $this->q;
  180. } elseif (isset($this->filter) && $this->filter != 'all') {
  181. $args['filter'] = $this->filter;
  182. }
  183. if (isset($this->sort)) {
  184. $args['sort'] = $this->sort;
  185. }
  186. if (!empty($this->reverse)) {
  187. $args['reverse'] = $this->reverse;
  188. }
  189. $this->pagination(
  190. $this->page > 1,
  191. $cnt > PROFILES_PER_PAGE,
  192. $this->page,
  193. 'userdirectory',
  194. $args
  195. );
  196. $this->elementEnd('div');
  197. }
  198. public function showForm($error=null)
  199. {
  200. $this->elementStart('form',
  201. ['method' => 'get',
  202. 'id' => 'form_search',
  203. 'class' => 'form_settings',
  204. 'action' => common_local_url('userdirectory')]);
  205. $this->elementStart('fieldset');
  206. // TRANS: Fieldset legend.
  207. $this->element('legend', null, _m('Search site'));
  208. $this->elementStart('ul', 'form_data');
  209. $this->elementStart('li');
  210. // TRANS: Field label for user directory filter.
  211. $this->input('q', _m('Keyword(s)'), $this->q);
  212. // TRANS: Button text.
  213. $this->submit('search', _m('BUTTON', 'Search'));
  214. $this->elementEnd('li');
  215. $this->elementEnd('ul');
  216. $this->elementEnd('fieldset');
  217. $this->elementEnd('form');
  218. }
  219. /*
  220. * Get users filtered by the current filter, sort key,
  221. * sort order, and page
  222. */
  223. public function getUsers()
  224. {
  225. $profile = new Profile();
  226. // Comment this out or disable to get global profile searches
  227. $profile->joinAdd(['id', 'user:id']);
  228. $offset = ($this->page - 1) * PROFILES_PER_PAGE;
  229. $limit = PROFILES_PER_PAGE + 1;
  230. if (!empty($this->q)) {
  231. // User is searching via query
  232. $search_engine = $profile->getSearchEngine('profile');
  233. $mode = 'reverse_chron';
  234. if ($this->sort == 'nickname') {
  235. if ($this->reverse) {
  236. $mode = 'nickname_desc';
  237. } else {
  238. $mode = 'nickname_asc';
  239. }
  240. } else {
  241. if ($this->reverse) {
  242. $mode = 'chron';
  243. }
  244. }
  245. $search_engine->set_sort_mode($mode);
  246. $search_engine->limit($offset, $limit);
  247. $search_engine->query($this->q);
  248. $profile->find();
  249. } else {
  250. // User is browsing via AlphaNav
  251. switch ($this->filter) {
  252. case 'all':
  253. // NOOP
  254. break;
  255. case '0-9':
  256. $profile->whereAdd(sprintf('LEFT(%1$s.%2$s, 1) BETWEEN %3$s AND %4$s',
  257. $profile->escapedTableName(),
  258. 'nickname',
  259. $profile->_quote("0"),
  260. $profile->_quote("9")));
  261. break;
  262. default:
  263. $profile->whereAdd(sprintf('LEFT(LOWER(%1$s.%2$s), 1) = %3$s',
  264. $profile->escapedTableName(),
  265. 'nickname',
  266. $profile->_quote($this->filter)));
  267. }
  268. $order = sprintf('%1$s.%2$s %3$s, %1$s.%4$s ASC',
  269. $profile->escapedTableName(),
  270. $this->getSortKey('nickname'),
  271. $this->reverse ? 'DESC' : 'ASC',
  272. 'nickname');
  273. $profile->orderBy($order);
  274. $profile->limit($offset, $limit);
  275. $profile->find();
  276. }
  277. return $profile;
  278. }
  279. /**
  280. * Filter the sort parameter
  281. *
  282. * @return string a column name for sorting
  283. */
  284. public function getSortKey($def='nickname')
  285. {
  286. switch ($this->sort) {
  287. case 'nickname':
  288. case 'created':
  289. return $this->sort;
  290. default:
  291. return 'nickname';
  292. }
  293. }
  294. /**
  295. * Show a nice message when there's no search results
  296. */
  297. public function showEmptyListMessage()
  298. {
  299. if (!empty($this->filter) && ($this->filter != 'all')) {
  300. $this->element('p',
  301. 'error',
  302. sprintf(
  303. // TRANS: Empty list message for user directory.
  304. _m('No users starting with %s'),
  305. $this->filter));
  306. } else {
  307. // TRANS: Empty list message for user directory.
  308. $this->element('p', 'error', _m('No results.'));
  309. // TRANS: Standard search suggestions shown when a search does not give any results.
  310. $message = _m("* Make sure all words are spelled correctly.
  311. * Try different keywords.
  312. * Try more general keywords.
  313. * Try fewer keywords.");
  314. $message .= "\n";
  315. $this->elementStart('div', 'help instructions');
  316. $this->raw(common_markup_to_html($message));
  317. $this->elementEnd('div');
  318. }
  319. }
  320. }