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