Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Object relationship from Stop to Selector #239

Merged
merged 7 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions app/Http/Controllers/Twill/CollectionObjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use A17\Twill\Services\Forms\Fields\Checkbox;
use A17\Twill\Services\Forms\Fields\Input;
use A17\Twill\Services\Forms\Fields\Medias;
use A17\Twill\Services\Forms\Fieldset;
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Listings\Columns\Boolean;
use A17\Twill\Services\Listings\Columns\Text;
Expand Down Expand Up @@ -191,24 +190,4 @@ protected function additionalFormFields($object, $apiCollectionObject): Form
->placeholder($longitude)
);
}

public function getSideFieldSets($object): Form
{
return parent::getSideFieldSets($object)
->addFieldset(
Fieldset::make()
->id('object_actions')
->title('Actions')
->fields([
BladePartial::make()
->view('admin.fields.action')
->withAdditionalParams([
'action' => 'Create Stop with Object',
'href' => route('twill.stops.createWithObject', parameters: [
'artwork_id' => $object->datahub_id,
]),
])
])
);
}
}
26 changes: 23 additions & 3 deletions app/Http/Controllers/Twill/SelectorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ protected function additionalIndexTableColumns(): TableColumns
return parent::additionalIndexTableColumns()
->add(
Text::make()
->field('selectable_title')
->title('Title')
->sortable()
->field('object_datahub_id')
->title('Object Id')
)
->add(
Text::make()
->field('object_title')
->title('Object')
)
->add(
Text::make()
Expand Down Expand Up @@ -70,6 +74,22 @@ protected function additionalFormFields(TwillModelContract $selector): Form
->note('Associate with a tour or a stop')
->sortable(false)
)
->add(
Browser::make()
->name('objects')
->label('Object')
->modulesCustom([
[
'name' => 'collectionObjects',
'label' => 'Collection Objects',
'params' => ['artwork_id' => $selector->object_id],
],
[
'name' => 'loanObjects',
'label' => 'Loan Objects',
],
])
)
->add(
Input::make()
->name('notes')
Expand Down
62 changes: 11 additions & 51 deletions app/Http/Controllers/Twill/StopController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,35 @@
namespace App\Http\Controllers\Twill;

use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Services\Forms\Fields\BaseFormField;
use A17\Twill\Services\Forms\Fields\Browser;
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Listings\Columns\Relation;
use A17\Twill\Services\Listings\TableColumns;
use App\Http\Controllers\Twill\Columns\ApiRelation;
use App\Models\Audio;
use App\Models\Selector;
use App\Models\Stop;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;

class StopController extends BaseController
{
protected function setUpController(): void
{
parent::setUpController();
$this->enableTitleMarkup();
$this->setModuleName('stops');
$this->setSearchColumns(['title']);
}

protected function additionalIndexTableColumns(): TableColumns
{
return parent::additionalIndexTableColumns()
->add(
Relation::make()
->field('number')
->title('Selector Number')
->relation('selector')
)
->add(
ApiRelation::make()
->field('title')
->title('Object')
->relation('object')
->sortable()
->title('Tour')
->relation('tours')
)
->add(
Relation::make()
->field('truncated_title')
->title('Tour(s)')
->relation('tours')
->optional()
->hide()
->field('number')
->title('Selector Number')
->relation('selector')
);
}

Expand Down Expand Up @@ -76,44 +61,19 @@ public function additionalFormFields(TwillModelContract $stop): Form
]
])
)
->add(
Browser::make()
->name('objects')
->label('Object')
->modulesCustom([
[
'name' => 'collectionObjects',
'label' => 'Collection Objects',
'params' => ['artwork_id' => $stop->artwork_id],
],
[
'name' => 'loanObjects',
'label' => 'Loan Objects',
],
])
)
->add(
Browser::make()
->name('tour_stops')
->label('Tours')
->label('Tour')
->modules([\App\Models\Tour::class])
->note('Add stop to tours if applicable')
->note('Add stop to tour')
->sortable(false)
->max(99)
);
}

public function createWithObject(): RedirectResponse
{
$stop = Stop::create(['artwork_id' => $this->request->query('artwork_id')]);
return Redirect::to(moduleRoute($this->moduleName, $this->routePrefix, 'edit', ['stop' => $stop->id]));
}

