PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/share_manager/forms/content_object_share_form.class.php

https://bitbucket.org/chamilo/chamilo-repository-dev/
PHP | 260 lines | 194 code | 51 blank | 15 comment | 13 complexity | dcdbade87500a7489742f4f398116e78 MD5 | raw file
  1. <?php
  2. namespace repository;
  3. use rights\RightsManager;
  4. use rights\PlatformGroupEntity;
  5. use rights\UserEntity;
  6. use user\UserManager;
  7. use group\GroupManager;
  8. use common\libraries\FormValidator;
  9. use common\libraries\Translation;
  10. use common\libraries\Path;
  11. use common\libraries\ToolbarItem;
  12. use common\libraries\Toolbar;
  13. use common\libraries\Theme;
  14. use common\libraries\Utilities;
  15. use common\libraries\Session;
  16. use common\libraries\Request;
  17. use common\libraries\AdvancedElementFinderElementTypes;
  18. /**
  19. * $Id: user_view_form.class.php 200 2009-11-13 12:30:04Z kariboe $
  20. * @package repository.lib.forms
  21. * @author Sven Vanpoucke
  22. */
  23. class ContentObjectShareForm extends FormValidator
  24. {
  25. const PARAM_RIGHT = 'right';
  26. const PARAM_TARGET = 'targets';
  27. const PARAM_USER = 'user';
  28. const PARAM_GROUP = 'group';
  29. const PARAM_COPY = 'copy';
  30. const TYPE_CREATE = 1;
  31. const TYPE_EDIT = 2;
  32. private $content_object_ids;
  33. private $form_type;
  34. private $user;
  35. /**
  36. * The entities to edit the rights
  37. * @var Array
  38. */
  39. private $entities;
  40. function __construct($form_type, $content_object_ids, $user, $action)
  41. {
  42. parent :: __construct('content_object_share_form', 'post', $action);
  43. $this->content_object_ids = $content_object_ids;
  44. $this->form_type = $form_type;
  45. $this->user = $user;
  46. $this->entities = array();
  47. $this->entities[UserEntity :: ENTITY_TYPE] = new UserEntity();
  48. $this->entities[PlatformGroupEntity :: ENTITY_TYPE] = new PlatformGroupEntity();
  49. if ($this->form_type == self :: TYPE_EDIT)
  50. {
  51. $this->build_editing_form();
  52. }
  53. elseif ($this->form_type == self :: TYPE_CREATE)
  54. {
  55. $this->build_creation_form();
  56. }
  57. $this->setDefaults();
  58. }
  59. function get_content_object_ids()
  60. {
  61. return $this->content_object_ids;
  62. }
  63. static function factory($form_type, $content_object_ids = array(), $user, $action)
  64. {
  65. if (count($content_object_ids) === 1)
  66. {
  67. $rdm = RepositoryDataManager :: get_instance();
  68. $content_object = $rdm->retrieve_content_object($content_object_ids[0]);
  69. if ($content_object)
  70. {
  71. $type = $content_object->get_type_name();
  72. $file = Path :: get_repository_content_object_path() . $type . '/php/' . $type . '_content_object_share_form.class.php';
  73. if (file_exists($file))
  74. {
  75. require_once $file;
  76. $class = __NAMESPACE__ . '\content_object\\' . $type . '\\' . Utilities :: underscores_to_camelcase($type) . 'ContentObjectShareForm';
  77. $share_form = new $class($form_type, $content_object_ids, $user, $action);
  78. return $share_form;
  79. }
  80. }
  81. }
  82. return new ContentObjectShareForm($form_type, $content_object_ids, $user, $action);
  83. }
  84. function build_basic_form()
  85. {
  86. $rights = RepositoryRights :: get_share_rights();
  87. // $rights[-1] = Translation :: get('--SelectShareRight--', null, Utilities :: COMMON_LIBRARIES);
  88. // ksort($rights);
  89. $this->addElement('select', self :: PARAM_RIGHT, Translation :: get('Rights', null, RightsManager :: APPLICATION_NAME), $rights);
  90. $this->addElement('checkbox', self :: PARAM_COPY, Translation :: get('CopyRight', null, RightsManager :: APPLICATION_NAME));
  91. }
  92. function build_editing_form()
  93. {
  94. $this->build_basic_form();
  95. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Update', null, Utilities :: COMMON_LIBRARIES), array(
  96. 'class' => 'positive update'));
  97. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
  98. 'class' => 'normal empty'));
  99. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  100. }
  101. function build_creation_form()
  102. {
  103. $this->build_basic_form();
  104. $types = new AdvancedElementFinderElementTypes();
  105. foreach ($this->entities as $entity)
  106. {
  107. $types->add_element_type($entity->get_element_finder_type());
  108. }
  109. $this->addElement('advanced_element_finder', self :: PARAM_TARGET, Translation :: get('Targets'), $types);
  110. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array(
  111. 'class' => 'positive'));
  112. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
  113. 'class' => 'normal empty'));
  114. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  115. }
  116. function create_content_object_share()
  117. {
  118. $values = $this->exportValues();
  119. $user_ids = $values[self :: PARAM_TARGET][self :: PARAM_USER];
  120. $group_ids = $values[self :: PARAM_TARGET][self :: PARAM_GROUP];
  121. $right_id = $values[self :: PARAM_RIGHT];
  122. $copy_right = $values[self :: PARAM_COPY];
  123. $succes = true;
  124. foreach ($this->content_object_ids as $content_object_id)
  125. {
  126. $location = ShareManager :: get_current_user_tree_location(Session :: get_user_id(), $content_object_id);
  127. foreach ($user_ids as $user_id)
  128. {
  129. if ($right_id > - 1)
  130. {
  131. $succes &= RepositoryRights :: get_instance()->set_location_entity_right($right_id, $user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
  132. }
  133. if ($copy_right)
  134. {
  135. $succes &= RepositoryRights :: get_instance()->set_location_entity_right(RepositoryRights :: COPY_RIGHT, $user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
  136. }
  137. }
  138. foreach ($group_ids as $group_id)
  139. {
  140. if ($right_id > - 1)
  141. {
  142. $succes &= RepositoryRights :: get_instance()->set_location_entity_right($right_id, $group_id, PlatformGroupEntity :: ENTITY_TYPE, $location->get_id());
  143. }
  144. if ($copy_right)
  145. {
  146. $succes &= RepositoryRights :: get_instance()->set_location_entity_right(RepositoryRights :: COPY_RIGHT, $group_id, PlatformGroupEntity :: ENTITY_TYPE, $location->get_id());
  147. }
  148. }
  149. }
  150. return $succes;
  151. }
  152. function update_content_object_share($target_user_ids = array(), $target_group_ids = array())
  153. {
  154. $rdm = RepositoryDataManager :: get_instance();
  155. $succes = true;
  156. $values = $this->exportValues();
  157. $right_id = $values[self :: PARAM_RIGHT];
  158. $has_copy_right = $values[self :: PARAM_COPY];
  159. foreach ($this->content_object_ids as $content_object_id)
  160. {
  161. $location = ShareManager :: get_current_user_tree_location(Session :: get_user_id(), $content_object_id);
  162. foreach ($target_user_ids as $target_user_id)
  163. {
  164. $succes = RepositoryRights :: get_instance()->clear_share_entity_rights($location, UserEntity :: ENTITY_TYPE, $target_user_id);
  165. $succes = RepositoryRights :: get_instance()->invert_repository_location_entity_right($right_id, $target_user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
  166. if ($has_copy_right)
  167. {
  168. $succes = RepositoryRights :: get_instance()->invert_repository_location_entity_right(RepositoryRights :: COPY_RIGHT, $target_user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
  169. }
  170. }
  171. foreach ($target_group_ids as $target_group_id)
  172. {
  173. $succes = RepositoryRights :: get_instance()->clear_share_entity_rights($location, PlatformGroupEntity :: ENTITY_TYPE, $target_group_id);
  174. $succes = RepositoryRights :: get_instance()->invert_repository_location_entity_right($right_id, $target_group_id, PlatformGroupEntity :: ENTITY_TYPE, $location->get_id());
  175. }
  176. }
  177. return $succes;
  178. }
  179. /**
  180. * Sets default values.
  181. * @param array $defaults Default values for this form's parameters.
  182. */
  183. function set_default_rights($target_user_ids = array(), $target_group_ids = array())
  184. {
  185. $rdm = RepositoryDataManager :: get_instance();
  186. $location = RepositoryRights :: get_instance()->get_location_by_identifier_from_users_subtree(RepositoryRights :: TYPE_USER_CONTENT_OBJECT, $this->content_object_ids[0], Session :: get_user_id());
  187. if (count($target_user_ids) > 0)
  188. {
  189. $granted_rights = RepositoryRights :: get_instance()->get_granted_rights_for_rights_entity_item(RepositoryManager :: APPLICATION_NAME, UserEntity :: ENTITY_TYPE, $target_user_ids[0], $location);
  190. }
  191. elseif (count($target_group_ids) > 0)
  192. {
  193. $granted_rights = RepositoryRights :: get_instance()->get_granted_rights_for_rights_entity_item(RepositoryManager :: APPLICATION_NAME, PlatformGroupEntity :: ENTITY_TYPE, $target_group_ids[0], $location);
  194. }
  195. $copy_right = array_search(RepositoryRights :: COPY_RIGHT, $granted_rights);
  196. $right = $granted_rights[0];
  197. if ($copy_right)
  198. {
  199. array_splice($granted_rights, $copy_right, 1);
  200. $copy_right = true;
  201. }
  202. else
  203. {
  204. $copy_right = false;
  205. }
  206. $this->setDefaults(array(self :: PARAM_RIGHT => max($granted_rights), self :: PARAM_COPY => $copy_right));
  207. }
  208. }
  209. ?>