Example #1
0
 /**
  * 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;
 }