2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-02-18 21:36:22 +00:00
|
|
|
namespace App\Http\Controllers\Admin;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2017-02-18 21:36:22 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-10-27 16:29:15 +00:00
|
|
|
use App\Models\Article;
|
2018-01-15 14:02:13 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\View\View;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2017-03-02 18:15:44 +00:00
|
|
|
class ArticlesController extends Controller
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List the articles that can be edited.
|
|
|
|
*
|
2018-01-15 14:02:13 +00:00
|
|
|
* @return \Illuminate\View\View
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function index(): View
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
$posts = Article::select('id', 'title', 'published')->orderBy('id', 'desc')->get();
|
|
|
|
|
2017-03-03 13:22:21 +00:00
|
|
|
return view('admin.articles.index', ['posts' => $posts]);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-18 21:36:22 +00:00
|
|
|
* Show the new article form.
|
2016-05-19 15:01:28 +01:00
|
|
|
*
|
2018-01-15 14:02:13 +00:00
|
|
|
* @return \Illuminate\View\View
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function create(): View
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
2017-02-18 21:36:22 +00:00
|
|
|
$message = session('message');
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2017-03-03 13:22:21 +00:00
|
|
|
return view('admin.articles.create', ['message' => $message]);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process an incoming request for a new article and save it.
|
|
|
|
*
|
2018-01-15 14:02:13 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function store(): RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
//if a `.md` is attached use that for the main content.
|
2018-01-15 14:02:13 +00:00
|
|
|
if (request()->hasFile('article')) {
|
|
|
|
$file = request()->file('article')->openFile();
|
2016-05-19 15:01:28 +01:00
|
|
|
$content = $file->fread($file->getSize());
|
|
|
|
}
|
2018-01-15 14:02:13 +00:00
|
|
|
$main = $content ?? request()->input('main');
|
2017-06-16 13:04:51 +01:00
|
|
|
$article = Article::create(
|
|
|
|
[
|
2018-01-15 14:02:13 +00:00
|
|
|
'url' => request()->input('url'),
|
|
|
|
'title' => request()->input('title'),
|
2017-06-16 13:04:51 +01:00
|
|
|
'main' => $main,
|
2018-01-15 14:02:13 +00:00
|
|
|
'published' => request()->input('published') ?? 0,
|
2017-06-16 13:04:51 +01:00
|
|
|
]
|
|
|
|
);
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2017-03-03 13:22:21 +00:00
|
|
|
return redirect('/admin/blog');
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
2017-02-18 21:36:22 +00:00
|
|
|
/**
|
|
|
|
* Show the edit form for an existing article.
|
|
|
|
*
|
2018-01-15 14:02:13 +00:00
|
|
|
* @param int $articleId
|
|
|
|
* @return \Illuminate\View\View
|
2017-02-18 21:36:22 +00:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function edit(int $articleId): View
|
2017-02-18 21:36:22 +00:00
|
|
|
{
|
|
|
|
$post = Article::select(
|
|
|
|
'title',
|
|
|
|
'main',
|
|
|
|
'url',
|
|
|
|
'published'
|
|
|
|
)->where('id', $articleId)->get();
|
|
|
|
|
2017-03-02 18:15:44 +00:00
|
|
|
return view('admin.articles.edit', ['id' => $articleId, 'post' => $post]);
|
2017-02-18 21:36:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
/**
|
|
|
|
* Process an incoming request to edit an article.
|
|
|
|
*
|
2018-01-15 14:02:13 +00:00
|
|
|
* @param int $articleId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function update(int $articleId): RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
$article = Article::find($articleId);
|
2018-01-15 14:02:13 +00:00
|
|
|
$article->title = request()->input('title');
|
|
|
|
$article->url = request()->input('url');
|
|
|
|
$article->main = request()->input('main');
|
|
|
|
$article->published = request()->input('published') ?? 0;
|
2016-05-19 15:01:28 +01:00
|
|
|
$article->save();
|
|
|
|
|
2017-03-03 13:22:21 +00:00
|
|
|
return redirect('/admin/blog');
|
2017-02-18 21:36:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
/**
|
|
|
|
* Process a request to delete an aricle.
|
|
|
|
*
|
2018-01-15 14:02:13 +00:00
|
|
|
* @param int $articleId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function destroy(int $articleId): RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
Article::where('id', $articleId)->delete();
|
|
|
|
|
2017-03-03 13:22:21 +00:00
|
|
|
return redirect('/admin/blog');
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
}
|