summaryrefslogtreecommitdiff
path: root/actions
diff options
context:
space:
mode:
authorEvan Prodromou <evan@controlyourself.ca>2009-06-14 23:37:24 -0700
committerEvan Prodromou <evan@controlyourself.ca>2009-06-14 23:37:24 -0700
commitecbd7718d57fc427d2aeac885d8be0321b3cf1fe (patch)
treef7cdd06b431c78c9a8bdbe3b3355c0ef67ba49b3 /actions
parent09c765f5bbc5a96415b82fe6a7d550d590d90747 (diff)
Code for adding and saving group aliases
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.
Diffstat (limited to 'actions')
-rw-r--r--actions/editgroup.php59
-rw-r--r--actions/newgroup.php51
2 files changed, 106 insertions, 4 deletions
diff --git a/actions/editgroup.php b/actions/editgroup.php
index 39dad0465..29a7bce43 100644
--- a/actions/editgroup.php
+++ b/actions/editgroup.php
@@ -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;
}
}
diff --git a/actions/newgroup.php b/actions/newgroup.php
index 67cd6b2f1..0289e77c2 100644
--- a/actions/newgroup.php
+++ b/actions/newgroup.php
@@ -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;
}
}