-
Notifications
You must be signed in to change notification settings - Fork 0
/
Content.php
182 lines (166 loc) · 4.56 KB
/
Content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php namespace Gzero\Entity;
use Gzero\Core\Exception;
use Gzero\Entity\Presenter\ContentPresenter;
use Robbo\Presenter\PresentableInterface;
use Robbo\Presenter\Robbo;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* This file is part of the GZERO CMS package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Class Content
*
* @package Gzero\Entity
* @author Adrian Skierniewski <[email protected]>
* @copyright Copyright (c) 2014, Adrian Skierniewski
*/
class Content extends BaseTree implements PresentableInterface, Uploadable {
use SoftDeletes;
/**
* @var array
*/
protected $fillable = [
'type',
'theme',
'path',
'weight',
'rating',
'visits',
'is_on_home',
'is_comment_allowed',
'is_promoted',
'is_sticky',
'is_active',
'published_at'
];
/**
* @var array
*/
protected $attributes = [
'is_on_home' => false,
'is_comment_allowed' => false,
'is_promoted' => false,
'is_sticky' => false,
'is_active' => false
];
/**
* @var array
*/
protected $dates = ['published_at', 'deleted_at'];
/**
* Get Content url in specified language.
* WARNING: This function use LAZY LOADING to get this information
*
* @param string $langCode Lang code
*
* @return mixed
* @throws Exception
*/
public function getUrl($langCode)
{
$routeTranslation = $this->route->translations->filter(
function ($translation) use ($langCode) {
return $translation->lang_code == $langCode;
}
)->first();
if (empty($routeTranslation->url)) {
throw new Exception("No route [$langCode] translation found for Content id: " . $this->getKey());
}
return $routeTranslation->url;
}
/**
* Content type relation
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function type()
{
return $this->belongsTo(ContentType::class, 'name', 'type');
}
/**
* Polymorphic relation with route
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function route()
{
return $this->morphOne(Route::class, 'routable');
}
/**
* Translation one to many relation
*
* @param bool $active Only active translations
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function translations($active = true)
{
if ($active) {
return $this->hasMany(ContentTranslation::class)->where('is_active', '=', 1);
}
return $this->hasMany(ContentTranslation::class);
}
/**
* Get all of the files for the content.
*
* @param bool $active Only active file
*
* @return \Illuminate\Database\Eloquent\Relations\morphToMany
*/
public function files($active = true)
{
if ($active) {
return $this->morphToMany(File::class, 'uploadable')->where('is_active', '=', 1)->withPivot('weight')
->withTimestamps();
}
return $this->morphToMany(File::class, 'uploadable')->withPivot('weight')->withTimestamps();
}
/**
* Content author relation
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function author()
{
return $this->belongsTo(User::class, 'author_id', 'id');
}
/**
* Content thumb relation
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function thumb()
{
return $this->belongsTo(File::class, 'thumb_id', 'id');
}
/**
* Return a created presenter.
*
* @return \Robbo\Presenter\Presenter
*/
public function getPresenter()
{
return new ContentPresenter($this);
}
/**
* Return true if content can be shown on frontend
*
* @return bool
*/
public function canBeShown()
{
return $this->is_active;
}
/**
* Find all trashed descendants for specific node with this node as root
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function findDescendantsWithTrashed()
{
return static::withTrashed()->where($this->getTreeColumn('path'), 'LIKE', $this->{$this->getTreeColumn('path')} . '%')
->orderBy($this->getTreeColumn('level'), 'ASC');
}
}