/** * 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 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; }
/** * @ORM\PostPersist */ public function createRoot(LifecycleEventArgs $event) { if ($this->getRoot() == null) { $em = $event->getEntityManager(); $rootLesson = $this->getRoot(); if ($rootLesson == null) { $rootLesson = new Chapter(); $rootLesson->setLesson($this); $rootLesson->setTitle("root_" . $this->getId()); $this->setRoot($rootLesson); $em->getRepository('IcapLessonBundle:Chapter')->persistAsFirstChild($rootLesson); $em->flush(); } } }