Use ContentController exclusively for content

This commit is contained in:
DariusIII
2025-11-27 11:27:48 +01:00
parent c35458a556
commit 7dcfdba4de
6 changed files with 299 additions and 306 deletions
@@ -3,20 +3,25 @@
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BasePageController;
use App\Models\Content;
use App\Models\User;
use Blacklight\Contents;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class AdminContentController extends BasePageController
{
/**
* Display list of all content.
*
* @throws \Exception
*/
public function index()
{
$this->setAdminPrefs();
$contentList = (new Contents)->getAll();
$contentList = Content::query()
->orderByRaw('contenttype, COALESCE(ordinal, 1000000)')
->get();
$this->viewData = array_merge($this->viewData, [
'contentlist' => $contentList,
@@ -28,14 +33,14 @@ class AdminContentController extends BasePageController
}
/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
* Show form to create or edit content.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
* @throws \Exception
*/
public function create(Request $request)
{
$this->setAdminPrefs();
$contents = new Contents;
$meta_title = 'Content Add';
// Set the current action.
@@ -58,17 +63,17 @@ class AdminContentController extends BasePageController
switch ($action) {
case 'add':
$meta_title = 'Content Add';
$content['status'] = '1';
$content['contenttype'] = '2';
$content['status'] = Content::STATUS_ENABLED;
$content['contenttype'] = Content::TYPE_ARTICLE;
break;
case 'submit':
// Validate and add or update.
// Validate and add or update content
if ($request->missing('id') || empty($request->input('id'))) {
$returnid = $contents->add($request->all());
$returnid = $this->addContent($request->all());
} else {
$content = $contents->update($request->all());
$returnid = $content['id'];
$this->updateContent($request->all());
$returnid = $request->input('id');
}
return redirect('admin/content-add?id='.$returnid);
@@ -78,17 +83,25 @@ class AdminContentController extends BasePageController
if ($request->has('id')) {
$meta_title = 'Content Edit';
$id = $request->input('id');
$content = $contents->getByID($id, User::ROLE_ADMIN);
$content = $this->getContentById($id);
}
break;
}
$contenttypelist = [1 => 'Useful Link', 2 => 'Article', 3 => 'Homepage'];
$rolelist = [1 => 'Everyone', 2 => 'Logged in Users', 3 => 'Admins'];
$contenttypelist = [
Content::TYPE_USEFUL => 'Useful Link',
Content::TYPE_ARTICLE => 'Article',
Content::TYPE_INDEX => 'Homepage'
];
$rolelist = [
Content::ROLE_EVERYONE => 'Everyone',
Content::ROLE_LOGGED_IN => 'Logged in Users',
Content::ROLE_ADMIN => 'Admins'
];
$this->viewData = array_merge($this->viewData, [
'status_ids' => [1, 0],
'status_ids' => [Content::STATUS_ENABLED, Content::STATUS_DISABLED],
'status_names' => ['Enabled', 'Disabled'],
'yesno_ids' => [1, 0],
'yesno_names' => ['Yes', 'No'],
@@ -102,15 +115,97 @@ class AdminContentController extends BasePageController
return view('admin.content.add', $this->viewData);
}
/**
* Delete content by ID.
*/
public function destroy(Request $request): \Illuminate\Routing\Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application
{
if ($request->has('id')) {
$contents = new Contents;
$contents->delete($request->input('id'));
Content::query()->where('id', $request->input('id'))->delete();
}
$referrer = $request->server('HTTP_REFERER');
return redirect()->to($referrer);
}
/**
* Add new content.
*/
protected function addContent(array $data): int
{
// Normalize URL
$data = $this->normalizeContentUrl($data);
// If ordinal is 1, increment all existing ordinals
if (($data['ordinal'] ?? 0) === 1) {
Content::query()->where('ordinal', '>', 0)->increment('ordinal');
}
return Content::query()->insertGetId([
'role' => $data['role'] ?? Content::ROLE_EVERYONE,
'title' => $data['title'] ?? '',
'url' => $data['url'] ?? '/',
'body' => $data['body'] ?? '',
'metadescription' => $data['metadescription'] ?? '',
'metakeywords' => $data['metakeywords'] ?? '',
'contenttype' => $data['contenttype'] ?? Content::TYPE_ARTICLE,
'status' => $data['status'] ?? Content::STATUS_ENABLED,
'ordinal' => $data['ordinal'] ?? 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
/**
* Update existing content.
*/
protected function updateContent(array $data): int
{
// Normalize URL
$data = $this->normalizeContentUrl($data);
return Content::query()
->where('id', $data['id'])
->update([
'role' => $data['role'] ?? Content::ROLE_EVERYONE,
'title' => $data['title'] ?? '',
'url' => $data['url'] ?? '/',
'body' => $data['body'] ?? '',
'metadescription' => $data['metadescription'] ?? '',
'metakeywords' => $data['metakeywords'] ?? '',
'contenttype' => $data['contenttype'] ?? Content::TYPE_ARTICLE,
'status' => $data['status'] ?? Content::STATUS_ENABLED,
'ordinal' => $data['ordinal'] ?? 0,
'updated_at' => now(),
]);
}
/**
* Get content by ID for admin viewing.
*/
protected function getContentById(int $id): ?Content
{
return Content::query()->find($id);
}
/**
* Normalize content URL to ensure proper formatting.
*/
protected function normalizeContentUrl(array $data): array
{
if (isset($data['url'])) {
// Ensure URL starts with /
if ($data['url'] !== '/' && !str_starts_with($data['url'], '/')) {
$data['url'] = '/'.$data['url'];
}
// Ensure URL ends with /
if (!str_ends_with($data['url'], '/')) {
$data['url'] .= '/';
}
}
return $data;
}
}