1 <?php
2
3 /* --------------------------------------------------------------
4 CategoryListProvider.inc.php 2015-11-25
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 CategoryListProvider
15 *
16 * This class provides methods for creating a list of flattened categories with just its essential data.
17 * The list of categories is filtered by its parent category ID and customer status permissions and for simplicity
18 * it contains language specific data only in one language.
19 *
20 * @category System
21 * @package Category
22 * @subpackage Providers
23 */
24 class CategoryListProvider implements CategoryListProviderInterface
25 {
26 /**
27 * Language code.
28 *
29 * @var LanguageCode
30 */
31 protected $languageCode;
32
33 /**
34 * Array of conditions.
35 *
36 * @var array
37 */
38 protected $conditions;
39
40 /**
41 * Database query builder.
42 *
43 * @var CI_DB_query_builder
44 */
45 protected $db;
46
47 /**
48 * Category Repository.
49 *
50 * @var CategoryRepositoryInterface
51 */
52 protected $categoryRepo;
53
54
55 /**
56 * CategoryListProvider constructor.
57 *
58 * @param LanguageCode $languageCode Two letter language code.
59 * @param array $conditions Additional data request conditions.
60 * @param CI_DB_query_builder $db Database connector.
61 * @param CategoryRepositoryInterface $categoryRepo Category repository.
62 */
63 public function __construct(LanguageCode $languageCode,
64 array $conditions = array(),
65 CategoryRepositoryInterface $categoryRepo,
66 CI_DB_query_builder $db)
67 {
68 $this->languageCode = $languageCode;
69 $this->conditions = $conditions;
70 $this->db = $db;
71 $this->categoryRepo = $categoryRepo;
72 }
73
74
75 /**
76 * Returns a category list based the parent ID provided.
77 *
78 * @param IdType $parentId Category parent ID.
79 *
80 * @return CategoryListItemCollection
81 */
82 public function getByParentId(IdType $parentId)
83 {
84 // Build select part of query.
85 $this->_select();
86
87 // Add where condition: Select by ID.
88 $this->db->where('categories.parent_id', $parentId->asInt());
89
90 // Get query result and pass to the method which processes the result.
91 $result = $this->db->get()->result_array();
92
93 return $this->_prepareCollection($result);
94 }
95
96
97 /**
98 * Build the select part of the query build.
99 *
100 * @return CategoryListProvider Same instance for chained method calls.
101 */
102 protected function _select()
103 {
104 // Build query.
105 $this->db->select('categories.*, categories_description.*')
106 ->from('categories, categories_description')
107 ->join('languages', 'languages.languages_id = categories_description.language_id', 'inner')
108 ->where('categories_description.categories_id = categories.categories_id')
109 ->where('languages.code', (string)$this->languageCode);
110
111 // Check for additional conditions to be appended to query.
112 if(count($this->conditions) > 0)
113 {
114 $this->db->where($this->conditions);
115 }
116
117 return $this;
118 }
119
120
121 /**
122 * Prepares the CategoryListItemCollection object.
123 *
124 * @param array $result Query result.
125 *
126 * @return CategoryListItemCollection
127 */
128 protected function _prepareCollection(array $result)
129 {
130 $listItems = array();
131
132 // Iterate over each query result row and
133 // create a CategoryListItem for each row which
134 // will be pushed into $listItems array.
135 foreach($result as $row)
136 {
137 $categoryRepo = $this->categoryRepo;
138 $categoryListProvider = $this;
139 $categoryId = new IdType((int)$row['categories_id']);
140 $parentId = new IdType((int)$row['parent_id']);
141 $isActive = new BoolType((bool)$row['categories_status']);
142 $name = new StringType((string)$row['categories_name']);
143 $headingTitle = new StringType((string)$row['categories_heading_title']);
144 $description = new StringType((string)$row['categories_description']);
145 $urlKeywords = new StringType((string)$row['categories_meta_keywords']);
146 $image = new StringType((string)$row['categories_image']);
147 $imageAltTest = new StringType((string)$row['gm_alt_text']);
148 $icon = new StringType((string)$row['categories_icon']);
149
150 $categoryListItem = MainFactory::create('CategoryListItem', $categoryRepo, $categoryListProvider,
151 $categoryId, $parentId, $isActive, $name, $headingTitle,
152 $description, $urlKeywords, $image, $imageAltTest, $icon);
153
154 $listItems[] = $categoryListItem;
155 }
156
157 $collection = MainFactory::create('CategoryListItemCollection', $listItems);
158
159 return $collection;
160 }
161 }