forked from thelia/thelia
/
Category.php
184 lines (149 loc) · 4.49 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
<?php
namespace Thelia\Model;
use Thelia\Core\Event\Category\CategoryEvent;
use Thelia\Files\FileModelParentInterface;
use Thelia\Model\Base\Category as BaseCategory;
use Thelia\Core\Event\TheliaEvents;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\Model\Tools\PositionManagementTrait;
use Thelia\Model\Tools\UrlRewritingTrait;
class Category extends BaseCategory implements FileModelParentInterface
{
use ModelEventDispatcherTrait;
use PositionManagementTrait;
use UrlRewritingTrait;
/**
* @return int number of child for the current category
*/
public function countChild()
{
return CategoryQuery::countChild($this->getId());
}
/**
* {@inheritDoc}
*/
public function getRewrittenUrlViewName()
{
return 'category';
}
/**
*
* count all products for current category and sub categories
*
* /!\ the number of queries is exponential, use it with caution
*
* @return int
*/
public function countAllProducts()
{
$children = CategoryQuery::findAllChild($this->getId());
array_push($children, $this);
$countProduct = 0;
foreach ($children as $child) {
$countProduct += ProductQuery::create()
->filterByCategory($child)
->count();
}
return $countProduct;
}
/**
* Get the root category
* @param int $categoryId
* @return mixed
*/
public function getRoot($categoryId)
{
$category = CategoryQuery::create()->findPk($categoryId);
if (0 !== $category->getParent()) {
$parentCategory = CategoryQuery::create()->findPk($category->getParent());
if (null !== $parentCategory) {
$categoryId = $this->getRoot($parentCategory->getId());
}
}
return $categoryId;
}
/**
* Calculate next position relative to our parent
*/
protected function addCriteriaToPositionQuery($query)
{
$query->filterByParent($this->getParent());
}
public function deleteProducts(ConnectionInterface $con = null)
{
$productsCategories = ProductCategoryQuery::create()
->filterByCategoryId($this->getId())
->filterByDefaultCategory(1)
->find($con);
if ($productsCategories) {
foreach ($productsCategories as $productCategory) {
$product = $productCategory->getProduct();
if ($product) {
$product->delete($con);
}
}
}
}
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->setPosition($this->getNextPosition());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATECATEGORY, new CategoryEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_CREATECATEGORY, new CategoryEvent($this));
}
/**
* {@inheritDoc}
*/
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATECATEGORY, new CategoryEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATECATEGORY, new CategoryEvent($this));
}
/**
* {@inheritDoc}
*/
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETECATEGORY, new CategoryEvent($this));
$this->reorderBeforeDelete(
array(
"parent" => $this->getParent(),
)
);
$this->deleteProducts($con);
return true;
}
/**
* {@inheritDoc}
*/
public function postDelete(ConnectionInterface $con = null)
{
$this->markRewrittenUrlObsolete();
//delete all subcategories
$subCategories = CategoryQuery::findAllChild($this->getId());
foreach ($subCategories as $category) {
if (!is_null($this->dispatcher)) {
$category->setDispatcher($this->getDispatcher());
}
$category->delete();
}
$this->dispatchEvent(TheliaEvents::AFTER_DELETECATEGORY, new CategoryEvent($this));
}
}