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\FileSystem\Directory; |
11
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
12
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
13
|
|
|
use Ffcms\Core\Helper\Date; |
14
|
|
|
use Ffcms\Core\Helper\Serialize; |
15
|
|
|
use Ffcms\Core\Helper\Type\Str; |
16
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
17
|
|
|
|
18
|
|
|
class EntityContentRead extends Model |
19
|
|
|
{ |
20
|
|
|
public $id; |
21
|
|
|
public $title; |
22
|
|
|
public $path; |
23
|
|
|
public $text; |
24
|
|
|
public $createDate; |
25
|
|
|
public $editDate; |
26
|
|
|
public $catName; |
27
|
|
|
public $catPath; |
28
|
|
|
public $authorId; |
29
|
|
|
public $authorName; |
30
|
|
|
public $views; |
31
|
|
|
public $catNesting = []; |
32
|
|
|
public $source; |
33
|
|
|
public $posterThumb; |
34
|
|
|
public $posterFull; |
35
|
|
|
public $rating; |
36
|
|
|
public $canRate; |
37
|
|
|
|
38
|
|
|
public $metaTitle; |
39
|
|
|
public $metaDescription; |
40
|
|
|
public $metaKeywords; |
41
|
|
|
|
42
|
|
|
// gallery image key-value array as thumb->full |
43
|
|
|
public $galleryItems; |
44
|
|
|
|
45
|
|
|
// private activerecord relation objects |
46
|
|
|
private $_category; |
47
|
|
|
private $_content; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Pass active record objects |
51
|
|
|
* @param ContentCategory $category |
52
|
|
|
* @param Content $content |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ContentCategory $category, Content $content) |
55
|
|
|
{ |
56
|
|
|
$this->_category = $category; |
57
|
|
|
$this->_content = $content; |
58
|
|
|
parent::__construct(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Prepare model attributes from passed objects |
63
|
|
|
* @throws ForbiddenException |
64
|
|
|
*/ |
65
|
|
|
public function before() |
66
|
|
|
{ |
67
|
|
|
$this->id = $this->_content->id; |
68
|
|
|
$this->title = Serialize::getDecodeLocale($this->_content->title); |
69
|
|
|
$this->text = Serialize::getDecodeLocale($this->_content->text); |
70
|
|
|
|
71
|
|
|
// check if title and text are exists |
72
|
|
|
if (Str::length($this->title) < 1 || Str::length($this->text) < 1) { |
|
|
|
|
73
|
|
|
throw new ForbiddenException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// get meta data |
77
|
|
|
$this->metaTitle = Serialize::getDecodeLocale($this->_content->meta_title); |
78
|
|
|
if (Str::likeEmpty($this->metaTitle)) { |
|
|
|
|
79
|
|
|
$this->metaTitle = $this->title; |
80
|
|
|
} |
81
|
|
|
$this->metaDescription = Serialize::getDecodeLocale($this->_content->meta_description); |
82
|
|
|
$tmpKeywords = Serialize::getDecodeLocale($this->_content->meta_keywords); |
83
|
|
|
$this->metaKeywords = explode(',', $tmpKeywords); |
84
|
|
|
|
85
|
|
|
// set content date, category data |
86
|
|
|
$this->createDate = Date::humanize($this->_content->created_at); |
|
|
|
|
87
|
|
|
$this->catName = Serialize::getDecodeLocale($this->_category->title); |
88
|
|
|
$this->catPath = $this->_category->path; |
89
|
|
|
|
90
|
|
|
// set user data |
91
|
|
|
if (App::$User->isExist($this->_content->author_id)) { |
92
|
|
|
$this->authorId = $this->_content->author_id; |
93
|
|
|
$profile = App::$User->identity($this->authorId)->getProfile(); |
94
|
|
|
$this->authorName = $profile->getNickname(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->source = $this->_content->source; |
98
|
|
|
$this->views = $this->_content->views+1; |
99
|
|
|
// check for dependence, add '' for general cat, ex: general/depend1/depend2/.../depend-n |
100
|
|
|
$catNestingArray = Arr::merge([0 => ''], explode('/', $this->catPath)); |
101
|
|
|
if ($catNestingArray > 1) { |
102
|
|
|
// latest element its a current nesting level, lets cleanup it |
103
|
|
|
array_pop($catNestingArray); |
104
|
|
|
$catNestingPath = null; |
105
|
|
|
foreach ($catNestingArray as $cPath) { |
106
|
|
|
$catNestingPath .= $cPath; |
107
|
|
|
|
108
|
|
|
// try to find category by path in db |
109
|
|
|
$record = ContentCategory::getByPath($catNestingPath); |
110
|
|
|
if ($record !== null && $record->count() > 0) { |
111
|
|
|
// if founded - add to nesting data |
112
|
|
|
$this->catNesting[] = [ |
113
|
|
|
'name' => Serialize::getDecodeLocale($record->title), |
114
|
|
|
'path' => $record->path |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
if (!Str::likeEmpty($catNestingPath)) { |
118
|
|
|
$catNestingPath .= '/'; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// build array of category nesting level |
124
|
|
|
$this->catNesting[] = [ |
125
|
|
|
'name' => $this->catName, |
126
|
|
|
'path' => $this->catPath |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
// get gallery images and poster data |
130
|
|
|
$galleryPath = '/upload/gallery/' . $this->_content->id; |
131
|
|
|
// check if gallery folder is exist |
132
|
|
|
if (Directory::exist($galleryPath)) { |
133
|
|
|
$originImages = File::listFiles($galleryPath . '/orig/', ['.jpg', '.png', '.gif', '.jpeg', '.bmp', '.webp'], true); |
134
|
|
|
// generate poster data |
135
|
|
|
if (Arr::in($this->_content->poster, $originImages)) { |
136
|
|
|
// original poster |
137
|
|
|
$posterName = $this->_content->poster; |
138
|
|
|
$this->posterFull = $galleryPath . '/orig/' . $posterName; |
139
|
|
|
if (!File::exist($this->posterFull)) { |
140
|
|
|
$this->posterFull = null; |
141
|
|
|
} |
142
|
|
|
// thumb poster |
143
|
|
|
$posterSplit = explode('.', $posterName); |
144
|
|
|
array_pop($posterSplit); |
145
|
|
|
$posterCleanName = implode('.', $posterSplit); |
146
|
|
|
$this->posterThumb = $galleryPath . '/thumb/' . $posterCleanName . '.jpg'; |
147
|
|
|
if (!File::exist($this->posterThumb)) { |
148
|
|
|
$this->posterThumb = null; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// generate full gallery |
153
|
|
|
foreach ($originImages as $image) { |
154
|
|
|
$imageSplit = explode('.', $image); |
155
|
|
|
array_pop($imageSplit); |
156
|
|
|
$imageClearName = implode('.', $imageSplit); |
157
|
|
|
// skip image used in poster |
158
|
|
|
if (Str::startsWith($imageClearName, $this->_content->poster)) { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
$thumbPath = $galleryPath . '/thumb/' . $imageClearName . '.jpg'; |
162
|
|
|
if (File::exist($thumbPath)) { |
163
|
|
|
$this->galleryItems[$thumbPath] = $galleryPath . '/orig/' . $image; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// set rating data |
169
|
|
|
$this->rating = $this->_content->rating; |
170
|
|
|
$ignoredRate = App::$Session->get('content.rate.ignore'); |
171
|
|
|
$this->canRate = true; |
172
|
|
|
if (Obj::isArray($ignoredRate) && Arr::in((string)$this->id, $ignoredRate)) { |
173
|
|
|
$this->canRate = false; |
174
|
|
|
} |
175
|
|
|
if (!App::$User->isAuth()) { |
176
|
|
|
$this->canRate = false; |
177
|
|
|
} elseif ($this->authorId === App::$User->identity()->getId()) { |
178
|
|
|
$this->canRate = false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// update views count |
182
|
|
|
$this->_content->views += 1; |
183
|
|
|
$this->_content->save(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get category relation of this content |
188
|
|
|
* @return ContentCategory |
189
|
|
|
*/ |
190
|
|
|
public function getCategory() |
191
|
|
|
{ |
192
|
|
|
return $this->_category; |
193
|
|
|
} |
194
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.