1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafolio\Models; |
4
|
|
|
|
5
|
|
|
use Larafolio\Helpers\Sluggable; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
|
8
|
|
|
class Project extends HasContent |
9
|
|
|
{ |
10
|
|
|
use Sluggable, SoftDeletes; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The table associated with the model. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $table = 'projects'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The attributes that are mass assignable. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $fillable = [ |
25
|
|
|
'name', 'slug', 'type', 'visible', 'order', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Properties to always eager load. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $with = ['blocks', 'images', 'links']; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The attributes that should be casted to native types. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $casts = [ |
41
|
|
|
'visible' => 'boolean', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Fields that are dates. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the route key for the model. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getRouteKeyName() |
57
|
|
|
{ |
58
|
|
|
return 'slug'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return all visible projects. |
63
|
|
|
* |
64
|
|
|
* @param bool $group If true, group projects by 'type'. |
65
|
|
|
* @param bool $order If true, order projects by 'order'. |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Support\Collection |
68
|
|
|
*/ |
69
|
|
|
public static function allVisible($group = true, $order = true) |
70
|
|
|
{ |
71
|
|
|
$query = static::where('visible', true); |
72
|
|
|
|
73
|
|
|
return static::orderAndGroupQuery($query, $group, $order); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return all hidden projects. |
78
|
|
|
* |
79
|
|
|
* @param bool $group If true, group projects by 'type'. |
80
|
|
|
* @param bool $order If true, order projects by 'order'. |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Support\Collection |
83
|
|
|
*/ |
84
|
|
|
public static function allHidden($group = true, $order = true) |
85
|
|
|
{ |
86
|
|
|
$query = static::where('visible', false); |
87
|
|
|
|
88
|
|
|
return static::orderAndGroupQuery($query, $group, $order); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return all projects grouped by 'type'. |
93
|
|
|
* |
94
|
|
|
* @param bool $order If true, order projects by 'order'. |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\Support\Collection |
97
|
|
|
*/ |
98
|
|
|
public static function allGrouped($order = true) |
99
|
|
|
{ |
100
|
|
|
$query = static::query(); |
101
|
|
|
|
102
|
|
|
return static::orderAndGroupQuery($query, true, $order); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return all projects ordered by 'order'. |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Support\Collection |
109
|
|
|
*/ |
110
|
|
|
public static function allOrdered() |
111
|
|
|
{ |
112
|
|
|
$query = static::query(); |
113
|
|
|
|
114
|
|
|
return static::orderAndGroupQuery($query, false, true); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get all projects with given block name. |
119
|
|
|
* |
120
|
|
|
* @param string $blockName Name of block. |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Support\Collection |
123
|
|
|
*/ |
124
|
|
|
public static function hasBlockNamed($blockName) |
125
|
|
|
{ |
126
|
|
|
return static::hasRelationshipNamed(Project::class, 'projects', 'text_blocks', $blockName); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get all projects with given image name. |
131
|
|
|
* |
132
|
|
|
* @param string $imageName Name of image. |
133
|
|
|
* |
134
|
|
|
* @return \Illuminate\Support\Collection |
135
|
|
|
*/ |
136
|
|
|
public static function hasImageNamed($imageName) |
137
|
|
|
{ |
138
|
|
|
return static::hasRelationshipNamed(Project::class, 'projects', 'images', $imageName); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get all projects with given link name. |
143
|
|
|
* |
144
|
|
|
* @param string $linkName Name of link. |
145
|
|
|
* |
146
|
|
|
* @return \Illuminate\Support\Collection |
147
|
|
|
*/ |
148
|
|
|
public static function hasLinkNamed($linkName) |
149
|
|
|
{ |
150
|
|
|
return static::hasRelationshipNamed(Project::class, 'projects', 'links', $linkName); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return the project id. |
155
|
|
|
* |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function id() |
159
|
|
|
{ |
160
|
|
|
return $this->id; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return the project name. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function name() |
169
|
|
|
{ |
170
|
|
|
return $this->name; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return the project type. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function type() |
179
|
|
|
{ |
180
|
|
|
return $this->type; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return the project slug. |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function slug() |
189
|
|
|
{ |
190
|
|
|
return $this->slug; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return the project order value. |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
|
|
public function order() |
199
|
|
|
{ |
200
|
|
|
return $this->order; |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get formatted text of block with project name or first block. |
205
|
|
|
* |
206
|
|
|
* @return Larafolio\Models\TextBlock |
207
|
|
|
*/ |
208
|
|
|
public function getProjectBlock() |
209
|
|
|
{ |
210
|
|
|
$block = $this->block($this->name()); |
211
|
|
|
|
212
|
|
|
if ($block) { |
213
|
|
|
return $block; |
|
|
|
|
214
|
|
|
} elseif ($this->hasBlocks()) { |
215
|
|
|
return $this->blocks()->first(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get formatted text of block with project name or first block. |
221
|
|
|
* |
222
|
|
|
* @param bool $formatted If true, return formatted text. |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getProjectBlockText($formatted = true) |
227
|
|
|
{ |
228
|
|
|
$block = $this->getProjectBlock(); |
229
|
|
|
|
230
|
|
|
if ($block && $formatted) { |
231
|
|
|
return $block->formattedText(); |
232
|
|
|
} elseif ($block) { |
233
|
|
|
return $block->text(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $block; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get url of small image with project name or first image in collection. |
241
|
|
|
* |
242
|
|
|
* @return Larafolio\Models\Image |
243
|
|
|
*/ |
244
|
|
|
public function getProjectImage() |
245
|
|
|
{ |
246
|
|
|
$projectImage = $this->image($this->name()); |
247
|
|
|
|
248
|
|
|
if ($projectImage) { |
249
|
|
|
return $projectImage; |
|
|
|
|
250
|
|
|
} elseif ($this->hasImages()) { |
251
|
|
|
return $this->images()->first(); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get url of small image with project name or first image in collection. |
257
|
|
|
* |
258
|
|
|
* @param string $size The size of the image, name of image cache filter. |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function getProjectImageUrl($size = 'small') |
263
|
|
|
{ |
264
|
|
|
$projectImage = $this->getProjectImage(); |
265
|
|
|
|
266
|
|
|
if ($projectImage) { |
267
|
|
|
return $projectImage->{$size}(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $projectImage; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.