userdirectory.php 10 KB

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