1 <?php
2
3 /* --------------------------------------------------------------
4 ProductListProvider.inc.php 2015-12-11
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2015 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 ProductListProvider
15 *
16 * @category System
17 * @package Product
18 * @subpackage Providers
19 */
20 class ProductListProvider implements ProductListProviderInterface
21 {
22 /**
23 * Two-letter language code.
24 *
25 * @var LanguageCode
26 */
27 protected $languageCode;
28
29 /**
30 * Database query conditions.
31 *
32 * @var array
33 */
34 protected $conditions;
35
36 /**
37 * Product repository.
38 *
39 * @var ProductRepositoryInterface
40 */
41 protected $productRepo;
42
43 /**
44 * Database connection.
45 *
46 * @var CI_DB_query_builder
47 */
48 protected $db;
49
50
51 /**
52 * ProductListProvider constructor.
53 *
54 * @param LanguageCode $languageCode Two-letter language code.
55 * @param array $conditions Database query conditions.
56 * @param ProductRepositoryInterface $productRepo Product repository.
57 * @param CI_DB_query_builder $db Database connection.
58 */
59 public function __construct(LanguageCode $languageCode,
60 array $conditions = array(),
61 ProductRepositoryInterface $productRepo,
62 CI_DB_query_builder $db)
63 {
64 $this->languageCode = $languageCode;
65 $this->conditions = $conditions;
66 $this->productRepo = $productRepo;
67 $this->db = $db;
68 }
69
70
71 /**
72 * Build the select part of the query build.
73 *
74 * @return ProductListProvider Same instance for chained method calls.
75 */
76 protected function _select()
77 {
78 // Build the database query.
79 $this->db->select('products.*, products_description.*')
80 ->from('products, products_description')
81 ->join('products_to_categories', 'products_to_categories.products_id = products.products_id', 'left')
82 ->join('languages', 'languages.languages_id = products_description.language_id', 'inner')
83 ->where('products_description.products_id = products.products_id')
84 ->where('languages.code', $this->languageCode->asString());
85
86 return $this;
87 }
88
89
90 /**
91 * Apply extra query conditions.
92 *
93 * @return ProductListProvider Same instance for chained method calls.
94 */
95 protected function _applyExtraConditions()
96 {
97 // Check for additional conditions to be appended to query (the AND operator will be used).
98 if(count($this->conditions) > 0)
99 {
100 $this->db->where($this->conditions);
101 }
102
103 return $this;
104 }
105
106
107 /**
108 * Prepares the ProductListItemCollection object.
109 *
110 * @param array $result Query result.
111 *
112 * @throws InvalidArgumentException if the provided result is not valid.
113 *
114 * @return ProductListItemCollection
115 */
116 protected function _prepareCollection(array $result)
117 {
118 $listItems = array();
119
120 // Iterate over each query result row and
121 // create a ProductListItem for each row which
122 // will be pushed into $listItems array.
123 foreach($result as $row)
124 {
125 $productRepo = $this->productRepo;
126 $id = new IdType((int)$row['products_id']);
127 $isActive = new BoolType((bool)$row['products_status']);
128 $name = new StringType((string)$row['products_name']);
129 $urlKeyWords = new StringType((string)$row['products_meta_keywords']);
130 $image = new StringType((string)$row['products_image']);
131 $imageAltText = new StringType((string)$row['gm_alt_text']);
132
133 $productListItem = MainFactory::create('ProductListItem', $productRepo, $id, $isActive, $name, $urlKeyWords,
134 $image, $imageAltText);
135
136 $listItems[] = $productListItem;
137 }
138
139 $collection = MainFactory::create('ProductListItemCollection', $listItems);
140
141 return $collection;
142 }
143
144
145 /**
146 * Returns a product list item collection by the provided category ID.
147 *
148 * @param IdType $categoryId Category ID.
149 *
150 * @throws InvalidArgumentException if the provided category ID is not valid.
151 *
152 * @return ProductListItemCollection
153 */
154 public function getByCategoryId(IdType $categoryId)
155 {
156 $this->_select()->_applyExtraConditions();
157
158 $this->db->where('products_to_categories.categories_id', $categoryId->asInt());
159
160 $result = $this->db->get()->result_array();
161
162 return $this->_prepareCollection($result);
163 }
164
165
166 /**
167 * Get all product list items.
168 *
169 * @return ProductListItemCollection
170 */
171 public function getAll()
172 {
173 // Build select part of query.
174 $this->db->select('products.*, products_description.*')
175 ->from('products, products_description')
176 ->join('languages', 'languages.languages_id = products_description.language_id', 'inner')
177 ->where('products_description.products_id = products.products_id')
178 ->where('languages.code', $this->languageCode->asString());
179
180 $this->_applyExtraConditions();
181
182 $result = $this->db->get()->result_array();
183
184 return $this->_prepareCollection($result);
185 }
186 }