1
|
|
|
<?php namespace Anomaly\Streams\Platform\Entry; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EntryTranslationsModel |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class EntryTranslationsModel extends EloquentModel |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This model uses timestamps. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
public $timestamps = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Cache minutes. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
//protected $cacheMinutes = 99999; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Boot the model. |
33
|
|
|
*/ |
34
|
|
|
protected static function boot() |
35
|
|
|
{ |
36
|
|
|
self::observe(app(substr(__CLASS__, 0, -5) . 'Observer')); |
37
|
|
|
|
38
|
|
|
parent::boot(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return the last modified datetime. |
43
|
|
|
* |
44
|
|
|
* @return Carbon |
45
|
|
|
*/ |
46
|
|
|
public function lastModified() |
47
|
|
|
{ |
48
|
|
|
return $this->updated_at ?: $this->created_at; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the locale. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getLocale() |
57
|
|
|
{ |
58
|
|
|
return $this->getAttributeFromArray($this->getLocaleKey()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get an attribute. |
63
|
|
|
* |
64
|
|
|
* @param string $key |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function getAttribute($key) |
68
|
|
|
{ |
69
|
|
|
if ($key === 'locale') { |
70
|
|
|
return parent::getAttribute('locale'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!$parent = $this->getParent()) { |
74
|
|
|
return $this->attributes[$key]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* @var AssignmentInterface $assignment */ |
78
|
|
|
$assignment = $parent->getAssignment($key); |
79
|
|
|
|
80
|
|
|
if (!$assignment) { |
81
|
|
|
return parent::getAttribute($key); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$type = $assignment->getFieldType($this); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$type->setEntry($this); |
87
|
|
|
$type->setLocale($this->locale); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$accessor = $type->getAccessor(); |
90
|
|
|
$modifier = $type->getModifier(); |
91
|
|
|
|
92
|
|
|
return $modifier->restore($accessor->get($key)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the parent. |
97
|
|
|
* |
98
|
|
|
* @return EntryModel |
99
|
|
|
*/ |
100
|
|
|
public function getParent() |
101
|
|
|
{ |
102
|
|
|
return isset($this->relations['parent']) ? $this->relations['parent'] : null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the attribute. |
107
|
|
|
* |
108
|
|
|
* @param string $key |
109
|
|
|
* @param mixed $value |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function setAttribute($key, $value) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if (!$parent = $this->getParent()) { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/* @var AssignmentInterface $assignment */ |
118
|
|
|
$assignment = $parent->getAssignment($key); |
119
|
|
|
|
120
|
|
|
if (!$assignment) { |
121
|
|
|
parent::setAttribute($key, $value); |
122
|
|
|
|
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$type = $assignment->getFieldType($this); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$type->setEntry($this); |
129
|
|
|
$type->setLocale($this->locale); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$accessor = $type->getAccessor(); |
132
|
|
|
$modifier = $type->getModifier(); |
133
|
|
|
|
134
|
|
|
$accessor->set($modifier->modify($value)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Fire field type events. |
139
|
|
|
* |
140
|
|
|
* @param $trigger |
141
|
|
|
* @param array $payload |
142
|
|
|
*/ |
143
|
|
|
public function fireFieldTypeEvents($trigger, $payload = []) |
144
|
|
|
{ |
145
|
|
|
if (!$parent = $this->getParent()) { |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$assignments = $parent->getAssignments(); |
150
|
|
|
|
151
|
|
|
/* @var AssignmentInterface $assignment */ |
152
|
|
|
foreach ($assignments->translatable() as $assignment) { |
153
|
|
|
$fieldType = $assignment->getFieldType(); |
154
|
|
|
|
155
|
|
|
$fieldType->setValue($parent->getFieldValue($assignment->getFieldSlug())); |
156
|
|
|
|
157
|
|
|
$fieldType->setEntry($this); |
158
|
|
|
$fieldType->setLocale($this->locale); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$fieldType->fire($trigger, array_merge(compact('fieldType', 'entry'), $payload)); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Truncate the translation's table. |
166
|
|
|
* |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
|
|
public function truncate() |
170
|
|
|
{ |
171
|
|
|
return $this->newQuery()->truncate(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Let the parent handle calls if they don't exist here. |
176
|
|
|
* |
177
|
|
|
* @param string $name |
178
|
|
|
* @param array $arguments |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
public function __call($name, $arguments) |
182
|
|
|
{ |
183
|
|
|
return call_user_func_array([$this->getParent(), $name], $arguments); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the attribute from the parent |
188
|
|
|
* if it does not exist here. |
189
|
|
|
* |
190
|
|
|
* @param string $key |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
|
|
public function __get($key) |
194
|
|
|
{ |
195
|
|
|
$value = parent::__get($key); |
196
|
|
|
|
197
|
|
|
if (!$value && $parent = $this->getParent()) { |
198
|
|
|
return $parent->{$key}; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $value; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.