Beispiel #1
0
 protected function migrate_events()
 {
     $website = Website::first();
     $data_users = User::get();
     foreach ($data_users as $user) {
         $users[$user['username']] = $user;
     }
     // get old articles
     DB::connection('mongodb')->collection('posts')->where('category', 'regex', '/events/i')->chunk(1000, function ($data) use($new_collection, $users, $website) {
         foreach ($data as $x) {
             unset($tags);
             $tags[] = 'events';
             if (str_is('*car free day*', strtolower($x['title']))) {
                 $tags[] = 'cfd';
             }
             if (str_is('*seminar*', strtolower($x['title']))) {
                 $tags[] = 'seminar';
             }
             if (str_is('*opening*', strtolower($x['title']))) {
                 $tags[] = 'opening';
             }
             if (str_is('*ustad*', strtolower($x['title']))) {
                 $tags[] = 'religius';
             }
             if (str_is('*clothing*', strtolower($x['title']))) {
                 $tags[] = 'pameran';
             }
             if (str_is('*konser*', strtolower($x['title'])) || str_is('*concert*', strtolower($x['title'])) || str_is('*cherry bell*', strtolower($x['title']))) {
                 $tags[] = 'konser';
             }
             if (str_is('*brawijaya*', strtolower($x['title']))) {
                 $tags[] = 'brawijaya';
             }
             if (str_is('*stand up comedy*', strtolower($x['title']))) {
                 $tags[] = 'stand up comedy';
             }
             if (str_is('*job fair*', strtolower($x['title'])) || str_is('*career expo*', strtolower($x['title']))) {
                 $tags[] = 'job fair';
             }
             if (str_is('*film*', strtolower($x['title']))) {
                 $tags[] = 'film';
             }
             if (str_is('*film*', strtolower($x['title']))) {
                 $tags[] = 'film';
             }
             if (str_is('*lomba*', strtolower($x['title'])) || str_is('*kompetisi*', strtolower($x['title']))) {
                 $tags[] = 'kompetisi';
             }
             if ($x['tgl_published']) {
                 $news = new Event(['title' => $x['title'], 'slug' => str_replace('.', '', $x['url']), 'summary' => str_limit(strip_tags(str_replace("\n", "", $x['full'])), 125), 'content' => $x['full'], 'published_at' => date('Y-m-d H:i:s', $x['tgl_published']->sec), 'created_at' => $x['tgl'], 'updated_at' => $x['tgl'], 'user_id' => array_key_exists($x['author'], $users) ? $users[$x['author']]->id : $users['dita']->id, 'started_at' => $x['extra_field']['event_tgl_start']->sec, 'ended_at' => isset($x['extra_field']['event_tgl_end']) ? $x['extra_field']['event_tgl_end']->sec : $x['extra_field']['event_tgl_start']->sec, 'location' => isset($x['extra_field']['event_lokasi']) ? $x['extra_field']['event_lokasi'] : '', 'komunitas_id' => Directory::where('ori_id', '=', $x['extra_field']['event_komunitas'])->first()->id, 'views' => isset($x['views']) ? $x['views'] : 0]);
                 if (!$news->save()) {
                     print_r($news->toArray());
                     dd($news->getErrors());
                 }
                 // ----------------------------------------------------------------------------------------------------
                 // IMAGE
                 // ----------------------------------------------------------------------------------------------------
                 $s_image = new Image(['name' => 'sm', 'path' => $x['thumbmail'], 'title' => '', 'description' => '']);
                 $m_image = new Image(['name' => 'md', 'path' => $x['image'], 'title' => '', 'description' => '']);
                 $l_image = new Image(['name' => 'lg', 'path' => $x['image'], 'title' => '', 'description' => '']);
                 $news->images()->updateOrCreate(['name' => 'sm'], $s_image->toArray());
                 $news->images()->updateOrCreate(['name' => 'md'], $m_image->toArray());
                 $news->images()->updateOrCreate(['name' => 'lg'], $l_image->toArray());
                 // ----------------------------------------------------------------------------------------------------
                 // TAG
                 // ----------------------------------------------------------------------------------------------------
                 unset($tags_model);
                 $tags_model = new Collection();
                 foreach ($tags as $tag) {
                     $tags_model[] = Tag::firstOrCreate(['name' => $tag]);
                 }
                 foreach ($tags_model as $k => $v) {
                     if (!$tags_model[$k]->save()) {
                         dd($tags_model[$k]->getErrors());
                     }
                 }
                 if (count($tags_model)) {
                     $news->tags()->sync($tags_model->lists('id'));
                 }
                 // ----------------------------------------------------------------------------------------------------
                 // WEBSITE
                 // ----------------------------------------------------------------------------------------------------
                 $news->websites()->sync([$website->id]);
                 // ----------------------------------------------------------------------------------------------------
                 // LOG
                 // ----------------------------------------------------------------------------------------------------
                 // $news->authored()->attach(array_key_exists($x['author'], $users) ? $users[$x['author']]->id : $users['dita']->id, ['original_data' => json_encode([]), 'updated_data' => $news->toJson()]);
             }
             // $news->websites()->associate($website);
             // $news->save();
         }
     });
     // save to new tables
 }