Example #1
0
 protected function createEntity(array $data)
 {
     $entity = new Page($data);
     $group = GroupSynker::select($this->groups(), $data['group']);
     if (!$group) {
         throw new \Exception('Failed to bind group: no matched group found: ' . $this->groupData['title']);
     }
     $entity->group()->associate($group);
     return $entity;
 }
Example #2
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $authors = Author::all();
     $this->iterate(self::PAGES_TO_SEED / 2, self::PAGES_TO_SEED, function ($i) use($authors) {
         $author = $authors->random();
         do {
             $author = $authors->random();
         } while ($author->groups->isEmpty());
         $faker = $this->faker();
         $page = new Page();
         $page->title = $faker->sentence(5);
         $page->annotation = Str::limit(join(' ', $faker->sentences(5)), 250);
         $page->link = $faker->slug(3);
         $page->size = $faker->numberBetween(0, 9999);
         $page->author()->associate($author);
         $page->group()->associate($author->groups->random());
         $page->save();
     });
 }
Example #3
0
 public function clean()
 {
     $pages = Page::onlyTrashed();
     $pageIds = $pages->get(['id'])->pluck('id', 'id');
     $trough = PageUpdate::withTrashed()->get(['id', 'entity_id'])->all();
     $same = array_filter(array_map(function ($e) use($pageIds) {
         return isset($pageIds[$e->entity_id]) ? $e->id : false;
     }, $trough));
     $updates = Update::withTrashed()->whereIn('id', $same);
     $updateIds = $updates->get(['id'])->pluck('id');
     $updates->forceDelete();
     $pages->forceDelete();
     $this->stats = ['pages' => $pageIds->toArray(), 'updates' => $updateIds->toArray()];
     return $this->stats;
 }
Example #4
0
 function synkGroups(Author $author, array $groupsData)
 {
     $stats = with($gs = new GroupSynker($author))->synk($groupsData);
     $groups = $gs->current();
     $pages = [];
     foreach ($groups as $group) {
         $data = GroupSynker::pick($group, $groupsData);
         if (!$group->inline) {
             $pagesData = $data['pages'];
         } else {
             $pagesData = $this->check($group);
         }
         if (!is_array($pagesData)) {
             debug($pagesData, $group->inline);
             continue;
         }
         $data = array_map(function (&$value) use(&$data) {
             $value['group'] = $data;
             return $value;
         }, $pagesData);
         $pages = array_merge($pages, $data);
     }
     if ($pages) {
         if ($pages = with(new PageSynker($group->author))->synk($pages)) {
             $grouped = [];
             $pagesData = [];
             foreach ($pages as $type => $pages) {
                 foreach ($pages as $page) {
                     $pagesData[$page['id']][$type][] = $page;
                 }
             }
             $grouped = [];
             $links = \Ankh\Page::whereIn('id', array_keys($pagesData))->withTrashed()->get(['id', 'group_id']);
             foreach ($links as $page) {
                 $grouped[$page->id] = $page->group_id;
             }
             foreach ($pagesData as $id => $data) {
                 $gId = $grouped[$id];
                 $found = false;
                 foreach ($stats as $type => $groups) {
                     foreach ($groups as $idx => $group) {
                         if ($found = $group['id'] == $gId) {
                             $stats[$type][$idx]['pages'] = array_merge_recursive(@$group['pages'] ?: [], $pagesData[$id]);
                             break;
                         }
                     }
                 }
                 if (!$found) {
                     $stats[Synker::UPDATED][] = ['id' => $gId, 'pages' => $pagesData[$id]];
                 }
             }
         }
     }
     return $stats;
 }