67 lines
2.5 KiB
PHP
67 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Blog\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Modules\Authors\Models\Author;
|
|
use Modules\Blog\Models\Blog;
|
|
use Modules\Categories\Models\Category;
|
|
use Modules\Tags\Models\Tag;
|
|
|
|
final class BlogController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
return view('blog::admin.index', ['records' => Blog::with(['author', 'category'])->latest()->paginate(25)]);
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('blog::admin.form', $this->lookups());
|
|
}
|
|
|
|
public function store(Request $r): RedirectResponse
|
|
{
|
|
$record = Blog::create($this->data($r));
|
|
$record->tags()->sync($r->input('tag_ids', []));
|
|
|
|
return redirect()->route('administration.blog.index')->with('status', 'Blog created.');
|
|
}
|
|
|
|
public function edit(Blog $blog_post): View
|
|
{
|
|
return view('blog::admin.form', array_merge($this->lookups(), ['record' => $blog_post]));
|
|
}
|
|
|
|
public function update(Request $r, Blog $blog_post): RedirectResponse
|
|
{
|
|
$blog_post->update($this->data($r));
|
|
$blog_post->tags()->sync($r->input('tag_ids', []));
|
|
|
|
return redirect()->route('administration.blog.index')->with('status', 'Blog updated.');
|
|
}
|
|
|
|
public function destroy(Blog $blog_post): RedirectResponse
|
|
{
|
|
$blog_post->delete();
|
|
|
|
return back()->with('status', 'Blog deleted.');
|
|
}
|
|
|
|
private function lookups(): array
|
|
{
|
|
return ['authors' => Author::where('is_active', true)->orderBy('name')->get(), 'categories' => Category::where('is_active', true)->orderBy('name')->get(), 'tags' => Tag::orderBy('name')->get()];
|
|
}
|
|
|
|
private function data(Request $r): array
|
|
{
|
|
$d = $r->validate(['author_id' => ['nullable', 'exists:authors,id'], 'category_id' => ['nullable', 'exists:categories,id'], 'title' => ['required', 'string', 'max:255'], 'slug' => ['required', 'string', 'max:255'], 'excerpt' => ['nullable', 'string'], 'content' => ['nullable', 'string'], 'featured_image' => ['nullable', 'string', 'max:255'], 'status' => ['required', 'in:draft,review,published,archived'], 'is_featured' => ['sometimes', 'boolean'], 'published_at' => ['nullable', 'date'], 'expires_at' => ['nullable', 'date', 'after:published_at'], 'meta_title' => ['nullable', 'string', 'max:255'], 'meta_description' => ['nullable', 'string']]);
|
|
$d['is_featured'] = $r->boolean('is_featured');
|
|
|
|
return $d;
|
|
}
|
|
}
|