1 <?php
2
3 /* --------------------------------------------------------------
4 ProductPermissionSetter.inc.php 2016-01-29
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 ProductPermissionSetter
15 *
16 * @category System
17 * @package Product
18 * @subpackage Repositories
19 */
20 class ProductPermissionSetter implements ProductPermissionSetterInterface
21 {
22 /**
23 * @var CI_DB_query_builder
24 */
25 protected $db;
26
27
28 /**
29 * ProductPermissionSetter constructor.
30 *
31 * @param CI_DB_query_builder $db
32 */
33 public function __construct(CI_DB_query_builder $db)
34 {
35 $this->db = $db;
36 }
37
38
39 /**
40 * Sets the Customer Status Permissions for all products which are linked with the provided category ID.
41 *
42 * @param IdType $categoryId
43 * @param IdType $customerStatusId
44 * @param BoolType $permitted
45 *
46 * @return ProductPermissionSetterInterface Same instance for chained method calls.
47 */
48 public function setProductsPermissionByCategoryId(IdType $categoryId,
49 IdType $customerStatusId,
50 BoolType $permitted)
51 {
52 $column = 'group_permission_' . $customerStatusId->asInt();
53
54 // Sub Query
55 $this->db->select('products_id')->from('products_to_categories')->where('categories_id', $categoryId->asInt());
56 $subQuery = $this->db->get_compiled_select();
57
58 // Main Query
59 $this->db->set($column, (int)$permitted->asBool())
60 ->where("products_id IN ($subQuery)", null, false)
61 ->update('products');
62
63 return $this;
64 }
65 }