groupmanagement.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 772 行 · 第 1/3 页

PHP
772
字号
                $oMemberGroup = Group::get($iMemberGroupID);
                $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);
                } else { $groupsRemoved[] = $oMemberGroup->getName(); }
            }
        }
        
        $msg = '';
        if (!empty($groupsAdded)) { $msg .= ' ' . _kt('Added') . ': ' . implode(', ', $groupsAdded) . '. '; }
        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. <strong>administrators</strong>.'),
                '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 <strong>system</strong> 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 <strong>unit</strong> administration privileges?'),
                    'important_description' => _kt("Note that its not possible to set a group without a unit 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->oPage->setBreadcrumbDetails(_kt('Add a new group'));
        
        $aAuthenticationSources = array();
        $aAllAuthenticationSources =& KTAuthenticationSource::getList();
        foreach ($aAllAuthenticationSources as $oSource) {
            $sProvider = $oSource->getAuthenticationProvider();
            $oRegistry =& KTAuthenticationProviderRegistry::getSingleton();
            $oProvider =& $oRegistry->getAuthenticationProvider($sProvider);
            if ($oProvider->bGroupSource) {
                $aAuthenticationSources[] = $oSource;
            }
        }
            
        $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() {
        $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(preg_match('/[\!\$\#\%\^\&\*]/', $data['group_name'])){
        	$extra_errors['group_name'][] = _kt("You have entered an invalid character.");
        }
		
        if ($data['group_name'] == ''){
        	$extra_errors['group_name'][] = _kt("You have entered an invalid 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' => KTUtil::arrayGet($data, 'unitadmin', false),
             '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
    function do_deleteGroup() {
        $old_search = KTUtil::arrayGet($_REQUEST, 'old_search');    
    
        $aErrorOptions = array(
            'redirect_to' => array('main', sprintf("old_search=%s&do_search=1", $old_search)),
        );
        $oGroup = $this->oValidator->validateGroup($_REQUEST['group_id'], $aErrorOptions);
        $sGroupName = $oGroup->getName();

        $this->startTransaction();
        
        foreach($oGroup->getParentGroups() as $oParentGroup) {
            $res = $oParentGroup->removeMemberGroup($oGroup);        
        }
        
        $res = $oGroup->delete();
        $this->oValidator->notError($res, $aErrorOptions);
        
        if (!Permission::userIsSystemAdministrator($_SESSION['userID'])) {
            $this->rollbackTransaction();
            $this->errorRedirectTo('main', _kt('For security purposes, you cannot remove your own administration priviledges.'), sprintf("old_search=%s&do_search=1", $old_search));
            exit(0);
        }
        $this->commitTransaction();
        $this->successRedirectToMain(sprintf(_kt('Group "%s" deleted.'), $sGroupName), sprintf("old_search=%s&do_search=1", $old_search));
    }
    // }}}

    // {{{ authentication provider stuff

    // {{{ do_addGroupFromSource
    function do_addGroupFromSource() {
        $oSource =& KTAuthenticationSource::get($_REQUEST['source_id']);
        $sProvider = $oSource->getAuthenticationProvider();
        $oRegistry =& KTAuthenticationProviderRegistry::getSingleton();
        $oProvider =& $oRegistry->getAuthenticationProvider($sProvider);

        $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Group Management'));
        $this->aBreadcrumbs[] = array('url' => KTUtil::addQueryStringSelf('action=addGroup'), 'name' => _kt('add a new group'));
        $oProvider->aBreadcrumbs = $this->aBreadcrumbs;
        $oProvider->oPage->setBreadcrumbDetails($oSource->getName());
        $oProvider->oPage->setTitle(_kt("Modify Group Details"));

        $oProvider->dispatch();
        exit(0);
    }
    // }}}

    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[] = '&hellip;';
        }
        
        return implode(', ', $aGroupNames);
    }
    // }}}
}

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?