-
Notifications
You must be signed in to change notification settings - Fork 98
/
Post.php
executable file
·557 lines (496 loc) · 15.8 KB
/
Post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Model;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime;
use Mageplaza\Blog\Helper\Data;
use Mageplaza\Blog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Mageplaza\Blog\Model\ResourceModel\Post as PostResource;
use Mageplaza\Blog\Model\ResourceModel\Post\Collection;
use Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory as PostCollectionFactory;
use Mageplaza\Blog\Model\ResourceModel\Tag\CollectionFactory;
use Mageplaza\Blog\Model\ResourceModel\Topic\CollectionFactory as TopicCollectionFactory;
/**
* @method Post setName($name)
* @method Post setShortDescription($shortDescription)
* @method Post setPostContent($postContent)
* @method Post setImage($image)
* @method Post setViews($views)
* @method Post setEnabled($enabled)
* @method Post setUrlKey($urlKey)
* @method Post setInRss($inRss)
* @method Post setAllowComment($allowComment)
* @method Post setMetaTitle($metaTitle)
* @method Post setMetaDescription($metaDescription)
* @method Post setMetaKeywords($metaKeywords)
* @method Post setMetaRobots($metaRobots)
* @method mixed getName()
* @method mixed getPostContent()
* @method mixed getImage()
* @method mixed getViews()
* @method mixed getEnabled()
* @method mixed getUrlKey()
* @method mixed getInRss()
* @method mixed getAllowComment()
* @method mixed getMetaTitle()
* @method mixed getMetaDescription()
* @method mixed getMetaKeywords()
* @method mixed getMetaRobots()
* @method Post setCreatedAt(string $createdAt)
* @method string getCreatedAt()
* @method Post setUpdatedAt(string $updatedAt)
* @method string getUpdatedAt()
* @method Post setTagsData(array $data)
* @method Post setTopicsData(array $data)
* @method Post setProductsData(array $data)
* @method array getTagsData()
* @method array getProductsData()
* @method array getTopicsData()
* @method Post setIsChangedTagList(bool $flag)
* @method Post setIsChangedProductList(bool $flag)
* @method Post setIsChangedTopicList(bool $flag)
* @method Post setIsChangedCategoryList(bool $flag)
* @method bool getIsChangedTagList()
* @method bool getIsChangedTopicList()
* @method bool getIsChangedCategoryList()
* @method Post setAffectedTagIds(array $ids)
* @method Post setAffectedEntityIds(array $ids)
* @method Post setAffectedTopicIds(array $ids)
* @method Post setAffectedCategoryIds(array $ids)
* @method bool getAffectedTagIds()
* @method bool getAffectedTopicIds()
* @method bool getAffectedCategoryIds()
* @method array getCategoriesIds()
* @method Post setCategoriesIds(array $categoryIds)
* @method array getTagsIds()
* @method Post setTagsIds(array $tagIds)
* @method array getTopicsIds()
* @method Post setTopicsIds(array $topicIds)
*/
class Post extends AbstractModel
{
/**
* Cache tag
*
* @var string
*/
const CACHE_TAG = 'mageplaza_blog_post';
/**
* Cache tag
*
* @var string
*/
protected $_cacheTag = 'mageplaza_blog_post';
/**
* Event prefix
*
* @var string
*/
protected $_eventPrefix = 'mageplaza_blog_post';
/**
* Tag Collection
*
* @var ResourceModel\Tag\Collection
*/
public $tagCollection;
/**
* Topic Collection
*
* @var ResourceModel\Topic\Collection
*/
public $topicCollection;
/**
* Blog Category Collection
*
* @var ResourceModel\Category\Collection
*/
public $categoryCollection;
/**
* Tag Collection Factory
*
* @var CollectionFactory
*/
public $tagCollectionFactory;
/**
* Topic Collection Factory
*
* @var TopicCollectionFactory
*/
public $topicCollectionFactory;
/**
* Blog Category Collection Factory
*
* @var CategoryCollectionFactory
*/
public $categoryCollectionFactory;
/**
* Post Collection Factory
*
* @var PostCollectionFactory
*/
public $postCollectionFactory;
/**
* Related Post Collection
*
* @var Collection
*/
public $relatedPostCollection;
/**
* Previous Post Collection
*
* @var Collection
*/
public $prevPostCollection;
/**
* Next Post Collection
*
* @var Collection
*/
public $nextPostCollection;
/**
* @var DateTime
*/
public $dateTime;
/**
* @var Data
*/
public $helperData;
/**
* @var ProductCollectionFactory
*/
public $productCollectionFactory;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public $productCollection;
/**
* @var TrafficFactory
*/
protected $trafficFactory;
/**
* Post constructor.
*
* @param Context $context
* @param Registry $registry
* @param DateTime $dateTime
* @param Data $helperData
* @param TrafficFactory $trafficFactory
* @param CollectionFactory $tagCollectionFactory
* @param TopicCollectionFactory $topicCollectionFactory
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param PostCollectionFactory $postCollectionFactory
* @param ProductCollectionFactory $productCollectionFactory
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
DateTime $dateTime,
Data $helperData,
TrafficFactory $trafficFactory,
CollectionFactory $tagCollectionFactory,
TopicCollectionFactory $topicCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory,
PostCollectionFactory $postCollectionFactory,
ProductCollectionFactory $productCollectionFactory,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
$this->tagCollectionFactory = $tagCollectionFactory;
$this->topicCollectionFactory = $topicCollectionFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->postCollectionFactory = $postCollectionFactory;
$this->productCollectionFactory = $productCollectionFactory;
$this->helperData = $helperData;
$this->dateTime = $dateTime;
$this->trafficFactory = $trafficFactory;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(PostResource::class);
}
/**
* @inheritdoc
*/
public function afterSave()
{
if ($this->isObjectNew()) {
$trafficModel = $this->trafficFactory->create()
->load($this->getId(), 'post_id');
if (!$trafficModel->getId()) {
$trafficModel->setData([
'post_id' => $this->getId(),
'numbers_view' => 0
])->save();
}
}
return parent::afterSave();
}
/**
* @param bool $shorten
*
* @return mixed|string
*/
public function getShortDescription($shorten = false)
{
$shortDescription = $this->getData('short_description');
$maxLength = 200;
if ($shorten && strlen($shortDescription) > $maxLength) {
$shortDescription = substr($shortDescription, 0, $maxLength) . '...';
}
return $shortDescription;
}
/**
* @param null $store
*
* @return string
*/
public function getUrl($store = null)
{
return $this->helperData->getBlogUrl($this, Data::TYPE_POST, $store);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
/**
* get entity default values
*
* @return array
*/
public function getDefaultValues()
{
$values = [];
$values['in_rss'] = '1';
$values['enabled'] = '1';
$values['allow_comment'] = '1';
$values['store_ids'] = '1';
return $values;
}
/**
* @return ResourceModel\Tag\Collection
*/
public function getSelectedTagsCollection()
{
if ($this->tagCollection === null) {
$collection = $this->tagCollectionFactory->create();
$collection->getSelect()->join(
$this->getResource()->getTable('mageplaza_blog_post_tag'),
'main_table.tag_id=' . $this->getResource()->getTable('mageplaza_blog_post_tag') . '.tag_id AND '
. $this->getResource()->getTable('mageplaza_blog_post_tag') . '.post_id=' . $this->getId(),
['position']
)->where("main_table.enabled='1'");
$this->tagCollection = $collection;
}
return $this->tagCollection;
}
/**
* @return ResourceModel\Topic\Collection
*/
public function getSelectedTopicsCollection()
{
if ($this->topicCollection === null) {
$collection = $this->topicCollectionFactory->create();
$collection->join(
$this->getResource()->getTable('mageplaza_blog_post_topic'),
'main_table.topic_id=' . $this->getResource()->getTable('mageplaza_blog_post_topic') . '.topic_id AND '
. $this->getResource()->getTable('mageplaza_blog_post_topic') . '.post_id=' . $this->getId(),
['position']
);
$this->topicCollection = $collection;
}
return $this->topicCollection;
}
/**
* @return ResourceModel\Category\Collection
*/
public function getSelectedCategoriesCollection()
{
if ($this->categoryCollection === null) {
$collection = $this->categoryCollectionFactory->create();
$collection->join(
$this->getResource()->getTable('mageplaza_blog_post_category'),
'main_table.category_id=' . $this->getResource()->getTable('mageplaza_blog_post_category') .
'.category_id AND ' . $this->getResource()->getTable('mageplaza_blog_post_category') . '.post_id="'
. $this->getId() . '"',
['position']
);
$this->categoryCollection = $collection;
}
return $this->categoryCollection;
}
/**
* @return array
* @throws LocalizedException
*/
public function getCategoryIds()
{
if (!$this->hasData('category_ids')) {
$ids = $this->_getResource()->getCategoryIds($this);
$this->setData('category_ids', $ids);
}
return (array) $this->_getData('category_ids');
}
/**
* @return array
* @throws LocalizedException
*/
public function getTagIds()
{
if (!$this->hasData('tag_ids')) {
$ids = $this->_getResource()->getTagIds($this);
$this->setData('tag_ids', $ids);
}
return (array) $this->_getData('tag_ids');
}
/**
* @return array
* @throws LocalizedException
*/
public function getTopicIds()
{
if (!$this->hasData('topic_ids')) {
$ids = $this->_getResource()->getTopicIds($this);
$this->setData('topic_ids', $ids);
}
return (array) $this->_getData('topic_ids');
}
/**
* @return int
* @throws LocalizedException
*/
public function getViewTraffic()
{
if (!$this->hasData('view_traffic')) {
$traffic = $this->_getResource()->getViewTraffic($this);
$this->setData('view_traffic', $traffic[0]);
}
return $this->_getData('view_traffic');
}
/**
* @param null $limit
*
* @return ResourceModel\Post\Collection|null
* @throws LocalizedException
*/
public function getRelatedPostsCollection($limit = null)
{
$topicIds = $this->_getResource()->getTopicIds($this);
if (count($topicIds)) {
$collection = $this->postCollectionFactory->create();
$collection->getSelect()
->join(
['topic' => $this->getResource()->getTable('mageplaza_blog_post_topic')],
'main_table.post_id=topic.post_id AND topic.post_id != "' . $this->getId()
. '" AND topic.topic_id IN (' . implode(',', $topicIds) . ')',
['position']
)->group('main_table.post_id');
if ($limit = (int) $this->helperData->getBlogConfig('general/related_post')) {
$collection->getSelect()
->limit($limit);
}
$collection->addFieldToFilter('enabled', '1');
return $collection;
}
return null;
}
/**
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function getSelectedProductsCollection()
{
if ($this->productCollection === null) {
$collection = $this->productCollectionFactory->create();
$collection->getSelect()->join(
$this->getResource()->getTable('mageplaza_blog_post_product'),
'e.entity_id=' . $this->getResource()->getTable('mageplaza_blog_post_product')
. '.entity_id AND ' . $this->getResource()->getTable('mageplaza_blog_post_product') . '.post_id='
. $this->getId(),
['position']
);
$this->productCollection = $collection;
}
return $this->productCollection;
}
/**
* @return array|mixed
*/
public function getProductsPosition()
{
if (!$this->getId()) {
return [];
}
$array = $this->getData('products_position');
if ($array === null) {
$array = $this->getResource()->getProductsPosition($this);
$this->setData('products_position', $array);
}
return $array;
}
/**
* get previous post
* @return Collection
*/
public function getPrevPost()
{
if ($this->prevPostCollection === null) {
$collection = $this->postCollectionFactory->create();
$collection->addFieldToFilter('post_id', ['lt' => $this->getId()])
->setOrder('post_id', 'DESC')->setPageSize(1)->setCurPage(1);
$this->prevPostCollection = $collection;
}
return $this->prevPostCollection;
}
/**
* get next post
* @return Collection
*/
public function getNextPost()
{
if ($this->nextPostCollection === null) {
$collection = $this->postCollectionFactory->create();
$collection->addFieldToFilter('post_id', ['gt' => $this->getId()])
->setOrder('post_id', 'ASC')->setPageSize(1)->setCurPage(1);
$this->nextPostCollection = $collection;
}
return $this->nextPostCollection;
}
}