/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
- <?php
- namespace common\extensions\external_repository_manager\implementation\bitbucket;
-
- use common\libraries\FormValidator;
- use common\libraries\Utilities;
- use common\libraries\Translation;
- use common\libraries\Path;
- use common\libraries\StringUtilities;
- use common\libraries\ResourceManager;
-
- use repository\ExternalSetting;
-
- use common\extensions\external_repository_manager\ExternalRepositoryObjectDisplay;
-
- class PriviligeGrantingForm extends FormValidator
- {
- /**
- * The renderer used to display the form
- */
- private $renderer;
- private $bitbucket;
-
- const TYPE_PRIVILEGE = 'type';
- const TYPE_READ = 'read';
- const TYPE_WRITE = 'write';
- const TYPE_ADMIN = 'admin';
-
- function __construct($action, $bitbucket)
- {
- parent :: __construct(Utilities :: get_classname_from_object($this, true), 'post', $action);
- $this->renderer = clone $this->defaultRenderer();
-
- $this->bitbucket = $bitbucket;
- $this->build();
-
- $this->accept($this->renderer);
- }
-
- function build()
- {
- $this->renderer->setElementTemplate('<div style="vertical-align: middle; float: left; margin-right: 5px;">{element}</div>');
- $this->addElement('text', 'username', Translation :: get('User'));
- $groups = self :: get_groups_name();
- if (count($groups) > 0)
- {
- $this->addElement('select', 'groups', Translation :: get('Groups'), $groups);
- }
- $this->addElement('select', self :: TYPE_PRIVILEGE, Translation :: get('PrivilegeType'), self :: get_privileges_types());
- $this->addElement('style_submit_button', 'submit', Translation :: get('Grant', null, Utilities :: COMMON_LIBRARIES), array(
- 'class' => 'positive update'));
-
- $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'));
- }
-
- function get_groups_name()
- {
- $username = ExternalSetting :: get('username', $this->bitbucket->get_external_repository()->get_id());
- $groups_name = $this->bitbucket->get_external_repository_manager_connector()->retrieve_groups('chamilo');
- foreach ($groups_name as $group_name)
- {
- $groups[$group_name->get_owner_username() . '/' . $group_name->get_slug()] = $group_name->get_slug();
- }
- return $groups;
- }
-
- /**
- * Display the form
- */
- function toHtml()
- {
- $html = array();
- $html[] = '<div>';
- $html[] = $this->renderer->toHTML();
- $html[] = '</div>';
- return implode('', $html);
- }
-
- function grant_privilege()
- {
- $values = $this->exportValues();
- $group = $values['groups'];
- $user = $values['username'];
-
- if ($user)
- {
- return $this->bitbucket->get_external_repository_manager_connector()->grant_user_privilege($this->bitbucket->get_repository()->get_id(), $values['username'], $values['type']);
- }
- elseif ($group)
- {
- return $this->bitbucket->get_external_repository_manager_connector()->grant_group_privileges($this->bitbucket->get_repository()->get_id(), $values['groups'], $values['type']);
- }
- }
-
- static function get_privileges_types()
- {
- $privileges_types = array();
- $privileges_types[self :: TYPE_READ] = Translation :: get('Read');
- $privileges_types[self :: TYPE_WRITE] = Translation :: get('Write');
- $privileges_types[self :: TYPE_ADMIN] = Translation :: get('Admin');
-
- return $privileges_types;
- }
- }
- ?>