/**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $user = $this->getReference("user/{$this->creator}");
     $resourceManager = $this->container->get('claroline.manager.resource_manager');
     $dirType = $manager->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('directory');
     foreach ($this->paths as $path) {
         $directories = explode('/', $path);
         for ($i = 0, $dirCount = count($directories); $i < $dirCount; ++$i) {
             if ($i > 0) {
                 if (!$this->hasReference("directory/{$directories[$i]}")) {
                     $directory = new Directory();
                     $directory->setName($directories[$i]);
                     $parent = $this->getReference("directory/{$directories[$i - 1]}");
                     $resourceManager->create($directory, $dirType, $user, $parent->getWorkspace(), $parent);
                     $this->addReference("directory/{$directories[$i]}", $directory->getResourceNode());
                 }
             }
         }
     }
 }
 private function getDropHiddenDirectory(Dropzone $dropzone, Drop $drop)
 {
     $em = $this->getDoctrine()->getManager();
     $hiddenDropDirectory = $drop->getHiddenDirectory();
     if ($hiddenDropDirectory === null) {
         $hiddenDropDirectory = new Directory();
         // slugify user name
         $slugify = new Slugify();
         $user = $drop->getUser();
         $str = $user->getFirstName() . "-" . $user->getLastName();
         $str = $slugify->slugify($str, ' ');
         $name = $this->get('translator')->trans('Copy n°%number%', array('%number%' => $drop->getNumber()), 'innova_collecticiel');
         $name .= " - " . $str;
         $hiddenDropDirectory->setName($name);
         $parent = $this->getDropZoneHiddenDirectory($dropzone);
         $role = $this->getDoctrine()->getRepository('ClarolineCoreBundle:Role')->findManagerRole($dropzone->getResourceNode()->getWorkspace());
         $resourceManager = $this->get('claroline.manager.resource_manager');
         $resourceManager->create($hiddenDropDirectory, $resourceManager->getResourceTypeByName('directory'), $parent->getCreator(), $parent->getWorkspace(), $parent, null, array('ROLE_WS_MANAGER' => array('open' => true, 'export' => true, 'create' => array(), 'role' => $role)));
         $drop->setHiddenDirectory($hiddenDropDirectory->getResourceNode());
         $em->persist($drop);
         $em->flush();
     }
     return $drop->getHiddenDirectory();
 }
 /**
  * @param array $resourcesModels
  * @param \Claroline\CoreBundle\Entity\Resource\Directory $rootDirectory
  * @param \Claroline\CoreBundle\Entity\Workspace\Workspace $workspace
  * @param \Claroline\CoreBundle\Entity\User $user
  */
 private function duplicateResources(array $resourcesModels, Directory $rootDirectory, Workspace $workspace, User $user, &$resourcesInfos)
 {
     $this->om->startFlushSuite();
     $copies = array();
     $resourcesErrors = array();
     $workspaceRoles = $this->getArrayRolesByWorkspace($workspace);
     foreach ($resourcesModels as $resourceModel) {
         $resourceNode = $resourceModel->getResourceNode();
         if ($resourceModel->isCopy()) {
             try {
                 $copy = $this->resourceManager->copy($resourceNode, $rootDirectory->getResourceNode(), $user, false, false);
                 $resourcesInfos['copies'][] = array('original' => $resourceNode, 'copy' => $copy);
             } catch (NotPopulatedEventException $e) {
                 $resourcesErrors[] = array('resourceName' => $resourceNode->getName(), 'resourceType' => $resourceNode->getResourceType()->getName(), 'type' => 'copy', 'error' => $e->getMessage());
                 continue;
             }
             /*** Copies rights ***/
             $this->duplicateRights($resourceNode, $copy->getResourceNode(), $workspaceRoles);
             /*** Copies content of a directory ***/
             if ($resourceNode->getResourceType()->getName() === 'directory') {
                 $errors = $this->duplicateDirectoryContent($resourceNode, $copy->getResourceNode(), $user, $workspaceRoles);
                 $resourcesErrors = array_merge_recursive($resourcesErrors, $errors);
             }
         } else {
             $shortcut = $this->resourceManager->makeShortcut($resourceNode, $rootDirectory->getResourceNode(), $user, new ResourceShortcut());
             $copies[] = $shortcut;
         }
     }
     /*** Sets previous and next for each copied resource ***/
     $this->linkResourcesArray($copies);
     $this->om->endFlushSuite();
     return $resourcesErrors;
 }
