|
1
|
|
|
<?php namespace Anomaly\Streams\Platform\Entry; |
|
2
|
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; |
|
4
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
|
5
|
|
|
use Anomaly\Streams\Platform\Model\EloquentPresenter; |
|
6
|
|
|
use Anomaly\Streams\Platform\Support\Value; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class EntryPresenter |
|
10
|
|
|
* |
|
11
|
|
|
* @link http://pyrocms.com/ |
|
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
13
|
|
|
* @author Ryan Thompson <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class EntryPresenter extends EloquentPresenter |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The resource object. |
|
20
|
|
|
* This is for IDE hinting. |
|
21
|
|
|
* |
|
22
|
|
|
* @var EntryInterface|EloquentModel |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $object; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Return the date string for created at. |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function createdAtDate() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->object->created_at |
|
34
|
|
|
->setTimezone(config('app.timezone')) |
|
35
|
|
|
->format(config('streams.date_format')); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Return the datetime string for created at. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function createdAtDatetime() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->object->created_at |
|
46
|
|
|
->setTimezone(config('app.timezone')) |
|
47
|
|
|
->format(config('streams.date_format') . ' ' . config('streams.time_format')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return the date string for updated at. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function updatedAtDate() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->object->updated_at |
|
58
|
|
|
->setTimezone(config('app.timezone')) |
|
59
|
|
|
->format(config('streams.date_format')); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return the datetime string for updated at. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function updatedAtDatetime() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->object->updated_at |
|
70
|
|
|
->setTimezone(config('app.timezone')) |
|
71
|
|
|
->format(config('streams.date_format') . ' ' . config('streams.time_format')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return a label. |
|
76
|
|
|
* |
|
77
|
|
|
* @param $text |
|
78
|
|
|
* @param string $context |
|
79
|
|
|
* @param string $size |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function label($text = null, $context = null, $size = null) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$text) { |
|
85
|
|
|
$text = $this->object->getTitleName(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!$context) { |
|
|
|
|
|
|
89
|
|
|
$context = 'default'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!$size) { |
|
|
|
|
|
|
93
|
|
|
$size = 'sm'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/* @var Value $value */ |
|
97
|
|
|
$value = app(Value::class); |
|
98
|
|
|
|
|
99
|
|
|
$text = $value->make($text, $this->object); |
|
100
|
|
|
|
|
101
|
|
|
if (trans()->has($text) && is_string(trans($text))) { |
|
|
|
|
|
|
102
|
|
|
$text = trans($text); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return '<span class="label label-' . $context . ' label-' . $size . '">' . $text . '</span>'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return the edit URL. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function editUrl() |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
return url( |
|
116
|
|
|
implode( |
|
117
|
|
|
'/', |
|
118
|
|
|
array_unique( |
|
119
|
|
|
array_filter( |
|
120
|
|
|
[ |
|
121
|
|
|
'admin', |
|
122
|
|
|
$this->object->getStreamNamespace(), |
|
|
|
|
|
|
123
|
|
|
$this->object->getStreamSlug(), |
|
|
|
|
|
|
124
|
|
|
'edit', |
|
125
|
|
|
$this->object->getId(), |
|
126
|
|
|
] |
|
127
|
|
|
) |
|
128
|
|
|
) |
|
129
|
|
|
) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Return the edit link. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function editLink() |
|
139
|
|
|
{ |
|
140
|
|
|
return app('html')->link($this->editUrl(), $this->object->{$this->object->getTitleName()}); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Return the view URL. |
|
145
|
|
|
* |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
public function viewUrl() |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
return url( |
|
151
|
|
|
implode( |
|
152
|
|
|
'/', |
|
153
|
|
|
array_unique( |
|
154
|
|
|
array_filter( |
|
155
|
|
|
[ |
|
156
|
|
|
'admin', |
|
157
|
|
|
$this->object->getStreamNamespace(), |
|
|
|
|
|
|
158
|
|
|
$this->object->getStreamSlug(), |
|
|
|
|
|
|
159
|
|
|
'view', |
|
160
|
|
|
$this->object->getId(), |
|
161
|
|
|
] |
|
162
|
|
|
) |
|
163
|
|
|
) |
|
164
|
|
|
) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Return the view link. |
|
170
|
|
|
* |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
public function viewLink() |
|
174
|
|
|
{ |
|
175
|
|
|
return app('html')->link($this->viewUrl(), $this->object->{$this->object->getTitleName()}); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* When accessing a property of a decorated entry |
|
180
|
|
|
* object first check to see if the key represents |
|
181
|
|
|
* a streams field. If it does then return the field |
|
182
|
|
|
* type's presenter object. Otherwise handle normally. |
|
183
|
|
|
* |
|
184
|
|
|
* @param $key |
|
185
|
|
|
* @return mixed |
|
186
|
|
|
*/ |
|
187
|
|
|
public function __get($key) |
|
188
|
|
|
{ |
|
189
|
|
|
if ($assignment = $this->object->getAssignment($key)) { |
|
|
|
|
|
|
190
|
|
|
$type = $assignment->getFieldType(); |
|
191
|
|
|
|
|
192
|
|
|
if ($assignment->isTranslatable() && $locale = config('app.locale')) { |
|
193
|
|
|
$entry = $this->object->translateOrDefault($locale); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
$type->setLocale($locale); |
|
196
|
|
|
} else { |
|
197
|
|
|
$entry = $this->object; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$type->setEntry($entry); |
|
201
|
|
|
|
|
202
|
|
|
if (method_exists($type, 'getRelation')) { |
|
203
|
|
|
return $type->decorate($entry->getRelationValue(camel_case($key))); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$type->setValue($entry->getFieldValue($key)); |
|
207
|
|
|
|
|
208
|
|
|
return $type->getPresenter(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $this->__getDecorator()->decorate(parent::__get($key)); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: