|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Content; |
|
6
|
|
|
use Apps\ActiveRecord\ContentCategory; |
|
7
|
|
|
use Ffcms\Core\App; |
|
8
|
|
|
use Ffcms\Core\Arch\Model; |
|
9
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
|
10
|
|
|
use Ffcms\Core\Helper\Date; |
|
11
|
|
|
use Ffcms\Core\Helper\FileSystem\Directory; |
|
12
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
|
13
|
|
|
use Ffcms\Core\Helper\Simplify; |
|
14
|
|
|
use Ffcms\Core\Helper\Type\Any; |
|
15
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
16
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
17
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class EntityContentRead. Prepare record object data to display. |
|
21
|
|
|
* @package Apps\Model\Front\Content |
|
22
|
|
|
*/ |
|
23
|
|
|
class EntityContentRead extends Model |
|
24
|
|
|
{ |
|
25
|
|
|
public $id; |
|
26
|
|
|
public $title; |
|
27
|
|
|
public $path; |
|
28
|
|
|
public $text; |
|
29
|
|
|
public $display; |
|
30
|
|
|
public $createDate; |
|
31
|
|
|
public $editDate; |
|
32
|
|
|
public $catName; |
|
33
|
|
|
public $catPath; |
|
34
|
|
|
public $authorId; |
|
35
|
|
|
public $authorName; |
|
36
|
|
|
public $views; |
|
37
|
|
|
public $catNesting = []; |
|
38
|
|
|
public $source; |
|
39
|
|
|
public $posterThumb; |
|
40
|
|
|
public $posterFull; |
|
41
|
|
|
public $rating; |
|
42
|
|
|
public $canRate; |
|
43
|
|
|
|
|
44
|
|
|
public $metaTitle; |
|
45
|
|
|
public $metaDescription; |
|
46
|
|
|
public $metaKeywords; |
|
47
|
|
|
|
|
48
|
|
|
// gallery image key-value array as thumb->full |
|
49
|
|
|
public $galleryItems; |
|
50
|
|
|
|
|
51
|
|
|
// private ActiveRecord relation objects |
|
52
|
|
|
private $_category; |
|
53
|
|
|
private $_content; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* EntityContentRead constructor. Pass active record objects |
|
57
|
|
|
* @param ContentCategory $category |
|
58
|
|
|
* @param Content $content |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(ContentCategory $category, Content $content) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->_category = $category; |
|
63
|
|
|
$this->_content = $content; |
|
64
|
|
|
parent::__construct(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Prepare model attributes from passed objects |
|
69
|
|
|
* @throws ForbiddenException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function before() |
|
72
|
|
|
{ |
|
73
|
|
|
// set class attributes from ActiveRecord objects |
|
74
|
|
|
$this->setAttributes(); |
|
75
|
|
|
$this->parseAttributes(); |
|
76
|
|
|
// build category nesting sorted array |
|
77
|
|
|
$this->expandCategoryNesting(); |
|
78
|
|
|
// set gallery thumbnail & image if exist |
|
79
|
|
|
$this->prepareGallery(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set class attributes from Content and ContentCategory objects |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
private function setAttributes(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->id = $this->_content->id; |
|
89
|
|
|
$this->title = $this->_content->getLocaled('title'); |
|
90
|
|
|
$this->text = $this->_content->getLocaled('text'); |
|
91
|
|
|
$this->display = (bool)$this->_content->display; |
|
92
|
|
|
|
|
93
|
|
|
$this->metaTitle = $this->_content->getLocaled('meta_title'); |
|
94
|
|
|
$this->metaDescription = $this->_content->getLocaled('meta_description'); |
|
95
|
|
|
$tmpKeywords = $this->_content->getLocaled('meta_keywords'); |
|
96
|
|
|
$this->metaKeywords = explode(',', $tmpKeywords); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
// set content date, category data |
|
99
|
|
|
$this->createDate = Date::humanize($this->_content->created_at); |
|
100
|
|
|
$this->catName = $this->_category->getLocaled('title'); |
|
101
|
|
|
$this->catPath = $this->_category->path; |
|
102
|
|
|
|
|
103
|
|
|
// set author user info |
|
104
|
|
|
if (App::$User->isExist($this->_content->author_id)) { |
|
105
|
|
|
$this->authorId = $this->_content->author_id; |
|
106
|
|
|
$this->authorName = Simplify::parseUserNick($this->authorId); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->source = $this->_content->source; |
|
110
|
|
|
$this->views = $this->_content->views+1; |
|
111
|
|
|
$this->rating = $this->_content->rating; |
|
112
|
|
|
|
|
113
|
|
|
// update views count |
|
114
|
|
|
$this->_content->views += 1; |
|
115
|
|
|
$this->_content->save(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Parse attribute by conditions and apply results |
|
120
|
|
|
* @throws ForbiddenException |
|
121
|
|
|
*/ |
|
122
|
|
|
private function parseAttributes(): void |
|
123
|
|
|
{ |
|
124
|
|
|
// check if title and text are exists |
|
125
|
|
|
if (Str::length($this->title) < 1 || Str::length($this->text) < 1) { |
|
126
|
|
|
throw new ForbiddenException('Content of this page is empty!'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// check if meta title is exist or set default title value |
|
130
|
|
|
if (Any::isEmpty($this->metaTitle)) { |
|
131
|
|
|
$this->metaTitle = $this->title; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$ignoredRate = App::$Session->get('content.rate.ignore'); |
|
135
|
|
|
$this->canRate = true; |
|
136
|
|
|
if (Any::isArray($ignoredRate) && Arr::in((string)$this->id, $ignoredRate)) { |
|
137
|
|
|
$this->canRate = false; |
|
138
|
|
|
} |
|
139
|
|
|
if (!App::$User->isAuth()) { |
|
140
|
|
|
$this->canRate = false; |
|
141
|
|
|
} elseif ($this->authorId === App::$User->identity()->getId()) { |
|
142
|
|
|
$this->canRate = false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Prepare gallery items, poster and thumb |
|
148
|
|
|
*/ |
|
149
|
|
|
private function prepareGallery() |
|
150
|
|
|
{ |
|
151
|
|
|
// get gallery images and poster data |
|
152
|
|
|
$galleryPath = '/upload/gallery/' . $this->_content->id; |
|
153
|
|
|
// check if gallery folder is exist |
|
154
|
|
|
if (Directory::exist($galleryPath)) { |
|
155
|
|
|
$originImages = File::listFiles($galleryPath . '/orig/', ['.jpg', '.png', '.gif', '.jpeg', '.bmp', '.webp'], true); |
|
156
|
|
|
// generate poster data |
|
157
|
|
|
if (Arr::in($this->_content->poster, $originImages)) { |
|
158
|
|
|
// original poster |
|
159
|
|
|
$posterName = $this->_content->poster; |
|
160
|
|
|
$this->posterFull = $galleryPath . '/orig/' . $posterName; |
|
161
|
|
|
if (!File::exist($this->posterFull)) { |
|
162
|
|
|
$this->posterFull = null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// thumb poster |
|
166
|
|
|
$posterSplit = explode('.', $posterName); |
|
167
|
|
|
array_pop($posterSplit); |
|
168
|
|
|
$posterCleanName = implode('.', $posterSplit); |
|
169
|
|
|
$this->posterThumb = $galleryPath . '/thumb/' . $posterCleanName . '.jpg'; |
|
170
|
|
|
if (!File::exist($this->posterThumb)) { |
|
171
|
|
|
$this->posterThumb = null; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// generate full gallery |
|
176
|
|
|
foreach ($originImages as $image) { |
|
177
|
|
|
$imageSplit = explode('.', $image); |
|
178
|
|
|
array_pop($imageSplit); |
|
179
|
|
|
$imageClearName = implode('.', $imageSplit); |
|
180
|
|
|
// skip image used in poster |
|
181
|
|
|
if (Str::startsWith($imageClearName, $this->_content->poster)) { |
|
182
|
|
|
continue; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$thumbPath = $galleryPath . '/thumb/' . $imageClearName . '.jpg'; |
|
186
|
|
|
if (File::exist($thumbPath)) { |
|
187
|
|
|
$this->galleryItems[$thumbPath] = $galleryPath . '/orig/' . $image; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Expand category nesting array |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
|
|
private function expandCategoryNesting(): void |
|
198
|
|
|
{ |
|
199
|
|
|
// check for dependence, add '' for general cat, ex: general/depend1/depend2/.../depend-n |
|
200
|
|
|
$catNestingArray = Arr::merge([0 => ''], explode('/', $this->catPath)); |
|
201
|
|
|
if ($catNestingArray > 1) { |
|
202
|
|
|
// latest element its a current nesting level, lets cleanup it |
|
203
|
|
|
array_pop($catNestingArray); |
|
204
|
|
|
$catNestingPath = null; |
|
205
|
|
|
foreach ($catNestingArray as $cPath) { |
|
206
|
|
|
$catNestingPath .= $cPath; |
|
207
|
|
|
|
|
208
|
|
|
// try to find category by path in db |
|
209
|
|
|
$record = ContentCategory::getByPath($catNestingPath); |
|
210
|
|
|
if ($record !== null && $record->count() > 0) { |
|
211
|
|
|
// if founded - add to nesting data |
|
212
|
|
|
$this->catNesting[] = [ |
|
213
|
|
|
'name' => $record->getLocaled('title'), |
|
214
|
|
|
'path' => $record->path |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
if (!Str::likeEmpty($catNestingPath)) { |
|
218
|
|
|
$catNestingPath .= '/'; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$this->catNesting[] = [ |
|
224
|
|
|
'name' => $this->catName, |
|
225
|
|
|
'path' => $this->catPath |
|
226
|
|
|
]; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get content record obj |
|
231
|
|
|
* @return Content |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getRecord() |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->_content; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Get category relation of this content |
|
240
|
|
|
* @return ContentCategory |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getCategory() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->_category; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|