1 | <?php |
||
2 | |||
3 | namespace Apps\Controller\Front\Content; |
||
4 | |||
5 | use Apps\ActiveRecord\Content as ContentEntity; |
||
6 | use Apps\ActiveRecord\ContentCategory; |
||
7 | use Apps\Model\Front\Content\EntityContentRead; |
||
8 | use Apps\Model\Front\Content\EntityContentSearch; |
||
9 | use Ffcms\Core\App; |
||
10 | use Ffcms\Core\Arch\View; |
||
11 | use Ffcms\Core\Exception\NotFoundException; |
||
12 | use Ffcms\Core\Network\Request; |
||
13 | use Ffcms\Core\Network\Response; |
||
14 | |||
15 | /** |
||
16 | * Trait ActionRead |
||
17 | * @package Apps\Controller\Front\Content |
||
18 | * @property View $view |
||
19 | * @property Request $request |
||
20 | * @property Response $response |
||
21 | * @method array getConfigs |
||
22 | */ |
||
23 | trait ActionRead |
||
24 | { |
||
25 | /** |
||
26 | * Show content item |
||
27 | * @throws NotFoundException |
||
28 | * @return string |
||
29 | */ |
||
30 | public function read(): ?string |
||
31 | { |
||
32 | // get raw path without controller-action |
||
33 | $rawPath = $this->request->getPathWithoutControllerAction(); |
||
34 | $arrayPath = explode('/', $rawPath); |
||
35 | // get category and content item path as string |
||
36 | $contentPath = array_pop($arrayPath); |
||
37 | $categoryPath = implode('/', $arrayPath); |
||
38 | |||
39 | // try to find category object by string pathway |
||
40 | $categoryRecord = ContentCategory::getByPath($categoryPath); |
||
41 | |||
42 | // if no categories are available for this path - throw exception |
||
43 | if (!$categoryRecord || $categoryRecord->count() < 1) { |
||
44 | throw new NotFoundException(__('Page not found')); |
||
45 | } |
||
46 | |||
47 | // try to find content entity record |
||
48 | $contentRecord = ContentEntity::where('path', '=', $contentPath) |
||
49 | ->where('category_id', '=', $categoryRecord->id); |
||
50 | $trash = false; |
||
51 | |||
52 | // if no entity is founded for this path lets try to find on trashed |
||
53 | if (!$contentRecord || $contentRecord->count() !== 1) { |
||
54 | // check if user can access to content list on admin panel |
||
55 | if (!App::$User->isAuth() || !App::$User->identity()->role->can('Admin/Content/Index')) { |
||
56 | throw new NotFoundException(__('Content not found!')); |
||
57 | } |
||
58 | // lets try to find in trashed |
||
59 | $contentRecord->withTrashed(); |
||
60 | // no way, lets throw exception |
||
61 | if ($contentRecord->count() !== 1) { |
||
62 | throw new NotFoundException(); |
||
63 | } |
||
64 | // set trashed marker for this item |
||
65 | $trash = true; |
||
66 | } |
||
67 | |||
68 | // lets init entity model for content transfer to view |
||
69 | $model = new EntityContentRead($categoryRecord, $contentRecord->first()); |
||
70 | $search = null; |
||
71 | // check if similar search is enabled for item category |
||
72 | if ((bool)$model->getCategory()->getProperty('showSimilar') && !$trash) { |
||
73 | $search = new EntityContentSearch($model->title, $model->id, $model->getCategory()->id); |
||
74 | } |
||
75 | |||
76 | // define read event |
||
77 | App::$Event->run(static::EVENT_CONTENT_READ, [ |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
78 | 'model' => $model |
||
79 | ]); |
||
80 | |||
81 | // render view output |
||
82 | return $this->view->render('content/read', [ |
||
83 | 'model' => $model, |
||
84 | 'search' => $search, |
||
85 | 'trash' => $trash, |
||
86 | 'configs' => $this->getConfigs() |
||
87 | ]); |
||
88 | } |
||
89 | } |
||
90 |