1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Publication\Model; |
4
|
|
|
|
5
|
|
|
use Application\Mvc\Model\Model; |
6
|
|
|
use Phalcon\Mvc\Model\Validator\Uniqueness; |
7
|
|
|
use Phalcon\Mvc\Model\Validator\PresenceOf; |
8
|
|
|
use Application\Localization\Transliterator; |
9
|
|
|
|
10
|
|
|
class Publication extends Model |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public function getSource() |
14
|
|
|
{ |
15
|
|
|
return "publication"; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected $translateModel = 'Publication\Model\Translate\PublicationTranslate'; // translate |
19
|
|
|
|
20
|
|
|
public function initialize() |
21
|
|
|
{ |
22
|
|
|
$this->hasMany('id', $this->translateModel, 'foreign_id'); // translate |
23
|
|
|
|
24
|
|
|
$this->belongsTo('type_id', 'Publication\Model\Type', 'id', [ |
25
|
|
|
'alias' => 'type' |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private $id; |
30
|
|
|
private $type_id; |
31
|
|
|
private $slug; |
32
|
|
|
private $created_at; |
33
|
|
|
private $updated_at; |
34
|
|
|
private $date; |
35
|
|
|
private $preview_src; |
36
|
|
|
private $preview_inner; |
37
|
|
|
|
38
|
|
|
protected $title; |
39
|
|
|
protected $text; |
40
|
|
|
protected $meta_title; |
41
|
|
|
protected $meta_description; |
42
|
|
|
protected $meta_keywords; |
43
|
|
|
|
44
|
|
|
public function beforeCreate() |
45
|
|
|
{ |
46
|
|
|
$this->created_at = date("Y-m-d H:i:s"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function beforeUpdate() |
50
|
|
|
{ |
51
|
|
|
$this->updated_at = date("Y-m-d H:i:s"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function afterUpdate() |
55
|
|
|
{ |
56
|
|
|
parent::afterUpdate(); |
57
|
|
|
|
58
|
|
|
$cache = $this->getDi()->get('cache'); |
59
|
|
|
|
60
|
|
|
$cache->delete(self::cacheSlugKey($this->getSlug())); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function validation() |
64
|
|
|
{ |
65
|
|
|
$this->validate(new Uniqueness( |
66
|
|
|
[ |
67
|
|
|
"field" => "slug", |
68
|
|
|
"message" => "Страница с такой транслитерацией = '" . $this->slug . "' уже существует" |
69
|
|
|
] |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
return $this->validationHasFailed() != true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function afterValidation() |
76
|
|
|
{ |
77
|
|
|
if (!$this->date) { |
78
|
|
|
$this->date = date("Y-m-d H:i:s"); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function updateFields($data) |
83
|
|
|
{ |
84
|
|
|
if (!$this->getSlug()) { |
85
|
|
|
$this->setSlug(Transliterator::slugify($data['title'])); |
86
|
|
|
} |
87
|
|
|
if (!$this->getMetaTitle()) { |
88
|
|
|
$this->setMetaTitle($data['title']); |
89
|
|
|
} |
90
|
|
|
$this->setPreviewInner(isset($data['preview_inner']) ? 1 : 0); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public static function findCachedBySlug($slug) |
94
|
|
|
{ |
95
|
|
|
$publication = self::findFirst(["slug = '$slug'", |
96
|
|
|
'cache' => [ |
97
|
|
|
'key' => self::cacheSlugKey($slug), |
98
|
|
|
'lifetime' => 60] |
99
|
|
|
]); |
100
|
|
|
return $publication; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function cacheSlugKey($slug) |
104
|
|
|
{ |
105
|
|
|
$key = HOST_HASH . md5('Publication\Model\Publication; slug = ' . $slug); |
106
|
|
|
return $key; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setCreatedAt($created_at) |
110
|
|
|
{ |
111
|
|
|
$this->created_at = $created_at; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getCreatedAt() |
116
|
|
|
{ |
117
|
|
|
return $this->created_at; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setId($id) |
121
|
|
|
{ |
122
|
|
|
$this->id = $id; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getId() |
127
|
|
|
{ |
128
|
|
|
return $this->id; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setMetaDescription($meta_description) |
132
|
|
|
{ |
133
|
|
|
$this->setMLVariable('meta_description', $meta_description); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getMetaDescription() |
138
|
|
|
{ |
139
|
|
|
return $this->getMLVariable('meta_description'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setMetaKeywords($meta_keywords) |
143
|
|
|
{ |
144
|
|
|
$this->setMLVariable('meta_keywords', $meta_keywords); |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getMetaKeywords() |
149
|
|
|
{ |
150
|
|
|
return $this->getMLVariable('meta_keywords'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setMetaTitle($meta_title) |
154
|
|
|
{ |
155
|
|
|
$this->setMLVariable('meta_title', $meta_title); |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getMetaTitle() |
160
|
|
|
{ |
161
|
|
|
return $this->getMLVariable('meta_title'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function setSlug($slug) |
165
|
|
|
{ |
166
|
|
|
$this->slug = $slug; |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getSlug() |
171
|
|
|
{ |
172
|
|
|
return $this->slug; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function setText($text) |
176
|
|
|
{ |
177
|
|
|
$this->setMLVariable('text', $text); |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getText() |
182
|
|
|
{ |
183
|
|
|
return $this->getMLVariable('text'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setTitle($title) |
187
|
|
|
{ |
188
|
|
|
$this->setMLVariable('title', $title); |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getTitle() |
193
|
|
|
{ |
194
|
|
|
return $this->getMLVariable('title'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setUpdatedAt($updated_at) |
198
|
|
|
{ |
199
|
|
|
$this->updated_at = $updated_at; |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getUpdatedAt() |
204
|
|
|
{ |
205
|
|
|
return $this->updated_at; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setDate($date) |
209
|
|
|
{ |
210
|
|
|
$this->date = $date; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function getDate($format = 'Y-m-d H:i:s') |
215
|
|
|
{ |
216
|
|
|
if ($format) { |
217
|
|
|
if ($this->date) { |
218
|
|
|
return date($format, strtotime($this->date)); |
219
|
|
|
} |
220
|
|
|
} else { |
221
|
|
|
return $this->date; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function setTypeId($type_id) |
226
|
|
|
{ |
227
|
|
|
$this->type_id = $type_id; |
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getTypeId() |
232
|
|
|
{ |
233
|
|
|
return $this->type_id; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function getTypeTitle() |
237
|
|
|
{ |
238
|
|
|
if ($this->type_id) { |
239
|
|
|
$types = Type::cachedListArray(['key' => 'id']); |
240
|
|
|
if (array_key_exists($this->type_id, $types)) { |
241
|
|
|
return $types[$this->type_id]; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
View Code Duplication |
public function getTypeSlug() |
247
|
|
|
{ |
248
|
|
|
if ($this->type_id) { |
249
|
|
|
$types = Type::cachedListArray(['key' => 'id', 'value' => 'slug']); |
250
|
|
|
if (array_key_exists($this->type_id, $types)) { |
251
|
|
|
return $types[$this->type_id]; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function setPreviewInner($preview_inner) |
257
|
|
|
{ |
258
|
|
|
$this->preview_inner = $preview_inner; |
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function getPreviewInner() |
263
|
|
|
{ |
264
|
|
|
return $this->preview_inner; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getPreviewSrc() |
268
|
|
|
{ |
269
|
|
|
return $this->preview_src; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function setPreviewSrc($preview_src) |
273
|
|
|
{ |
274
|
|
|
$this->preview_src = $preview_src; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|