Beispiel #4
0
 /**
  * Create a directory in the personal workspace of user to import documents
  *
  * @access private
  *
  * @param Claroline\CoreBundle\Entity\Workspace
  *
  */
 private function createDirQTIImport($ws)
 {
     $manager = $this->container->get('claroline.manager.resource_manager');
     $parent = $this->om->getRepository('ClarolineCoreBundle:Resource\\ResourceNode')->findWorkspaceRoot($ws);
     $dir = new Directory();
     $dir->setName('QTI_SYS');
     $abstractResource = $manager->create($dir, $manager->getResourceTypeByName('directory'), $this->user, $ws, $parent);
     $this->dirQTI = $abstractResource->getResourceNode();
 }
Beispiel #5
0
 /**
  * Adds the public file directory in a workspace
  *
  * @param Workspace $workspace
  *
  * @return Directory
  */
 public function addPublicFileDirectory(Workspace $workspace)
 {
     $directory = new Directory();
     $dirName = $this->translator->trans('my_public_documents', array(), 'platform');
     $directory->setName($dirName);
     $directory->setIsUploadDestination(true);
     $parent = $this->getNodeScheduledForInsert($workspace, $workspace->getName());
     if (!$parent) {
         $parent = $this->resourceNodeRepo->findOneBy(array('workspace' => $workspace->getId(), 'parent' => $parent));
     }
     $role = $this->roleManager->getRoleByName('ROLE_ANONYMOUS');
     return $this->create($directory, $this->getResourceTypeByName('directory'), $workspace->getCreator(), $workspace, $parent, null, array('ROLE_ANONYMOUS' => array('open' => true, 'export' => true, 'create' => array(), 'role' => $role)), true);
 }
