X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FDirectory%2Factions%2Fuserdirectory.php;h=85835e2c1f85b3e70bddfe6f54c9f06e2ed62736;hb=579fc11862173c8be3a623ebc3248ce9d61835a8;hp=7e9f20c369ca1afb0114c8a3963e6563fc20f0bf;hpb=3b186e1bae91ae0030c8eeb58fdfbc114dfd0b84;p=quix0rs-gnu-social.git diff --git a/plugins/Directory/actions/userdirectory.php b/plugins/Directory/actions/userdirectory.php index 7e9f20c369..85835e2c1f 100644 --- a/plugins/Directory/actions/userdirectory.php +++ b/plugins/Directory/actions/userdirectory.php @@ -46,14 +46,39 @@ require_once INSTALLDIR . '/lib/publicgroupnav.php'; class UserdirectoryAction extends Action { /** - * @var $page integer the page we're on + * The page we're on + * + * @var integer + */ + public $page; + + /** + * What to filter the search results by + * + * @var string */ - protected $page = null; + public $filter; /** - * @var $filter string what to filter the search results by + * Column to sort by + * + * @var string */ - protected $filter = null; + public $sort; + + /** + * How to order search results, ascending or descending + * + * @var string + */ + public $reverse; + + /** + * Query + * + * @var string + */ + public $q; /** * Title of the page @@ -66,20 +91,18 @@ class UserdirectoryAction extends Action if ($this->filter == 'all') { if ($this->page != 1) { - return(sprintf(_m('All users, page %d'), $this->page)); + return(sprintf(_m('User Directory, page %d'), $this->page)); } - return _m('All users'); - } - - if ($this->page == 1) { + return _m('User directory'); + } else if ($this->page == 1) { return sprintf( - _m('Users with nicknames beginning with %s'), - $this->filter + _m('User directory - %s'), + strtoupper($this->filter) ); } else { return sprintf( - _m('Users with nicknames starting with %s, page %d'), - $this->filter, + _m('User directory - %s, page %d'), + strtoupper($this->filter), $this->page ); } @@ -92,7 +115,12 @@ class UserdirectoryAction extends Action */ function getInstructions() { - return _('User directory'); + // TRANS: %%site.name%% is the name of the StatusNet site. + return _( + 'Search for people on %%site.name%% by their name, ' + . 'location, or interests. Separate the terms by spaces; ' + . ' they must be 3 characters or more.' + ); } /** @@ -111,15 +139,17 @@ class UserdirectoryAction extends Action * @param array $args $_REQUEST args * * @return boolean success flag - * - * @todo move queries from showContent() to here */ function prepare($args) { parent::prepare($args); - $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1; - $this->filter = $this->arg('filter') ? $this->arg('filter') : 'all'; + $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1; + $this->filter = $this->arg('filter', 'all'); + $this->reverse = $this->boolean('reverse'); + $this->q = $this->trimmed('q'); + $this->sort = $this->arg('sort', 'nickname'); + common_set_returnto($this->selfUrl()); return true; @@ -157,18 +187,6 @@ class UserdirectoryAction extends Action $this->elementEnd('div'); } - /** - * Local navigation - * - * This page is part of the public group, so show that. - * - * @return void - */ - function showLocalNav() - { - $nav = new PublicGroupNav($this); - $nav->show(); - } /** * Content area @@ -179,80 +197,193 @@ class UserdirectoryAction extends Action */ function showContent() { - // XXX Need search bar + $this->showForm(); $this->elementStart('div', array('id' => 'user_directory')); - $alphaNav = new AlphaNav($this, true, array('All')); + $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All')); $alphaNav->show(); - // XXX Maybe use a more specialized version of ProfileList here - + $profile = null; $profile = $this->getUsers(); $cnt = 0; if (!empty($profile)) { - $profileList = new SubscriptionList( + $profileList = new SortableSubscriptionList( $profile, common_current_user(), $this ); $cnt = $profileList->show(); + $profile->free(); if (0 == $cnt) { $this->showEmptyListMessage(); } } + $args = array(); + if (isset($this->q)) { + $args['q'] = $this->q; + } else { + $args['filter'] = $this->filter; + } + $this->pagination( $this->page > 1, $cnt > PROFILES_PER_PAGE, $this->page, 'userdirectory', - array('filter' => $this->filter) + $args ); $this->elementEnd('div'); } + function showForm($error=null) + { + $this->elementStart( + 'form', + array( + 'method' => 'get', + 'id' => 'form_search', + 'class' => 'form_settings', + 'action' => common_local_url('userdirectory') + ) + ); + + $this->elementStart('fieldset'); + + $this->element('legend', null, _('Search site')); + $this->elementStart('ul', 'form_data'); + $this->elementStart('li'); + + $this->input('q', _('Keyword(s)'), $this->q); + + $this->submit('search', _m('BUTTON','Search')); + $this->elementEnd('li'); + $this->elementEnd('ul'); + $this->elementEnd('fieldset'); + $this->elementEnd('form'); + } + /* - * Get users filtered by the current filter and page + * Get users filtered by the current filter, sort key, + * sort order, and page */ function getUsers() { - $offset = ($this->page - 1) * PROFILES_PER_PAGE; - $limit = PROFILES_PER_PAGE + 1; - $profile = new Profile(); - // XXX Any chance of SQL injection here? + $offset = ($this->page - 1) * PROFILES_PER_PAGE; + $limit = PROFILES_PER_PAGE + 1; + + if (isset($this->q)) { + // User is searching via query + $search_engine = $profile->getSearchEngine('profile'); + + $mode = 'reverse_chron'; + + if ($this->sort == 'nickname') { + if ($this->reverse) { + $mode = 'nickname_desc'; + } else { + $mode = 'nickname_asc'; + } + } else { + if ($this->reverse) { + $mode = 'chron'; + } + } + + $search_engine->set_sort_mode($mode); + $search_engine->limit($offset, $limit); + $search_engine->query($this->q); + + $profile->find(); + } else { + // User is browsing via AlphaNav + $sort = $this->getSortKey(); + $sql = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id'; + + switch($this->filter) + { + case 'all': + // NOOP + break; + case '0-9': + $sql .= + ' AND LEFT(profile.nickname, 1) BETWEEN \'0\' AND \'9\''; + break; + default: + $sql .= sprintf( + ' AND LEFT(LOWER(profile.nickname), 1) = \'%s\'', + $this->filter + ); + } - if ($this->filter != 'all') { - $profile->whereAdd( - sprintf('LEFT(lower(nickname), 1) = \'%s\'', $this->filter) + $sql .= sprintf( + ' ORDER BY profile.%s %s, profile.nickname ASC LIMIT %d, %d', + $sort, + $this->reverse ? 'DESC' : 'ASC', + $offset, + $limit ); - } - $profile->orderBy('created DESC, nickname'); - $profile->limit($limit, $offset); - - $profile->find(); + $profile->query($sql); + } return $profile; } + /** + * Filter the sort parameter + * + * @return string a column name for sorting + */ + function getSortKey() + { + switch ($this->sort) { + case 'nickname': + return $this->sort; + break; + case 'created': + return $this->sort; + break; + default: + return 'nickname'; + } + } + /** * Show a nice message when there's no search results */ function showEmptyListMessage() { - $message = sprintf(_m('No users starting with %s'), $this->filter); - - $this->elementStart('div', 'guide'); - $this->raw(common_markup_to_html($message)); - $this->elementEnd('div'); + if (!empty($this->filter) && ($this->filter != 'all')) { + $this->element( + 'p', + 'error', + sprintf( + _m('No users starting with %s'), + $this->filter + ) + ); + } else { + $this->element('p', 'error', _('No results.')); + $message = _m(<<elementStart('div', 'help instructions'); + $this->raw(common_markup_to_html($message)); + $this->elementEnd('div'); + } } }