119 lines
3.9 KiB
PHP
119 lines
3.9 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use App\Models\Vehicle;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Logic\Document\DocumentRepository;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\File;
|
|
use App\Models\User;
|
|
use App\Http\Controllers\Auth;
|
|
use App\Models\Post;
|
|
use App\Models\TagMigration;
|
|
use App\Models\PostMigration;
|
|
use App\Helpers\MigrationHelper;
|
|
use \Carbon\Carbon;
|
|
use App\Http\Controllers\ExtendedController;
|
|
use App\Http\Controllers\ServiceController;
|
|
|
|
class ServiceSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run($mode = 'update')
|
|
{
|
|
if($mode == 'replace')
|
|
{
|
|
DB::connection('mysql')->table('posts')->where('type', 'dienst')->delete();
|
|
}
|
|
$services = DB::connection('mysql_alt')->select("SELECT * FROM ikarus_dienst ORDER BY 'id'");
|
|
|
|
foreach($services as $service)
|
|
{
|
|
// Fallunterscheidung:
|
|
// Das Element wird nuer importiert, wenn
|
|
// A) $mode == 'replace'
|
|
// oder
|
|
// B) $mode != 'replace' und die ID aus der Altdatenbank ist noch nicht importiert worden
|
|
$checkMigration = PostMigration::where('type', 'dienst')->where('old_id', $service->id)->get();
|
|
|
|
if(($mode == 'replace') || ($mode == 'update' && $checkMigration->count() == 0))
|
|
{
|
|
$new_user = User::find($service->benutzer_id);
|
|
if(!$new_user)
|
|
{
|
|
$service->benutzer_id = 1;
|
|
$new_user = User::find($service->benutzer_id);
|
|
}
|
|
\Auth::login($new_user);
|
|
|
|
echo "INSERT service: $service->id\n";
|
|
$post = new Post;
|
|
$post->type = 'dienst';
|
|
$post->title = '';
|
|
$post->datetime = $service->datum . " " . $service->uhrzeit;
|
|
// $post->datetime = Carbon::parse($post->datetime)->addYears(2);
|
|
$post->content = json_encode([
|
|
'beschreibung' => MigrationHelper::normalize($service->beschreibung),
|
|
'ort' => $service->ort,
|
|
]);
|
|
$post->user_id = $service->benutzer_id;
|
|
$post->created_at = $service->created_at;
|
|
$post->updated_at = ($service->updated_at == $service->created_at) ? Carbon::createFromFormat(config('formats.datetime.database'), $service->updated_at)->addMinute() : $service->updated_at;
|
|
$post->published = true;
|
|
$post->save();
|
|
|
|
$controller = new ServiceController();
|
|
$publishDate = $controller->calculatePublicationDate($post, true);
|
|
$publication = $post->publication('facebook');
|
|
$publication->setPublishDate($publishDate);
|
|
$publication->noteForPublication(($publishDate > Carbon::now() ? true : false));
|
|
$publication->save();
|
|
$publication = $post->publication('twitter');
|
|
$publication->setPublishDate($publishDate);
|
|
$publication->noteForPublication(($publishDate > Carbon::now() ? true : false));
|
|
$publication->save();
|
|
$publication = $post->publication('stadtanzeiger');
|
|
$publication->setPublishDate($publishDate);
|
|
// $publication->noteForPublication(($publishDate > Carbon::now() ? true : false));
|
|
$publication->published_at = $publishDate;
|
|
$publication->state = 'published';
|
|
$publication->save();
|
|
|
|
$migration = new PostMigration;
|
|
$migration->type = $post->type;
|
|
$migration->old_id = $service->id;
|
|
$migration->post_id = $post->id;
|
|
$migration->save();
|
|
|
|
$migrations = TagMigration::where('type', 'abteilung')->where('old_id', $service->abteilung_id)->get();
|
|
$tags = array();
|
|
foreach($migrations as $m)
|
|
{
|
|
$tags[] = $m->tag_id;
|
|
}
|
|
$migrations = TagMigration::where('type', 'instanz')->where('old_id', $service->instanz_id)->get();
|
|
foreach($migrations as $m)
|
|
{
|
|
$tags[] = $m->tag_id;
|
|
}
|
|
$migrations = TagMigration::where('type', 'dienstart')->where('old_id', $service->dienstart_id)->get();
|
|
foreach($migrations as $m)
|
|
{
|
|
$tags[] = $m->tag_id;
|
|
}
|
|
$post->tags()->sync($tags);
|
|
|
|
$post = Post::with('tags')->find($post->id);
|
|
$post->save([
|
|
'slug_update' => true,
|
|
'timestamps' => false
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|