]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Directory/actions/userdirectory.php
Merge branch 'nightly' into file_urlhash
[quix0rs-gnu-social.git] / plugins / Directory / actions / userdirectory.php
index 60ab43693bc1f72f363958615f6c366f19a7c908..f178408e8bf11596d067fb5501ad1ead7c843956 100644 (file)
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET'))
-{
-    exit(1);
-}
-
-require_once INSTALLDIR . '/lib/publicgroupnav.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * User directory
@@ -43,17 +38,42 @@ require_once INSTALLDIR . '/lib/publicgroupnav.php';
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
-class UserdirectoryAction extends Action
+class UserdirectoryAction extends ManagedAction
 {
     /**
-     * @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
+     */
+    public $filter;
+
+    /**
+     * Column to sort by
+     *
+     * @var string
      */
-    protected $page   = null;
+    public $sort;
 
     /**
-     * @var $filter     string    what to filter the search results by
+     * How to order search results, ascending or descending
+     *
+     * @var string
      */
-    protected $filter = null;
+    public $reverse;
+
+    /**
+     * Query
+     *
+     * @var string
+     */
+    public $q;
 
     /**
      * Title of the page
@@ -62,24 +82,27 @@ class UserdirectoryAction extends Action
      */
     function title()
     {
-        // @fixme: This looks kinda gross
+        // @todo fixme: This looks kinda gross
 
         if ($this->filter == 'all') {
             if ($this->page != 1) {
-                return(sprintf(_m('All users, page %d'), $this->page));
+                // TRANS: Page title for user directory. %d is a page number.
+                return(sprintf(_m('User Directory, page %d'), $this->page));
             }
-            return _m('All users');
-        }
-
-        if ($this->page == 1) {
+            // TRANS: Page title for user directory.
+            return _m('User directory');
+        } else if ($this->page == 1) {
             return sprintf(
-                _m('Users with nicknames beginning with %s'),
-                $this->filter
+                // TRANS: Page title for user directory. %s is the applied filter.
+                _m('User directory - %s'),
+                strtoupper($this->filter)
             );
         } else {
             return sprintf(
-                _m('Users with nicknames starting with %s, page %d'),
-                $this->filter,
+                // TRANS: Page title for user directory.
+                // TRANS: %1$s is the applied filter, %2$d is a page number.
+                _m('User directory - %1$s, page %2$d'),
+                strtoupper($this->filter),
                 $this->page
             );
         }
@@ -92,7 +115,11 @@ class UserdirectoryAction extends Action
      */
     function getInstructions()
     {
-        return _('User directory');
+        // TRANS: %%site.name%% is the name of the StatusNet site.
+        return _m('Search for people on %%site.name%% by their name, '
+            . 'location, or interests. Separate the terms by spaces; '
+            . ' they must be 3 characters or more.'
+        );
     }
 
     /**
@@ -105,43 +132,15 @@ class UserdirectoryAction extends Action
         return true;
     }
 
-    /**
-     * Take arguments for running
-     *
-     * @param array $args $_REQUEST args
-     *
-     * @return boolean success flag
-     *
-     * @todo move queries from showContent() to here
-     */
-    function prepare($args)
+    protected function doPreparation()
     {
-        parent::prepare($args);
-
-        $this->page   = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
-        $filter       = $this->arg('filter');
-        $this->filter = isset($filter) ? $filter : 'all';
-        $this->sort   = $this->arg('sort');
-        $this->order  = $this->boolean('asc'); // ascending or decending
+        $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;
-    }
-
-    /**
-     * Handle request
-     *
-     * Shows the page
-     *
-     * @param array $args $_REQUEST args; handled in prepare()
-     *
-     * @return void
-     */
-    function handle($args)
-    {
-        parent::handle($args);
-        $this->showPage();
     }
 
     /**
@@ -161,18 +160,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
@@ -183,15 +170,14 @@ class UserdirectoryAction extends Action
      */
     function showContent()
     {
-        // XXX Need search bar
+        $this->showForm();
 
-        $this->elementStart('div', array('id' => 'user_directory'));
+        $this->elementStart('div', array('id' => 'profile_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;
 
@@ -203,53 +189,137 @@ class UserdirectoryAction extends Action
             );
 
             $cnt = $profileList->show();
+            $profile->free();
 
             if (0 == $cnt) {
                 $this->showEmptyListMessage();
             }
         }
 
+        $args = array();
+        if (isset($this->q)) {
+            $args['q'] = $this->q;
+        } elseif (isset($this->filter) && $this->filter != 'all') {
+            $args['filter'] = $this->filter;
+        }
+        
+        if (isset($this->sort)) {
+            $args['sort'] = $this->sort;
+        }        
+        if (!empty($this->reverse)) {
+            $args['reverse'] = $this->reverse;
+        }
+
         $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');
+
+        // TRANS: Fieldset legend.
+        $this->element('legend', null, _m('Search site'));
+        $this->elementStart('ul', 'form_data');
+        $this->elementStart('li');
+
+        // TRANS: Field label for user directory filter.
+        $this->input('q', _m('Keyword(s)'), $this->q);
+
+        // TRANS: Button text.
+        $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()
     {
-
         $profile = new Profile();
 
+        // Comment this out or disable to get global profile searches
+        $profile->joinAdd(array('id', 'user:id'));
+
         $offset = ($this->page - 1) * PROFILES_PER_PAGE;
         $limit  = PROFILES_PER_PAGE + 1;
-        $sort   = $this->getSortKey();
-        $sql    = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id';
 
-        if ($this->filter != 'all') {
-            $sql .= sprintf(
-                ' AND LEFT(LOWER(profile.nickname), 1) = \'%s\'',
-                $this->filter
-            );
-        }
+        if (!empty($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
+
+            switch ($this->filter) {
+            case 'all':
+                // NOOP
+                break;
+            case '0-9':
+                $profile->whereAdd(sprintf('LEFT(%1$s.%2$s, 1) BETWEEN %3$s AND %4$s',
+                                            $profile->escapedTableName(),
+                                            'nickname',
+                                            $profile->_quote("0"),
+                                            $profile->_quote("9")));
+                break;
+            default:
+                $profile->whereAdd(sprintf('LEFT(LOWER(%1$s.%2$s), 1) = %3$s',
+                                            $profile->escapedTableName(),
+                                            'nickname',
+                                            $profile->_quote($this->filter)));
+            }
 
-        $sql .= sprintf(
-            ' ORDER BY profile.%s %s, profile.nickname DESC LIMIT %d, %d',
-            $sort,
-            ($this->order) ? 'ASC' : 'DESC',
-            $offset,
-            $limit
-        );
+            $order = sprintf('%1$s.%2$s %3$s, %1$s.%4$s ASC',
+                            $profile->escapedTableName(),
+                            $this->getSortKey('nickname'),
+                            $this->reverse ? 'DESC' : 'ASC',
+                            'nickname');
+            $profile->orderBy($order);
+            $profile->limit($offset, $limit);
 
-        $profile->query($sql);
+            $profile->find();
+        }
 
         return $profile;
     }
@@ -259,15 +329,12 @@ class UserdirectoryAction extends Action
      *
      * @return string   a column name for sorting
      */
-    function getSortKey()
+    function getSortKey($def='nickname')
     {
         switch ($this->sort) {
         case 'nickname':
-            return $this->sort;
-            break;
         case 'created':
             return $this->sort;
-            break;
         default:
             return 'nickname';
         }
@@ -278,11 +345,29 @@ class UserdirectoryAction extends Action
      */
     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(
+                    // TRANS: Empty list message for user directory.
+                    _m('No users starting with %s'),
+                    $this->filter
+                )
+            );
+        } else {
+            // TRANS: Empty list message for user directory.
+            $this->element('p', 'error', _m('No results.'));
+            // TRANS: Standard search suggestions shown when a search does not give any results.
+            $message = _m("* Make sure all words are spelled correctly.
+* Try different keywords.
+* Try more general keywords.
+* Try fewer keywords.");
+            $message .= "\n";
+
+            $this->elementStart('div', 'help instructions');
+            $this->raw(common_markup_to_html($message));
+            $this->elementEnd('div');
+        }
     }
-
 }