From 3d234db59d3147cb11aa441f94ae41a82f10d4d6 Mon Sep 17 00:00:00 2001
From: Brad Shuttleworth
'; }
if (!empty($usersRemoved)) { $msg .= ' ' . _kt('Removed') . ': ' . implode(', ',$usersRemoved) . '.'; }
-
+
if (!Permission::userIsSystemAdministrator($_SESSION['userID'])) {
$this->rollbackTransaction();
$this->errorRedirectTo('manageUsers', _kt('For security purposes, you cannot remove your own administration priviledges.'), sprintf('group_id=%d', $oGroup->getId()), sprintf("old_search=%s&do_search=1", $old_search));
@@ -363,8 +370,8 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
$this->successRedirectToMain($msg, sprintf("old_search=%s&do_search=1", $old_search));
}
// }}}
-
- // FIXME copy-paste ...
+
+ // FIXME copy-paste ...
// {{{ do_manageSubgroups
function do_manageSubgroups() {
$old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
@@ -381,27 +388,27 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
$aMemberGroupsUnkeyed = $oGroup->getMemberGroups();
- $aMemberGroups = array();
+ $aMemberGroups = array();
$aMemberIDs = array();
foreach ($aMemberGroupsUnkeyed as $oMemberGroup) {
$aMemberIDs[] = $oMemberGroup->getID();
- $aMemberGroups[$oMemberGroup->getID()] = $oMemberGroup;
- }
-
- $oJSONWidget = new KTJSONLookupWidget(_kt('Groups'),
- _kt('Select the groups from the left-hand list that you would like to add to this group and then click the right pointing arrows. Once you have added all the groups that you require, press save changes. Only groups that are logically capable of being included in this group will be available to be added.'),
- 'groups', '', $this->oPage, false, null, null,
- array('action' => sprintf('getSubGroups&group_id=%d', $oGroup->getID()),
- 'assigned' => $aMemberGroups,
- 'multi' => 'true',
- 'size' => '8'));
+ $aMemberGroups[$oMemberGroup->getID()] = $oMemberGroup;
+ }
+
+ $oJSONWidget = new KTJSONLookupWidget(_kt('Groups'),
+ _kt('Select the groups from the left-hand list that you would like to add to this group and then click the right pointing arrows. Once you have added all the groups that you require, press save changes. Only groups that are logically capable of being included in this group will be available to be added.'),
+ 'groups', '', $this->oPage, false, null, null,
+ array('action' => sprintf('getSubGroups&group_id=%d', $oGroup->getID()),
+ 'assigned' => $aMemberGroups,
+ 'multi' => 'true',
+ 'size' => '8'));
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate("ktcore/principals/groups_managesubgroups");
$aTemplateData = array("context" => $this,
- "edit_group" => $oGroup,
- 'widget'=>$oJSONWidget,
- "old_search" => $old_search,
+ "edit_group" => $oGroup,
+ 'widget'=>$oJSONWidget,
+ "old_search" => $old_search,
);
return $oTemplate->render($aTemplateData);
}
@@ -409,57 +416,57 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
function json_getSubGroups() {
- $sFilter = KTUtil::arrayGet($_REQUEST, 'filter', false);
- $aAllowedGroups = array('off'=>'-- Please filter --');
-
- if($sFilter && trim($sFilter)) {
- $iGroupID = KTUtil::arrayGet($_REQUEST, 'group_id', false);
- if(!$iGroupID) {
- return array('error'=>true, 'type'=>'kt.invalid_entity', 'message'=>_kt('An invalid group was selected'));
- }
-
- $oGroup = Group::get($iGroupID);
- $aMemberGroupsUnkeyed = $oGroup->getMemberGroups();
- $aMemberGroups = array();
- $aMemberIDs = array();
- foreach ($aMemberGroupsUnkeyed as $oMemberGroup) {
- $aMemberIDs[] = $oMemberGroup->getID();
- $aMemberGroups[$oMemberGroup->getID()] = $oMemberGroup;
- }
-
- $aGroupArray = GroupUtil::buildGroupArray();
- $aAllowedGroupIDs = GroupUtil::filterCyclicalGroups($oGroup->getID(), $aGroupArray);
- $aAllowedGroupIDs = array_diff($aAllowedGroupIDs, $aMemberIDs);
- $aAllowedGroups = array();
- foreach ($aAllowedGroupIDs as $iAllowedGroupID) {
- $g = Group::get($iAllowedGroupID);
- if (!PEAR::isError($g) && ($g != false)) {
- $aAllowedGroups[$iAllowedGroupID] = $g->getName();
- }
- }
- }
-
- return $aAllowedGroups;
+ $sFilter = KTUtil::arrayGet($_REQUEST, 'filter', false);
+ $aAllowedGroups = array('off'=>'-- Please filter --');
+
+ if($sFilter && trim($sFilter)) {
+ $iGroupID = KTUtil::arrayGet($_REQUEST, 'group_id', false);
+ if(!$iGroupID) {
+ return array('error'=>true, 'type'=>'kt.invalid_entity', 'message'=>_kt('An invalid group was selected'));
+ }
+
+ $oGroup = Group::get($iGroupID);
+ $aMemberGroupsUnkeyed = $oGroup->getMemberGroups();
+ $aMemberGroups = array();
+ $aMemberIDs = array();
+ foreach ($aMemberGroupsUnkeyed as $oMemberGroup) {
+ $aMemberIDs[] = $oMemberGroup->getID();
+ $aMemberGroups[$oMemberGroup->getID()] = $oMemberGroup;
+ }
+
+ $aGroupArray = GroupUtil::buildGroupArray();
+ $aAllowedGroupIDs = GroupUtil::filterCyclicalGroups($oGroup->getID(), $aGroupArray);
+ $aAllowedGroupIDs = array_diff($aAllowedGroupIDs, $aMemberIDs);
+ $aAllowedGroups = array();
+ foreach ($aAllowedGroupIDs as $iAllowedGroupID) {
+ $g = Group::get($iAllowedGroupID);
+ if (!PEAR::isError($g) && ($g != false)) {
+ $aAllowedGroups[$iAllowedGroupID] = $g->getName();
+ }
+ }
+ }
+
+ return $aAllowedGroups;
}
// {{{ _getUnitName
- function _getUnitName($oGroup) {
+ function _getUnitName($oGroup) {
$iUnitId = $oGroup->getUnitId();
if (empty($iUnitId)) {
return null;
}
- $u = Unit::get($iUnitId);
- if (PEAR::isError($u)) {
- return null; // XXX: prevent failure if the $u is a PEAR::error
- }
-
- return $u->getName();
- }
+ $u = Unit::get($iUnitId);
+ if (PEAR::isError($u)) {
+ return null; // XXX: prevent failure if the $u is a PEAR::error
+ }
+
+ return $u->getName();
+ }
// }}}
- // FIXME copy-paste ...
+ // FIXME copy-paste ...
// {{{ do_updateGroupMembers
function do_updateGroupMembers() {
$old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
@@ -488,7 +495,7 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
$res = $oGroup->addMemberGroup($oMemberGroup);
if (PEAR::isError($res)) {
$this->errorRedirectToMain(sprintf(_kt("Failed to add %s to %s"), $oMemberGroup->getName(), $oGroup->getName()), sprintf("old_search=%s&do_search=1", $old_search));
- exit(0);
+ exit(0);
} else { $groupsAdded[] = $oMemberGroup->getName(); }
}
}
@@ -499,7 +506,7 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
$res = $oGroup->removeMemberGroup($oMemberGroup);
if (PEAR::isError($res)) {
$this->errorRedirectToMain(sprintf(_kt("Failed to remove %s from %s"), $oMemberGroup->getName(), $oGroup->getName()), sprintf("old_search=%s&do_search=1", $old_search));
- exit(0);
+ exit(0);
} else { $groupsRemoved[] = $oMemberGroup->getName(); }
}
}
@@ -509,33 +516,94 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
if (!empty($groupsRemoved)) { $msg .= ' '. _kt('Removed'). ': ' . implode(', ',$groupsRemoved) . '.'; }
$this->commitTransaction();
-
+
$this->successRedirectToMain($msg, sprintf("old_search=%s&do_search=1", $old_search));
- }
+ }
// }}}
-
+
+ // overloaded because i'm lazy
+ // FIXME we probably want some way to generalise this
+ // FIXME (its a common entity-problem)
+ function form_addgroup() {
+ $oForm = new KTForm;
+ $oForm->setOptions(array(
+ 'identifier' => 'ktcore.groups.add',
+ 'label' => _kt("Create a new group"),
+ 'submit_label' => _kt("Create group"),
+ 'action' => 'creategroup',
+ 'fail_action' => 'addgroup',
+ 'cancel_action' => 'main',
+ 'context' => $this,
+ ));
+ $oForm->setWidgets(array(
+ array('ktcore.widgets.string',array(
+ 'name' => 'group_name',
+ 'label' => _kt("Group Name"),
+ 'description' => _kt('A short name for the group. e.g. administrators.'),
+ 'value' => null,
+ 'required' => true,
+ )),
+ array('ktcore.widgets.boolean',array(
+ 'name' => 'sysadmin',
+ 'label' => _kt("System Administrators"),
+ 'description' => _kt('Should all the members of this group be given system administration privileges?'),
+ 'value' => null,
+ )),
+ ));
+
+ $oForm->setValidators(array(
+ array('ktcore.validators.string', array(
+ 'test' => 'group_name',
+ 'output' => 'group_name',
+ )),
+ array('ktcore.validators.boolean', array(
+ 'test' => 'sysadmin',
+ 'output' => 'sysadmin',
+ )),
+ ));
+
+ // if we have any units.
+ $aUnits = Unit::getList();
+ if (!PEAR::isError($aUnits) && !empty($aUnits)) {
+ $oForm->addWidgets(array(
+ array('ktcore.widgets.entityselection', array(
+ 'name' => 'unit',
+ 'label' => _kt('Unit'),
+ 'description' => _kt('Which Unit is this group part of?'),
+ 'vocab' => $aUnits,
+ 'label_method' => 'getName',
+ 'simple_select' => false,
+ 'unselected_label' => _kt("No unit"),
+ )),
+ array('ktcore.widgets.boolean',array(
+ 'name' => 'unitadmin',
+ 'label' => _kt("Unit Administrators"),
+ 'description' => _kt('Should all the members of this group be given unit administration privileges?'),
+ 'important_description' => _kt("Note that its not possible to set a group without a unit as as having unit administration privileges."),
+ 'value' => null,
+ )),
+ ));
+
+ $oForm->addValidators(array(
+ array('ktcore.validators.entity', array(
+ 'test' => 'unit',
+ 'class' => 'Unit',
+ 'output' => 'unit',
+ )),
+ array('ktcore.validators.boolean', array(
+ 'test' => 'unitadmin',
+ 'output' => 'unitadmin',
+ )),
+ ));
+ }
+
+ return $oForm;
+ }
+
// {{{ do_addGroup
function do_addGroup() {
- $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Group Management'));
- $this->oPage->setBreadcrumbDetails(_kt('Add a new group'));
-
-
- $this->oPage->setTitle(_kt("Add a new group"));
-
- $edit_fields = array();
- $add_fields[] = new KTStringWidget(_kt('Group Name'), _kt('A short name for the group. e.g. administrators.'), 'group_name', null, $this->oPage, true);
- $add_fields[] = new KTCheckboxWidget(_kt('Unit Administrators'), _kt('Should all the members of this group be given unit administration privileges?'), 'is_unitadmin', false, $this->oPage, false);
- $add_fields[] = new KTCheckboxWidget(_kt('System Administrators'), _kt('Should all the members of this group be given system administration privileges?'), 'is_sysadmin', false, $this->oPage, false);
- // grab all units.
-
- $oUnits = Unit::getList();
- $vocab = array();
- $vocab[0] = _kt('No Unit');
- foreach ($oUnits as $oUnit) { $vocab[$oUnit->getID()] = $oUnit->getName(); }
- $aOptions = array('vocab' => $vocab);
-
- $add_fields[] = new KTLookupWidget(_kt('Unit'), _kt('Which Unit is this group part of?'), 'unit_id', 0, $this->oPage, false, null, null, $aOptions);
-
+ $this->oPage->setBreadcrumbDetails(_kt('Add a new group'));
+
$aAuthenticationSources = array();
$aAllAuthenticationSources =& KTAuthenticationSource::getList();
foreach ($aAllAuthenticationSources as $oSource) {
@@ -546,48 +614,60 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
$aAuthenticationSources[] = $oSource;
}
}
-
- $oTemplating =& KTTemplating::getSingleton();
- $oTemplate = $oTemplating->loadTemplate("ktcore/principals/addgroup");
- $aTemplateData = array(
- "context" => $this,
- "add_fields" => $add_fields,
- "authentication_sources" => $aAuthenticationSources,
- );
- return $oTemplate->render($aTemplateData);
+
+ $oTemplating =& KTTemplating::getSingleton();
+ $oTemplate = $oTemplating->loadTemplate("ktcore/principals/addgroup");
+ $aTemplateData = array(
+ "context" => $this,
+ "add_fields" => $add_fields,
+ "authentication_sources" => $aAuthenticationSources,
+ 'form' => $this->form_addgroup(),
+ );
+ return $oTemplate->render($aTemplateData);
}
// }}}
// {{{ do_createGroup
- function do_createGroup() {
-
- $group_name = KTUtil::arrayGet($_REQUEST, 'group_name');
- if (empty($group_name)) { $this->errorRedirectToMain(_kt('Please specify a name for the group.')); }
- $is_unitadmin = KTUtil::arrayGet($_REQUEST, 'is_unitadmin', false);
- if ($is_unitadmin !== false) { $is_unitadmin = true; }
- $is_sysadmin = KTUtil::arrayGet($_REQUEST, 'is_sysadmin', false);
- if ($is_sysadmin !== false) { $is_sysadmin = true; }
- $unit_id = KTUtil::arrayGet($_REQUEST, 'unit_id', null);
-
- $this->startTransaction();
-
- $oGroup =& Group::createFromArray(array(
- 'sName' => $group_name,
- 'bIsUnitAdmin' => $is_unitadmin,
- 'bIsSysAdmin' => $is_sysadmin,
- 'UnitId' => $unit_id,
- ));
- //$res = $oGroup->create();
- //if (($res == false) || (PEAR::isError($res))) { return $this->errorRedirectToMain('Failed to create group "' . $group_name . '"'); }
- // do i need to "create"
- $this->commitTransaction();
-
- if($unit_id == 0 && $is_unitadmin) {
- $this->successRedirectToMain(sprintf(_kt('Group "%s" created.'), $group_name) . _kt(' Note: group is set as unit administrator, but is not assigned to a unit.'));
- } else {
- $this->successRedirectToMain(sprintf(_kt('Group "%s" created.'), $group_name));
- }
- }
+ function do_creategroup() {
+ $oForm = $this->form_addgroup();
+ $res = $oForm->validate();
+ $data = $res['results'];
+ $errors = $res['errors'];
+ $extra_errors = array();
+
+ if (is_null($data['unit']) && $data['unitadmin']) {
+ $extra_errors['unitadmin'] = _kt("Groups without units cannot be Unit Administrators.");
+ }
+
+ $oGroup = Group::getByName($data['group_name']);
+ if (!PEAR::isError($oGroup)) {
+ $extra_errors['group_name'][] = _kt("There is already a group with that name.");
+ }
+
+ if (!empty($errors) || !empty($extra_errors)) {
+ return $oForm->handleError(null, $extra_errors);
+ }
+
+ $this->startTransaction();
+
+ $unit = null;
+ if (!is_null($data['unit'])) {
+ $unit = $data['unit']->getId();
+ }
+
+ $oGroup =& Group::createFromArray(array(
+ 'sName' => $data['group_name'],
+ 'bIsUnitAdmin' => $data['unitadmin'],
+ 'bIsSysAdmin' => $data['sysadmin'],
+ 'UnitId' => $unit,
+ ));
+ if (PEAR::isError($oGroup)) {
+ return $oForm->handleError(sprintf(_kt("Unable to create group: %s"), $oGroup->getMessage()));
+ }
+ $this->commitTransaction();
+
+ $this->successRedirectToMain(sprintf(_kt('Group "%s" created.'), $data['group_name']));
+ }
// }}}
// {{{ do_deleteGroup
@@ -603,7 +683,7 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
$this->startTransaction();
foreach($oGroup->getParentGroups() as $oParentGroup) {
- $res = $oParentGroup->removeMemberGroup($oGroup);
+ $res = $oParentGroup->removeMemberGroup($oGroup);
}
$res = $oGroup->delete();
@@ -639,25 +719,25 @@ class KTGroupAdminDispatcher extends KTAdminDispatcher {
}
// }}}
- function getGroupStringForGroup($oGroup) {
- $aGroupNames = array();
- $aGroups = $oGroup->getMemberGroups();
- $MAX_GROUPS = 6;
- $add_elipsis = false;
- if (count($aGroups) == 0) { return _kt('Group currently has no subgroups.'); }
- if (count($aGroups) > $MAX_GROUPS) {
- $aGroups = array_slice($aGroups, 0, $MAX_GROUPS);
- $add_elipsis = true;
- }
- foreach ($aGroups as $oGroup) {
- $aGroupNames[] = $oGroup->getName();
- }
- if ($add_elipsis) {
- $aGroupNames[] = '…';
- }
-
- return implode(', ', $aGroupNames);
- }
+ function getGroupStringForGroup($oGroup) {
+ $aGroupNames = array();
+ $aGroups = $oGroup->getMemberGroups();
+ $MAX_GROUPS = 6;
+ $add_elipsis = false;
+ if (count($aGroups) == 0) { return _kt('Group currently has no subgroups.'); }
+ if (count($aGroups) > $MAX_GROUPS) {
+ $aGroups = array_slice($aGroups, 0, $MAX_GROUPS);
+ $add_elipsis = true;
+ }
+ foreach ($aGroups as $oGroup) {
+ $aGroupNames[] = $oGroup->getName();
+ }
+ if ($add_elipsis) {
+ $aGroupNames[] = '…';
+ }
+
+ return implode(', ', $aGroupNames);
+ }
// }}}
}
diff --git a/templates/ktcore/principals/addgroup.smarty b/templates/ktcore/principals/addgroup.smarty
index 1dd7afb..dee7a50 100644
--- a/templates/ktcore/principals/addgroup.smarty
+++ b/templates/ktcore/principals/addgroup.smarty
@@ -25,7 +25,7 @@ will be maintained as it is in the authentication source.{/i18n}
{i18n}Alternatively, you can manually create a user within KnowledgeTree below.{/i18n}
{/if} - +{* +*} +{$form->render()} -- libgit2 0.21.4