Skip to content

Commit

Permalink
Merge pull request #19 from art-institute-of-chicago/drift/sort-prompts
Browse files Browse the repository at this point in the history
Add sorting to themes and prompts
  • Loading branch information
driftingly authored May 1, 2024
2 parents 07d54bb + 9c2b704 commit cb6e754
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 4 deletions.
27 changes: 27 additions & 0 deletions app/Console/Commands/CacheImageInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Console\Commands;

use App\Libraries\DamsImageService;
use App\Models\Artwork;
use Illuminate\Console\Command;

class CacheImageInfo extends Command
{
protected $signature = 'app:cache-image-info';

protected $description = 'Caches image info required for building JSON output.';

public function __construct(
private readonly DamsImageService $damsImageService
) {
parent::__construct();
}

public function handle()
{
$this->withProgressBar(Artwork::active()->get(), function (Artwork $artwork) {
$this->damsImageService->getImage($artwork);
});
}
}
11 changes: 10 additions & 1 deletion app/Console/Commands/CacheJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ public function handle()
$this->themeRepository->active()->with(
[
'prompts' => fn (Builder $query) => $query->active(),
'prompts.artworks' => fn (Builder $query) => $query->active()->limit(8),
'prompts.artworks' => fn (Builder $query) => $query->active(),
]
)->get()
),
];

// Limit the number of artworks per prompt to 8
// This can be removed once we upgrade to Laravel 11
// https://github.com/laravel/framework/pull/49695
foreach ($data['themes'] as $themeKey => $theme) {
foreach ($theme['prompts'] as $promptKey => $prompt) {
$data['themes'][$themeKey]['prompts'][$promptKey]['artworks'] = $prompt['artworks']->take(8);
}
}

Cache::put($key, json_encode($data));
});
}
Expand Down
4 changes: 4 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ protected function schedule(Schedule $schedule): void
->command('app:cache-json')
->everyFiveMinutes()
->withoutOverlapping();

$schedule
->command('app:cache-image-info')
->daily();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Twill/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected function setUpController(): void
$this->disableBulkPublish();
$this->disableBulkRestore();
$this->disableBulkForceDelete();
$this->disableSortable();
$this->enableReorder();
}

public function getCreateForm(): Form
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Twill/ThemePromptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected function setUpController(): void
$this->disableBulkPublish();
$this->disableBulkRestore();
$this->disableBulkForceDelete();
$this->disableSortable();
$this->enableReorder();

if (request('theme')) {
$this->setBreadcrumbs(
Expand Down
7 changes: 7 additions & 0 deletions app/Models/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class Theme extends Model implements Sortable
],
];

protected static function booted(): void
{
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderBy('position');
});
}

public function prompts()
{
return $this->hasMany(ThemePrompt::class);
Expand Down
9 changes: 8 additions & 1 deletion app/Models/ThemePrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ class ThemePrompt extends Model implements Sortable
'subtitle',
];

protected static function booted(): void
{
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderBy('position');
});
}

public function theme()
{
return $this->belongsTo(Theme::class);
}

public function artworks()
{
return $this->hasMany(ThemePromptArtwork::class)->orderBy('position');
return $this->hasMany(ThemePromptArtwork::class);
}

public function scopeActive(Builder $query): void
Expand Down
7 changes: 7 additions & 0 deletions app/Models/ThemePromptArtwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class ThemePromptArtwork extends Model implements Sortable
'activity_instructions',
];

protected static function booted(): void
{
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderBy('position');
});
}

public function artwork()
{
return $this->belongsTo(Artwork::class);
Expand Down
12 changes: 10 additions & 2 deletions resources/views/forms/prompts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
@endphp

<div class="custom">
<ul class="py-2">
<ul class="py-2 text-lg">
<li>
<a
class="block p-2 hover:bg-slate-100"
href="{{ route('twill.themes.prompts.index', $theme->id) }}"
>
ALL PROMPTS
</a>
</li>
@foreach($theme->prompts()->orderBy('position')->get() as $prompt)
<li class="text-lg">
<li>
<a
@class(['block p-2 hover:bg-slate-100', 'font-semibold' => $prompt->id == $currentPromptId])
href="{{ route('twill.themes.prompts.show', [$theme->id, $prompt->id]) }}"
Expand Down

0 comments on commit cb6e754

Please sign in to comment.