Skip to content

Commit

Permalink
Create Stop model et al [MA-116]
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgarwood committed Aug 17, 2023
1 parent 2ca5c77 commit 8ae2099
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Twill/Columns/ApiRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function getRenderValue(TwillModelContract $model): string
if (null === $this->relation) {
throw new ColumnMissingPropertyException('Relation column missing relation value: ' . $this->field);
}
if ($augmentedModel = $model->getAugmentedModel()) {
if (method_exists($model, 'getAugmentedModel') && $augmentedModel = $model->getAugmentedModel()) {
if ($relation = $augmentedModel->{$this->relation}()) {
return $relation;
}
Expand Down
132 changes: 132 additions & 0 deletions app/Http/Controllers/Twill/StopController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace App\Http\Controllers\Twill;

use A17\Twill\Http\Controllers\Admin\ModuleController as BaseModuleController;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Services\Forms\Fields\Input;
use A17\Twill\Services\Forms\Fields\Select;
use A17\Twill\Services\Forms\Fieldset;
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Forms\Option;
use A17\Twill\Services\Forms\Options;
use A17\Twill\Services\Listings\TableColumns;
use App\Http\Controllers\Twill\Columns\ApiRelation;
use Illuminate\Support\Str;

class StopController extends BaseModuleController
{
protected $moduleName = 'stops';

protected function setUpController(): void
{
$this->disablePermalink();
$this->enableReorder();
}

protected function additionalIndexTableColumns(): TableColumns
{
$table = parent::additionalIndexTableColumns();

$table->add(
ApiRelation::make()
->field('artwork_id')
->title('Object')
->relation('object')
);
$table->add(
ApiRelation::make()
->field('sound_id')
->title('Audio')
->relation('audio')
);

return $table;
}

public function getForm(TwillModelContract $stop): Form
{
$form = parent::getForm($stop);
$form->add(
Input::make()
->name('title')
);
$form->add(
Input::make()
->name('artwork_id')
->note('Datahub Id')
);
$form->add(
Input::make()
->name('sound_id')
->note('Datahub Id')
);

return $form;
}

public function getSideFieldSets(TwillModelContract $stop): Form
{
return parent::getSideFieldSets($stop)
// For some reason, the side form will not render unless there is a
// field in the default Content fieldset.
->add(
Input::make()
->name('title')
->disabled()
)
->addFieldset(
Fieldset::make()
->id('api_relations')
->title('API Relations')
->fields([
Select::make()
->name('artwork_id')
->label('Object')
->placeholder('Select Object')
->note('100 most recent')
->options(
Options::make(
$this->objectOptions()
)
),
Select::make()
->name('sound_id')
->label('Audio')
->placeholder('Select Audio')
->note('100 most recent')
->options(
Options::make(
$this->audioOptions()
)
),
])
);
}

private function objectOptions(): array
{
$options = [];
$objects = \App\Models\Api\Artwork::query()
->limit(100)
->get()
->sortBy('source_updated_at');
foreach ($objects as $object) {
$options[] = Option::make($object->id, Str::words($object->title, 5));
}
return $options;
}

private function audioOptions(): array
{
$options = [];
$audios = \App\Models\Api\Sound::query()
->limit(100)
->get()
->sortByDesc('source_updated_at');
foreach ($audios as $audio) {
$options[] = Option::make($audio->id, Str::words($audio->title, 5));
}
return $options;
}
}
18 changes: 18 additions & 0 deletions app/Http/Requests/Twill/StopRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Requests\Twill;

use A17\Twill\Http\Requests\Admin\Request;

class StopRequest extends Request
{
public function rulesForCreate()
{
return [];
}

public function rulesForUpdate()
{
return [];
}
}
5 changes: 5 additions & 0 deletions app/Models/Api/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public function scopeOnView($query)
]
]);
}

public function __toString(): string
{
return "$this->title - $this->artist_display";
}
}
4 changes: 2 additions & 2 deletions app/Models/Api/Sound.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function getTypeAttribute()
return 'sound';
}

public function getTitleSlugAttribute()
public function __toString(): string
{
return StringHelpers::getUtf8Slug($this->title);
return $this->title;
}
}
31 changes: 31 additions & 0 deletions app/Models/Stop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Models;

use A17\Twill\Models\Behaviors\HasPosition;
use A17\Twill\Models\Behaviors\Sortable;
use A17\Twill\Models\Model;
use App\Models\Behaviors\HasApiRelations;

class Stop extends Model implements Sortable
{
use HasApiRelations;
use HasPosition;

protected $fillable = [
'title',
'position',
'artwork_id',
'sound_id',
];

public function audio()
{
return $this->belongsToApi(\App\Models\Api\Sound::class, 'sound_id');
}

public function object()
{
return $this->belongsToApi(\App\Models\Api\Artwork::class, 'artwork_id');
}
}
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ public function boot(): void
TwillNavigation::addLink(
NavigationLink::make()->title('Objects')->forModule('artworks')
);
TwillNavigation::addLink(
NavigationLink::make()->forModule('stops')
);
}
}
14 changes: 14 additions & 0 deletions app/Repositories/StopRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Repositories;

use A17\Twill\Repositories\ModuleRepository;
use App\Models\Stop;

class StopRepository extends ModuleRepository
{
public function __construct(Stop $model)
{
$this->model = $model;
}
}
24 changes: 24 additions & 0 deletions database/migrations/2023_08_16_151938_create_stops_tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStopsTables extends Migration
{
public function up()
{
Schema::create('stops', function (Blueprint $table) {
createDefaultTableFields($table);
$table->string('title')->nullable();
$table->integer('position')->unsigned()->nullable();
$table->string('artwork_id')->nullable()->comment('Datahub foreign key');
$table->string('sound_id')->nullable()->comment('Datahub foreign key');
});
}

public function down()
{
Schema::dropIfExists('stops');
}
}
2 changes: 2 additions & 0 deletions routes/twill.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@

TwillRoutes::module('sounds');
Route::get('sounds/augment/{datahub_id}', [SoundController::class, 'augment'])->name('sounds.augment');

TwillRoutes::module('stops');

0 comments on commit 8ae2099

Please sign in to comment.