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
|
|
|
* Return all visible projects. |
30
|
|
|
* |
31
|
|
|
* @param bool $group If true, group projects by 'type'. |
32
|
|
|
* @param bool $order If true, order projects by 'order'. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Support\Collection |
35
|
|
|
*/ |
36
|
|
|
public static function allVisible($group = true, $order = true) |
37
|
|
|
{ |
38
|
|
|
$query = static::where('visible', true); |
39
|
|
|
|
40
|
|
|
return static::orderAndGroupQuery($query, $group, $order); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return all hidden projects. |
45
|
|
|
* |
46
|
|
|
* @param bool $group If true, group projects by 'type'. |
47
|
|
|
* @param bool $order If true, order projects by 'order'. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Support\Collection |
50
|
|
|
*/ |
51
|
|
|
public static function allHidden($group = true, $order = true) |
52
|
|
|
{ |
53
|
|
|
$query = static::where('visible', false); |
54
|
|
|
|
55
|
|
|
return static::orderAndGroupQuery($query, $group, $order); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return all projects grouped by 'type'. |
60
|
|
|
* |
61
|
|
|
* @param bool $order If true, order projects by 'order'. |
62
|
|
|
* |
63
|
|
|
* @return \Illuminate\Support\Collection |
64
|
|
|
*/ |
65
|
|
|
public static function allGrouped($order = true) |
66
|
|
|
{ |
67
|
|
|
$query = static::query(); |
68
|
|
|
|
69
|
|
|
return static::orderAndGroupQuery($query, true, $order); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return all projects ordered by 'order'. |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Support\Collection |
76
|
|
|
*/ |
77
|
|
|
public static function allOrdered() |
78
|
|
|
{ |
79
|
|
|
$query = static::query(); |
80
|
|
|
|
81
|
|
|
return static::orderAndGroupQuery($query, false, true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get all projects with given block name. |
86
|
|
|
* |
87
|
|
|
* @param string $blockName Name of block. |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Support\Collection |
90
|
|
|
*/ |
91
|
|
|
public static function hasBlockNamed($blockName) |
92
|
|
|
{ |
93
|
|
|
return static::hasRelationshipNamed('projects', 'text_blocks', $blockName); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get all projects with given image name. |
98
|
|
|
* |
99
|
|
|
* @param string $imageName Name of image. |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Support\Collection |
102
|
|
|
*/ |
103
|
|
|
public static function hasImageNamed($imageName) |
104
|
|
|
{ |
105
|
|
|
return static::hasRelationshipNamed('projects', 'images', $imageName); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get all projects with given link name. |
110
|
|
|
* |
111
|
|
|
* @param string $linkName Name of link. |
112
|
|
|
* |
113
|
|
|
* @return \Illuminate\Support\Collection |
114
|
|
|
*/ |
115
|
|
|
public static function hasLinkNamed($linkName) |
116
|
|
|
{ |
117
|
|
|
return static::hasRelationshipNamed('projects', 'links', $linkName); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the project id. |
122
|
|
|
* |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
|
|
public function id() |
126
|
|
|
{ |
127
|
|
|
return $this->id; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return the project name. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function name() |
136
|
|
|
{ |
137
|
|
|
return $this->name; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return the project type. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function type() |
146
|
|
|
{ |
147
|
|
|
return $this->type; |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return the project slug. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function slug() |
156
|
|
|
{ |
157
|
|
|
return $this->slug; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return the project order value. |
162
|
|
|
* |
163
|
|
|
* @return int |
164
|
|
|
*/ |
165
|
|
|
public function order() |
166
|
|
|
{ |
167
|
|
|
return $this->order; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get formatted text of block with project name or first block. |
172
|
|
|
* |
173
|
|
|
* @return \Larafolio\Models\TextBlock |
174
|
|
|
*/ |
175
|
|
|
public function getProjectBlock() |
176
|
|
|
{ |
177
|
|
|
$block = $this->block($this->name()); |
178
|
|
|
|
179
|
|
|
if ($block) { |
180
|
|
|
return $block; |
181
|
|
|
} elseif ($this->hasBlocks()) { |
182
|
|
|
return $this->blocks()->first(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get formatted text of block with project name or first block. |
188
|
|
|
* |
189
|
|
|
* @param bool $formatted If true, return formatted text. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getProjectBlockText($formatted = true) |
194
|
|
|
{ |
195
|
|
|
$block = $this->getProjectBlock(); |
196
|
|
|
|
197
|
|
|
if ($block && $formatted) { |
198
|
|
|
return $block->formattedText(); |
199
|
|
|
} elseif ($block) { |
200
|
|
|
return $block->text(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $block; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get url of small image with project name or first image in collection. |
208
|
|
|
* |
209
|
|
|
* @return \Larafolio\Models\Image |
210
|
|
|
*/ |
211
|
|
|
public function getProjectImage() |
212
|
|
|
{ |
213
|
|
|
$projectImage = $this->image($this->name()); |
214
|
|
|
|
215
|
|
|
if ($projectImage) { |
216
|
|
|
return $projectImage; |
217
|
|
|
} elseif ($this->hasImages()) { |
218
|
|
|
return $this->images()->first(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get url of small image with project name or first image in collection. |
224
|
|
|
* |
225
|
|
|
* @param string $size The size of the image, name of image cache filter. |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getProjectImageUrl($size = 'small') |
230
|
|
|
{ |
231
|
|
|
$projectImage = $this->getProjectImage(); |
232
|
|
|
|
233
|
|
|
if ($projectImage) { |
234
|
|
|
return $projectImage->{$size}(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $projectImage; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
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.