userdirectory.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php declare(strict_types=1);
  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. * Count for display (`count` URL query).
  76. */
  77. private int $count;
  78. /**
  79. * Title of the page
  80. *
  81. * @return string Title of the page
  82. */
  83. public function title()
  84. {
  85. // @todo fixme: This looks kinda gross
  86. if ($this->filter == 'all') {
  87. if ($this->page != 1) {
  88. // TRANS: Page title for user directory. %d is a page number.
  89. return(sprintf(_m('User Directory, page %d'), $this->page));
  90. }
  91. // TRANS: Page title for user directory.
  92. return _m('User directory');
  93. } elseif ($this->page == 1) {
  94. return sprintf(
  95. // TRANS: Page title for user directory. %s is the applied filter.
  96. _m('User directory - %s'),
  97. strtoupper($this->filter)
  98. );
  99. } else {
  100. return sprintf(
  101. // TRANS: Page title for user directory.
  102. // TRANS: %1$s is the applied filter, %2$d is a page number.
  103. _m('User directory - %1$s, page %2$d'),
  104. strtoupper($this->filter),
  105. $this->page
  106. );
  107. }
  108. }
  109. /**
  110. * Instructions for use
  111. *
  112. * @return instructions for use
  113. */
  114. public function getInstructions()
  115. {
  116. // TRANS: %%site.name%% is the name of the StatusNet site.
  117. return _m('Search for people on %%site.name%% by their name, '
  118. . 'location, or interests. Separate the terms by spaces; '
  119. . ' they must be 3 characters or more.');
  120. }
  121. /**
  122. * Is this page read-only?
  123. *
  124. * @return boolean true
  125. */
  126. public function isReadOnly($args)
  127. {
  128. return true;
  129. }
  130. protected function doPreparation()
  131. {
  132. $this->page = ($this->arg('page')) ? ((int) $this->arg('page')) : 1;
  133. $this->filter = $this->arg('filter', 'all');
  134. $this->reverse = $this->boolean('reverse');
  135. $this->q = $this->trimmed('q');
  136. $this->sort = $this->arg('sort', 'nickname');
  137. $this->count = (int) $this->trimmed('count', (string) PROFILES_PER_PAGE);
  138. }
  139. /**
  140. * Show the page notice
  141. *
  142. * Shows instructions for the page
  143. *
  144. * @return void
  145. */
  146. public function showPageNotice()
  147. {
  148. $instr = $this->getInstructions();
  149. $output = common_markup_to_html($instr);
  150. $this->elementStart('div', 'instructions');
  151. $this->raw($output);
  152. $this->elementEnd('div');
  153. }
  154. /**
  155. * Content area
  156. *
  157. * Shows the list of popular notices
  158. *
  159. * @return void
  160. */
  161. public function showContent()
  162. {
  163. $this->showForm();
  164. $this->elementStart('div', ['id' => 'profile_directory']);
  165. $alphaNav = new AlphaNav($this, false, false, ['0-9', 'All']);
  166. $alphaNav->show();
  167. $profile = null;
  168. $profile = $this->getUsers();
  169. $cnt = 0;
  170. $args = [];
  171. if (!empty($profile)) {
  172. $profileList = new SortableSubscriptionList(
  173. $profile,
  174. common_current_user(),
  175. $this
  176. );
  177. $cnt = $profileList->profile->N;
  178. $this->pagination(
  179. $this->page > 1,
  180. $cnt > $this->count,
  181. $this->page,
  182. 'userdirectory',
  183. $args
  184. );
  185. $profileList->show($this->count);
  186. $profile->free();
  187. if (0 == $cnt) {
  188. $this->showEmptyListMessage();
  189. }
  190. }
  191. if (isset($this->q)) {
  192. $args['q'] = $this->q;
  193. } elseif (isset($this->filter) && $this->filter != 'all') {
  194. $args['filter'] = $this->filter;
  195. }
  196. if (isset($this->sort)) {
  197. $args['sort'] = $this->sort;
  198. }
  199. if (!empty($this->reverse)) {
  200. $args['reverse'] = $this->reverse;
  201. }
  202. $this->pagination(
  203. $this->page > 1,
  204. $cnt > $this->count,
  205. $this->page,
  206. 'userdirectory',
  207. $args
  208. );
  209. $this->elementEnd('div');
  210. // JavaScript for checkbox
  211. $this->inlineScript(<<<'EOT'
  212. /**
  213. * @param e - header checkbox input element Event
  214. */
  215. function allChanged(e) {
  216. let boxes = document.getElementsByName('ids[]');
  217. for (let i = 0; i < boxes.length; ++i) {
  218. if (e.target.checked === true) {
  219. boxes[i].checked = true;
  220. } else {
  221. boxes[i].checked = false;
  222. }
  223. }
  224. }
  225. document.querySelector('thead input').addEventListener('click', allChanged);
  226. EOT
  227. );
  228. }
  229. public function showForm($error=null): void
  230. {
  231. $this->elementStart('form',
  232. ['method' => 'get',
  233. 'id' => 'form_search',
  234. 'class' => 'form_settings',
  235. 'action' => common_local_url('userdirectory')]);
  236. $this->elementStart('fieldset');
  237. // TRANS: Fieldset legend.
  238. $this->element('legend', null, _m('Search site'));
  239. $this->elementStart('ul', 'form_data');
  240. $this->elementStart('li');
  241. // TRANS: Field label for user directory filter.
  242. $this->input('q', _m('Keyword(s)'), $this->q);
  243. // TRANS: Button text.
  244. $this->submit('search', _m('BUTTON', 'Search'));
  245. $this->elementEnd('li');
  246. $this->elementEnd('ul');
  247. $this->elementEnd('fieldset');
  248. $this->elementEnd('form');
  249. // Bulk action.
  250. $this->elementStart('fieldset');
  251. // TRANS: Button text.
  252. // TODO: tag edit, form class.
  253. $choices = [
  254. '' => 'Bulk actions',
  255. 'delete' => 'Delete',
  256. 'subscribe' => 'Subscribe',
  257. 'unsubscribe' => 'Unsubscribe'
  258. ];
  259. $this->dropdown('selected_action', '', $choices, '', false, '');
  260. $this->elementStart('button', ['id' => 'bulk']);
  261. $this->text('Bulk');
  262. $this->elementEnd('button');
  263. $this->inlineScript(<<<'EOT'
  264. async function bulkApply(e) {
  265. let map = {
  266. 'subscribe': {method: 'POST', path: '/api/friendships/create.json'},
  267. 'unsubscribe': {method: 'POST', path: '/api/friendships/destroy.json'},
  268. 'delete': {method: 'DELETE', path: '/api/account/delete.json'},
  269. };
  270. let choice = map[this.previousElementSibling.value];
  271. let boxes = document.querySelectorAll('input[name="ids[]"]:checked');
  272. for (let i = 0; i < boxes.length; ++i) {
  273. const response = await fetch(choice.path + '?user_id=' + boxes[i].value, {
  274. method: choice.method,
  275. headers: {'Content-Type': 'application/json'}
  276. });
  277. }
  278. }
  279. document.getElementById('bulk').addEventListener('click', bulkApply);
  280. EOT);
  281. $this->elementEnd('fieldset');
  282. // Display count.
  283. $this->elementStart('form', [
  284. 'action' => common_local_url('userdirectory')]);
  285. foreach(['page', 'count', 'sort', 'reverse'] as $e) {
  286. if (empty($this->args[$e])) {
  287. continue;
  288. }
  289. $this->hidden($e, $this->args[$e]);
  290. }
  291. // Number of items per page.
  292. $this->input('count', '', '', '', 'count', true, [
  293. 'type' => 'number',
  294. 'list' => 'countList',
  295. 'placeholder' => 'Display count',
  296. 'pattern' => '^[1-9][0-9]*',
  297. ]);
  298. $this->elementStart('datalist', ['id' => 'countList']);
  299. foreach ([20, 50, 100, 200, 500, 1000, 2000] as $e) {
  300. $this->elementStart('option', ['value' => $e]);
  301. $this->elementEnd('option');
  302. }
  303. $this->elementEnd('datalist');
  304. $this->submit('', _m('BUTTON', 'Apply'),);
  305. $this->elementEnd('form');
  306. }
  307. /*
  308. * Get users filtered by the current filter, sort key,
  309. * sort order, and page
  310. */
  311. public function getUsers()
  312. {
  313. $profile = new Profile();
  314. // Comment this out or disable to get global profile searches
  315. $profile->joinAdd(['id', 'user:id']);
  316. // Overflow for pagenation (max count + 1).
  317. $limit = $this->count + 1;
  318. $offset = ($this->page - 1) * $this->count;
  319. if (!empty($this->q)) {
  320. // User is searching via query
  321. $search_engine = $profile->getSearchEngine('profile');
  322. $mode = 'reverse_chron';
  323. if ($this->sort == 'nickname') {
  324. if ($this->reverse) {
  325. $mode = 'nickname_desc';
  326. } else {
  327. $mode = 'nickname_asc';
  328. }
  329. } else {
  330. if ($this->reverse) {
  331. $mode = 'chron';
  332. }
  333. }
  334. $search_engine->set_sort_mode($mode);
  335. $search_engine->limit($offset, $limit);
  336. $search_engine->query($this->q);
  337. $profile->find();
  338. } else {
  339. // User is browsing via AlphaNav
  340. switch ($this->filter) {
  341. case 'all':
  342. // NOOP
  343. break;
  344. case '0-9':
  345. $profile->whereAdd(sprintf('LEFT(%1$s.%2$s, 1) BETWEEN %3$s AND %4$s',
  346. $profile->escapedTableName(),
  347. 'nickname',
  348. $profile->_quote("0"),
  349. $profile->_quote("9")));
  350. break;
  351. default:
  352. $profile->whereAdd(sprintf('LEFT(LOWER(%1$s.%2$s), 1) = %3$s',
  353. $profile->escapedTableName(),
  354. 'nickname',
  355. $profile->_quote($this->filter)));
  356. }
  357. $order = sprintf('%1$s.%2$s %3$s, %1$s.%4$s ASC',
  358. $profile->escapedTableName(),
  359. $this->getSortKey('nickname'),
  360. $this->reverse ? 'DESC' : 'ASC',
  361. 'nickname');
  362. $profile->orderBy($order);
  363. $profile->limit($offset, $limit);
  364. $profile->find();
  365. }
  366. return $profile;
  367. }
  368. /**
  369. * Filter the sort parameter
  370. *
  371. * @return string a column name for sorting
  372. */
  373. public function getSortKey($def='nickname')
  374. {
  375. switch ($this->sort) {
  376. case 'nickname':
  377. case 'created':
  378. return $this->sort;
  379. default:
  380. return 'nickname';
  381. }
  382. }
  383. /**
  384. * Show a nice message when there's no search results
  385. */
  386. public function showEmptyListMessage()
  387. {
  388. if (!empty($this->filter) && ($this->filter != 'all')) {
  389. $this->element('p',
  390. 'error',
  391. sprintf(
  392. // TRANS: Empty list message for user directory.
  393. _m('No users starting with %s'),
  394. $this->filter));
  395. } else {
  396. // TRANS: Empty list message for user directory.
  397. $this->element('p', 'error', _m('No results.'));
  398. // TRANS: Standard search suggestions shown when a search does not give any results.
  399. $message = _m("* Make sure all words are spelled correctly.
  400. * Try different keywords.
  401. * Try more general keywords.
  402. * Try fewer keywords.");
  403. $message .= "\n";
  404. $this->elementStart('div', 'help instructions');
  405. $this->raw(common_markup_to_html($message));
  406. $this->elementEnd('div');
  407. }
  408. }
  409. }