diff --git a/app/modules/view/ViewModule.php b/app/modules/view/ViewModule.php new file mode 100644 index 0000000..e37befb --- /dev/null +++ b/app/modules/view/ViewModule.php @@ -0,0 +1,8 @@ +registerCSS(slug: "select2", resource: "https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css", addResourceURI: false); + $this->registerCSS( + slug: "select2", + resource: "https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css", + addResourceURI: false, + after: 'bootstrap' + ); } protected function createJS(): void { - $this->registerJS(slug: "select2", resource: "https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js", addResourceURI: false); + $this->registerJS( + slug: "select2", + resource: "https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js", + addResourceURI: false, + after: 'jquery', + ); + $this->registerJS( + slug: "slider", + resource: "/slider.js", + after: 'select2', + ); } } \ No newline at end of file diff --git a/app/themes/svo/assets/SvoLpThemeAssets.php b/app/themes/svo/assets/SvoLpThemeAssets.php index dad7c1c..2b90eef 100644 --- a/app/themes/svo/assets/SvoLpThemeAssets.php +++ b/app/themes/svo/assets/SvoLpThemeAssets.php @@ -11,6 +11,7 @@ class SvoLpThemeAssets extends Assets { $this->registerCSS(slug: "bootstrap", resource: "/css/netic/bootstrap.css"); $this->registerCSS(slug: "style", resource: "/css/netic/style.css"); + $this->registerCSS(slug: "posts", resource: "/css/netic/posts.css"); $this->registerCSS(slug: "responsive", resource: "/css/netic/responsive.css"); $this->registerCSS(slug: "mCustomScrollbar", resource: "/css/netic/jquery.mCustomScrollbar.min.css"); } diff --git a/app/themes/svo/controllers/LpController.php b/app/themes/svo/controllers/LpController.php index 5c304b2..984bdc7 100644 --- a/app/themes/svo/controllers/LpController.php +++ b/app/themes/svo/controllers/LpController.php @@ -2,13 +2,18 @@ namespace app\themes\svo\controllers; +use app\themes\svo\services\MainPageSliderService; use kernel\Controller; +use kernel\helpers\Debug; +use kernel\modules\post\models\Post; +use kernel\modules\post\service\PostService; use kernel\modules\user\service\UserService; class LpController extends Controller { protected \kernel\modules\user\models\User $user; + protected MainPageSliderService $mainPageSliderService; protected function init(): void { @@ -19,6 +24,7 @@ class LpController extends Controller $this->cgView->addVarToLayout("resources", "/resources/themes/svo/assets"); $user = UserService::getAuthUser(); + $this->mainPageSliderService = new MainPageSliderService(); if ($user){ $this->cgView->addVarToLayout("currentUser", $user); $this->user = $user; @@ -27,7 +33,14 @@ class LpController extends Controller public function actionIndex(): void { - $this->cgView->render('index.php'); + $this->cgView->render('index.php', []); + } + + public function actionPost(int $postId): void + { + $post = Post::find($postId); + + $this->cgView->render('post.php', ['post' => $post]); } } \ No newline at end of file diff --git a/app/themes/svo/controllers/SvoAdminController.php b/app/themes/svo/controllers/SvoAdminController.php index b87e69f..44b1474 100644 --- a/app/themes/svo/controllers/SvoAdminController.php +++ b/app/themes/svo/controllers/SvoAdminController.php @@ -2,7 +2,13 @@ namespace app\themes\svo\controllers; +use app\themes\svo\services\MainPageSliderService; +use app\themes\svo\services\SvoThemeService; +use JetBrains\PhpStorm\NoReturn; use kernel\AdminController; +use kernel\Flash; +use kernel\helpers\Debug; +use kernel\Request; class SvoAdminController extends AdminController { @@ -11,6 +17,7 @@ class SvoAdminController extends AdminController { parent::init(); $this->cgView->viewPath = APP_DIR . "/themes/svo/views/admin/"; + $this->cgView->addVarToLayout("svo_theme_resources", "/resources/themes/svo"); } public function actionThemeSettings(): void @@ -18,4 +25,24 @@ class SvoAdminController extends AdminController $this->cgView->render('theme_settings.php'); } + #[NoReturn] public function actionAddSlide(): void + { + $request = new Request(); + $slideId = $request->post('slide'); + + SvoThemeService::addPostSlide($slideId); + + Flash::setMessage('success', 'Слайд добавлен.'); + $this->redirect('/admin/svo-theme/settings?tab=slider', 302); + } + + #[NoReturn] public function actionDeleteSlide(int $post_id): void + { + $mainPageSlideService = new MainPageSliderService(); + $mainPageSlideService->removeSlide($post_id); + + Flash::setMessage('success', 'Слайд удален.'); + $this->redirect('/admin/svo-theme/settings?tab=slider', 302); + } + } \ No newline at end of file diff --git a/app/themes/svo/routs/svo.php b/app/themes/svo/routs/svo.php index fc5e427..c7968cf 100644 --- a/app/themes/svo/routs/svo.php +++ b/app/themes/svo/routs/svo.php @@ -6,11 +6,14 @@ use kernel\CgRouteCollector; App::$collector->filter("auth", [\app\themes\svo\middlewares\LkAuthMiddleware::class, "handler"]); App::$collector->get('/', [\app\themes\svo\controllers\LpController::class, 'actionIndex']); +App::$collector->get('/post/{postId}', [\app\themes\svo\controllers\LpController::class, 'actionPost']); App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) { App::$collector->group(["before" => "auth"], function (CGRouteCollector $router) { App::$collector->group(["prefix" => "svo-theme"], function (CGRouteCollector $router) { App::$collector->get('/settings', [\app\themes\svo\controllers\SvoAdminController::class, 'actionThemeSettings']); + App::$collector->get('/delete_slide/{post_id}', [\app\themes\svo\controllers\SvoAdminController::class, 'actionDeleteSlide']); + App::$collector->post('/add_slide', [\app\themes\svo\controllers\SvoAdminController::class, 'actionAddSlide']); }); }); }); diff --git a/app/themes/svo/services/MainPageSliderService.php b/app/themes/svo/services/MainPageSliderService.php new file mode 100644 index 0000000..a510f62 --- /dev/null +++ b/app/themes/svo/services/MainPageSliderService.php @@ -0,0 +1,90 @@ +optionService = new OptionService(); + } + + /** + * Получить список слайдов + * + * @return array + */ + public function getSlides(): array + { + $option = Option::where('key', $this->sliderKey)->first(); + + if (!$option || empty($option->value)) { + return []; + } + + $data = json_decode($option->value, true); + + return $data['ids'] ?? []; + } + + /** + * Добавить слайд + * + * @param int $slideId + * @return bool + */ + public function addSlide(int $slideId): bool + { + return App::$db->capsule::connection()->transaction(function () use ($slideId) { + $option = Option::firstOrNew(['key' => $this->sliderKey]); + + $currentValue = $option->value ? json_decode($option->value, true) : ['ids' => []]; + + if (in_array($slideId, $currentValue['ids'])) { + return true; // слайд уже есть, ничего не делаем + } + + $currentValue['ids'][] = $slideId; + $option->value = json_encode($currentValue); + + return $option->save(); + }); + } + + /** + * Удалить слайд + * + * @param int $slideId + * @return bool + */ + public function removeSlide(int $slideId): bool + { + return App::$db->capsule::connection()->transaction(function () use ($slideId) { + /** @var Option $option */ + $option = Option::where('key', $this->sliderKey)->first(); + + if (!$option || empty($option->value)) { + return false; + } + + $currentValue = json_decode($option->value, true); + + if (!in_array($slideId, $currentValue['ids'] ?? [])) { + return false; // слайда нет в списке + } + + $currentValue['ids'] = array_diff($currentValue['ids'], [$slideId]); + $option->value = json_encode($currentValue); + + return $option->save(); + }); + } +} \ No newline at end of file diff --git a/app/themes/svo/services/SvoThemeService.php b/app/themes/svo/services/SvoThemeService.php new file mode 100644 index 0000000..16da503 --- /dev/null +++ b/app/themes/svo/services/SvoThemeService.php @@ -0,0 +1,34 @@ +get(); + } + + public static function addPostSlide(int $postId) + { + $slides = json_decode(OptionService::getItem('main_news_slider'), true); + + if (!in_array($postId, $slides['ids'])) { + $slides['ids'][] = $postId; + OptionService::createOrUpdate('main_news_slider', json_encode($slides)); + } + + return $slides; + } + + + +} \ No newline at end of file diff --git a/app/themes/svo/views/admin/_slider_post_list.php b/app/themes/svo/views/admin/_slider_post_list.php new file mode 100644 index 0000000..808bcce --- /dev/null +++ b/app/themes/svo/views/admin/_slider_post_list.php @@ -0,0 +1,3 @@ + +Посты diff --git a/app/themes/svo/views/admin/theme_settings.php b/app/themes/svo/views/admin/theme_settings.php index dcc71d1..9de2dd9 100644 --- a/app/themes/svo/views/admin/theme_settings.php +++ b/app/themes/svo/views/admin/theme_settings.php @@ -1,24 +1,28 @@ registerAsset(new \app\themes\svo\assets\AdminSliderAssets($svo_theme_resources)); ?>