/** * Imports lesson object from array * (see LessonImporter for structure and description). * * @param array $data * @param $rootPath * * @return Lesson */ public function importLesson(array $data, $rootPath) { $lesson = new Lesson(); if (isset($data['data'])) { $lessonData = $data['data']; $chaptersMap = []; foreach ($lessonData['chapters'] as $chapter) { $entityChapter = new Chapter(); $entityChapter->setLesson($lesson); $entityChapter->setTitle($chapter['title']); $text = file_get_contents($rootPath . DIRECTORY_SEPARATOR . $chapter['path']); $entityChapter->setText($text); if ($chapter['is_root']) { $lesson->setRoot($entityChapter); } $parentChapter = null; if ($chapter['parent_id'] !== null) { $parentChapter = $chaptersMap[$chapter['parent_id']]; $entityChapter->setParent($parentChapter); $this->chapterRepository->persistAsLastChildOf($entityChapter, $parentChapter); } else { $this->chapterRepository->persistAsFirstChild($entityChapter); } $chaptersMap[$chapter['id']] = $entityChapter; } } return $lesson; }
public function prePersist(Chapter $chapter, LifecycleEventArgs $event) { if ($chapter->getText() != null) { $userPicker = new UserPickerContent($chapter->getText()); $chapter->setUserPicker($userPicker); $chapter->setText($userPicker->getFinalText()); } }
public function chapter($title, $text, $lesson, $root) { $chapter = new Chapter(); $chapter->setTitle($title); $chapter->setText($text); $chapter->setLesson($lesson); $this->om->persist($chapter); $this->om->flush(); return $chapter; }
/** * Copy chapter_org subchapters into provided chapter_copy * @param Chapter $chapter_org * @param Chapter $parent * @param boolean $copy_children * @param Lesson $copyName * * @return Chapter $chapter_copy */ public function copyChapter(Chapter $chapter_org, Chapter $parent, $copy_children, $copyName = null) { $chapter_copy = new Chapter(); if (!$copyName) { $copyName = $chapter_org->getTitle(); } $chapter_copy->setTitle($copyName); $chapter_copy->setText($chapter_org->getText()); $chapter_copy->setLesson($parent->getLesson()); $this->insertChapter($chapter_copy, $parent); if ($copy_children) { $this->copyChildren($chapter_org, $chapter_copy, $copy_children); } return $chapter_copy; }
/** * @Put("/chapters/{lesson}/{chapter}", * requirements={"lesson" = "\d+"}) * @ParamConverter("lesson", class="IcapLessonBundle:Lesson") * @ParamConverter("chapter", class="IcapLessonBundle:Chapter", options={"mapping": {"chapter": "slug"}}) */ public function editChapterAction(Lesson $lesson, Chapter $chapter) { // CHECK ACCESS $this->apiCheckAccess('EDIT', $lesson); $translator = $this->get('translator'); $chapterType = new ChapterType($this->localeManager); $defaults = array('csrf_protection' => false, 'allow_extra_fields' => true); $form = $this->formFactory->create($chapterType, new Chapter(), $defaults); $payload = $this->request->request->get('chapter'); $form->submit($payload); if ($form->isValid()) { $chapter->setTitle($payload['title']); // Chapter text is not mandatory if (array_key_exists('text', $payload)) { $chapter->setText($payload['text']); } // Set slug to null in order to re-generate it $chapter->setSlug(null); $em = $this->getDoctrine()->getManager(); $unitOfWork = $em->getUnitOfWork(); $unitOfWork->computeChangeSets(); $changeSet = $unitOfWork->getEntityChangeSet($chapter); $em->flush(); $this->dispatchChapterUpdateEvent($lesson, $chapter, $changeSet); return array('message' => $translator->trans('Your chapter has been modified', array(), 'icap_lesson'), 'chapter' => $this->formatChapterData($chapter)); } // Form is not valid. What should we return? return array('message' => $translator->trans('Your chapter has not been modified', array(), 'icap_lesson'), 'errors' => $form); }