/** * @ORM\PostPersist */ public function createRoot(LifecycleEventArgs $event) { if ($this->getRoot() == null) { $em = $event->getEntityManager(); $rootSection = $this->getRoot(); if ($rootSection == null) { $rootSection = new Section(); $rootSection->setWiki($this); if ($this->getResourceNode() !== null) { $rootSection->setAuthor($this->getResourceNode()->getCreator()); } else { $rootSection->setAuthor($this->getWikiCreator()); } $this->setRoot($rootSection); $em->getRepository('IcapWikiBundle:Section')->persistAsFirstChild($rootSection); $em->flush(); } } }
/** * Imports wiki object from array * (see WikiImporter for structure and description). * * @param array $data * @param $rootPath * @param $loggedUser * * @return Wiki */ public function importWiki(array $data, $rootPath, $loggedUser) { $wiki = new Wiki(); if (isset($data['data'])) { $wikiData = $data['data']; $wiki->setMode($wikiData['options']['mode']); $sectionsMap = array(); foreach ($wikiData['sections'] as $section) { $entitySection = new Section(); $entitySection->setWiki($wiki); $entitySection->setDeleted($section['deleted']); $entitySection->setDeletionDate($section['deletion_date']); $entitySection->setCreationDate($section['creation_date']); $author = null; if ($section['author'] !== null) { $author = $this->userRepository->findOneByUsername($section['author']); } if ($author === null) { $author = $loggedUser; } $entitySection->setAuthor($author); $parentSection = null; if ($section['parent_id'] !== null) { $parentSection = $sectionsMap[$section['parent_id']]; $entitySection->setParent($parentSection); } if ($section['is_root']) { $wiki->setRoot($entitySection); $this->om->persist($wiki); } foreach ($section['contributions'] as $contribution) { $contributionData = $contribution['contribution']; $entityContribution = new Contribution(); $entityContribution->setSection($entitySection); $entityContribution->setTitle($contributionData['title']); $entityContribution->setCreationDate($contributionData['creation_date']); $contributor = null; if ($contributionData['contributor'] !== null) { $contributor = $this->userRepository->findOneByUsername($contributionData['contributor']); } if ($contributor === null) { $contributor = $loggedUser; } $entityContribution->setContributor($contributor); $text = file_get_contents($rootPath . DIRECTORY_SEPARATOR . $contributionData['path']); $entityContribution->setText($text); if ($contributionData['is_active']) { $entitySection->setActiveContribution($entityContribution); if ($parentSection !== null) { $this->sectionRepository->persistAsLastChildOf($entitySection, $parentSection); } else { $this->sectionRepository->persistAsFirstChild($entitySection); } } $this->om->persist($entityContribution); } $sectionsMap[$section['id']] = $entitySection; } } return $wiki; }
/** * Update (or create) a section when creating a new contribution. * * @param ParamFetcher $paramFetcher * @param Wiki $wiki * @param Section $section * * @return mixed[] * * @Route( * requirements={ "wiki" = "\d+", "section" = "\d+" } * ) * @QueryParam( * name="visible", * requirements="(true|false)", * description="Sets the visibility of the section" * ) * @QueryParam( * name="isBrother", * requirements="(true|false|undefined)", * nullable=true, * description="Should the section be treated as sibling of the reference section ?" * ) * @RequestParam( * name="title", * nullable=true, * description="Title of the new contribution" * ) * @RequestParam( * name="text", * nullable=true, * description="Content of the new contribution" * ) * @RequestParam( * name="contributor", * requirements="\d+", * description="ID of the contributor" * ) * @RequestParam( * name="parentSectionId", * requirements="\d+", * description="ID of the parent section" * ) */ public function postWikiSectionContributionAction(ParamFetcher $paramFetcher, Wiki $wiki, Section $section = null) { $this->checkAccess('OPEN', $wiki); $createSection = empty($section); $em = $this->getDoctrine()->getManager(); $sectionRepository = $this->get('icap.wiki.section_repository'); $contributor = $this->getDoctrine()->getRepository('ClarolineCoreBundle:User')->findOneById($paramFetcher->get('contributor')); $collection = $collection = new ResourceCollection([$wiki->getResourceNode()]); $isAdmin = $this->isUserGranted('EDIT', $wiki, $collection); // Section ID in URL is equal to 0, we need to create a new section if ($createSection) { $section = new Section(); $section->setWiki($wiki); $section->setAuthor($contributor); $section->setIsWikiAdmin($isAdmin); $parentSectionId = $paramFetcher->get('parentSectionId'); $parent = $this->getSection($wiki, $parentSectionId); $sectionRepository->persistAsLastChildOf($section, $parent); } $contribution = new Contribution(); $contribution->setTitle($paramFetcher->get('title')); $contribution->setText($paramFetcher->get('text')); $contribution->setSection($section); $contribution->setContributor($contributor); $contribution->setCreationDate(new \DateTime()); $section->setActiveContribution($contribution); // Adjust section visibility $visible = $paramFetcher->get('visible') === 'true'; $visibility = $visible && $wiki->getMode() === 0 || $visible && $isAdmin; $section->setVisible($visibility); $em->persist($section); $em->flush(); if ($createSection) { $this->dispatchSectionCreateEvent($wiki, $section); } else { $unitOfWork = $em->getUnitOfWork(); $unitOfWork->computeChangeSets(); $changeSet = $unitOfWork->getEntityChangeSet($section); $this->dispatchSectionUpdateEvent($wiki, $section, $changeSet); } return ['section' => ['id' => $section->getId(), 'visible' => $section->getVisible()], 'sections' => $sectionRepository->buildSectionTree($wiki, $isAdmin, $contributor), 'contribution' => ['id' => $contribution->getId(), 'is_active' => true, 'contributor' => ['last_name' => $contribution->getContributor()->getLastName(), 'first_name' => $contribution->getContributor()->getFirstName(), 'user_name' => $contribution->getContributor()->getUserName()], 'creation_date' => $contribution->getCreationDate(), 'text' => $contribution->getText(), 'title' => $contribution->getTitle()]]; }