1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thelia\Model; |
4
|
|
|
|
5
|
|
|
use Propel\Runtime\ActiveQuery\Criteria; |
6
|
|
|
use Thelia\Core\Event\Category\CategoryEvent; |
7
|
|
|
use Thelia\Files\FileModelParentInterface; |
8
|
|
|
use Thelia\Model\Base\Category as BaseCategory; |
9
|
|
|
use Thelia\Core\Event\TheliaEvents; |
10
|
|
|
use Propel\Runtime\Connection\ConnectionInterface; |
11
|
|
|
use Thelia\Model\Tools\ModelEventDispatcherTrait; |
12
|
|
|
use Thelia\Model\Tools\PositionManagementTrait; |
13
|
|
|
use Thelia\Model\Tools\UrlRewritingTrait; |
14
|
|
|
|
15
|
|
|
class Category extends BaseCategory implements FileModelParentInterface |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
use ModelEventDispatcherTrait; |
18
|
|
|
|
19
|
|
|
use PositionManagementTrait; |
20
|
|
|
|
21
|
|
|
use UrlRewritingTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return int number of child for the current category |
25
|
|
|
*/ |
26
|
|
|
public function countChild() |
27
|
|
|
{ |
28
|
|
|
return CategoryQuery::countChild($this->getId()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
public function getRewrittenUrlViewName() |
35
|
|
|
{ |
36
|
|
|
return 'category'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* count all products for current category and sub categories |
42
|
|
|
* |
43
|
|
|
* /!\ the number of queries is exponential, use it with caution |
44
|
|
|
* |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
|
|
public function countAllProducts($visibleOnly = false) |
48
|
|
|
{ |
49
|
|
|
$children = CategoryQuery::findAllChild($this->getId()); |
50
|
|
|
array_push($children, $this); |
51
|
|
|
|
52
|
|
|
$countProduct = 0; |
53
|
|
|
|
54
|
|
|
foreach ($children as $child) { |
55
|
|
|
$req = ProductQuery::create(); |
56
|
|
|
$req->filterByCategory($child); |
57
|
|
|
if($visibleOnly) { |
58
|
|
|
$req->filterByVisible(true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$countProduct += $req->count(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $countProduct; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* count visible products only for current category and sub categories |
70
|
|
|
* |
71
|
|
|
* /!\ the number of queries is exponential, use it with caution |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public function countAllProductsVisibleOnly() |
76
|
|
|
{ |
77
|
|
|
return $this->countAllProducts(true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the root category |
82
|
|
|
* @param int $categoryId |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getRoot($categoryId) |
86
|
|
|
{ |
87
|
|
|
$category = CategoryQuery::create()->findPk($categoryId); |
88
|
|
|
|
89
|
|
|
if (0 !== $category->getParent()) { |
90
|
|
|
$parentCategory = CategoryQuery::create()->findPk($category->getParent()); |
91
|
|
|
|
92
|
|
|
if (null !== $parentCategory) { |
93
|
|
|
$categoryId = $this->getRoot($parentCategory->getId()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $categoryId; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Calculate next position relative to our parent |
102
|
|
|
*/ |
103
|
|
|
protected function addCriteriaToPositionQuery($query) |
104
|
|
|
{ |
105
|
|
|
$query->filterByParent($this->getParent()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function deleteProducts(ConnectionInterface $con = null) |
109
|
|
|
{ |
110
|
|
|
$productsCategories = ProductCategoryQuery::create() |
111
|
|
|
->filterByCategoryId($this->getId()) |
112
|
|
|
->filterByDefaultCategory(1) |
113
|
|
|
->find($con); |
114
|
|
|
|
115
|
|
|
if ($productsCategories) { |
116
|
|
|
foreach ($productsCategories as $productCategory) { |
117
|
|
|
$product = $productCategory->getProduct(); |
118
|
|
|
if ($product) { |
119
|
|
|
$product->delete($con); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
public function preInsert(ConnectionInterface $con = null) |
129
|
|
|
{ |
130
|
|
|
$this->setPosition($this->getNextPosition()); |
131
|
|
|
|
132
|
|
|
$this->dispatchEvent(TheliaEvents::BEFORE_CREATECATEGORY, new CategoryEvent($this)); |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
|
public function postInsert(ConnectionInterface $con = null) |
141
|
|
|
{ |
142
|
|
|
$this->dispatchEvent(TheliaEvents::AFTER_CREATECATEGORY, new CategoryEvent($this)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function preUpdate(ConnectionInterface $con = null) |
149
|
|
|
{ |
150
|
|
|
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATECATEGORY, new CategoryEvent($this)); |
151
|
|
|
|
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritDoc} |
157
|
|
|
*/ |
158
|
|
|
public function postUpdate(ConnectionInterface $con = null) |
159
|
|
|
{ |
160
|
|
|
$this->dispatchEvent(TheliaEvents::AFTER_UPDATECATEGORY, new CategoryEvent($this)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritDoc} |
165
|
|
|
*/ |
166
|
|
|
public function preDelete(ConnectionInterface $con = null) |
167
|
|
|
{ |
168
|
|
|
$this->dispatchEvent(TheliaEvents::BEFORE_DELETECATEGORY, new CategoryEvent($this)); |
169
|
|
|
$this->reorderBeforeDelete( |
170
|
|
|
array( |
171
|
|
|
"parent" => $this->getParent(), |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
$this->deleteProducts($con); |
175
|
|
|
|
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritDoc} |
181
|
|
|
*/ |
182
|
|
|
public function postDelete(ConnectionInterface $con = null) |
183
|
|
|
{ |
184
|
|
|
$this->markRewrittenUrlObsolete(); |
185
|
|
|
|
186
|
|
|
//delete all subcategories |
187
|
|
|
$subCategories = CategoryQuery::findAllChild($this->getId()); |
188
|
|
|
|
189
|
|
|
foreach ($subCategories as $category) { |
190
|
|
|
if (!is_null($this->dispatcher)) { |
191
|
|
|
$category->setDispatcher($this->getDispatcher()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$category->delete(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$this->dispatchEvent(TheliaEvents::AFTER_DELETECATEGORY, new CategoryEvent($this)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Overload for the position management |
202
|
|
|
* @param Base\ProductCategory $productCategory |
203
|
|
|
* @inheritdoc |
204
|
|
|
*/ |
205
|
|
|
protected function doAddProductCategory($productCategory) |
206
|
|
|
{ |
207
|
|
|
parent::doAddProductCategory($productCategory); |
208
|
|
|
|
209
|
|
|
$productCategoryPosition = ProductCategoryQuery::create() |
210
|
|
|
->filterByCategoryId($productCategory->getCategoryId()) |
211
|
|
|
->orderByPosition(Criteria::DESC) |
212
|
|
|
->findOne(); |
213
|
|
|
|
214
|
|
|
$productCategory->setPosition($productCategoryPosition !== null ? $productCategoryPosition->getPosition() + 1 : 1); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|