/php/lib/share_manager/forms/content_object_share_form.class.php
PHP | 260 lines | 194 code | 51 blank | 15 comment | 13 complexity | dcdbade87500a7489742f4f398116e78 MD5 | raw file
- <?php
-
- namespace repository;
-
- use rights\RightsManager;
- use rights\PlatformGroupEntity;
- use rights\UserEntity;
- use user\UserManager;
- use group\GroupManager;
- use common\libraries\FormValidator;
- use common\libraries\Translation;
- use common\libraries\Path;
- use common\libraries\ToolbarItem;
- use common\libraries\Toolbar;
- use common\libraries\Theme;
- use common\libraries\Utilities;
- use common\libraries\Session;
- use common\libraries\Request;
- use common\libraries\AdvancedElementFinderElementTypes;
-
- /**
- * $Id: user_view_form.class.php 200 2009-11-13 12:30:04Z kariboe $
- * @package repository.lib.forms
- * @author Sven Vanpoucke
- */
- class ContentObjectShareForm extends FormValidator
- {
- const PARAM_RIGHT = 'right';
- const PARAM_TARGET = 'targets';
- const PARAM_USER = 'user';
- const PARAM_GROUP = 'group';
- const PARAM_COPY = 'copy';
-
- const TYPE_CREATE = 1;
- const TYPE_EDIT = 2;
-
- private $content_object_ids;
- private $form_type;
- private $user;
- /**
- * The entities to edit the rights
- * @var Array
- */
- private $entities;
-
- function __construct($form_type, $content_object_ids, $user, $action)
- {
- parent :: __construct('content_object_share_form', 'post', $action);
-
- $this->content_object_ids = $content_object_ids;
- $this->form_type = $form_type;
- $this->user = $user;
-
- $this->entities = array();
- $this->entities[UserEntity :: ENTITY_TYPE] = new UserEntity();
- $this->entities[PlatformGroupEntity :: ENTITY_TYPE] = new PlatformGroupEntity();
-
- if ($this->form_type == self :: TYPE_EDIT)
- {
- $this->build_editing_form();
- }
- elseif ($this->form_type == self :: TYPE_CREATE)
- {
- $this->build_creation_form();
- }
-
- $this->setDefaults();
- }
-
- function get_content_object_ids()
- {
- return $this->content_object_ids;
- }
-
- static function factory($form_type, $content_object_ids = array(), $user, $action)
- {
- if (count($content_object_ids) === 1)
- {
-
- $rdm = RepositoryDataManager :: get_instance();
-
- $content_object = $rdm->retrieve_content_object($content_object_ids[0]);
-
- if ($content_object)
- {
- $type = $content_object->get_type_name();
- $file = Path :: get_repository_content_object_path() . $type . '/php/' . $type . '_content_object_share_form.class.php';
- if (file_exists($file))
- {
- require_once $file;
- $class = __NAMESPACE__ . '\content_object\\' . $type . '\\' . Utilities :: underscores_to_camelcase($type) . 'ContentObjectShareForm';
-
- $share_form = new $class($form_type, $content_object_ids, $user, $action);
-
- return $share_form;
- }
- }
- }
-
- return new ContentObjectShareForm($form_type, $content_object_ids, $user, $action);
- }
-
- function build_basic_form()
- {
- $rights = RepositoryRights :: get_share_rights();
- // $rights[-1] = Translation :: get('--SelectShareRight--', null, Utilities :: COMMON_LIBRARIES);
- // ksort($rights);
-
-
- $this->addElement('select', self :: PARAM_RIGHT, Translation :: get('Rights', null, RightsManager :: APPLICATION_NAME), $rights);
- $this->addElement('checkbox', self :: PARAM_COPY, Translation :: get('CopyRight', null, RightsManager :: APPLICATION_NAME));
- }
-
- function build_editing_form()
- {
- $this->build_basic_form();
-
- $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Update', null, Utilities :: COMMON_LIBRARIES), array(
- 'class' => 'positive update'));
- $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
- 'class' => 'normal empty'));
-
- $this->addGroup($buttons, 'buttons', null, ' ', false);
- }
-
- function build_creation_form()
- {
- $this->build_basic_form();
-
- $types = new AdvancedElementFinderElementTypes();
- foreach ($this->entities as $entity)
- {
- $types->add_element_type($entity->get_element_finder_type());
- }
-
- $this->addElement('advanced_element_finder', self :: PARAM_TARGET, Translation :: get('Targets'), $types);
-
- $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array(
- 'class' => 'positive'));
- $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array(
- 'class' => 'normal empty'));
-
- $this->addGroup($buttons, 'buttons', null, ' ', false);
- }
-
- function create_content_object_share()
- {
- $values = $this->exportValues();
-
- $user_ids = $values[self :: PARAM_TARGET][self :: PARAM_USER];
- $group_ids = $values[self :: PARAM_TARGET][self :: PARAM_GROUP];
- $right_id = $values[self :: PARAM_RIGHT];
- $copy_right = $values[self :: PARAM_COPY];
-
- $succes = true;
-
- foreach ($this->content_object_ids as $content_object_id)
- {
- $location = ShareManager :: get_current_user_tree_location(Session :: get_user_id(), $content_object_id);
-
- foreach ($user_ids as $user_id)
- {
-
- if ($right_id > - 1)
- {
- $succes &= RepositoryRights :: get_instance()->set_location_entity_right($right_id, $user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
- }
- if ($copy_right)
- {
- $succes &= RepositoryRights :: get_instance()->set_location_entity_right(RepositoryRights :: COPY_RIGHT, $user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
- }
- }
-
- foreach ($group_ids as $group_id)
- {
- if ($right_id > - 1)
- {
- $succes &= RepositoryRights :: get_instance()->set_location_entity_right($right_id, $group_id, PlatformGroupEntity :: ENTITY_TYPE, $location->get_id());
- }
- if ($copy_right)
- {
- $succes &= RepositoryRights :: get_instance()->set_location_entity_right(RepositoryRights :: COPY_RIGHT, $group_id, PlatformGroupEntity :: ENTITY_TYPE, $location->get_id());
- }
- }
- }
- return $succes;
- }
-
- function update_content_object_share($target_user_ids = array(), $target_group_ids = array())
- {
- $rdm = RepositoryDataManager :: get_instance();
- $succes = true;
-
- $values = $this->exportValues();
- $right_id = $values[self :: PARAM_RIGHT];
- $has_copy_right = $values[self :: PARAM_COPY];
-
- foreach ($this->content_object_ids as $content_object_id)
- {
- $location = ShareManager :: get_current_user_tree_location(Session :: get_user_id(), $content_object_id);
-
- foreach ($target_user_ids as $target_user_id)
- {
- $succes = RepositoryRights :: get_instance()->clear_share_entity_rights($location, UserEntity :: ENTITY_TYPE, $target_user_id);
- $succes = RepositoryRights :: get_instance()->invert_repository_location_entity_right($right_id, $target_user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
- if ($has_copy_right)
- {
- $succes = RepositoryRights :: get_instance()->invert_repository_location_entity_right(RepositoryRights :: COPY_RIGHT, $target_user_id, UserEntity :: ENTITY_TYPE, $location->get_id());
- }
- }
-
- foreach ($target_group_ids as $target_group_id)
- {
- $succes = RepositoryRights :: get_instance()->clear_share_entity_rights($location, PlatformGroupEntity :: ENTITY_TYPE, $target_group_id);
- $succes = RepositoryRights :: get_instance()->invert_repository_location_entity_right($right_id, $target_group_id, PlatformGroupEntity :: ENTITY_TYPE, $location->get_id());
- }
- }
-
- return $succes;
- }
-
- /**
- * Sets default values.
- * @param array $defaults Default values for this form's parameters.
- */
- function set_default_rights($target_user_ids = array(), $target_group_ids = array())
- {
- $rdm = RepositoryDataManager :: get_instance();
-
- $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());
-
- if (count($target_user_ids) > 0)
- {
- $granted_rights = RepositoryRights :: get_instance()->get_granted_rights_for_rights_entity_item(RepositoryManager :: APPLICATION_NAME, UserEntity :: ENTITY_TYPE, $target_user_ids[0], $location);
-
- }
- elseif (count($target_group_ids) > 0)
- {
- $granted_rights = RepositoryRights :: get_instance()->get_granted_rights_for_rights_entity_item(RepositoryManager :: APPLICATION_NAME, PlatformGroupEntity :: ENTITY_TYPE, $target_group_ids[0], $location);
- }
-
- $copy_right = array_search(RepositoryRights :: COPY_RIGHT, $granted_rights);
-
- $right = $granted_rights[0];
-
- if ($copy_right)
- {
- array_splice($granted_rights, $copy_right, 1);
- $copy_right = true;
- }
- else
- {
- $copy_right = false;
- }
- $this->setDefaults(array(self :: PARAM_RIGHT => max($granted_rights), self :: PARAM_COPY => $copy_right));
- }
-
- }
-
- ?>