1 <?php
2
3 /* --------------------------------------------------------------
4 CategoryWriteService.inc.php 2016-02-15
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class CategoryWriteService
15 *
16 * This class provides methods for creating, updating and deleting categories data.
17 *
18 * @category System
19 * @package Category
20 * @implements CategoryWriteServiceInterface
21 */
22 class CategoryWriteService implements CategoryWriteServiceInterface
23 {
24 /**
25 * Category repository.
26 *
27 * @var CategoryRepositoryInterface
28 */
29 protected $categoryRepo;
30
31 /**
32 * Category image.
33 *
34 * @var AbstractFileStorage
35 */
36 protected $categoryImageStorage;
37
38 /**
39 * Category icon.
40 *
41 * @var AbstractFileStorage
42 */
43 protected $categoryIconStorage;
44
45 /**
46 * ProductPermissionSetter
47 *
48 * @var ProductPermissionSetterInterface
49 */
50 protected $productPermissionSetter;
51
52
53 /**
54 * CategoryWriteService constructor.
55 *
56 * @param CategoryRepositoryInterface $categoryRepo Category repository.
57 * @param AbstractFileStorage $categoryImageStorage Category image.
58 * @param AbstractFileStorage $categoryIconStorage Category icon.
59 * @param ProductPermissionSetterInterface $productPermissionSetter ProductPermissionSetter.
60 */
61 public function __construct(CategoryRepositoryInterface $categoryRepo,
62 AbstractFileStorage $categoryImageStorage,
63 AbstractFileStorage $categoryIconStorage,
64 ProductPermissionSetterInterface $productPermissionSetter)
65 {
66 $this->categoryRepo = $categoryRepo;
67 $this->categoryImageStorage = $categoryImageStorage;
68 $this->categoryIconStorage = $categoryIconStorage;
69 $this->productPermissionSetter = $productPermissionSetter;
70 }
71
72
73 /**
74 * Stores a category in the database and returns the newly created ID of it.
75 *
76 * @param CategoryInterface $category The category to store.
77 *
78 * @return int Returns the ID of the new category record.
79 */
80 public function createCategory(CategoryInterface $category)
81 {
82 return $this->categoryRepo->add($category);
83 }
84
85
86 /**
87 * Updates the provided category and returns itself.
88 *
89 * @param StoredCategoryInterface $category The category to update.
90 *
91 * @return CategoryWriteService Same instance for chained method calls.
92 */
93 public function updateCategory(StoredCategoryInterface $category)
94 {
95 $this->categoryRepo->store($category);
96
97 return $this;
98 }
99
100
101 /**
102 * Deletes a category depending on the provided category ID.
103 *
104 * @param IdType $categoryId Category ID of the category to delete.
105 *
106 * @return CategoryWriteService Same instance for chained method calls.
107 */
108 public function deleteCategoryById(IdType $categoryId)
109 {
110 $category = $this->categoryRepo->getCategoryById($categoryId);
111
112 $this->categoryIconStorage->deleteFile(new FilenameStringType($category->getIcon()));
113 $this->categoryImageStorage->deleteFile(new FilenameStringType($category->getImage()));
114
115 $this->categoryRepo->deleteCategoryById($categoryId);
116
117 return $this;
118 }
119
120
121 /**
122 * Moves a category into another category.
123 *
124 * This method moves a category specified by its category ID into another parent category specified by its
125 * category ID.
126 *
127 * @param IdType $categoryId Category ID of the category to move.
128 * @param IdType $newParentId The new parent ID.
129 *
130 * @return CategoryWriteService Same instance for chained method calls.
131 */
132 public function moveCategory(IdType $categoryId, IdType $newParentId)
133 {
134 $storedCategory = $this->categoryRepo->getCategoryById($categoryId);
135 $storedCategory->setParentId($newParentId);
136
137 $this->categoryRepo->store($storedCategory);
138
139 return $this;
140 }
141
142
143 /**
144 * Duplicates a category specified by its category ID.
145 *
146 * This method duplicates the category which are identified by the provided category ID and links the duplicated
147 * category with the provided parent category ID. Containing subcategories and products will also be recursively
148 * duplicated with their attributes, specials and cross selling data depending on the last four arguments.
149 *
150 * @param IdType $categoryId The category ID of the category to duplicate.
151 * @param IdType $targetParentId The target parent ID of the duplicated category.
152 * @param BoolType $duplicateProducts Should the products be duplicated?
153 * @param BoolType $duplicateAttributes Should the attributes be duplicated?
154 * @param BoolType $duplicateSpecials Should the specials be duplicated?
155 * @param BoolType $duplicateCrossSelling Should cross selling be duplicated?
156 *
157 * @return int Returns the ID of the new category record.
158 *
159 * @todo Implement the last four arguments when finished in UML.
160 */
161 public function duplicateCategory(IdType $categoryId,
162 IdType $targetParentId,
163 BoolType $duplicateProducts = null,
164 BoolType $duplicateAttributes = null,
165 BoolType $duplicateSpecials = null,
166 BoolType $duplicateCrossSelling = null)
167 {
168 $storedCategory = $this->categoryRepo->getCategoryById($categoryId);
169 $storedCategory->setParentId($targetParentId);
170 $newCategoryId = $this->categoryRepo->add($storedCategory);
171
172 return $newCategoryId;
173 }
174
175
176 /**
177 * Imports an image file and stores it.
178 *
179 * @param ExistingFile $sourceFile The image file to import.
180 * @param FilenameStringType $saveAsFilename The name under which the image should to be stored.
181 *
182 * @return string The new filename.
183 */
184 public function importCategoryImageFile(ExistingFile $sourceFile, FilenameStringType $saveAsFilename)
185 {
186 return $this->categoryImageStorage->importFile($sourceFile, $saveAsFilename);
187 }
188
189
190 /**
191 * Imports an icon file and stores it.
192 *
193 * @param ExistingFile $sourceFile The icon file to import.
194 * @param FilenameStringType $saveAsFilename The name under which the icon should be stored.
195 *
196 * @return string The new filename.
197 */
198 public function importCategoryIconFile(ExistingFile $sourceFile, FilenameStringType $saveAsFilename)
199 {
200 return $this->categoryIconStorage->importFile($sourceFile, $saveAsFilename);
201 }
202
203
204 /**
205 * Renames a category image file.
206 *
207 * @param FilenameStringType $oldName Old file name.
208 * @param FilenameStringType $newName New file name.
209 *
210 * @return CategoryWriteServiceInterface Same instance for chained method calls.
211 */
212 public function renameCategoryImageFile(FilenameStringType $oldName, FilenameStringType $newName)
213 {
214 $this->categoryImageStorage->renameFile($oldName, $newName);
215
216 return $this;
217 }
218
219
220 /**
221 * Renames a category icon file.
222 *
223 * @param FilenameStringType $oldName Old file name.
224 * @param FilenameStringType $newName New file name.
225 *
226 * @return CategoryWriteServiceInterface Same instance for chained method calls.
227 */
228 public function renameCategoryIconFile(FilenameStringType $oldName, FilenameStringType $newName)
229 {
230 $this->categoryIconStorage->renameFile($oldName, $newName);
231
232 return $this;
233 }
234
235
236 /**
237 * Deletes a category image file.
238 *
239 * @param FilenameStringType $filename Category image file name.
240 *
241 * @return CategoryWriteServiceInterface Same instance for chained method calls.
242 */
243 public function deleteCategoryImageFile(FilenameStringType $filename)
244 {
245 $this->categoryImageStorage->deleteFile($filename);
246
247 return $this;
248 }
249
250
251 /**
252 * Deletes a category icon file.
253 *
254 * @param FilenameStringType $filename Category icon file name.
255 *
256 * @return CategoryWriteServiceInterface Same instance for chained method calls.
257 */
258 public function deleteCategoryIconFile(FilenameStringType $filename)
259 {
260 $this->categoryIconStorage->deleteFile($filename);
261
262 return $this;
263 }
264
265
266 /**
267 * Activates a specific category and its subcategories if desired.
268 *
269 * @param IdType $categoryId Category ID of the category to activate.
270 * @param BoolType $includeSubcategories Shall the subcategories be activated also?
271 *
272 * @return CategoryWriteServiceInterface Same instance for chained method calls.
273 */
274 public function activateCategory(IdType $categoryId, BoolType $includeSubcategories)
275 {
276 $storedCategory = $this->categoryRepo->getCategoryById($categoryId);
277 $storedCategory->setActive(new BoolType(true));
278 $this->categoryRepo->store($storedCategory);
279
280 if($includeSubcategories->asBool())
281 {
282 foreach($this->categoryRepo->getCategoryIdsByParentId($categoryId)->getArray() as $subCategoryId)
283 {
284 $this->activateCategory($subCategoryId, $includeSubcategories);
285 }
286 }
287
288 return $this;
289 }
290
291
292 /**
293 * Deactivates a specific category and its subcategories if desired.
294 *
295 * @param IdType $categoryId Category ID of the category to deactivate.
296 * @param BoolType $includeSubcategories Shall the subcategories be deactivated also?
297 *
298 * @return CategoryWriteServiceInterface Same instance for chained method calls.
299 */
300 public function deactivateCategory(IdType $categoryId, BoolType $includeSubcategories)
301 {
302 $storedCategory = $this->categoryRepo->getCategoryById($categoryId);
303 $storedCategory->setActive(new BoolType(false));
304 $this->categoryRepo->store($storedCategory);
305
306 if($includeSubcategories->asBool())
307 {
308 foreach($this->categoryRepo->getCategoryIdsByParentId($categoryId)->getArray() as $subCategoryId)
309 {
310 $this->deactivateCategory($subCategoryId, $includeSubcategories);
311 }
312 }
313
314 return $this;
315 }
316
317
318 /**
319 * Sets the customer status permissions.
320 *
321 * The customer status permissions decides if the category is visible for a specific customer group. The
322 * permissions can be applied for subcategories also if desired.
323 *
324 * @param IdType $categoryId Category ID.
325 * @param IdType $customerStatusId Customer status ID.
326 * @param BoolType $permitted Grant permission?
327 * @param BoolType $includeSubcategoriesAndProducts Grant permission including subcategories?
328 *
329 * @return CategoryWriteServiceInterface Same instance for chained method calls.
330 */
331 public function setCustomerStatusPermission(IdType $categoryId,
332 IdType $customerStatusId,
333 BoolType $permitted,
334 BoolType $includeSubcategoriesAndProducts)
335 {
336 $storedCategory = $this->categoryRepo->getCategoryById($categoryId);
337 $storedCategorySettings = $storedCategory->getSettings();
338 $storedCategorySettings->setPermittedCustomerStatus($customerStatusId, $permitted);
339 $this->categoryRepo->store($storedCategory);
340
341 if($includeSubcategoriesAndProducts->asBool())
342 {
343 $this->productPermissionSetter->setProductsPermissionByCategoryId($categoryId, $customerStatusId,
344 $permitted);
345
346 foreach($this->categoryRepo->getCategoryIdsByParentId($categoryId)->getArray() as $subCategoryId)
347 {
348 $this->setCustomerStatusPermission($subCategoryId, $customerStatusId, $permitted,
349 $includeSubcategoriesAndProducts);
350 }
351 }
352
353 return $this;
354 }
355 }
356