public function createWithAudio(): RedirectResponse
protected function getTitleField(): BaseFormField
{
$audio = Audio::find(request('sound_id'));
$stop = Stop::create();
$stop->selector()->save($audio->selector);
return Redirect::to(moduleRoute($this->moduleName, $this->routePrefix, 'edit', ['stop' => $stop->id]));
return parent::getTitleField()
->note('Title of related object');
}
}
7 changes: 6 additions & 1 deletion app/Models/CollectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Models\Behaviors\HasApiRelations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class CollectionObject extends AbstractModel
{
Expand Down Expand Up @@ -44,4 +44,9 @@ public function gallery(): BelongsTo
{
return $this->belongsToApi(\App\Models\Api\Gallery::class, 'gallery_id');
}

public function selectors(): MorphMany
{
return $this->morphMany(Selector::class, 'object', localKey: 'datahub_id');
}
}
8 changes: 5 additions & 3 deletions app/Models/LoanObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use A17\Twill\Models\Behaviors\HasMedias;
use App\Models\Behaviors\HasApiRelations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class LoanObject extends AbstractModel
{
use HasApiRelations;
use HasFactory;
use HasMedias;

protected $fillable = [
Expand All @@ -28,8 +30,8 @@ public function gallery(): BelongsTo
return $this->belongsToApi(\App\Models\Api\Gallery::class, 'gallery_id');
}

public function stop(): MorphOne
public function selectors(): MorphMany
{
return $this->morphOne(Stop::class, 'object');
return $this->morphMany(Selector::class, 'object');
}
}
49 changes: 33 additions & 16 deletions app/Models/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\DB;

Expand All @@ -21,16 +21,17 @@ class Selector extends Model
protected $fillable = [
'notes',
'number',
'object_id',
'object_type',
];

protected $casts = [
'number' => 'string',
];

protected $appends = [
'locales',
'selectable_title',
'title',
'object_datahub_id',
'object_title',
'tour_title',
];

Expand All @@ -39,44 +40,60 @@ public function apiRelatables(): MorphMany
return $this->morphMany(ApiRelatable::class, 'api_relatable');
}

public function audio(): MorphToMany
public function object(): BelongsTo|MorphTo
{
return $this->apiElements()->where('relation', 'audio');
if ($this->object_type === 'collectionObject') {
return $this->belongsToApi(Api\CollectionObject::class, 'object_id');
} else {
return $this->morphTo();
}
}

public function selectable(): MorphTo
{
return $this->morphTo();
}

public function locales(): Attribute
public function number(): Attribute
{
return Attribute::make(
get: fn (): string => $this->apiModels('audio', 'Audio')->pluck('locale')->join(', '),
get: fn ($number): string => (string) ($number ?? '--'),
set: fn ($number) => intval($number) ? $number : null,
);
}

/**
* Alias of number
*/
public function title(): Attribute
{
return Attribute::make(
get: fn (): string => (string) $this->number,
get: fn (): string => $this->number,
);
}

public function selectableTitle(): Attribute
public function objectDatahubId(): Attribute
{
$selectable = $this->selectable;
return Attribute::make(
get: function () use ($selectable): string {
$title = $selectable?->title;
if ($selectable instanceof Tour) {
$title .= ' Intro';
get: function (): string {
$id = null;
if ($this->object && $this->object_type == 'collectionObject') {
$id = $this->object_id;
} elseif ($this->object) {
$id = '(loan)';
}
return (string) $title;
return (string) $id;
}
);
}

public function objectTitle(): Attribute
{
return Attribute::make(
get: fn (): string => (string) $this->object?->title,
);
}

public function tourTitle(): Attribute
{
$selectable = $this->selectable;
Expand Down
29 changes: 7 additions & 22 deletions app/Models/Stop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,36 @@

namespace App\Models;

use A17\Twill\Models\Behaviors\HasRelated;
use A17\Twill\Models\Behaviors\HasRevisions;
use A17\Twill\Models\Behaviors\HasTranslation;
use A17\Twill\Models\Model;
use App\Models\Behaviors\HasApiRelations;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Stop extends Model
{
use HasApiRelations;
use HasFactory;
use HasRelated;
use HasRevisions;
use HasTranslation;

protected $fillable = [
'object_id',
'object_type',
'publish_end_date',
'publish_start_date',
'published',
];

public $casts = [
protected $casts = [
'publish_end_date' => 'datetime',
'publish_start_date' => 'datetime',
];

public $translatedAttributes = [
'active',
protected $appends = [
'title',
'title_markup',
];

public function object(): BelongsTo|MorphTo
public function title(): Attribute
{
if ($this->object_type === 'collectionObject') {
return $this->belongsToApi(Api\CollectionObject::class, 'object_id');
} else {
return $this->morphTo();
}
return Attribute::make(
get: fn (): string => $this->selector?->object->title,
);
}

public function selector(): MorphOne
Expand Down
Loading