|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2011 - 2014 Oleksandr Torosh (http://wezoom.net) |
|
4
|
|
|
* @author Oleksandr Torosh <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Publication\Model; |
|
8
|
|
|
|
|
9
|
|
|
use Application\Mvc\Helper\CmsCache; |
|
10
|
|
|
use Application\Mvc\Model\Model; |
|
11
|
|
|
use Phalcon\DI; |
|
12
|
|
|
use Phalcon\Validation; |
|
13
|
|
|
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator; |
|
14
|
|
|
|
|
15
|
|
|
class Type extends Model |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
public function getSource() |
|
19
|
|
|
{ |
|
20
|
|
|
return "publication_type"; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected $translateModel = 'Publication\Model\Translate\TypeTranslate'; |
|
24
|
|
|
|
|
25
|
|
|
public $id; |
|
26
|
|
|
public $title; // translate |
|
27
|
|
|
public $slug; |
|
28
|
|
|
public $limit = 10; |
|
29
|
|
|
public $display_date; |
|
30
|
|
|
public $format = 'list'; |
|
31
|
|
|
public $head_title; // translate |
|
32
|
|
|
public $meta_description; // translate |
|
33
|
|
|
public $meta_keywords; // translate |
|
34
|
|
|
public $seo_text; // translate |
|
35
|
|
|
|
|
36
|
|
|
public static $formats = [ |
|
37
|
|
|
'list' => 'List', |
|
38
|
|
|
'grid' => 'Grid', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
public function initialize() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->hasMany('id', $this->translateModel, 'foreign_id'); // translate |
|
44
|
|
|
|
|
45
|
|
|
$this->hasMany('id', 'Publication\Model\Publication', 'type_id', [ |
|
46
|
|
|
'alias' => 'publications' |
|
47
|
|
|
]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
public function validation() |
|
51
|
|
|
{ |
|
52
|
|
|
$validator = new Validation(); |
|
53
|
|
|
$validator->add('slug', new UniquenessValidator( |
|
54
|
|
|
[ |
|
55
|
|
|
"model" => $this, |
|
56
|
|
|
"message" => "Тип публикаций с таким URL раздела = '" . $this->slug . "' уже существует" |
|
57
|
|
|
] |
|
58
|
|
|
)); |
|
59
|
|
|
return $this->validate($validator); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function afterUpdate() |
|
63
|
|
|
{ |
|
64
|
|
|
parent::afterUpdate(); |
|
65
|
|
|
|
|
66
|
|
|
$cache = $this->getDi()->get('cache'); |
|
|
|
|
|
|
67
|
|
|
$cache->delete(self::cacheSlugKey($this->getSlug())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function afterSave() |
|
71
|
|
|
{ |
|
72
|
|
|
CmsCache::getInstance()->save('publication_types', $this->buildCmsTypesCache()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function afterDelete() |
|
76
|
|
|
{ |
|
77
|
|
|
CmsCache::getInstance()->save('publication_types', $this->buildCmsTypesCache()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function buildCmsTypesCache() |
|
81
|
|
|
{ |
|
82
|
|
|
$types = self::find(); |
|
|
|
|
|
|
83
|
|
|
$save = []; |
|
84
|
|
|
foreach ($types as $type) { |
|
|
|
|
|
|
85
|
|
|
$save[$type->getSlug()] = [ |
|
86
|
|
|
'id' => $type->getId(), |
|
87
|
|
|
'slug' => $type->getSlug(), |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
return $save; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function updateFields($data) |
|
94
|
|
|
{ |
|
95
|
|
|
if (!$this->getSlug()) { |
|
96
|
|
|
$this->setSlug(Transliterator::slugify($data['title'])); |
|
97
|
|
|
} |
|
98
|
|
|
if (!$this->getTitle()) { |
|
99
|
|
|
$this->setTitle($data['title']); |
|
100
|
|
|
} |
|
101
|
|
|
if (!$this->getHeadTitle()) { |
|
102
|
|
|
$this->setHeadTitle($data['title']); |
|
103
|
|
|
} |
|
104
|
|
|
if (isset($data['display_date'])) { |
|
105
|
|
|
$this->setDisplayDate(1); |
|
106
|
|
|
} else { |
|
107
|
|
|
$this->setDisplayDate(0); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public static function types() |
|
112
|
|
|
{ |
|
113
|
|
|
return CmsCache::getInstance()->get('publication_types'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public static function cachedListArray($params = []) |
|
117
|
|
|
{ |
|
118
|
|
|
$cache = DI::getDefault()->get('cache'); |
|
|
|
|
|
|
119
|
|
|
$key = self::cacheListKey($params); |
|
120
|
|
|
$list = $cache->get($key); |
|
121
|
|
|
if (!$list) { |
|
122
|
|
|
$result = self::find(); |
|
|
|
|
|
|
123
|
|
|
$list = []; |
|
124
|
|
|
foreach ($result as $el) { |
|
|
|
|
|
|
125
|
|
View Code Duplication |
if (isset($params['value']) && $params['value']) { |
|
126
|
|
|
$value = $el->{$params['value']}; |
|
127
|
|
|
} else { |
|
128
|
|
|
$value = $el->getTitle(); |
|
129
|
|
|
} |
|
130
|
|
View Code Duplication |
if (isset($params['key']) && $params['key']) { |
|
131
|
|
|
$list[$el->{$params['key']}] = $value; |
|
132
|
|
|
} else { |
|
133
|
|
|
$list[$el->getSlug()] = $value; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
$cache->save($key, $list, 120); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $list; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
View Code Duplication |
public static function getCachedBySlug($slug) |
|
143
|
|
|
{ |
|
144
|
|
|
$data = self::findFirst([ |
|
|
|
|
|
|
145
|
|
|
'slug = :slug:', |
|
146
|
|
|
'bind' => [ |
|
147
|
|
|
'slug' => $slug, |
|
148
|
|
|
], |
|
149
|
|
|
'cache' => [ |
|
150
|
|
|
'key' => self::cacheSlugKey($slug), |
|
151
|
|
|
'lifetime' => 86400, |
|
152
|
|
|
] |
|
153
|
|
|
]); |
|
154
|
|
|
|
|
155
|
|
|
return $data; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public static function cacheSlugKey($slug) |
|
159
|
|
|
{ |
|
160
|
|
|
return HOST_HASH . md5('Publication\Model\Type; slug = ' . $slug); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public static function cacheListKey($params) |
|
164
|
|
|
{ |
|
165
|
|
|
return HOST_HASH . md5('Publication\Model\Type; list; ' . serialize($params)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param mixed $title |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setTitle($title) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->setMLVariable('title', $title); |
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getTitle() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->getMLVariable('title'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $format |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setFormat($format) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->format = $format; |
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getFormat() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->format; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function getFormatTitle() |
|
203
|
|
|
{ |
|
204
|
|
|
if (array_key_exists($this->format, self::$formats)) { |
|
205
|
|
|
return self::$formats[$this->format]; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param mixed $head_title |
|
211
|
|
|
*/ |
|
212
|
|
|
public function setHeadTitle($head_title) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->setMLVariable('head_title', $head_title); |
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return mixed |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getHeadTitle() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->getMLVariable('head_title'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param mixed $id |
|
228
|
|
|
*/ |
|
229
|
|
|
public function setId($id) |
|
230
|
|
|
{ |
|
231
|
|
|
$this->id = $id; |
|
232
|
|
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return mixed |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getId() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->id; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param mixed $limit |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setLimit($limit) |
|
247
|
|
|
{ |
|
248
|
|
|
$this->limit = $limit; |
|
249
|
|
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return mixed |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getLimit() |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->limit; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param mixed $meta_description |
|
262
|
|
|
*/ |
|
263
|
|
|
public function setMetaDescription($meta_description) |
|
264
|
|
|
{ |
|
265
|
|
|
$this->setMLVariable('meta_description', $meta_description); |
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @return mixed |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getMetaDescription() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->getMLVariable('meta_description'); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param mixed $meta_keywords |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setMetaKeywords($meta_keywords) |
|
281
|
|
|
{ |
|
282
|
|
|
$this->setMLVariable('meta_keywords', $meta_keywords); |
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @return mixed |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getMetaKeywords() |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->getMLVariable('meta_keywords'); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param mixed $seo_text |
|
296
|
|
|
*/ |
|
297
|
|
|
public function setSeoText($seo_text) |
|
298
|
|
|
{ |
|
299
|
|
|
$this->setMLVariable('seo_text', $seo_text); |
|
300
|
|
|
return $this; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @return mixed |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getSeoText() |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->getMLVariable('seo_text'); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @param mixed $slug |
|
313
|
|
|
*/ |
|
314
|
|
|
public function setSlug($slug) |
|
315
|
|
|
{ |
|
316
|
|
|
$this->slug = $slug; |
|
317
|
|
|
return $this; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @return mixed |
|
322
|
|
|
*/ |
|
323
|
|
|
public function getSlug() |
|
324
|
|
|
{ |
|
325
|
|
|
return $this->slug; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param mixed $display_date |
|
330
|
|
|
*/ |
|
331
|
|
|
public function setDisplayDate($display_date) |
|
332
|
|
|
{ |
|
333
|
|
|
$this->display_date = $display_date; |
|
334
|
|
|
return $this; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @return mixed |
|
339
|
|
|
*/ |
|
340
|
|
|
public function getDisplayDate() |
|
341
|
|
|
{ |
|
342
|
|
|
return $this->display_date; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
|
|
346
|
|
|
} |
|
347
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.