/** * @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; }
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'); }
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); }
/** * @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; }