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.
This commit is contained in:
Evan Prodromou 2009-06-14 23:37:24 -07:00
parent 09c765f5bb
commit ecbd7718d5
6 changed files with 185 additions and 6 deletions

8
README
View File

@ -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 set 'run' to 'never' than to set this value to something
nonsensical. nonsensical.
attachments 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 monthly_quota: total size permitted in the current month. This is the total
size in bytes that a user can upload each month. 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 Troubleshooting
=============== ===============

View File

@ -171,6 +171,7 @@ class EditgroupAction extends Action
$homepage = $this->trimmed('homepage'); $homepage = $this->trimmed('homepage');
$description = $this->trimmed('description'); $description = $this->trimmed('description');
$location = $this->trimmed('location'); $location = $this->trimmed('location');
$aliasstring = $this->trimmed('aliases');
if (!Validate::string($nickname, array('min_length' => 1, if (!Validate::string($nickname, array('min_length' => 1,
'max_length' => 64, 'max_length' => 64,
@ -201,6 +202,39 @@ class EditgroupAction extends Action
return; 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); $orig = clone($this->group);
$this->group->nickname = $nickname; $this->group->nickname = $nickname;
@ -217,6 +251,14 @@ class EditgroupAction extends Action
$this->serverError(_('Could not update group.')); $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) { if ($this->group->nickname != $orig->nickname) {
common_redirect(common_local_url('editgroup', common_redirect(common_local_url('editgroup',
array('nickname' => $nickname)), array('nickname' => $nickname)),
@ -229,9 +271,20 @@ class EditgroupAction extends Action
function nicknameExists($nickname) function nicknameExists($nickname)
{ {
$group = User_group::staticGet('nickname', $nickname); $group = User_group::staticGet('nickname', $nickname);
return (!is_null($group) &&
$group != false && if (!empty($group) &&
$group->id != $this->group->id); $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;
} }
} }

View File

@ -123,6 +123,7 @@ class NewgroupAction extends Action
$homepage = $this->trimmed('homepage'); $homepage = $this->trimmed('homepage');
$description = $this->trimmed('description'); $description = $this->trimmed('description');
$location = $this->trimmed('location'); $location = $this->trimmed('location');
$aliasstring = $this->trimmed('aliases');
if (!Validate::string($nickname, array('min_length' => 1, if (!Validate::string($nickname, array('min_length' => 1,
'max_length' => 64, 'max_length' => 64,
@ -153,6 +154,37 @@ class NewgroupAction extends Action
return; 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(); $cur = common_current_user();
// Checked in prepare() above // Checked in prepare() above
@ -177,6 +209,12 @@ class NewgroupAction extends Action
$this->serverError(_('Could not create group.')); $this->serverError(_('Could not create group.'));
} }
$result = $group->setAliases($aliases);
if (!$result) {
$this->serverError(_('Could not create aliases.'));
}
$member = new Group_member(); $member = new Group_member();
$member->group_id = $group->id; $member->group_id = $group->id;
@ -199,7 +237,18 @@ class NewgroupAction extends Action
function nicknameExists($nickname) function nicknameExists($nickname)
{ {
$group = User_group::staticGet('nickname', $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;
} }
} }

View File

@ -165,4 +165,64 @@ class User_group extends Memcached_DataObject
{ {
return ($this->fullname) ? $this->fullname : $this->nickname; 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;
}
} }

View File

@ -198,6 +198,8 @@ $config =
'user_quota' => 50000000, 'user_quota' => 50000000,
'monthly_quota' => 15000000, 'monthly_quota' => 15000000,
), ),
'group' =>
array('maxaliases' => 3),
); );
$config['db'] = &PEAR::getStaticProperty('DB_DataObject','options'); $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');

View File

@ -111,7 +111,6 @@ class GroupEditForm extends Form
} }
} }
/** /**
* Name of the form * Name of the form
* *
@ -157,6 +156,16 @@ class GroupEditForm extends Form
($this->out->arg('location')) ? $this->out->arg('location') : $this->group->location, ($this->out->arg('location')) ? $this->out->arg('location') : $this->group->location,
_('Location for the group, if any, like "City, State (or Region), Country"')); _('Location for the group, if any, like "City, State (or Region), Country"'));
$this->out->elementEnd('li'); $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'); $this->out->elementEnd('ul');
} }