Beispiel #6
0
 /**
  * @param Configuration $configuration
  * @param User $owner
  * @param bool $isValidated
  *
  * @throws InvalidConfigurationException
  * @return SimpleWorkbolspace
  *
  * The template doesn't need to be validated anymore if
  *  - it comes from the self::import() function
  *  - we want to create a user from the default template (it should work no matter what)
  */
 public function createWorkspace(Configuration $configuration, User $owner, $isValidated = false)
 {
     $configuration->setOwner($owner);
     $data = $configuration->getData();
     $this->data = $data;
     $this->om->startFlushSuite();
     $this->setImporters($configuration, $data);
     if (!$isValidated) {
         $this->validate($data, false);
         $isValidated = true;
     }
     $workspace = new Workspace();
     $workspace->setName($configuration->getWorkspaceName());
     $workspace->setCode($configuration->getWorkspaceCode());
     $workspace->setDescription($configuration->getWorkspaceDescription());
     $workspace->setGuid($this->container->get('claroline.utilities.misc')->generateGuid());
     $workspace->setDisplayable($configuration->isDisplayable());
     $workspace->setSelfRegistration($configuration->getSelfRegistration());
     $workspace->setRegistrationValidation($configuration->getRegistrationValidation());
     $workspace->setSelfUnregistration($configuration->getSelfUnregistration());
     $date = new \Datetime(date('d-m-Y H:i'));
     $workspace->setCreationDate($date->getTimestamp());
     $this->om->persist($workspace);
     $this->om->flush();
     $this->log('Base workspace created...');
     //load roles
     $this->log('Importing roles...');
     $entityRoles = $this->getImporterByName('roles')->import($data['roles'], $workspace);
     //The manager role is required for every workspace
     $entityRoles['ROLE_WS_MANAGER'] = $this->container->get('claroline.manager.role_manager')->createWorkspaceRole("ROLE_WS_MANAGER_{$workspace->getGuid()}", 'manager', $workspace, true);
     $defaultZip = $this->container->getParameter('claroline.param.templates_directory') . 'default.zip';
     //batch import with default template shouldn't be flushed
     if ($configuration->getArchive() !== $defaultZip) {
         $this->om->forceFlush();
     }
     $this->log('Roles imported...');
     $owner->addRole($entityRoles['ROLE_WS_MANAGER']);
     $this->om->persist($owner);
     //add base roles to the role array
     $pfRoles = $this->om->getRepository('ClarolineCoreBundle:Role')->findAllPlatformRoles();
     foreach ($pfRoles as $pfRole) {
         $entityRoles[$pfRole->getName()] = $pfRole;
     }
     $entityRoles['ROLE_ANONYMOUS'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_ANONYMOUS');
     $entityRoles['ROLE_USER'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_USER');
     $dir = new Directory();
     $dir->setName($workspace->getName());
     $dir->setIsUploadDestination(true);
     $root = $this->container->get('claroline.manager.resource_manager')->create($dir, $this->om->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('directory'), $owner, $workspace, null, null, array());
     $this->log('Populating the workspace...');
     $this->populateWorkspace($workspace, $configuration, $root, $entityRoles, true, false);
     $this->container->get('claroline.manager.workspace_manager')->createWorkspace($workspace);
     if ($owner) {
         $this->log('Set the owner...');
         $workspace->setCreator($owner);
     }
     $this->om->endFlushSuite();
     $fs = new FileSystem();
     return $workspace;
 }
 /**
  * @param File $template
  * @param User $owner
  * @param bool $isValidated
  *
  * @throws InvalidConfigurationException
  *
  * @return SimpleWorkbolspace
  *
  * The template doesn't need to be validated anymore if
  *  - it comes from the self::import() function
  *  - we want to create a user from the default template (it should work no matter what)
  */
 public function createWorkspace(Workspace $workspace, File $template, $isValidated = false)
 {
     $data = $this->container->get('claroline.manager.workspace_manager')->getTemplateData($template, true);
     if ($workspace->getCode() === null) {
         $workspace->setCode($data['parameters']['code']);
     }
     if ($workspace->getName() === null) {
         $workspace->setName($data['parameters']['name']);
     }
     //just to be sure doctrine is ok before doing all the workspace
     $this->om->startFlushSuite();
     $this->setImporters($template, $workspace->getCreator());
     if (!$isValidated) {
         $this->validate($data, false);
         $isValidated = true;
     }
     $workspace->setGuid($this->container->get('claroline.utilities.misc')->generateGuid());
     $date = new \Datetime(date('d-m-Y H:i'));
     $workspace->setCreationDate($date->getTimestamp());
     $this->om->persist($workspace);
     $this->om->flush();
     $this->log("Base {$workspace->getCode()} workspace created...");
     //load roles
     $this->log('Importing roles...');
     $entityRoles = $this->getImporterByName('roles')->import($data['roles'], $workspace);
     //The manager role is required for every workspace
     $entityRoles['ROLE_WS_MANAGER'] = $this->container->get('claroline.manager.role_manager')->createWorkspaceRole("ROLE_WS_MANAGER_{$workspace->getGuid()}", 'manager', $workspace, true);
     //batch import with default template shouldn't be flushed
     if (strpos($template->getPathname(), 'default.zip') === false && strpos($template->getPathname(), 'personal.zip') === false) {
         $this->om->forceFlush();
     }
     $owner = $workspace->getCreator();
     $this->log('Roles imported...');
     $owner->addRole($entityRoles['ROLE_WS_MANAGER']);
     $this->om->persist($owner);
     //add base roles to the role array
     $pfRoles = $this->om->getRepository('ClarolineCoreBundle:Role')->findAllPlatformRoles();
     foreach ($pfRoles as $pfRole) {
         $entityRoles[$pfRole->getName()] = $pfRole;
     }
     $entityRoles['ROLE_ANONYMOUS'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_ANONYMOUS');
     $entityRoles['ROLE_USER'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_USER');
     $dir = new Directory();
     $dir->setName($workspace->getName());
     $dir->setIsUploadDestination(true);
     $root = $this->container->get('claroline.manager.resource_manager')->create($dir, $this->om->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('directory'), $owner, $workspace, null, null, []);
     $this->log('Populating the workspace...');
     $this->populateWorkspace($workspace, $template, $root, $entityRoles, true, false);
     $this->container->get('claroline.manager.workspace_manager')->createWorkspace($workspace);
     $this->om->endFlushSuite();
     return $workspace;
 }
 public function importDirectoriesFromCsv($file)
 {
     $data = file_get_contents($file);
     $data = $this->container->get('claroline.utilities.misc')->formatCsvOutput($data);
     $lines = str_getcsv($data, PHP_EOL);
     $this->om->startFlushSuite();
     $i = 0;
     $resourceType = $this->om->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('directory');
     foreach ($lines as $line) {
         $values = str_getcsv($line, ';');
         $code = $values[0];
         $workspace = $this->om->getRepository('ClarolineCoreBundle:Workspace\\Workspace')->findOneByCode($code);
         $name = $values[1];
         $directory = $this->om->getRepository('ClarolineCoreBundle:Resource\\ResourceNode')->findOneBy(['workspace' => $workspace, 'name' => $name, 'resourceType' => $resourceType]);
         if (!$directory) {
             $directory = new Directory();
             $directory->setName($name);
             $this->log("Create directory {$name} for workspace {$code}");
             $this->create($directory, $resourceType, $workspace->getCreator(), $workspace, $this->getWorkspaceRoot($workspace));
             ++$i;
         } else {
             $this->log("Directory {$name} already exists for workspace {$code}");
         }
         if ($i % 100 === 0) {
             $this->om->forceFlush();
             $this->om->clear();
             $resourceType = $this->om->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('directory');
             $this->om->merge($resourceType);
         }
     }
     $this->om->endFlushSuite();
 }
Beispiel #9
0
 private function uploadDir($dir, ResourceNode $parent, array $perms, $first = false, $published = true)
 {
     $resources = [];
     $iterator = new \DirectoryIterator($dir);
     foreach ($iterator as $item) {
         if ($item->isFile()) {
             $resources[] = $this->uploadFile($item, $parent, $perms, $published);
         }
         if ($item->isDir() && !$item->isDot()) {
             //create new dir
             $directory = new Directory();
             $directory->setName($item->getBasename());
             $resources[] = $this->resourceManager->create($directory, $this->resourceManager->getResourceTypeByName('directory'), $this->tokenStorage->getToken()->getUser(), $parent->getWorkspace(), $parent, null, $perms, $published);
             $this->uploadDir($dir . DIRECTORY_SEPARATOR . $item->getBasename(), $directory->getResourceNode(), $perms, false, $published);
         }
         //            $this->om->forceFlush();
     }
     // set order manually as we are inside a flush suite
     for ($i = 0, $count = count($resources); $i < $count; ++$i) {
         $resources[$i]->getResourceNode()->setIndex($i + 1);
     }
     return $resources;
 }
 protected static function createShortcut($name, ResourceType $type, AbstractResource $target, User $creator, Directory $parent)
 {
     $shortcut = self::prepareResource(new ResourceShortcut(), $type, $creator, $parent->getResourceNode()->getWorkspace(), $name, 'shortcut/mime', $parent->getResourceNode());
     $shortcut->setTarget($target->getResourceNode());
     self::create($name, $shortcut);
 }
 public function import(array $data, $workspace, $entityRoles, Directory $root, $fullImport = true)
 {
     $this->log('Importing resources...');
     /*
      * Each directory is created without parent.
      * The parent is set after the ResourceManager::create method is fired.
      * When there is no parent and no right array, the resource creation will copy
      * the parent rights (ROLE_USER and ROLE_ANONYMOUS) and we only need to add the roles from the $data
      * instead of the full array with default perms.
      * The implementation will change later (if we need to change the perms of
      * ROLE_USER and ROLE_ANONYMOUS) but it's easier to code it that way.
      */
     $createdResources = array();
     if ($fullImport) {
         $directories[$data['data']['root']['uid']] = $root;
     } else {
         $directories[$root->getResourceNode()->getId()] = $root;
     }
     $resourceNodes = [];
     /*************************/
     /* WORKSPACE DIRECTORIES */
     /*************************/
     if (isset($data['data']['directories'])) {
         $this->log('Importing directories...');
         //build the nodes
         foreach ($data['data']['directories'] as $directory) {
             $directoryEntity = new Directory();
             $directoryEntity->setName($directory['directory']['name']);
             if ($directory['directory']['creator']) {
                 $owner = $this->om->getRepository('ClarolineCoreBundle:User')->findOneByUsername($directory['directory']['creator']);
             } else {
                 $owner = $this->getOwner();
             }
             $isDirectoryPublished = isset($directory['directory']['published']) || false === $directory['directory']['published'] ? $directory['directory']['published'] : true;
             $icon = null;
             //add the custom icons
             if (isset($directory['directory']['icon']) && $directory['directory']['icon']) {
                 $icon = $this->generateIcon($this->getRootPath() . '/' . $directory['directory']['icon']);
             }
             $directories[$directory['directory']['uid']] = $this->resourceManager->create($directoryEntity, $this->om->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('directory'), $owner, $workspace, null, $icon, array(), $isDirectoryPublished);
             //let's order everything !
             if (isset($directory['directory']['index'])) {
                 $node = $directoryEntity->getResourceNode();
                 $node->setIndex($directory['directory']['index']);
                 $this->om->persist($node);
                 $this->om->flush();
             }
             if ($fullImport) {
                 //add the missing roles
                 foreach ($directory['directory']['roles'] as $role) {
                     $this->setPermissions($role, $entityRoles[$role['role']['name']], $directoryEntity);
                 }
             }
         }
         //set the correct parent
         foreach ($data['data']['directories'] as $directory) {
             $node = $directories[$directory['directory']['uid']]->getResourceNode();
             $directory['directory']['parent'] && isset($directories[$directory['directory']['parent']]) ? $node->setParent($directories[$directory['directory']['parent']]->getResourceNode()) : $node->setParent($root->getResourceNode());
         }
         $this->log('Directories imported...');
     }
     /*************/
     /* RESOURCES */
     /*************/
     $created = array();
     if (isset($data['data']['items'])) {
         $this->log('Importing resources...');
         foreach ($data['data']['items'] as $item) {
             //THIS IS WHERE RES COME FROM !
             $res = array();
             if (isset($item['item']['data'])) {
                 $res['data'] = $item['item']['data'];
             }
             //get the entity from an importer
             $importer = $this->getImporterByName($item['item']['type']);
             if ($importer) {
                 $this->log("Importing {$item['item']['name']} - uid={$item['item']['uid']} - type={$item['item']['type']}");
                 $entity = $importer->import($res, $item['item']['name'], $created, $workspace);
                 //some importers are not fully functionnal yet
                 if ($entity) {
                     $created[$item['item']['uid']] = $entity;
                     $entity->setName($item['item']['name']);
                     $type = $this->om->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName($item['item']['type']);
                     if ($item['item']['creator']) {
                         $owner = $this->om->getRepository('ClarolineCoreBundle:User')->findOneByUsername($item['item']['creator']);
                     } else {
                         $owner = $this->getOwner();
                     }
                     $isPublished = isset($item['item']['published']) || false === $item['item']['published'] ? $item['item']['published'] : true;
                     //add the custom icons
                     $icon = null;
                     if (isset($item['item']['icon']) && $item['item']['icon']) {
                         $icon = $this->generateIcon($this->getRootPath() . '/' . $item['item']['icon']);
                     }
                     $entity = $this->resourceManager->create($entity, $type, $owner, $workspace, null, $icon, array(), $isPublished);
                     if ($item['item']['parent'] && isset($directories[$item['item']['parent']])) {
                         $entity->getResourceNode()->setParent($directories[$item['item']['parent']]->getResourceNode());
                     } else {
                         $entity->getResourceNode()->setParent($root->getResourceNode());
                     }
                     //let's order everything !
                     if (isset($item['item']['index'])) {
                         $node = $entity->getResourceNode();
                         $node->setIndex($directory['directory']['index']);
                         $this->om->persist($node);
                         $this->om->flush();
                     }
                     $this->om->persist($entity);
                     if ($fullImport) {
                         //add the missing roles
                         if (isset($item['item']['roles'])) {
                             foreach ($item['item']['roles'] as $role) {
                                 $this->setPermissions($role, $entityRoles[$role['role']['name']], $entity);
                             }
                         }
                     }
                     $resourceNodes[$item['item']['uid']] = $entity;
                 }
             }
         }
         $this->log('Resources import done !');
     }
     /***************/
     /* ROOT RIGHTS */
     /***************/
     if ($fullImport) {
         //add the missing roles
         foreach ($data['data']['root']['roles'] as $role) {
             $this->setPermissions($role, $entityRoles[$role['role']['name']], $root);
         }
     }
     //throw new \Exception('boom');
 }
 /**
  * @param array                                            $resourcesModels
  * @param \Claroline\CoreBundle\Entity\Resource\Directory  $rootDirectory
  * @param \Claroline\CoreBundle\Entity\Workspace\Workspace $workspace
  * @param \Claroline\CoreBundle\Entity\User                $user
  */
 private function duplicateResources(array $resourcesModels, Directory $rootDirectory, Workspace $workspace, User $user, &$resourcesInfos)
 {
     $this->log('Duplicating ' . count($resourcesModels) . ' resources...');
     $this->om->startFlushSuite();
     $copies = [];
     $resourcesErrors = [];
     $workspaceRoles = $this->getArrayRolesByWorkspace($workspace);
     foreach ($resourcesModels as $key => $resourceModel) {
         $resourceNode = $resourceModel->getResourceNode();
         if ($resourceModel->isCopy()) {
             try {
                 $this->log('Duplicating ' . $resourceNode->getName() . ' from type ' . $resourceNode->getResourceType()->getName());
                 $copy = $this->resourceManager->copy($resourceNode, $rootDirectory->getResourceNode(), $user, false, false);
                 $copy->getResourceNode()->setIndex($resourceNode->getIndex());
                 $this->om->persist($copy->getResourceNode());
                 $resourcesInfos['copies'][] = ['original' => $resourceNode, 'copy' => $copy->getResourceNode()];
             } catch (NotPopulatedEventException $e) {
                 $resourcesErrors[] = ['resourceName' => $resourceNode->getName(), 'resourceType' => $resourceNode->getResourceType()->getName(), 'type' => 'copy', 'error' => $e->getMessage()];
                 continue;
             }
             /*** Copies rights ***/
             $this->duplicateRights($resourceNode, $copy->getResourceNode(), $workspaceRoles);
         } else {
             $shortcut = $this->resourceManager->makeShortcut($resourceNode, $rootDirectory->getResourceNode(), $user, new ResourceShortcut());
             $copies[] = $shortcut;
         }
         $position = $key + 1;
         $this->log('Resource [' . $resourceModel->getResourceNode()->getName() . '] ' . $position . '/' . count($resourcesModels) . ' copied');
     }
     /*** Sets previous and next for each copied resource ***/
     $this->linkResourcesArray($copies);
     $this->om->endFlushSuite();
     return $resourcesErrors;
 }
Beispiel #13
0
 private function addDirectory(ResourceNode $parent, User $user)
 {
     $dirType = $this->container->get('claroline.persistence.object_manager')->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('directory');
     $this->suffixName++;
     $name = 'dir_' . $this->suffixName;
     $dir = new Directory();
     $dir->setName($name);
     $this->log('create ' . $name);
     $dir = $this->container->get('claroline.manager.resource_manager')->create($dir, $dirType, $user, $this->pws, $parent);
     $this->totalResources++;
     return $dir->getResourceNode();
 }