1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App as AppMain; |
6
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
7
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
use Ffcms\Core\Traits\SearchableTrait; |
10
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Content. Active record object for content items with relation to category active record |
14
|
|
|
* @package Apps\ActiveRecord |
15
|
|
|
* @property int $id |
16
|
|
|
* @property string $title |
17
|
|
|
* @property string $text |
18
|
|
|
* @property string $path |
19
|
|
|
* @property int $category_id |
20
|
|
|
* @property int $author_id |
21
|
|
|
* @property string $poster |
22
|
|
|
* @property int $display |
23
|
|
|
* @property string $meta_title |
24
|
|
|
* @property string $meta_keywords |
25
|
|
|
* @property string $meta_description |
26
|
|
|
* @property int $views |
27
|
|
|
* @property int $rating |
28
|
|
|
* @property string $source |
29
|
|
|
* @property string $comment_hash |
30
|
|
|
* @property string $created_at |
31
|
|
|
* @property string $updated_at |
32
|
|
|
* @property string|null $deleted_at |
33
|
|
|
*/ |
34
|
|
|
class Content extends ActiveModel |
35
|
|
|
{ |
36
|
|
|
use SoftDeletes, SearchableTrait; |
37
|
|
|
|
38
|
|
|
/** @var string $title */ |
39
|
|
|
|
40
|
|
|
protected $searchable = [ |
41
|
|
|
'columns' => [ |
42
|
|
|
'title' => 3, |
43
|
|
|
'text' => 1 |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get category relation of this content id |
49
|
|
|
* @return \Apps\ActiveRecord\ContentCategory|null |
50
|
|
|
*/ |
51
|
|
|
public function getCategory() |
52
|
|
|
{ |
53
|
|
|
return ContentCategory::getById($this->category_id); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get content_rating relation one-to-many |
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
59
|
|
|
*/ |
60
|
|
|
public function getRating() |
61
|
|
|
{ |
62
|
|
|
return $this->hasMany('Apps\\ActiveRecord\\ContentRating', 'content_id'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get item path URI - category/item |
67
|
|
|
* @return null|string |
68
|
|
|
*/ |
69
|
|
|
public function getPath() |
70
|
|
|
{ |
71
|
|
|
if ($this->path === null) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// get category pathway |
76
|
|
|
$path = $this->getCategory()->path; |
77
|
|
|
if (!Str::likeEmpty($path)) { |
78
|
|
|
$path .= '/'; |
79
|
|
|
} |
80
|
|
|
// add item path |
81
|
|
|
$path .= $this->path; |
82
|
|
|
|
83
|
|
|
return $path; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get poster URI like /upload/gallery/1/orig/9ds2jd1.png |
88
|
|
|
* @return null|string |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function getPosterUri() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$pName = $this->poster; |
93
|
|
|
// check if poster is defined |
94
|
|
|
if ($pName === null || Str::likeEmpty($pName)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// build path and check is file exists on disk |
99
|
|
|
$path = '/upload/gallery/' . $this->id . '/orig/' . $pName; |
100
|
|
|
if (!File::exist($path)) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $path; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get poster thumbnail uri |
109
|
|
|
* @return null|string |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function getPosterThumbUri() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$pName = $this->poster; |
114
|
|
|
if ($pName === null || Str::likeEmpty($pName)) { |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// remove extension, thumbs always in jpeg ;D |
119
|
|
|
$pName = Str::cleanExtension($pName); |
120
|
|
|
$path = '/upload/gallery/' . $this->id . '/thumb/' . $pName . '.jpg'; |
121
|
|
|
|
122
|
|
|
if (!File::exist($path)) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $path; |
127
|
|
|
} |
128
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.