/
Category.php
executable file
·308 lines (279 loc) · 8.57 KB
/
Category.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
<?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 Exception;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Mageplaza\Blog\Model\ResourceModel\Post\Collection;
use Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory;
/**
* @method Category setName($name)
* @method Category setDescription($description)
* @method Category setUrlKey($urlKey)
* @method Category setEnabled($enabled)
* @method Category setMetaTitle($metaTitle)
* @method Category setMetaDescription($metaDescription)
* @method Category setMetaKeywords($metaKeywords)
* @method Category setMetaRobots($metaRobots)
* @method mixed getName()
* @method mixed getDescription()
* @method mixed getUrlKey()
* @method mixed getEnabled()
* @method mixed getMetaTitle()
* @method mixed getMetaDescription()
* @method mixed getMetaKeywords()
* @method mixed getMetaRobots()
* @method Category setParentId(int $parentId)
* @method int getParentId()
* @method Category setPath(string $path)
* @method string getPath()
* @method Category setPosition(int $path)
* @method int getPosition()
* @method Category setChildrenCount(int $path)
* @method int getChildrenCount()
* @method Category setCreatedAt(string $createdAt)
* @method string getCreatedAt()
* @method Category setUpdatedAt(string $updatedAt)
* @method string getUpdatedAt()
* @method Category setMovedCategoryId(string $id)
* @method Category setAffectedCategoryIds(array $ids)
* @method Category setPostsData(array $data)
* @method array getPostsData()
* @method Category setIsChangedPostList(bool $flag)
* @method bool getIsChangedPostList()
* @method Category setAffectedPostIds(array $ids)
* @method bool getAffectedPostIds()
*/
class Category extends AbstractModel
{
/**
* Cache tag
*
* @var string
*/
const CACHE_TAG = 'mageplaza_blog_category';
/**
* Cache tag
*
* @var string
*/
protected $_cacheTag = 'mageplaza_blog_category';
/**
* Event prefix
*
* @var string
*/
protected $_eventPrefix = 'mageplaza_blog_category';
/**
* Post Collection
*
* @var Collection
*/
public $postCollection;
/**
* Blog Category Factory
*
* @var CategoryFactory
*/
public $categoryFactory;
/**
* Post Collection Factory
*
* @var CollectionFactory
*/
public $postCollectionFactory;
/**
* @var CategoryCollectionFactory
*/
public $categoryCollectionFactory;
/**
* Category constructor.
*
* @param Context $context
* @param Registry $registry
* @param CategoryFactory $categoryFactory
* @param CollectionFactory $postCollectionFactory
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
CategoryFactory $categoryFactory,
CollectionFactory $postCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
$this->categoryFactory = $categoryFactory;
$this->postCollectionFactory = $postCollectionFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(ResourceModel\Category::class);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
/**
* get entity default values
*
* @return array
*/
public function getDefaultValues()
{
$values = [];
$values['store_ids'] = '1';
$values['enabled'] = '1';
return $values;
}
/**
* get tree path ids
*
* @return array
*/
public function getPathIds()
{
$ids = $this->getData('path_ids');
if ($ids === null) {
$ids = explode('/', $this->getPath());
$this->setData('path_ids', $ids);
}
return $ids;
}
/**
* get all parent ids
*
* @return array
*/
public function getParentIds()
{
return array_diff($this->getPathIds(), [$this->getId()]);
}
/**
* move Blog Category in tree
*
* @param $parentId
* @param $afterCategoryId
*
* @return $this
* @throws LocalizedException
* @throws Exception
*/
public function move($parentId, $afterCategoryId)
{
try {
$parent = $this->categoryFactory->create()->load($parentId);
} catch (NoSuchEntityException $e) {
throw new LocalizedException(
__('Sorry, but we can\'t move the Blog Category because we can\'t find the new parent Blog Category you selected.'),
$e
);
}
if (!$this->getId()) {
throw new LocalizedException(
__('Sorry, but we can\'t move the Blog Category because we can\'t find the new parent Blog Category you selected.')
);
}
if ($parent->getId() == $this->getId()) {
throw new LocalizedException(
__('We can\'t perform this Blog Category move operation because the parent Blog Category matches the child Blog Category.')
);
}
$this->setMovedCategoryId($this->getId());
$oldParentId = $this->getParentId();
$eventParams = [
$this->_eventObject => $this,
'parent' => $parent,
'category_id' => $this->getId(),
'prev_parent_id' => $oldParentId,
'parent_id' => $parentId,
];
$this->_getResource()->beginTransaction();
try {
$this->_eventManager->dispatch($this->_eventPrefix . '_move_before', $eventParams);
$this->getResource()->changeParent($this, $parent, $afterCategoryId);
$this->_eventManager->dispatch($this->_eventPrefix . '_move_after', $eventParams);
$this->_getResource()->commit();
// Set data for indexer
$this->setAffectedCategoryIds([$this->getId(), $oldParentId, $parentId]);
} catch (Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
$this->_eventManager->dispatch($this->_eventPrefix . '_move', $eventParams);
$this->_cacheManager->clean([self::CACHE_TAG]);
return $this;
}
/**
* @return array|mixed
*/
public function getPostsPosition()
{
if (!$this->getId()) {
return [];
}
$array = $this->getData('posts_position');
if ($array === null) {
$array = $this->getResource()->getPostsPosition($this);
$this->setData('posts_position', $array);
}
return $array;
}
/**
* @return Collection
*/
public function getSelectedPostsCollection()
{
if (!$this->postCollection) {
$collection = $this->postCollectionFactory->create();
$collection->join(
['cat' => $this->getResource()->getTable('mageplaza_blog_post_category')],
'main_table.post_id=cat.post_id AND cat.category_id=' . $this->getId(),
['position']
);
$this->postCollection = $collection;
}
return $this->postCollection;
}
}