Skip to content

Commit

Permalink
Retrieve and augment from /artworks
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgarwood committed Jul 21, 2023
1 parent 7ddf5b7 commit 0777738
Show file tree
Hide file tree
Showing 27 changed files with 825 additions and 289 deletions.
159 changes: 159 additions & 0 deletions app/Http/Controllers/Twill/ArtworkController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace App\Http\Controllers\Twill;

use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Services\Forms\Fields\Checkbox;
use A17\Twill\Services\Forms\Fields\Input;
use A17\Twill\Services\Forms\Fields\Map;
use A17\Twill\Services\Forms\Fields\Select;
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Forms\Option;
use A17\Twill\Services\Forms\Options;
use A17\Twill\Services\Listings\Columns\Boolean;
use A17\Twill\Services\Listings\Columns\Text;
use A17\Twill\Services\Listings\TableColumns;
use App\Http\Controllers\Twill\Columns\ApiRelation;
use App\Models\Api\Artwork;

class ArtworkController extends BaseApiController
{
protected $moduleName = 'artworks';
protected $hasAugmentedModel = true;

private $galleries = [];

protected function setUpController(): void
{
parent::setUpController();

$this->setSearchColumns(['title', 'artist_display', 'catalogue_display']);

$this->eagerLoadListingRelations(['gallery']);
}

protected function additionalIndexTableColumns(): TableColumns
{
return parent::additionalIndexTableColumns()
->add(Text::make()
->field('artist_display'))
->add(Boolean::make()
->field('is_on_view'))
->add(ApiRelation::make()
->field('gallery_id')
->title('Gallery')
->relation('gallery'))
->add(Text::make()
->field('credit_line')
->optional()
->hide())
->add(Text::make()
->field('copyright_notice')
->optional()
->hide())
->add(Text::make()
->field('latitude')
->customRender(function (Artwork $artwork) {
return $artwork->latitude ? number_format((float) $artwork->latitude, 13) : '';
})
->optional()
->hide())
->add(Text::make()
->field('longitude')
->customRender(function (Artwork $artwork) {
return $artwork->longitude ? number_format((float) $artwork->longitude, 13) : '';
})
->optional()
->hide())
->add(Text::make()
->optional()
->field('image_id')
->hide())
->add(Text::make()
->optional()
->field('gallery_id')
->hide());
}

protected function additionalFormFields($artwork, $apiArtwork): Form
{
$apiValues = array_map(
fn ($value) => $value ?? (string) $value,
$apiArtwork->getAttributes()
);
return Form::make()
->add(Input::make()
->name('artist_display')
->placeholder($apiValues['artist_display']))
->add(Checkbox::make()
->name('is_on_view')
->default($apiValues['is_on_view']))
->add(Input::make()
->name('credit_line')
->placeholder($apiValues['credit_line']))
->add(Input::make()
->name('copyright_notice')
->placeholder($apiValues['copyright_notice']))
->add(Map::make()
->name('latlng')
->label('Location (out of order)'))
->add(Input::make()
->name('latlng')
->label("Location's map data")
->type('textarea'))
->add(Input::make()
->name('latitude')
->placeholder($apiValues['latitude']))
->add(Input::make()
->name('longitude')
->placeholder($apiValues['longitude']))
->add(Input::make()
->name('image_id')
->placeholder($apiValues['image_id'])
->disabled()
->note('readonly'))
->add(Input::make()
->name('gallery_id')
->placeholder($apiValues['gallery_id'])
->disabled()
->note('readonly'));
}

public function getSideFieldSets(TwillModelContract $artwork): Form
{
$artwork->refresh();
$apiValues = $artwork->getApiModel()->getAttributes();
return Form::make()
->add(Select::make()
->name('gallery_id')
->label('Gallery')
->placeholder('Select Gallery')
->options(
Options::make(
[Option::make('', 'Unset Gallery')] +
$this->galleryOptions()
)
)
->default($apiValues['gallery_id']));
}

private function galleryOptions(): array
{
$options = [];
if (!$this->galleries) {
$this->galleries = \App\Models\Api\Gallery::query()
->orderBy('title')
->limit(100)
->get()
->sortBy([
['floor', 'asc'],
['number', 'asc'],
['title', 'asc'],
]);
}
foreach ($this->galleries as $gallery) {
$options []= Option::make($gallery->id, $gallery);
}
return $options;
}
}
129 changes: 75 additions & 54 deletions app/Http/Controllers/Twill/BaseApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Repositories\ModuleRepository;
use A17\Twill\Services\Forms\Fields\Input;
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 All @@ -43,7 +44,7 @@ class BaseApiController extends ModuleController
'search' => 'search',
];

