Skip to content

Commit 46ec354

Browse files
committed
First commit.
Signed-off-by: Joshua Parker <[email protected]>
0 parents  commit 46ec354

26 files changed

+3191
-0
lines changed

Controllers/VaporThemeController.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Theme\Vapor\Controllers;
6+
7+
use App\Application\Devflow;
8+
use App\Domain\Content\Model\Content;
9+
use App\Infrastructure\Services\Options;
10+
use App\Infrastructure\Services\Paginator;
11+
use App\Infrastructure\Services\UserAuth;
12+
use Codefy\CommandBus\Exceptions\CommandPropertyNotFoundException;
13+
use Codefy\Framework\Http\BaseController;
14+
use Codefy\QueryBus\UnresolvableQueryHandlerException;
15+
use Psr\Container\ContainerExceptionInterface;
16+
use Psr\Container\NotFoundExceptionInterface;
17+
use Psr\Http\Message\ResponseInterface;
18+
use Psr\SimpleCache\InvalidArgumentException;
19+
use Qubus\Config\ConfigContainer;
20+
use Qubus\Exception\Data\TypeException;
21+
use Qubus\Exception\Exception;
22+
use Qubus\Http\ServerRequest;
23+
use Qubus\Http\Session\SessionException;
24+
use Qubus\Http\Session\SessionService;
25+
use Qubus\Routing\Router;
26+
use Qubus\View\Renderer;
27+
use ReflectionException;
28+
29+
use function App\Shared\Helpers\admin_url;
30+
use function App\Shared\Helpers\get_all_content_types;
31+
use function App\Shared\Helpers\get_all_content_with_filters;
32+
use function App\Shared\Helpers\get_content_by;
33+
use function App\Shared\Helpers\sort_list;
34+
use function count;
35+
use function Qubus\Security\Helpers\t__;
36+
use function Qubus\Support\Helpers\is_false__;
37+
38+
class VaporThemeController extends BaseController
39+
{
40+
public function __construct(
41+
SessionService $sessionService,
42+
Router $router,
43+
protected ConfigContainer $configContainer,
44+
protected UserAuth $user,
45+
?Renderer $view = null
46+
) {
47+
parent::__construct($sessionService, $router, $view);
48+
}
49+
50+
/**
51+
* @return string|null
52+
* @throws CommandPropertyNotFoundException
53+
* @throws ContainerExceptionInterface
54+
* @throws Exception
55+
* @throws InvalidArgumentException
56+
* @throws NotFoundExceptionInterface
57+
* @throws ReflectionException
58+
* @throws UnresolvableQueryHandlerException
59+
*/
60+
public function index(): ?string
61+
{
62+
$options = Options::factory()->read(optionKey: 'vapor_theme_settings');
63+
$content = get_all_content_with_filters(
64+
contentTypeSlug: $options['vapor_content_type'] ?? '',
65+
limit: (int) Options::factory()->read(optionKey: 'content_per_page') ?? 6,
66+
offset: isset($options['vapor_content_offset']) ? (int) $options['vapor_content_offset'] : 0,
67+
status: $options['vapor_content_status'] ?? 'all',
68+
);
69+
70+
if (empty(array_filter($content))) {
71+
return $this->view->render(
72+
'theme::Vapor/views/no-content',
73+
['title' => t__(msgid: '404: Not Found', domain: 'vapor-theme'), 'single' => false]
74+
);
75+
}
76+
77+
$sort = sort_list(
78+
$content,
79+
$options['vapor_content_orderby'] ?? 'published',
80+
$options['vapor_content_order'] ?? 'desc',
81+
);
82+
return $this->view->render(
83+
'theme::Vapor/views/index',
84+
[
85+
'title' => Options::factory()->read(optionKey: 'sitename'),
86+
'content' => $sort,
87+
'paginator' => new Paginator(
88+
totalItems: (int) count(array_filter($sort)),
89+
itemsPerPage: (int) Options::factory()->read(optionKey: 'content_per_page'),
90+
currentPage: 0,
91+
),
92+
'single' => false,
93+
]
94+
);
95+
}
96+
97+
/**
98+
* @param ServerRequest $request
99+
* @return string|ResponseInterface
100+
* @throws ContainerExceptionInterface
101+
* @throws Exception
102+
* @throws InvalidArgumentException
103+
* @throws NotFoundExceptionInterface
104+
* @throws ReflectionException
105+
* @throws TypeException
106+
* @throws UnresolvableQueryHandlerException
107+
* @throws SessionException
108+
*/
109+
public function show(ServerRequest $request): string|ResponseInterface
110+
{
111+
if (false === $this->user->can(permissionName: 'manage:themes', request: $request)) {
112+
Devflow::inst()::$APP->flash->error(
113+
message: t__(msgid: 'Access denied.', domain: 'vapor-theme')
114+
);
115+
return $this->redirect(admin_url());
116+
}
117+
118+
if ($request->getMethod() === 'POST') {
119+
$options = [
120+
'vapor_content_type' => $request->get('vapor_content_type') ?? null,
121+
'vapor_content_offset' => $request->get('vapor_content_offset') ?? 0,
122+
'vapor_content_status' => $request->get('vapor_content_status') ?? 'all',
123+
'vapor_content_orderby' => $request->get('vapor_content_orderby') ?? null,
124+
'vapor_content_order' => $request->get('vapor_content_order') ?? 'desc',
125+
];
126+
127+
$update = Options::factory()->update('vapor_theme_settings', $options);
128+
129+
if ($update === false) {
130+
Devflow::inst()::$APP->flash->error(
131+
message: t__(msgid: 'Update error.', domain: 'vapor-theme')
132+
);
133+
} else {
134+
Devflow::inst()::$APP->flash->success(
135+
message: t__(msgid: 'Updated successfully.', domain: 'vapor-theme')
136+
);
137+
}
138+
139+
return $this->redirect($request->getServerParams()['HTTP_REFERER']);
140+
}
141+
142+
return $this->view->render(
143+
'theme::Vapor/views/show',
144+
[
145+
'types' => get_all_content_types()
146+
]
147+
);
148+
}
149+
150+
/**
151+
* @param string $contentSlug
152+
* @return string|null
153+
* @throws ContainerExceptionInterface
154+
* @throws Exception
155+
* @throws InvalidArgumentException
156+
* @throws NotFoundExceptionInterface
157+
* @throws ReflectionException
158+
*/
159+
public function single(string $contentSlug): ?string
160+
{
161+
/** @var Content $content */
162+
$content = get_content_by('slug', $contentSlug);
163+
if (is_false__($content)) {
164+
return $this->view->render(
165+
'theme::Vapor/views/no-content',
166+
['title' => t__(msgid: '404: Not Found', domain: 'vapor-theme'), 'single' => false]
167+
);
168+
}
169+
170+
return $this->view->render(
171+
'theme::Vapor/views/single',
172+
[
173+
'title' => $content->title,
174+
'content' => $content,
175+
'single' => true,
176+
]
177+
);
178+
}
179+
}

0 commit comments

Comments
 (0)