first commit
This commit is contained in:
165
src/Controllers/SiteController.php
Normal file
165
src/Controllers/SiteController.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?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;
|
||||
use App\Services\WordPressService;
|
||||
|
||||
class SiteController extends Controller
|
||||
{
|
||||
public function index(): void
|
||||
{
|
||||
Auth::requireLogin();
|
||||
|
||||
$sites = Site::findAll('name ASC');
|
||||
|
||||
// Attach topic count for each site
|
||||
foreach ($sites as &$site) {
|
||||
$site['topic_count'] = Topic::count('site_id = :sid', ['sid' => $site['id']]);
|
||||
}
|
||||
|
||||
$this->view('sites/index', ['sites' => $sites]);
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
Auth::requireLogin();
|
||||
$globalTopics = GlobalTopic::findAllGrouped();
|
||||
$this->view('sites/create', ['globalTopics' => $globalTopics]);
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
Auth::requireLogin();
|
||||
|
||||
$validator = new Validator();
|
||||
$validator
|
||||
->required('name', $this->input('name'), 'Nazwa')
|
||||
->required('url', $this->input('url'), 'URL')
|
||||
->url('url', $this->input('url'), 'URL')
|
||||
->required('api_user', $this->input('api_user'), 'Użytkownik API')
|
||||
->required('api_token', $this->input('api_token'), 'Token API');
|
||||
|
||||
if (!$validator->isValid()) {
|
||||
$this->flash('danger', $validator->getFirstError());
|
||||
$this->redirect('/sites/create');
|
||||
return;
|
||||
}
|
||||
|
||||
$siteId = Site::create([
|
||||
'name' => $this->input('name'),
|
||||
'url' => rtrim($this->input('url'), '/'),
|
||||
'api_user' => $this->input('api_user'),
|
||||
'api_token' => $this->input('api_token'),
|
||||
'publish_interval_days' => (int) ($this->input('publish_interval_days', 3)),
|
||||
'is_active' => $this->input('is_active') ? 1 : 0,
|
||||
'is_multisite' => $this->input('is_multisite') ? 1 : 0,
|
||||
]);
|
||||
|
||||
// Create topics from selected global topics
|
||||
$selectedTopics = $this->input('topics');
|
||||
if (is_array($selectedTopics)) {
|
||||
foreach ($selectedTopics as $globalTopicId) {
|
||||
$globalTopic = GlobalTopic::find((int) $globalTopicId);
|
||||
if ($globalTopic) {
|
||||
Topic::create([
|
||||
'site_id' => $siteId,
|
||||
'global_topic_id' => (int) $globalTopicId,
|
||||
'name' => $globalTopic['name'],
|
||||
'description' => $globalTopic['description'] ?? '',
|
||||
'is_active' => 1,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->flash('success', 'Strona została dodana.');
|
||||
$this->redirect("/sites/{$siteId}/edit");
|
||||
}
|
||||
|
||||
public function edit(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();
|
||||
$assignedGlobalIds = array_filter(array_column($topics, 'global_topic_id'));
|
||||
|
||||
$this->view('sites/edit', [
|
||||
'site' => $site,
|
||||
'topics' => $topics,
|
||||
'globalTopics' => $globalTopics,
|
||||
'assignedGlobalIds' => $assignedGlobalIds,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(string $id): void
|
||||
{
|
||||
Auth::requireLogin();
|
||||
|
||||
$validator = new Validator();
|
||||
$validator
|
||||
->required('name', $this->input('name'), 'Nazwa')
|
||||
->required('url', $this->input('url'), 'URL')
|
||||
->url('url', $this->input('url'), 'URL')
|
||||
->required('api_user', $this->input('api_user'), 'Użytkownik API')
|
||||
->required('api_token', $this->input('api_token'), 'Token API');
|
||||
|
||||
if (!$validator->isValid()) {
|
||||
$this->flash('danger', $validator->getFirstError());
|
||||
$this->redirect("/sites/{$id}/edit");
|
||||
return;
|
||||
}
|
||||
|
||||
Site::update((int) $id, [
|
||||
'name' => $this->input('name'),
|
||||
'url' => rtrim($this->input('url'), '/'),
|
||||
'api_user' => $this->input('api_user'),
|
||||
'api_token' => $this->input('api_token'),
|
||||
'publish_interval_days' => (int) ($this->input('publish_interval_days', 3)),
|
||||
'is_active' => $this->input('is_active') ? 1 : 0,
|
||||
'is_multisite' => $this->input('is_multisite') ? 1 : 0,
|
||||
]);
|
||||
|
||||
$this->flash('success', 'Strona została zaktualizowana.');
|
||||
$this->redirect('/sites');
|
||||
}
|
||||
|
||||
public function destroy(string $id): void
|
||||
{
|
||||
Auth::requireLogin();
|
||||
|
||||
Site::delete((int) $id);
|
||||
|
||||
$this->flash('success', 'Strona została usunięta.');
|
||||
$this->redirect('/sites');
|
||||
}
|
||||
|
||||
public function testConnection(string $id): void
|
||||
{
|
||||
Auth::requireLogin();
|
||||
|
||||
$site = Site::find((int) $id);
|
||||
if (!$site) {
|
||||
$this->json(['success' => false, 'message' => 'Strona nie znaleziona.']);
|
||||
return;
|
||||
}
|
||||
|
||||
$wp = new WordPressService();
|
||||
$result = $wp->testConnection($site);
|
||||
|
||||
$this->json($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user