public function setUpController(): void
protected function setUpController(): void
{
$this->setFeatureField('is_featured');

Expand Down Expand Up @@ -72,10 +73,10 @@ public function getIndexTableMainFilters($items, $scopes = []): array
*/
public function augment(string $datahubId)
{
$apiItem = $this->getApiRepository()->getById($datahubId);
$item = $this->getRepository()->firstOrCreate(['datahub_id' => $apiItem->id]);

return $this->redirectToForm($item->id);
if ($apiModel = $this->getApiRepository()->getById($datahubId)) {
$augmentedModel = $this->getRepository()->firstOrCreate(['datahub_id' => $apiModel->id]);
}
return $this->redirectToForm($augmentedModel->id);
}

public function feature()
Expand Down Expand Up @@ -130,78 +131,98 @@ public function getIndexItems(array $scopes = [], bool $forcePagination = false)
$this->applyQuickFilters($requestFilters, $appliedFilters);
$this->applyBasicFilters($requestFilters, $appliedFilters);
return $this->transformIndexItems(
$this->getApiRepository()->get(
with: $this->indexWith,
scopes: $scopes,
orders: $this->orderScope(),
perPage: $this->request->get('offset') ?? $this->perPage,
forcePagination: $forcePagination,
appliedFilters: $appliedFilters
)
$this->getApiData($scopes, $forcePagination, $appliedFilters)
);
}

public function getApiData($scopes = [], $forcePagination = false, $appliedFilters = [])
{
return $this->getApiRepository()->get(
with: $this->indexWith,
scopes: $scopes,
orders: $this->orderScope(),
perPage: $this->request->get('offset') ?? $this->perPage,
forcePagination: $forcePagination,
appliedFilters: $appliedFilters
);
}

protected function getIndexTableColumns(): TableColumns
{
$table = parent::getIndexTableColumns();
$after = $table->splice(0);
$table->push(
Boolean::make()
return $table
->push(Boolean::make()
->field('is_augmented')
->optional()
->hide()
);
$table->push(
Text::make()
->hide())
->push(Text::make()
->field('id')
->title('Datahub Id')
->optional()
->hide()
);
$table->push(
Text::make()
->hide())
->push(Text::make()
->field('source_updated_at')
->optional()
->hide()
);
$table->push(
Text::make()
->hide())
->push(Text::make()
->field('updated_at')
->optional()
->hide()
);
return $table->merge($after);
->hide())
->merge($after);
}

public function getForm(TwillModelContract $model): Form
{
$apiValues = $model->refreshApi()->getApiModel()->getAttributes();
$form = parent::getForm($model);
$form->add(
Input::make()
->name('datahub_id')
->disabled()
->disabled()
);
$form->add(
Input::make()
->name('source_updated_at')
->disabled()
);
$form->add(
Input::make()
->name('updated_at')
->disabled()
);
$form->add(
Input::make()
->name('title')
->placeholder($apiValues['title'])
$model->refreshApi();
$form = Form::make()
->addFieldset(
Fieldset::make()
->title('Datahub')
->id('datahub')
->closed()
->fields([
Input::make()
->name('datahub_id')
->disabled()
->note('readonly'),
Input::make()
->name('source_updated_at')
->disabled()
->note('readonly'),
])
)
->addFieldset(
Fieldset::make()
->title('Timestamps')
->id('timestamps')
->closed()
->fields([
Input::make()
->name('created_at')
->disabled()
->note('readonly'),
Input::make()
->name('updated_at')
->disabled()
->note('readonly'),
])
);
$content = Form::make()
->add(Input::make()
->name('title')
->placeholder($model->getApiModel()->title))
->merge($this->additionalFormFields($model, $model->getApiModel()));
$form->addFieldset(
Fieldset::make()
->title('Content')
->id('content')
->fields($content->toArray())
);
return $form->merge($this->additionalFormFields($model, $model->getApiModel()));
return $form;
}

public function additionalFormFields($model, $apiModel): Form
protected function additionalFormFields($model, $apiModel): Form
{
return new Form();
}
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Controllers/Twill/Columns/ApiRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Controllers\Twill\Columns;

use A17\Twill\Exceptions\ColumnMissingPropertyException;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Services\Listings\Columns\Relation;

class ApiRelation extends Relation
{
private ?string $relation = null;

public function relation(string $relation): static
{
$this->relation = $relation;
return $this;
}

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 ($relation = $augmentedModel->{$this->relation}()) {
return $relation;
}
}
$relation = $model->{$this->relation}();
return (string) $relation;
}
}
Loading

0 comments on commit 0777738

Please sign in to comment.