1 <?php
2
3 /* --------------------------------------------------------------
4 ProductCategoryLinker.inc.php 2016-01-28
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 ProductCategoryLinker
15 *
16 * @category System
17 * @package Product
18 * @subpackage Repositories
19 */
20 class ProductCategoryLinker implements ProductCategoryLinkerInterface
21 {
22 /**
23 * @var CI_DB_query_builder
24 */
25 protected $db;
26
27 /**
28 * @var string
29 */
30 protected $tableName = 'products_to_categories';
31
32
33 /**
34 * Initialize the product category linker.
35 *
36 * @param CI_DB_query_builder $db Database connector.
37 */
38 public function __construct(CI_DB_query_builder $db)
39 {
40 $this->db = $db;
41 }
42
43
44 /**
45 * Links a product to a category.
46 *
47 * @param IdType $productId Product ID.
48 * @param IdType $targetCategoryId Target category ID.
49 *
50 * @return $this|ProductCategoryLinker Same instance for chained method calls.
51 */
52 public function linkProduct(IdType $productId, IdType $targetCategoryId)
53 {
54 $this->db->replace($this->tableName,
55 array('products_id' => $productId->asInt(), 'categories_id' => $targetCategoryId->asInt()));
56
57 return $this;
58 }
59
60
61 /**
62 * Changes a link to a new category.
63 *
64 * @param IdType $productId Product ID.
65 * @param IdType $currentCategoryId Category ID which the product is linked to.
66 * @param IdType $newCategoryId New category to be linked to.
67 *
68 * @return $this|ProductCategoryLinker Same instance for chained method calls.
69 */
70 public function changeProductLink(IdType $productId, IdType $currentCategoryId, IdType $newCategoryId)
71 {
72 $dataArray = array('categories_id' => $newCategoryId->asInt());
73 $whereArray = array('products_id' => $productId->asInt(), 'categories_id' => $currentCategoryId->asInt());
74 $this->db->update($this->tableName, $dataArray, $whereArray);
75
76 return $this;
77 }
78
79
80 /**
81 * Removes a link to a category.
82 *
83 * @param IdType $productId Product ID.
84 * @param IdType $categoryId Category ID which the link should be removed to.
85 *
86 * @return $this|ProductCategoryLinker Same instance for chained method calls.
87 */
88 public function deleteProductLink(IdType $productId, IdType $categoryId)
89 {
90 $whereArray = array('products_id' => $productId->asInt(), 'categories_id' => $categoryId->asInt());
91 $this->db->delete($this->tableName, $whereArray);
92
93 return $this;
94 }
95
96
97 /**
98 * Removes all links from a product.
99 *
100 * @param IdType $productId Product ID.
101 *
102 * @return $this|ProductCategoryLinker Same instance for chained method calls.
103 */
104 public function deleteProductLinks(IdType $productId)
105 {
106 $whereArray = array('products_id' => $productId->asInt());
107 $this->db->delete($this->tableName, $whereArray);
108
109 return $this;
110 }
111
112
113 /**
114 * Returns the category Ids which are linked with given product id.
115 *
116 * @param IdType $productId Product ID.
117 *
118 * @return IdCollection
119 */
120 public function getProductLinks(IdType $productId)
121 {
122 $fetchedCategories = array();
123 $result = $this->db->get_where($this->tableName, array('products_id' => $productId->asInt()));
124
125 foreach($result->result_array() as $row)
126 {
127 $fetchedCategories[] = new IdType($row['categories_id']);
128 }
129
130 $collection = MainFactory::create('IdCollection', $fetchedCategories);
131
132 return $collection;
133 }
134 }