59 lines
2 KiB
PHP
59 lines
2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin\Pages;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\Page;
|
||
|
|
use App\Models\Media;
|
||
|
|
use App\Services\AccessibilityAnalyzer;
|
||
|
|
use App\Services\SettingService;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Gate;
|
||
|
|
|
||
|
|
class PageEditController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Handle the incoming request.
|
||
|
|
*/
|
||
|
|
public function __invoke(Request $request, Page $page, AccessibilityAnalyzer $analyzer, SettingService $settingService)
|
||
|
|
{
|
||
|
|
$content = $page->content ?? [];
|
||
|
|
|
||
|
|
// Hydrate media filenames if missing
|
||
|
|
$this->hydrateMedia($content);
|
||
|
|
|
||
|
|
return view('admin.pages.editor', [
|
||
|
|
'page' => $page->setAttribute('content', $content),
|
||
|
|
'availableLocales' => $settingService->getSupportedLocales(),
|
||
|
|
'defaultLocale' => $settingService->get('default_locale', config('app.locale')),
|
||
|
|
'a11yIssues' => $analyzer->analyze($content),
|
||
|
|
'includeInNavigation' => $page->navigationItem()->exists(),
|
||
|
|
'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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|