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