135 lines
4.6 KiB
PHP
135 lines
4.6 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\OperationController;
|
|
|
|
class OperationSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run($mode = 'update')
|
|
{
|
|
if($mode == 'replace')
|
|
{
|
|
DB::connection('mysql')->table('posts')->where('type', 'einsatz')->delete();
|
|
Storage::disk('upload')->deleteDirectory('uploads/einsatz');
|
|
}
|
|
$operations = DB::connection('mysql_alt')->select("SELECT * FROM ikarus_einsatz ORDER BY 'id'");
|
|
|
|
foreach($operations as $operation)
|
|
{
|
|
// 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', 'einsatz')->where('old_id', $operation->id)->get();
|
|
|
|
if(($mode == 'replace') || ($mode == 'update' && $checkMigration->count() == 0))
|
|
{
|
|
$new_user = User::find($operation->benutzer_id);
|
|
if(!$new_user)
|
|
{
|
|
$operation->benutzer_id = 1;
|
|
$new_user = User::find($operation->benutzer_id);
|
|
}
|
|
\Auth::login($new_user);
|
|
|
|
echo "INSERT operation: $operation->id\n";
|
|
$post = new Post;
|
|
$post->type = 'einsatz';
|
|
$post->title = $operation->titel;
|
|
$post->datetime = $operation->datum . " " . $operation->uhrzeit;
|
|
// $post->datetime = Carbon::parse($post->datetime)->addYears(2);
|
|
$post->content = MigrationHelper::normalize($operation->beschreibung);
|
|
$post->user_id = $operation->benutzer_id;
|
|
$post->created_at = $operation->created_at;
|
|
$post->updated_at = ($operation->updated_at == $operation->created_at) ? Carbon::createFromFormat(config('formats.datetime.database'), $operation->updated_at)->addMinute() : $operation->updated_at;
|
|
$post->published = true;
|
|
$post->save();
|
|
|
|
$controller = new OperationController();
|
|
$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 = $operation->id;
|
|
$migration->post_id = $post->id;
|
|
$migration->save();
|
|
|
|
$migrations = TagMigration::where('type', 'abteilung')->where('old_id', $operation->abteilung_id)->get();
|
|
$tags = array();
|
|
foreach($migrations as $m)
|
|
{
|
|
$tags[] = $m->tag_id;
|
|
}
|
|
$migrations = TagMigration::where('type', 'einsatzart')->where('old_id', $operation->einsatzart_id)->get();
|
|
foreach($migrations as $m)
|
|
{
|
|
$tags[] = $m->tag_id;
|
|
}
|
|
$post->tags()->sync($tags);
|
|
$vehicles = DB::connection('mysql_alt')->select("SELECT * FROM ikarus_einsatz_fahrzeug WHERE `einsatz_id` = " . $operation->id);
|
|
$posts = array();
|
|
foreach($vehicles as $vehicle)
|
|
{
|
|
echo "VEHICLE ID: " . $vehicle->fahrzeug_id . "\n";
|
|
$migrations = PostMigration::where('type', 'fahrzeug')->where('old_id', $vehicle->fahrzeug_id)->get();
|
|
echo "COUNT: " . count($migrations) . "\n";
|
|
foreach($migrations as $m)
|
|
{
|
|
$posts[] = $m->post_id;
|
|
}
|
|
}
|
|
$post->posts()->sync($posts);
|
|
|
|
$files = Storage::disk('import')->files('import/einsatz/' . $operation->id);
|
|
$repo = new DocumentRepository();
|
|
$request = new Request();
|
|
foreach($files as $file)
|
|
{
|
|
$is_preview = false;
|
|
if(basename($file) == $operation->startbild)
|
|
{
|
|
$is_preview = true;
|
|
}
|
|
echo "ADD FILE $file \n";
|
|
$filedata = Storage::disk('import')->get($file);
|
|
$extension = File::extension($file);
|
|
$repo->upload('einsatz', $post->id, basename($file), $extension, $filedata, $post->user_id, $is_preview);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|