PageRenderTime 21ms CodeModel.GetById 7ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/php/forms/privilege_granting_form.class.php

https://bitbucket.org/chamilo/chamilo-ext-repo-bitbucket-dev/
PHP | 104 lines | 82 code | 16 blank | 6 comment | 2 complexity | 24619e2b51235abdb241a3931b4e7ab0 MD5 | raw file
  1<?php
  2namespace common\extensions\external_repository_manager\implementation\bitbucket;
  3
  4use common\libraries\FormValidator;
  5use common\libraries\Utilities;
  6use common\libraries\Translation;
  7use common\libraries\Path;
  8use common\libraries\StringUtilities;
  9use common\libraries\ResourceManager;
 10
 11use repository\ExternalSetting;
 12
 13use common\extensions\external_repository_manager\ExternalRepositoryObjectDisplay;
 14
 15class PriviligeGrantingForm extends FormValidator
 16{
 17    /**
 18     * The renderer used to display the form
 19     */
 20    private $renderer;
 21    private $bitbucket;
 22    
 23    const TYPE_PRIVILEGE = 'type';
 24    const TYPE_READ = 'read';
 25    const TYPE_WRITE = 'write';
 26    const TYPE_ADMIN = 'admin';
 27
 28    function __construct($action, $bitbucket)
 29    {
 30        parent :: __construct(Utilities :: get_classname_from_object($this, true), 'post', $action);
 31        $this->renderer = clone $this->defaultRenderer();
 32        
 33        $this->bitbucket = $bitbucket;
 34        $this->build();
 35        
 36        $this->accept($this->renderer);
 37    }
 38
 39    function build()
 40    {
 41        $this->renderer->setElementTemplate('<div style="vertical-align: middle; float: left; margin-right: 5px;">{element}</div>');
 42        $this->addElement('text', 'username', Translation :: get('User'));
 43        $groups = self :: get_groups_name();
 44        if (count($groups) > 0)
 45        {
 46            $this->addElement('select', 'groups', Translation :: get('Groups'), $groups);
 47        }
 48        $this->addElement('select', self :: TYPE_PRIVILEGE, Translation :: get('PrivilegeType'), self :: get_privileges_types());
 49        $this->addElement('style_submit_button', 'submit', Translation :: get('Grant', null, Utilities :: COMMON_LIBRARIES), array(
 50                'class' => 'positive update'));
 51        
 52        $this->addElement('html', ResourceManager :: get_instance()->get_resource_html(Path :: get_common_extensions_path(true) . 'external_repository_manager/implementation/bitbucket/resources/javascript/privilege_granting_form.js'));
 53    }
 54
 55    function get_groups_name()
 56    {
 57        $username = ExternalSetting :: get('username', $this->bitbucket->get_external_repository()->get_id());
 58        $groups_name = $this->bitbucket->get_external_repository_manager_connector()->retrieve_groups('chamilo');
 59        foreach ($groups_name as $group_name)
 60        {
 61            $groups[$group_name->get_owner_username() . '/' . $group_name->get_slug()] = $group_name->get_slug();
 62        }
 63        return $groups;
 64    }
 65
 66    /**
 67     * Display the form
 68     */
 69    function toHtml()
 70    {
 71        $html = array();
 72        $html[] = '<div>';
 73        $html[] = $this->renderer->toHTML();
 74        $html[] = '</div>';
 75        return implode('', $html);
 76    }
 77
 78    function grant_privilege()
 79    {
 80        $values = $this->exportValues();
 81        $group = $values['groups'];
 82        $user = $values['username'];
 83        
 84        if ($user)
 85        {
 86            return $this->bitbucket->get_external_repository_manager_connector()->grant_user_privilege($this->bitbucket->get_repository()->get_id(), $values['username'], $values['type']);
 87        }
 88        elseif ($group)
 89        {
 90            return $this->bitbucket->get_external_repository_manager_connector()->grant_group_privileges($this->bitbucket->get_repository()->get_id(), $values['groups'], $values['type']);
 91        }
 92    }
 93
 94    static function get_privileges_types()
 95    {
 96        $privileges_types = array();
 97        $privileges_types[self :: TYPE_READ] = Translation :: get('Read');
 98        $privileges_types[self :: TYPE_WRITE] = Translation :: get('Write');
 99        $privileges_types[self :: TYPE_ADMIN] = Translation :: get('Admin');
100        
101        return $privileges_types;
102    }
103}
104?>