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