userdirectory.php 10 KB

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