diff --git a/app/Http/Controllers/Api/Pages/PagesController.php b/app/Http/Controllers/Api/Pages/PagesController.php index 304e0d0..533998b 100644 --- a/app/Http/Controllers/Api/Pages/PagesController.php +++ b/app/Http/Controllers/Api/Pages/PagesController.php @@ -4,7 +4,9 @@ namespace App\Http\Controllers\Api\Pages; use App\Http\Controllers\Controller; use App\Models\Pages\Page; +use App\Models\Publications\Publication; use App\Transformers\Pages\PageTransformer; +use App\Transformers\Publications\PublicationTransformer; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -20,7 +22,12 @@ class PagesController extends Controller { } public function find(Request $request): ?JsonResponse { - return ($model = Page::byUrl($request->get('url'))) ? fractal($model, new PageTransformer())->respond() : null; + if ($model = Page::byUrl($request->get('url'))) { + return fractal($model, new PageTransformer())->respond(); + } elseif ($model = Publication::byUrl($request->get('url'))) { + return fractal($model, new PublicationTransformer())->respond(); + } + return null; } public function index(Request $request): JsonResponse { diff --git a/app/Models/Publications/Publication.php b/app/Models/Publications/Publication.php index a03476d..f8b9036 100644 --- a/app/Models/Publications/Publication.php +++ b/app/Models/Publications/Publication.php @@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Collection; class Publication extends Model { use UuidScopeTrait, SoftDeletes, HasObjectsTrait, RelationValuesTrait; @@ -66,6 +67,14 @@ class Publication extends Model { return ($this->page->link ?? '') . "/{$this->slug}"; } + public function getParentsAttribute(): Collection { + $page = $this->page; + $result = collect([$page]); + while ($page = $page->parent) $result->push($page); + return $result; + } + + public function getParsedTypeAttribute(): array { return ['name' => $this->type, 'title' => PublicationType::TITLES[$this->type] ?? null]; } diff --git a/app/Transformers/Publications/PublicationTransformer.php b/app/Transformers/Publications/PublicationTransformer.php index d1f6abe..98867e6 100644 --- a/app/Transformers/Publications/PublicationTransformer.php +++ b/app/Transformers/Publications/PublicationTransformer.php @@ -2,6 +2,7 @@ namespace App\Transformers\Publications; +use App\Models\Pages\Page; use App\Models\Publications\Publication; use App\Services\PermissionsService; use App\Transformers\Assets\AssetTransformer; @@ -19,7 +20,7 @@ class PublicationTransformer extends TransformerAbstract { ]; protected array $availableIncludes = [ - 'page', 'poster', 'author', 'sections', 'sidebars', 'permissions' + 'page', 'parents', 'poster', 'author', 'sections', 'sidebars', 'permissions' ]; public function transform(Publication $model): array { @@ -27,7 +28,8 @@ class PublicationTransformer extends TransformerAbstract { 'id' => $model->uuid, 'slug' => $model->slug, 'link' => $model->link, - 'type' => $model->parsedType, + 'type' => 'publication', + 'subtype' => $model->parsedType, 'name' => $model->name, 'excerpt' => $model->excerpt, 'is_published' => boolval($model->is_published), @@ -40,6 +42,10 @@ class PublicationTransformer extends TransformerAbstract { return $model->page ? $this->item($model->page, new PageTransformer()) : null; } + public function includeParents(Publication $model): Collection { + return $this->collection($model->parents->reverse(), new PageTransformer()); + } + public function includePoster(Publication $model): ?Item { return $model->poster ? $this->item($model->poster, new AssetTransformer()) : null; }