1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front\Content; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\ContentCategory; |
6
|
|
|
use Apps\Model\Front\Content\EntityContentRead; |
7
|
|
|
use Apps\Model\Front\Content\EntityContentSearch; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\View; |
10
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
11
|
|
|
use Ffcms\Core\Network\Request; |
12
|
|
|
use Ffcms\Core\Network\Response; |
13
|
|
|
use Apps\ActiveRecord\Content as ContentEntity; |
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
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function read() |
32
|
|
|
{ |
33
|
|
|
// get raw path without controller-action |
34
|
|
|
$rawPath = $this->request->getPathWithoutControllerAction(); |
35
|
|
|
$arrayPath = explode('/', $rawPath); |
36
|
|
|
// get category and content item path as string |
37
|
|
|
$contentPath = array_pop($arrayPath); |
38
|
|
|
$categoryPath = implode('/', $arrayPath); |
39
|
|
|
|
40
|
|
|
// try to find category object by string pathway |
41
|
|
|
$categoryRecord = ContentCategory::getByPath($categoryPath); |
42
|
|
|
|
43
|
|
|
// if no categories are available for this path - throw exception |
44
|
|
|
if ($categoryRecord === null || $categoryRecord->count() < 1) { |
45
|
|
|
throw new NotFoundException(__('Page not found')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// try to find content entity record |
49
|
|
|
$contentRecord = ContentEntity::where('path', '=', $contentPath) |
50
|
|
|
->where('category_id', '=', $categoryRecord->id); |
51
|
|
|
$trash = false; |
52
|
|
|
|
53
|
|
|
// if no entity is founded for this path lets try to find on trashed |
54
|
|
|
if ($contentRecord === null || $contentRecord->count() !== 1) { |
55
|
|
|
// check if user can access to content list on admin panel |
56
|
|
|
if (!App::$User->isAuth() || !App::$User->identity()->role->can('Admin/Content/Index')) { |
57
|
|
|
throw new NotFoundException(); |
58
|
|
|
} |
59
|
|
|
// lets try to find in trashed |
60
|
|
|
$contentRecord->withTrashed(); |
61
|
|
|
// no way, lets throw exception |
62
|
|
|
if ($contentRecord->count() !== 1) { |
63
|
|
|
throw new NotFoundException(); |
64
|
|
|
} |
65
|
|
|
// set trashed marker for this item |
66
|
|
|
$trash = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// lets init entity model for content transfer to view |
70
|
|
|
$model = new EntityContentRead($categoryRecord, $contentRecord->first()); |
|
|
|
|
71
|
|
|
$search = null; |
72
|
|
|
// check if similar search is enabled for item category |
73
|
|
|
if ((int)$model->getCategory()->getProperty('showSimilar') === 1 && $trash === false) { |
74
|
|
|
$search = new EntityContentSearch($model->title, $model->id, $model->getCategory()->id); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// define read event |
78
|
|
|
App::$Event->run(static::EVENT_CONTENT_READ, [ |
79
|
|
|
'model' => $model |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
// render view output |
83
|
|
|
return $this->view->render('read', [ |
84
|
|
|
'model' => $model, |
85
|
|
|
'search' => $search, |
86
|
|
|
'trash' => $trash, |
87
|
|
|
'configs' => $this->getConfigs() |
|
|
|
|
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: