]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Code for adding and saving group aliases
authorEvan Prodromou <evan@controlyourself.ca>
Mon, 15 Jun 2009 06:37:24 +0000 (23:37 -0700)
committerEvan Prodromou <evan@controlyourself.ca>
Mon, 15 Jun 2009 06:37:24 +0000 (23:37 -0700)
Added code to add and save group aliases. Like tags, aliases are
free-texted in to the group admin page. configurable max number of
aliases, default is three.

README
actions/editgroup.php
actions/newgroup.php
classes/User_group.php
lib/common.php
lib/groupeditform.php

diff --git a/README b/README
index 2099f94d62f46f953bd13716ef9b3abc0d98cd00..8fb4a941cf6c1555802eef2a8321297bf8f7ca69 100644 (file)
--- a/README
+++ b/README
@@ -1196,7 +1196,6 @@ reporturl: URL to post statistics to. Defaults to Laconica developers'
            set 'run' to 'never' than to set this value to something
            nonsensical.
 
-
 attachments
 -----------
 
@@ -1226,6 +1225,13 @@ user_quota: total size in bytes a user can store on this server. Each user
 monthly_quota: total size permitted in the current month. This is the total
             size in bytes that a user can upload each month.
 
+group
+-----
+
+Options for group functionality.
+
+maxaliases: maximum number of aliases a group can have. Default 3. Set
+            to 0 or less to prevent aliases in a group.
 
 Troubleshooting
 ===============
index 39dad0465eb070ced615f205661e1115cb7f3b39..29a7bce437c661c582213254767d43b0a941bab2 100644 (file)
@@ -171,6 +171,7 @@ class EditgroupAction extends Action
         $homepage    = $this->trimmed('homepage');
         $description = $this->trimmed('description');
         $location    = $this->trimmed('location');
+        $aliasstring = $this->trimmed('aliases');
 
         if (!Validate::string($nickname, array('min_length' => 1,
                                                'max_length' => 64,
@@ -201,6 +202,39 @@ class EditgroupAction extends Action
             return;
         }
 
+        if (!empty($aliasstring)) {
+            $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
+        } else {
+            $aliases = array();
+        }
+
+        if (count($aliases) > common_config('group', 'maxaliases')) {
+            $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
+                                    common_config('group', 'maxaliases')));
+            return;
+        }
+
+        foreach ($aliases as $alias) {
+            if (!Validate::string($alias, array('min_length' => 1,
+                                                'max_length' => 64,
+                                                'format' => NICKNAME_FMT))) {
+                $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
+                return;
+            }
+            if ($this->nicknameExists($alias)) {
+                $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
+                                        $alias));
+                return;
+            }
+            // XXX assumes alphanum nicknames
+            if (strcmp($alias, $nickname) == 0) {
+                $this->showForm(_('Alias can\'t be the same as nickname.'));
+                return;
+            }
+        }
+
+        $this->group->query('BEGIN');
+
         $orig = clone($this->group);
 
         $this->group->nickname    = $nickname;
@@ -217,6 +251,14 @@ class EditgroupAction extends Action
             $this->serverError(_('Could not update group.'));
         }
 
+        $result = $this->group->setAliases($aliases);
+
+        if (!$result) {
+            $this->serverError(_('Could not create aliases.'));
+        }
+
+        $this->group->query('COMMIT');
+
         if ($this->group->nickname != $orig->nickname) {
             common_redirect(common_local_url('editgroup',
                                              array('nickname' => $nickname)),
@@ -229,9 +271,20 @@ class EditgroupAction extends Action
     function nicknameExists($nickname)
     {
         $group = User_group::staticGet('nickname', $nickname);
-        return (!is_null($group) &&
-                $group != false &&
-                $group->id != $this->group->id);
+
+        if (!empty($group) &&
+            $group->id != $this->group->id) {
+            return true;
+        }
+
+        $alias = Group_alias::staticGet('alias', $nickname);
+
+        if (!empty($alias) &&
+            $alias->group_id != $this->group->id) {
+            return true;
+        }
+
+        return false;
     }
 }
 
index 67cd6b2f18005f3efc3878a71bec65cbe92a5365..0289e77c2511a7aebd096a4d78e66d54ac72dbe3 100644 (file)
@@ -123,6 +123,7 @@ class NewgroupAction extends Action
         $homepage    = $this->trimmed('homepage');
         $description = $this->trimmed('description');
         $location    = $this->trimmed('location');
+        $aliasstring = $this->trimmed('aliases');
 
         if (!Validate::string($nickname, array('min_length' => 1,
                                                'max_length' => 64,
@@ -153,6 +154,37 @@ class NewgroupAction extends Action
             return;
         }
 
+        if (!empty($aliasstring)) {
+            $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
+        } else {
+            $aliases = array();
+        }
+
+        if (count($aliases) > common_config('group', 'maxaliases')) {
+            $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
+                                    common_config('group', 'maxaliases')));
+            return;
+        }
+
+        foreach ($aliases as $alias) {
+            if (!Validate::string($alias, array('min_length' => 1,
+                                                'max_length' => 64,
+                                                'format' => NICKNAME_FMT))) {
+                $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
+                return;
+            }
+            if ($this->nicknameExists($alias)) {
+                $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
+                                        $alias));
+                return;
+            }
+            // XXX assumes alphanum nicknames
+            if (strcmp($alias, $nickname) == 0) {
+                $this->showForm(_('Alias can\'t be the same as nickname.'));
+                return;
+            }
+        }
+
         $cur = common_current_user();
 
         // Checked in prepare() above
