From 6c9d80a6cb5cd0b94b31467784bfa7aeb5b340b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20St=C3=B6ckel?= Date: Sun, 10 Mar 2024 22:41:25 +0100 Subject: [PATCH] :card_file_box: add database layout for IFOPT see #2411 --- app/Models/Station.php | 29 +++++++++++++++-- database/factories/StationFactory.php | 10 +++++- ...024_03_10_211526_add_ifopt_to_stations.php | 31 +++++++++++++++++++ resources/views/admin/stations/show.blade.php | 8 +++++ 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2024_03_10_211526_add_ifopt_to_stations.php diff --git a/app/Models/Station.php b/app/Models/Station.php index 1027f888d..a82a35897 100644 --- a/app/Models/Station.php +++ b/app/Models/Station.php @@ -29,22 +29,45 @@ class Station extends Model use HasFactory, LogsActivity; protected $table = 'train_stations'; - protected $fillable = ['ibnr', 'wikidata_id', 'rilIdentifier', 'name', 'latitude', 'longitude', 'time_offset', 'shift_time']; + protected $fillable = [ + 'ibnr', 'wikidata_id', 'rilIdentifier', + 'ifopt_a', 'ifopt_b', 'ifopt_c', 'ifopt_d', 'ifopt_e', + 'name', 'latitude', 'longitude', 'time_offset', 'shift_time' + ]; protected $hidden = ['created_at', 'updated_at', 'time_offset', 'shift_time']; protected $casts = [ 'id' => 'integer', - 'rilIdentifier' => 'string', - 'name' => 'string', 'ibnr' => 'integer', 'wikidata_id' => 'string', + 'ifopt_a' => 'string', + 'ifopt_b' => 'integer', + 'ifopt_c' => 'integer', + 'ifopt_d' => 'integer', + 'ifopt_e' => 'integer', + 'rilIdentifier' => 'string', + 'name' => 'string', 'latitude' => 'float', 'longitude' => 'float', ]; + protected $appends = ['ifopt']; public function wikidataEntity(): BelongsTo { return $this->belongsTo(WikidataEntity::class, 'wikidata_id', 'id'); } + public function getIfoptAttribute(): ?string { + if (!$this->ifopt_a) { + return null; + } + $ifopt = $this->ifopt_a; + foreach (['b', 'c', 'd', 'e'] as $level) { + if ($this->{"ifopt_$level"}) { + $ifopt .= ':' . $this->{"ifopt_$level"}; + } + } + return $ifopt; + } + public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->dontSubmitEmptyLogs() diff --git a/database/factories/StationFactory.php b/database/factories/StationFactory.php index 0b10ea849..41c266d2d 100644 --- a/database/factories/StationFactory.php +++ b/database/factories/StationFactory.php @@ -9,10 +9,18 @@ class StationFactory extends Factory public function definition(): array { return [ 'ibnr' => $this->faker->unique()->numberBetween(8000001, 8999999), + 'wikidata_id' => null, + 'ifopt_a' => $this->faker->randomElement(['de', 'at', 'ch', 'fr']), + 'ifopt_b' => $this->faker->numberBetween(10000, 99999), + 'ifopt_c' => $this->faker->numberBetween(1, 9999), + 'ifopt_d' => $this->faker->boolean() ? $this->faker->numberBetween(1, 9999) : null, + 'ifopt_e' => $this->faker->boolean() ? $this->faker->numberBetween(1, 9) : null, + 'rilIdentifier' => substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4), 'name' => $this->faker->unique()->city, 'latitude' => $this->faker->latitude, 'longitude' => $this->faker->longitude, - 'rilIdentifier' => substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4), + 'time_offset' => null, + 'shift_time' => null, ]; } } diff --git a/database/migrations/2024_03_10_211526_add_ifopt_to_stations.php b/database/migrations/2024_03_10_211526_add_ifopt_to_stations.php new file mode 100644 index 000000000..050e01062 --- /dev/null +++ b/database/migrations/2024_03_10_211526_add_ifopt_to_stations.php @@ -0,0 +1,31 @@ +unsignedInteger('ifopt_e')->nullable()->comment('Platform')->after('wikidata_id'); + $table->unsignedInteger('ifopt_d')->nullable()->comment('Area')->after('wikidata_id'); + $table->unsignedInteger('ifopt_c')->nullable()->comment('Station')->after('wikidata_id'); + $table->unsignedInteger('ifopt_b')->nullable()->comment('Municipal')->after('wikidata_id'); + $table->string('ifopt_a')->nullable()->comment('Country')->after('wikidata_id'); + + $table->index(['ifopt_a', 'ifopt_b', 'ifopt_c', 'ifopt_d', 'ifopt_e'], 'ifopt'); + }); + } + + public function down(): void { + Schema::table('train_stations', static function(Blueprint $table) { + $table->dropIndex('ifopt'); + $table->dropColumn('ifopt_e'); + $table->dropColumn('ifopt_d'); + $table->dropColumn('ifopt_c'); + $table->dropColumn('ifopt_b'); + $table->dropColumn('ifopt_a'); + }); + } +}; diff --git a/resources/views/admin/stations/show.blade.php b/resources/views/admin/stations/show.blade.php index 9a20b1f2f..064821764 100644 --- a/resources/views/admin/stations/show.blade.php +++ b/resources/views/admin/stations/show.blade.php @@ -17,6 +17,14 @@ Name {{ $station->name }} + + IBNR + {{ $station->ibnr }} + + + IFOPT + {{ $station->ifopt }} +