cms/app/Http/Controllers/Admin/Posts/PostEditController.php

67 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Controllers\Admin\Posts;
use App\Http\Controllers\Controller;
use App\Models\CustomPostType;
use App\Models\Post;
use App\Models\Media;
use App\Services\AccessibilityAnalyzer;
use App\Services\SettingService;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
/**
* Controller for showing the post edit UI.
*/
class PostEditController extends Controller
{
/**
* Show the form for editing the specified post.
*
* @param \App\Models\CustomPostType $customPostType
* @param \App\Models\Post $post
* @param \App\Services\AccessibilityAnalyzer $analyzer
* @param \App\Services\SettingService $settingService
* @return \Illuminate\View\View
*/
public function __invoke(CustomPostType $customPostType, Post $post, AccessibilityAnalyzer $analyzer, SettingService $settingService): View
{
$content = $post->content ?? [];
$this->hydrateMedia($content);
return view('admin.posts.editor', [
'customPostType' => $customPostType->load('fields'),
'post' => $post->setAttribute('content', $content),
'a11yIssues' => $analyzer->analyze($content),
'availableLocales' => $settingService->getSupportedLocales(),
'defaultLocale' => $settingService->get('default_locale', config('app.locale')),
'permissions' => [
'view-media' => Gate::allows('view-media'),
'upload-media' => Gate::allows('upload-media'),
'update-media' => Gate::allows('update-media'),
'delete-media' => Gate::allows('delete-media'),
],
]);
}
/**
* Recursively hydrate media blocks with filenames if only media_id is present.
*/
protected function hydrateMedia(array &$content): void
{
foreach ($content as $locale => &$blocks) {
if (is_array($blocks)) {
foreach ($blocks as &$block) {
if (($block['type'] ?? '') === 'media' && !empty($block['data']['media_id']) && empty($block['data']['filename'])) {
$media = Media::find($block['data']['media_id']);
if ($media) {
$block['data']['filename'] = $media->filename;
}
}
}
}
}
}
}