@@ -177,6 +209,12 @@ class NewgroupAction extends Action
             $this->serverError(_('Could not create group.'));
         }
 
+        $result = $group->setAliases($aliases);
+
+        if (!$result) {
+            $this->serverError(_('Could not create aliases.'));
+        }
+
         $member = new Group_member();
 
         $member->group_id   = $group->id;
@@ -199,7 +237,18 @@ class NewgroupAction extends Action
     function nicknameExists($nickname)
     {
         $group = User_group::staticGet('nickname', $nickname);
-        return (!is_null($group) && $group != false);
+
+        if (!empty($group)) {
+            return true;
+        }
+
+        $alias = Group_alias::staticGet('alias', $nickname);
+
+        if (!empty($alias)) {
+            return true;
+        }
+
+        return false;
     }
 }
 
index 9f9977755be5e8182a1f20637f7c42e7bdb7b6b9..0fcfee8c62e7739bb234a663f793a52ab5dd55a5 100644 (file)
@@ -165,4 +165,64 @@ class User_group extends Memcached_DataObject
     {
         return ($this->fullname) ? $this->fullname : $this->nickname;
     }
+
+    function getAliases()
+    {
+        $aliases = array();
+
+        // XXX: cache this
+
+        $alias = new Group_alias();
+
+        $alias->group_id = $this->id;
+
+        if ($alias->find()) {
+            while ($alias->fetch()) {
+                $aliases[] = $alias->alias;
+            }
+        }
+
+        $alias->free();
+
+        return $aliases;
+    }
+
+    function setAliases($newaliases) {
+
+        $newaliases = array_unique($newaliases);
+
+        $oldaliases = $this->getAliases();
+
+        # Delete stuff that's old that not in new
+
+        $to_delete = array_diff($oldaliases, $newaliases);
+
+        # Insert stuff that's in new and not in old
+
+        $to_insert = array_diff($newaliases, $oldaliases);
+
+        $alias = new Group_alias();
+
+        $alias->group_id = $this->id;
+
+        foreach ($to_delete as $delalias) {
+            $alias->alias = $delalias;
+            $result = $alias->delete();
+            if (!$result) {
+                common_log_db_error($alias, 'DELETE', __FILE__);
+                return false;
+            }
+        }
+
+        foreach ($to_insert as $insalias) {
+            $alias->alias = $insalias;
+            $result = $alias->insert();
+            if (!$result) {
+                common_log_db_error($alias, 'INSERT', __FILE__);
+                return false;
+            }
+        }
+
+        return true;
+    }
 }
index 6bf4ad21f5fea78baa57e86919b1e43008645aec..b4e87445e6a476237a3d6057ed7ea484a277edfa 100644 (file)
@@ -198,6 +198,8 @@ $config =
         'user_quota' => 50000000,
         'monthly_quota' => 15000000,
         ),
+        'group' =>
+        array('maxaliases' => 3),
         );
 
 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
index ca674f3c8e24a925c93b194c0866ce5748fc0663..7e8d6eea3ad93cb9d8f567e4dde8abfabe8f52b3 100644 (file)
@@ -111,7 +111,6 @@ class GroupEditForm extends Form
         }
     }
 
-
     /**
      * Name of the form
      *
@@ -157,6 +156,16 @@ class GroupEditForm extends Form
                      ($this->out->arg('location')) ? $this->out->arg('location') : $this->group->location,
                      _('Location for the group, if any, like "City, State (or Region), Country"'));
         $this->out->elementEnd('li');
+        if (common_config('group', 'maxaliases') > 0) {
+            $aliases = (empty($this->group)) ? array() : $this->group->getAliases();
+            $this->out->elementStart('li');
+            $this->out->input('aliases', _('Aliases'),
+                              ($this->out->arg('aliases')) ? $this->out->arg('aliases') :
+                              (!empty($aliases)) ? implode(' ', $aliases) : '',
+                              sprintf(_('Extra nicknames for the group, comma- or space- separated, max %d'),
+                                      common_config('group', 'maxaliases')));;
+            $this->out->elementEnd('li');
+        }
         $this->out->elementEnd('ul');
     }