/** * 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()]]; }