98 lines
2.9 KiB
PHP
98 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\Auth;
|
|
use App\Core\Controller;
|
|
use App\Helpers\Validator;
|
|
use App\Models\Site;
|
|
use App\Models\Topic;
|
|
use App\Models\GlobalTopic;
|
|
|
|
class TopicController extends Controller
|
|
{
|
|
public function index(string $id): void
|
|
{
|
|
Auth::requireLogin();
|
|
|
|
$site = Site::find((int) $id);
|
|
if (!$site) {
|
|
$this->flash('danger', 'Strona nie znaleziona.');
|
|
$this->redirect('/sites');
|
|
return;
|
|
}
|
|
|
|
$topics = Topic::findBySiteWithGlobal((int) $id);
|
|
$globalTopics = GlobalTopic::findAllGrouped();
|
|
|
|
$this->view('topics/index', ['site' => $site, 'topics' => $topics, 'globalTopics' => $globalTopics]);
|
|
}
|
|
|
|
public function store(string $id): void
|
|
{
|
|
Auth::requireLogin();
|
|
|
|
$validator = new Validator();
|
|
$validator->required('name', $this->input('name'), 'Nazwa tematu');
|
|
|
|
if (!$validator->isValid()) {
|
|
$this->flash('danger', $validator->getFirstError());
|
|
$this->redirect("/sites/{$id}/topics");
|
|
return;
|
|
}
|
|
|
|
Topic::create([
|
|
'site_id' => (int) $id,
|
|
'global_topic_id' => $this->input('global_topic_id') ? (int) $this->input('global_topic_id') : null,
|
|
'name' => $this->input('name'),
|
|
'description' => $this->input('description', ''),
|
|
'wp_category_id' => $this->input('wp_category_id') ? (int) $this->input('wp_category_id') : null,
|
|
'is_active' => $this->input('is_active') ? 1 : 0,
|
|
]);
|
|
|
|
$this->flash('success', 'Temat został dodany.');
|
|
$this->redirect("/sites/{$id}/topics");
|
|
}
|
|
|
|
public function update(string $id): void
|
|
{
|
|
Auth::requireLogin();
|
|
|
|
$topic = Topic::find((int) $id);
|
|
if (!$topic) {
|
|
$this->flash('danger', 'Temat nie znaleziony.');
|
|
$this->redirect('/sites');
|
|
return;
|
|
}
|
|
|
|
Topic::update((int) $id, [
|
|
'global_topic_id' => $this->input('global_topic_id') ? (int) $this->input('global_topic_id') : null,
|
|
'name' => $this->input('name'),
|
|
'description' => $this->input('description', ''),
|
|
'wp_category_id' => $this->input('wp_category_id') ? (int) $this->input('wp_category_id') : null,
|
|
'is_active' => $this->input('is_active') ? 1 : 0,
|
|
]);
|
|
|
|
$this->flash('success', 'Temat został zaktualizowany.');
|
|
$this->redirect("/sites/{$topic['site_id']}/topics");
|
|
}
|
|
|
|
public function destroy(string $id): void
|
|
{
|
|
Auth::requireLogin();
|
|
|
|
$topic = Topic::find((int) $id);
|
|
if (!$topic) {
|
|
$this->flash('danger', 'Temat nie znaleziony.');
|
|
$this->redirect('/sites');
|
|
return;
|
|
}
|
|
|
|
$siteId = $topic['site_id'];
|
|
Topic::delete((int) $id);
|
|
|
|
$this->flash('success', 'Temat został usunięty.');
|
|
$this->redirect("/sites/{$siteId}/topics");
|
|
}
|
|
}
|