]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Directory/actions/groupdirectory.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / Directory / actions / groupdirectory.php
index 4e8e422bf23a96d018714116d72710f474cf5759..8aef1742a884cc0f5b268fa81bd9f55f61ff0158 100644 (file)
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET'))
-{
-    exit(1);
-}
-
-require_once INSTALLDIR . '/lib/publicgroupnav.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Group directory
@@ -40,10 +35,11 @@ require_once INSTALLDIR . '/lib/publicgroupnav.php';
  * @category Directory
  * @package  StatusNet
  * @author   Zach Copley <zach@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
-class GroupdirectoryAction extends Action
+class GroupdirectoryAction extends ManagedAction
 {
     /**
      * The page we're on
@@ -91,17 +87,23 @@ class GroupdirectoryAction extends Action
 
         if ($this->filter == 'all') {
             if ($this->page != 1) {
+                // TRANS: Title for group directory page. %d is a page number.
                 return(sprintf(_m('Group Directory, page %d'), $this->page));
             }
+            // TRANS: Title for group directory page.
             return _m('Group directory');
         } else if ($this->page == 1) {
             return sprintf(
+                // TRANS: Title for group directory page when it is filtered.
+                // TRANS: %s is the filter string.
                 _m('Group directory - %s'),
                 strtoupper($this->filter)
             );
         } else {
             return sprintf(
-                _m('Group directory - %s, page %d'),
+                // TRANS: Title for group directory page when it is filtered.
+                // TRANS: %1$s is the filter string, %2$d is a page number.
+                _m('Group directory - %1$s, page %2$d'),
                 strtoupper($this->filter),
                 $this->page
             );
@@ -115,19 +117,11 @@ class GroupdirectoryAction extends Action
      */
     function getInstructions()
     {
-        // TRANS: Page notice for groups directory.
-        // TRANS: %%site.name%% is the name of the StatusNet site.
-        // TRANS: %%action.newgroup%% is a URL. Do not change it.
-        // TRANS: This message contains Markdown links in the form [link text](link).
-        $instructions = <<< END_OF_INSTRUCTIONS
-After you join a group you can send messages to all other members
-using the syntax "!groupname".
-
-Browse groups, or search for groups on by their name, location or topic.
-Separate the terms by spaces; they must be three characters or more.
-END_OF_INSTRUCTIONS;
-
-        return _m($instructions);
+        // TRANS: Page instructions.
+        return _m("After you join a group you can send messages to all other members\n".
+                 "using the syntax \"!groupname\".\n\n".
+                 "Browse groups, or search for groups by their name, location or topic.\n".
+                 "Separate the terms by spaces; they must be three characters or more.") . "\n";
     }
 
     /**
@@ -135,22 +129,13 @@ END_OF_INSTRUCTIONS;
      *
      * @return boolean true
      */
-    function isReadOnly($args)
+    function isReadOnly(array $args=array())
     {
         return true;
     }
 
-    /**
-     * Take arguments for running
-     *
-     * @param array $args $_REQUEST args
-     *
-     * @return boolean success flag
-     */
-    function prepare($args)
+    protected function doPreparation()
     {
-        parent::prepare($args);
-
         $this->page    = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
         $this->filter  = $this->arg('filter', 'all');
         $this->reverse = $this->boolean('reverse');
@@ -158,23 +143,6 @@ END_OF_INSTRUCTIONS;
         $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();
     }
 
     /**
@@ -217,7 +185,7 @@ END_OF_INSTRUCTIONS;
                     'href'  => common_local_url('newgroup'),
                     'class' => 'more'),
                     // TRANS: Link to create a new group on the group list page.
-                    _('Create a new group')
+                    _m('Create a new group')
             );
             $this->elementEnd('p');
         }
@@ -226,6 +194,7 @@ END_OF_INSTRUCTIONS;
 
         $this->elementStart('div', array('id' => 'profile_directory'));
 
+        // @todo FIXME: Does "All" need i18n here?
         $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All'));
         $alphaNav->show();
 
@@ -280,12 +249,15 @@ END_OF_INSTRUCTIONS;
 
         $this->elementStart('fieldset');
 
+        // TRANS: Fieldset legend.
         $this->element('legend', null, _m('Search groups'));
         $this->elementStart('ul', 'form_data');
         $this->elementStart('li');
 
+        // TRANS: Field label for input of one or more keywords.
         $this->input('q', _m('Keyword(s)'), $this->q);
 
+        // TRANS: Button text for searching group directory.
         $this->submit('search', _m('BUTTON','Search'));
         $this->elementEnd('li');
         $this->elementEnd('ul');
@@ -301,74 +273,61 @@ END_OF_INSTRUCTIONS;
     {
         $group = new User_group();
 
-        $offset = ($this->page-1) * PROFILES_PER_PAGE;
-        $limit  = PROFILES_PER_PAGE + 1;
+        // Disable this to get global group searches
+        $group->joinAdd(array('id', 'local_group:group_id'));
 
-        if (isset($this->q)) {
+        $order = false;
 
-             $order = 'user_group.created ASC';
-
-             if ($this->sort == 'nickname') {
-                 if ($this->reverse) {
-                     $order = 'user_group.nickname DESC';
-                 } else {
-                     $order = 'user_group.nickname ASC';
-                 }
-             } else {
-                 if ($this->reverse) {
-                     $order = 'user_group.created DESC';
-                 }
-             }
-
-             $sql = <<< GROUP_QUERY_END
-SELECT user_group.*
-FROM user_group
-JOIN local_group ON user_group.id = local_group.group_id
-ORDER BY %s
-LIMIT %d, %d
-GROUP_QUERY_END;
-
-        $cnt = 0;
-        $group->query(sprintf($sql, $order, $limit, $offset));
-        $group->find();
+        if (!empty($this->q)) {
+            $wheres = array('nickname', 'fullname', 'homepage', 'description', 'location');
+            foreach ($wheres as $where) {
+                // Double % because of sprintf
+                $group->whereAdd(sprintf('LOWER(%1$s.%2$s) LIKE LOWER("%%%3$s%%")',
+                                        $group->escapedTableName(), $where,
+                                        $group->escape($this->q)),
+                                    'OR');
+            }
 
+            $order = sprintf('%1$s.%2$s %3$s',
+                            $group->escapedTableName(),
+                            $this->getSortKey('created'),
+                            $this->reverse ? 'DESC' : 'ASC');
         } else {
             // User is browsing via AlphaNav
-            $sort   = $this->getSortKey();
-
-            $sql = <<< GROUP_QUERY_END
-SELECT user_group.*
-FROM user_group
-JOIN local_group ON user_group.id = local_group.group_id
-GROUP_QUERY_END;
 
-            switch($this->filter)
-            {
+            switch($this->filter) {
             case 'all':
                 // NOOP
                 break;
             case '0-9':
-                $sql .=
-                    '  AND LEFT(user_group.nickname, 1) BETWEEN \'0\' AND \'9\'';
+                $group->whereAdd(sprintf('LEFT(%1$s.%2$s, 1) BETWEEN %3$s AND %4$s',
+                                        $group->escapedTableName(),
+                                        'nickname',
+                                        $group->_quote("0"),
+                                        $group->_quote("9")));
                 break;
             default:
-                $sql .= sprintf(
-                    ' AND LEFT(LOWER(user_group.nickname), 1) = \'%s\'',
-                    $this->filter
-                );
+                $group->whereAdd(sprintf('LEFT(LOWER(%1$s.%2$s), 1) = %3$s',
+                                            $group->escapedTableName(),
+                                            'nickname',
+                                            $group->_quote($this->filter)));
             }
 
-            $sql .= sprintf(
-                ' ORDER BY user_group.%s %s, user_group.nickname ASC LIMIT %d, %d',
-                $sort,
-                $this->reverse ? 'DESC' : 'ASC',
-                $offset,
-                $limit
-            );
-
-            $group->query($sql);
+            $order = sprintf('%1$s.%2$s %3$s, %1$s.%4$s ASC',
+                            $group->escapedTableName(),
+                            $this->getSortKey('nickname'),
+                            $this->reverse ? 'DESC' : 'ASC',
+                            'nickname');
         }
 
+        $offset = ($this->page-1) * PROFILES_PER_PAGE;
+        $limit  = PROFILES_PER_PAGE + 1;
+
+        $group->orderBy($order);
+        $group->limit($offset, $limit);
+
+        $group->find();
+
         return $group;
     }
 
@@ -377,17 +336,14 @@ GROUP_QUERY_END;
      *
      * @return string   a column name for sorting
      */
-    function getSortKey()
+    function getSortKey($def='created')
     {
         switch ($this->sort) {
         case 'nickname':
-            return $this->sort;
-            break;
         case 'created':
             return $this->sort;
-            break;
         default:
-            return 'nickname';
+            return $def;
         }
     }
 
@@ -401,19 +357,20 @@ GROUP_QUERY_END;
                 'p',
                 'error',
                 sprintf(
-                    _m('No groups starting with %s'),
+                    // TRANS: Empty list message for searching group directory.
+                    // TRANS: %s is the search string.
+                    _m('No groups starting with %s.'),
                     $this->filter
                 )
             );
         } else {
+            // TRANS: Empty list message for searching group directory.
             $this->element('p', 'error', _m('No results.'));
-            $message = _m(<<<E_O_T
-* Make sure all words are spelled correctly.
-* Try different keywords.
-* Try more general keywords.
-* Try fewer keywords.
-E_O_T
-);
+            // TRANS: Help text for searching group directory.
+            $message = _m("* Make sure all words are spelled correctly.\n".
+                          "* Try different keywords.\n".
+                          "* Try more general keywords.\n".
+                          "* Try fewer keywords.");
             $this->elementStart('div', 'help instructions');
             $this->raw(common_markup_to_html($message));
             $this->elementEnd('div');
@@ -427,5 +384,4 @@ E_O_T
         $gbm = new GroupsByMembersSection($this);
         $gbm->show();
     }
-
 }