/php/forms/box_external_repository_manager_form.class.php

https://bitbucket.org/chamilo/chamilo-ext-repo-box-dev/ · PHP · 155 lines · 118 code · 33 blank · 4 comment · 10 complexity · b6a2737098093337d0033521c1bae261 MD5 · raw file

  1. <?php
  2. namespace common\extensions\external_repository_manager\implementation\box;
  3. use common\libraries\Translation;
  4. use common\libraries\Utilities;
  5. use common\libraries\StringUtilities;
  6. use common\libraries\FormValidator;
  7. use common\libraries\Request;
  8. use common\extensions\external_repository_manager\ExternalRepositoryObjectDisplay;
  9. /**
  10. * $Id: boxexternal_repository_manager_form.class.php 224 2009-11-13 14:40:30Z kariboe $
  11. * @package
  12. */
  13. class BoxExternalRepositoryManagerForm extends FormValidator
  14. {
  15. const TYPE_CREATE = 1;
  16. const TYPE_EDIT = 2;
  17. const TYPE_NEW_FOLDER = 3;
  18. const PREVIEW = 'preview';
  19. const FILE = 'file';
  20. private $application;
  21. private $form_type;
  22. private $external_repository_object;
  23. function __construct($form_type, $action, $application)
  24. {
  25. parent :: __construct(Utilities :: get_classname_from_object($this, true), 'post', $action);
  26. $this->application = $application;
  27. $this->form_type = $form_type;
  28. if ($this->form_type == self :: TYPE_EDIT)
  29. {
  30. $this->build_editing_form();
  31. }
  32. elseif ($this->form_type == self :: TYPE_CREATE)
  33. {
  34. $this->build_creation_form();
  35. }
  36. elseif ($this->form_type == self :: TYPE_NEW_FOLDER)
  37. {
  38. $this->build_newfolder_form();
  39. }
  40. $this->setDefaults();
  41. }
  42. public function set_external_repository_object(BoxExternalRepositoryObject $external_repository_object)
  43. {
  44. $this->external_repository_object = $external_repository_object;
  45. $defaults[BoxExternalRepositoryObject :: PROPERTY_ID] = $external_repository_object->get_id();
  46. $display = ExternalRepositoryObjectDisplay :: factory($external_repository_object);
  47. $defaults[self :: PREVIEW] = $display->get_preview();
  48. parent :: setDefaults($defaults);
  49. }
  50. function build_basic_form()
  51. {
  52. if ($this->form_type == self :: TYPE_EDIT)
  53. {
  54. $this->add_information_message('box_api_move', null, Translation :: get('BoxAPIMoveImpossible'));
  55. }
  56. }
  57. function build_editing_form()
  58. {
  59. $this->addElement('static', self :: PREVIEW);
  60. $this->build_basic_form();
  61. $this->addElement('hidden', BoxExternalRepositoryObject :: PROPERTY_ID);
  62. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Edit', null, Utilities :: COMMON_LIBRARIES), array(
  63. 'class' => 'positive update'));
  64. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
  65. 'class' => 'normal empty'));
  66. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  67. }
  68. function update_file()
  69. {
  70. return $this->application->get_external_repository_manager_connector()->update_external_repository_object($this->exportValues());
  71. }
  72. function upload_file()
  73. {
  74. if (StringUtilities :: has_value(($_FILES[self :: FILE]['name'])))
  75. {
  76. if ($this->application->get_external_repository_manager_connector()->create_external_repository_object($_FILES[self :: FILE]))
  77. return true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84. function build_creation_form()
  85. {
  86. $this->build_basic_form();
  87. $this->addElement('file', self :: FILE, Translation :: get('FileName'));
  88. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array(
  89. 'class' => 'positive'));
  90. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
  91. 'class' => 'normal empty'));
  92. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  93. }
  94. function build_newfolder_form()
  95. {
  96. $this->addElement('text', 'foldername', 'Name of new folder', array('size' => '50'));
  97. $this->addRule('foldername', Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES), 'required');
  98. $this->addElement('hidden', 'folder');
  99. $this->setDefaults(array('folder' => Request :: get('folder')));
  100. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array(
  101. 'class' => 'positive'));
  102. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
  103. 'class' => 'normal empty'));
  104. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  105. }
  106. function create_folder($folder)
  107. {
  108. if (empty($_POST['folder']))
  109. {
  110. $folder = 0;
  111. }
  112. else
  113. $folder = $_POST['folder'];
  114. if (! is_null($_POST['foldername']))
  115. {
  116. return $this->application->get_external_repository_manager_connector()->create_external_repository_folder($_POST['foldername'], $folder);
  117. }
  118. else
  119. return null;
  120. }
  121. }
  122. ?>