Feuerwehr-eppingen/database/seeds/VehicleSeeder.php

118 lines
4.0 KiB
PHP
Executable File

<?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\VehicleController;
class VehicleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run($mode = 'update')
{
if($mode == 'replace')
{
DB::connection('mysql')->table('posts')->where('type', 'fahrzeug')->delete();
Storage::disk('upload')->deleteDirectory('uploads/fahrzeug');
}
$vehicles = DB::connection('mysql_alt')->select("SELECT * FROM ikarus_fahrzeug");
foreach ($vehicles as $vehicle)
{
// 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', 'fahrzeug')->where('old_id', $vehicle->id)->get();
if(($mode == 'replace') || ($mode == 'update' && $checkMigration->count() == 0))
{
$new_user = User::find($vehicle->benutzer_id);
\Auth::login( $new_user );
echo "INSERT vehicle: $vehicle->id\n";
$post = new Post;
$post->type = 'fahrzeug';
$post->title = $vehicle->name;
$post->content = json_encode([
'kurzbezeichnung' => $vehicle->kurzbezeichnung,
'name' => $vehicle->name,
'funkrufname' => MigrationHelper::normalizeVehicle($vehicle->funkrufname),
'mannschaft' => $vehicle->mannschaft,
'hersteller' => $vehicle->hersteller,
'aufbau' => $vehicle->aufbau,
'baujahr' => $vehicle->baujahr,
'leistung' => $vehicle->leistung,
'gewicht' => $vehicle->gewicht,
'einsatz' => $vehicle->einsatz,
]);
$post->user_id = $vehicle->benutzer_id;
$post->created_at = $vehicle->created_at;
$post->updated_at = ($vehicle->updated_at == $vehicle->created_at) ? Carbon::createFromFormat(config('formats.datetime.database'), $vehicle->updated_at)->addMinute() : $vehicle->updated_at;
$post->published = true;
$post->save();
$controller = new VehicleController();
$publishDate = $controller->calculatePublicationDate($post);
$publication = $post->publication('facebook');
$publication->setPublishDate($publishDate);
$publication->noteForPublication(false);
$publication->save();
$publication = $post->publication('twitter');
$publication->setPublishDate($publishDate);
$publication->noteForPublication(false);
$publication->save();
$publication = $post->publication('stadtanzeiger');
$publication->setPublishDate($publishDate);
// $publication->noteForPublication(($publishDate > Carbon::now() ? true : false));
$publication->save();
$migration = new PostMigration;
$migration->type = $post->type;
$migration->old_id = $vehicle->id;
$migration->post_id = $post->id;
$migration->save();
$migrations = TagMigration::where('type', 'abteilung')->where('old_id', $vehicle->abteilung_id)->get();
$tags = array();
foreach($migrations as $m)
{
$tags[] = $m->tag_id;
}
$post->tags()->sync($tags);
$files = Storage::disk('import')->files('import/fahrzeug/' . $vehicle->id);
$repo = new DocumentRepository();
$request = new Request();
foreach ($files as $file) {
$is_preview = false;
if(basename($file) == $vehicle->startbild)
{
$is_preview = true;
}
echo "ADD FILE $file \n";
$filedata = Storage::disk('import')->get($file);
$extension = File::extension($file);
$repo->upload('fahrzeug', $post->id, basename($file), $extension, $filedata, $post->user_id, $is_preview);
}
}
}
}
}