PluginProbe
CSS & JavaScript Toolbox / 6.0.6
CSS & JavaScript Toolbox v6.0.6
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / db / mysql / xtable.inc.php

xtable.inc.php in CSS & JavaScript Toolbox 6.0.6, at framework/db/mysql/xtable.inc.php

423 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6 // Disallow direct access.
7 defined('ABSPATH') or die("Access denied");
8
9 /**
10 *
11 */
12 abstract class CJTxTable extends CJTHookableClass {
13
14 /**
15 * put your comment there...
16 *
17 * @var mixed
18 */
19 protected $dbDriver;
20
21 /**
22 * put your comment there...
23 *
24 * @var mixed
25 */
26 private $fields;
27
28 /**
29 * put your comment there...
30 *
31 * @var mixed
32 */
33 protected $item;
34
35 /**
36 * put your comment there...
37 *
38 * @var mixed
39 */
40 protected $key;
41
42 /**
43 *
44 */
45 private $name;
46
47 /**
48 * put your comment there...
49 *
50 * @var mixed
51 */
52 protected $onconcatquery = array('parameters' => array('query'));
53
54 /**
55 * put your comment there...
56 *
57 * @var mixed
58 */
59 protected $ondelete = array('parameters' => array('query', 'key'));
60
61 /**
62 * put your comment there...
63 *
64 * @var mixed
65 */
66 protected $ongetdata = array('parameters' => array('item'));
67
68 /**
69 * put your comment there...
70 *
71 * @var mixed
72 */
73 protected $ongetfield = array('parameters' => array('value', 'name'));
74
75 /**
76 * put your comment there...
77 *
78 * @var mixed
79 */
80 protected static $onimport = array('parameters' => array('type'));
81
82 /**
83 * put your comment there...
84 *
85 * @var mixed
86 */
87 protected static $oninstantiate = array('parameters' => array('args'));
88
89 /**
90 * put your comment there...
91 *
92 * @var mixed
93 */
94 protected $oninsert = array('parameters' => array('query'));
95
96 /**
97 * put your comment there...
98 *
99 * @var mixed
100 */
101 protected $oninserted = array('hookType' => CJTWordpressEvents::HOOK_ACTION);
102
103 /**
104 * put your comment there...
105 *
106 * @var mixed
107 */
108 protected $onloadquery = array('parameters' => array('query'));
109
110 /**
111 * put your comment there...
112 *
113 * @var mixed
114 */
115 protected $onsetfield = array('parameters' => array('property'));
116
117 /**
118 * put your comment there...
119 *
120 * @var mixed
121 */
122 protected $onupdate = array('parameters' => array('query'));
123
124 /**
125 * put your comment there...
126 *
127 * @var mixed
128 */
129 protected $onupdated = array('hookType' => CJTWordpressEvents::HOOK_ACTION);
130
131 /**
132 * put your comment there...
133 *
134 * @param mixed $table
135 * @return CJTxTable
136 */
137 public function __construct($dbDriver, $table, $key = array('id')) {
138 // Hookable!
139 parent::__construct();
140 // Initialize!
141 $this->dbDriver =$dbDriver;
142 $this->name = "#__cjtoolbox_{$table}";
143 $this->key = $key;
144 // Read table fields.
145 $this->fields = $this->dbDriver->getColumns($this->table());
146 }
147
148 /**
149 * DELETE!
150 *
151 * THIS METHOD SUPPORT COMPOUND KEYS!
152 *
153 */
154 public function delete($key = null) {
155 // building DELETE query!
156 $query['from'] = "DELETE FROM {$this->table()}";
157 $query['where'] = 'WHERE ' . implode(' AND ', $this->prepareQueryParameters($key = $this->getKey($key)));
158 // filtering!
159 if ($query = $this->ondelete($query, $key)) {
160 $query = "{$query['from']} {$query['where']}";
161 // Delete record.
162 $this->dbDriver->delete($query)->processQueue();
163 }
164 // Chaining!
165 return $this;
166 }
167
168 /**
169 * put your comment there...
170 *
171 * @param mixed $field
172 */
173 public function get($field) {
174 // Get the value of the field with E_ALL complain!
175 $value = (is_object($this->item) && property_exists($this->item, $field)) ? $this->item->{$field} : null;
176 return $this->ongetfield($value, $field);
177 }
178
179 /**
180 * put your comment there...
181 *
182 */
183 public function getData() {
184 return $this->ongetdata($this->item);
185 }
186
187 /**
188 * put your comment there...
189 *
190 * @param mixed $object
191 * @param mixed $dbDriver
192 * @return CJTxTable
193 */
194 public static function getInstance($type, $dbDriver = null, $query = null) {
195 // Filter all parameters.
196 extract(self::trigger('CJTxTable.instantiate', compact('type', 'dbDriver', 'query')));
197 // Getting dbdriver!
198 $dbDriver = !$dbDriver ? cssJSToolbox::getInstance()->getDBDriver() : $dbDriver;
199 // Import table file.
200 self::import($type);
201 // Get class name.
202 $type = str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $type)));
203 $className = "CJT{$type}Table";
204 $table = new $className($dbDriver);
205 if ($query) {
206 $table->load($query);
207 }
208 return $table;
209 }
210
211 /**
212 * put your comment there...
213 *
214 * @param mixed $tableKey
215 */
216 public function getKey($tableKey = null) {
217 if (!$tableKey) {
218 $tableKey = $this->getTableKey();
219 }
220 $key = array_intersect_key(((array) $this->item), array_flip($tableKey));
221 return $key;
222 }
223
224 /**
225 * put your comment there...
226 *
227 */
228 public function getTableKey() {
229 return $this->key;
230 }
231
232 /**
233 * put your comment there...
234 *
235 * @param mixed
236 */
237 public static function import($type) {
238 // Filtering parameters!
239 extract(self::trigger('CJTxTable.import', compact('type')));
240 // Implort table file.
241 cssJSToolbox::import("tables:{$type}.php");
242 }
243
244 /**
245 * put your comment there...
246 *
247 * @param mixed $tableKey
248 */
249 public function isValidKey($tableKey = null) {
250 $isValid = false;
251 // Get key!
252 $key = $this->getKey($tableKey);
253 // If any field has a value then its not null key!
254 foreach ($key as $field) {
255 if ($field !== null) {
256 $isValid = $key;
257 break;
258 }
259 }
260 return $isValid;
261 }
262
263 /**
264 * Load record into table!
265 *
266 * @param mixed
267 */
268 public function load($query = null) {
269 $tableKey = null;
270 $key = null;
271 // Query might be an array of keys!
272 if (is_array($query)) {
273 $tableKey = $query;
274 $query = null;
275 }
276 if (!$query) {
277 $item = (array) $this->item;
278 $query['select'] = 'SELECT *';
279 $query['from'] = "FROM {$this->table()}";
280 // Load only if key is not NULL!
281 if ($key = $this->isValidKey($tableKey)) {
282 $query['where'] = 'WHERE ' . implode(' AND ', $this->prepareQueryParameters($key));
283 if ($query = $this->onconcatquery($query)) {
284 // Read DB record!
285 $query = "{$query['select']} {$query['from']} {$query['where']}";
286 $this->item = array_shift($this->dbDriver->select($this->onloadquery($query)));
287 }
288 }
289 }
290 else {
291 $this->item = array_shift($this->dbDriver->select($this->onloadquery($query)));
292 }
293 return $this;
294 }
295
296 /**
297 * put your comment there...
298 *
299 * @todo Delete method and use CJTMYSQLQuery instead.
300 *
301 * @param mixed $parameters
302 */
303 protected function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) {
304 $prepared = array();
305 // For every parameter esacape name value.
306 foreach ($parameters as $name => $value) {
307 if (!$excludeNulls || ($value !== null)) {
308 if (array_key_exists($name, $this->fields) === FALSE) {
309 throw new Exception("Field:{$name} is not found!");
310 }
311 else {
312 $field = $this->fields[$name];
313 // Escape field name and value.
314 $value = $this->dbDriver->escapeValue($value, $field);
315 // Get name-value operator.
316 $operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator;
317 $prepared[] = "`{$name}`{$operator}{$value}";
318 }
319 }
320 }
321 return $prepared;
322 }
323
324 /**
325 * put your comment there...
326 *
327 */
328 public function reset() {
329 $this->item = null;
330 return $this;
331 }
332
333 /**
334 * UPDATE/INSERT
335 *
336 * THIS METHOD STILL DOESNT SUPPORT COMPOUND KEYS!!
337 *
338 * @param mixed $forceInsert
339 * @return CJTxTable
340 */
341 public function save($forceInsert = false) {
342 $keyFieldName = $this->key[0];
343 $item = (array) $this->item;
344 // Don't update id field.
345 $fieldsList = array_diff_key($item, array_flip($this->key));
346 $fieldsList = implode(',', $this->prepareQueryParameters($fieldsList));
347 if (!$forceInsert && $this->get($keyFieldName)) { // Update
348 // Where clause.
349 $condition = implode(' AND ', $this->prepareQueryParameters($this->getKey()));
350 $query = "UPDATE {$this->table()} SET {$fieldsList} WHERE {$condition}";
351 if ($query = $this->onupdate($query)) {
352 $this->dbDriver->update($query)
353 ->processQueue();
354 $this->onupdated();
355 }
356 }
357 else { // Insert.
358 $query = "INSERT {$this->table()} SET {$fieldsList}";
359 if ($query = $this->oninsert($query)) {
360 $this->dbDriver->insert($query)
361 ->processQueue();
362 $this->item->{$keyFieldName} = $this->dbDriver->getInsertId();
363 $this->oninserted();
364 }
365 }
366 return $this;
367 }
368
369 /**
370 * put your comment there...
371 *
372 * @param mixed $prop
373 * @param mixed $value
374 */
375 public function set($prop, $value) {
376 extract($this->onsetfield(compact('prop', 'value')));
377 $this->item->{$prop} = $value;
378 return $this;
379 }
380
381 /**
382 * put your comment there...
383 *
384 * @param mixed $data
385 */
386 public function setData($data) {
387 // Cast to array.
388 $data = (array) $data;
389 if (is_null($this->item)) {
390 $item = (object) array();
391 }
392 // Copy values!
393 foreach ($data as $name => $value) {
394 if ($value !== null) {
395 $this->set($name, $value);
396 }
397 }
398 return $this;
399 }
400
401 /**
402 * put your comment there...
403 *
404 * @param mixed $key
405 */
406 public function setTableKey($key) {
407 $this->key = $key;
408 return $this;
409 }
410
411 /**
412 * put your comment there...
413 *
414 */
415 public function table() {
416 return $this->name;
417 }
418
419 } // End class.
420
421 // Hookable.
422 CJTxTable::define('CJTxTable', array('hookType' =>CJTWordpressEvents::HOOK_FILTER));
423