1 <?php
2
3 /* --------------------------------------------------------------
4 AbstractAddonValueStorage.inc.php 2015-12-17
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 AbstractAddonValueStorage
15 *
16 * @category System
17 * @package AddonValue
18 */
19 abstract class AbstractAddonValueStorage
20 {
21 /**
22 * @var CI_DB_query_builder
23 */
24 protected $db;
25
26 /**
27 * @var array Multidimensional array of database tables with corresponding primary keys and the fields which are
28 * relevant for addon values which are not stored in the addon_values_storage table.
29 */
30 protected $externalFields = array();
31
32
33 /**
34 * AbstractAddonValueStorage Constructor
35 *
36 * @param CI_DB_query_builder $db
37 */
38 public function __construct(CI_DB_query_builder $db)
39 {
40 $this->db = $db;
41 $this->externalFields = $this->_getExternalFieldsArray();
42 }
43
44
45 /**
46 * Get the addon values by the given container ID.
47 *
48 * @param IdType $containerId Container database ID.
49 *
50 * @return KeyValueCollection
51 */
52 public function getValuesByContainerId(IdType $containerId)
53 {
54 $values = $this->_getExternalValuesByContainerId($containerId);
55
56 $result = $this->db->get_where('addon_values_storage', array(
57 'container_id' => $containerId->asInt(),
58 'container_type' => $this->_getContainerType()
59 ));
60
61 if($result->num_rows() && $result->num_fields())
62 {
63 foreach($result->result_array() as $row)
64 {
65 $values[$row['addon_key']] = $row['addon_value'];
66 }
67 }
68
69 $keyValueCollection = MainFactory::create('KeyValueCollection', $values);
70
71 return $keyValueCollection;
72 }
73
74
75 /**
76 * Sets addon values to the database.
77 *
78 * @param IdType $containerId Container database ID.
79 * @param KeyValueCollection $values KeyValueCollection which should set.
80 *
81 * @return AbstractAddonValueStorage Returns the class instance.
82 */
83 public function setValues(IdType $containerId, KeyValueCollection $values)
84 {
85 $this->_setExternalValues($containerId, $values);
86
87 foreach($values->getArray() as $key => $value)
88 {
89 $row = $this->db->get_where('addon_values_storage', array(
90 'container_id' => $containerId->asInt(),
91 'container_type' => $this->_getContainerType(),
92 'addon_key' => $key
93 ))->row_array();
94
95 if($row === null) // insert
96 {
97 $newRow = array(
98 'container_type' => $this->_getContainerType(),
99 'container_id' => $containerId->asInt(),
100 'addon_key' => $key,
101 'addon_value' => $value
102 );
103
104 $this->db->insert('addon_values_storage', $newRow);
105 }
106 else // update
107 {
108 $updatedRow = array('addon_value' => $value);
109 $this->db->update('addon_values_storage', $updatedRow, array(
110 'container_id' => $containerId->asInt(),
111 'container_type' => $this->_getContainerType(),
112 'addon_key' => $key
113 ));
114 }
115 }
116
117 return $this;
118 }
119
120
121 /**
122 * Selects the fields specified in the external fields array $this->externalFields by the given container ID and
123 * returns the associative array which will be merged with the associative array with values from the
124 * addon_values_storage table.
125 *
126 * @param IdType $containerId
127 *
128 * @return array Associative array of
129 */
130 protected function _getExternalValuesByContainerId(IdType $containerId)
131 {
132 $values = array();
133
134 if(0 === count($this->externalFields))
135 {
136 return $values;
137 }
138
139 foreach($this->externalFields as $tableName => $tableData)
140 {
141 $result = $this->db->select(implode(',', array_keys($tableData['fields'])))
142 ->from($tableName)
143 ->where($tableData['primary_key'], $containerId->asInt())
144 ->get();
145
146 if($result->num_rows() && $result->num_fields())
147 {
148 foreach($result->row_array() as $key => $value)
149 {
150 $values[$tableData['fields'][$key]] = $value;
151 }
152 }
153 }
154
155 return $values;
156 }
157
158
159 /**
160 * Updates the fields specified in the external fields array $this->externalFields with the corresponding values
161 * from the provided KeyValueCollection and removes the elements from the collection before the called method
162 * writes into the addon_values_storage table.
163 *
164 * @param IdType $containerId
165 * @param KeyValueCollection $values
166 */
167 protected function _setExternalValues(IdType $containerId, KeyValueCollection $values)
168 {
169 if(0 === count($this->externalFields))
170 {
171 return;
172 }
173
174 foreach($this->externalFields as $tableName => $tableData)
175 {
176 $row = array();
177 foreach($tableData['fields'] as $columnName => $addonValueKey)
178 {
179 if($values->keyExists($addonValueKey))
180 {
181 $row[$columnName] = $values->getValue($addonValueKey);
182 $values->deleteValue($addonValueKey);
183 }
184 }
185
186 $exists = $this->db->select($tableData['primary_key'])->from($tableName)->where($tableData['primary_key'], $containerId->asInt())->get()->row_array();
187
188 if($exists)
189 {
190 $this->db->update($tableName, $row, array($tableData['primary_key'] => $containerId->asInt()));
191 }
192 else
193 {
194 $row[$tableData['primary_key']] = $containerId->asInt();
195 $this->db->insert($tableName, $row);
196 }
197 }
198 }
199
200
201 /**
202 * Remove addon values by the given source id.
203 *
204 * @param IdType $containerId Id of expected source.
205 *
206 * @return AbstractAddonValueStorage Returns the class instance.
207 */
208 public function deleteValuesByContainerId(IdType $containerId)
209 {
210 $this->db->delete('addon_values_storage', array(
211 'container_id' => $containerId->asInt(),
212 'container_type' => $this->_getContainerType()
213 ));
214
215 return $this;
216 }
217
218
219 /**
220 * Should return a multidimensional array of database tables with corresponding primary keys and the column names
221 * with the corresponding key used in the KeyValueCollection which are relevant for addon values and not stored in
222 * the addon_values_storage table.
223 *
224 * Example:
225 * $externalFields = array();
226 * $externalFields['orders']['primary_key'] = 'orders_id';
227 * $externalFields['orders']['fields'] = array(
228 * 'customers_ip' => 'customerIp',
229 * 'abandonment_download' => 'downloadAbandonmentStatus',
230 * 'abandonment_service' => 'serviceAbandonmentStatus',
231 * 'cc_type' => 'ccType',
232 * 'cc_owner' => 'ccOwner',
233 * 'cc_number' => 'ccNumber',
234 * 'cc_expires' => 'ccExpires',
235 * 'cc_start' => 'ccStart',
236 * 'cc_issue' => 'ccIssue',
237 * 'cc_cvv' => 'ccCvv'
238 * );
239 *
240 * return $externalFields;
241 *
242 * @return array
243 */
244 abstract protected function _getExternalFieldsArray();
245
246
247 /**
248 * Get the container class type.
249 *
250 * @return string
251 */
252 abstract protected function _getContainerType();
253 }