PluginProbe
CSS & JavaScript Toolbox / 6.0.11
CSS & JavaScript Toolbox v6.0.11
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.11, at framework/db/mysql/xtable.inc.php

430 lines 9.0 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 // Reset intenal item object!
141 $this->setItem();
142 // Initialize!
143 $this->dbDriver =$dbDriver;
144 $this->name = "#__cjtoolbox_{$table}";
145 $this->key = $key;
146 // Read table fields.
147 $this->fields = $this->dbDriver->getColumns($this->table());
148 }
149
150 /**
151 * DELETE!
152 *
153 * THIS METHOD SUPPORT COMPOUND KEYS!
154 *
155 */
156 public function delete($key = null) {
157 // building DELETE query!
158 $query['from'] = "DELETE FROM {$this->table()}";
159 $query['where'] = 'WHERE ' . implode(' AND ', $this->prepareQueryParameters($key = $this->getKey($key)));
160 // filtering!
161 if ($query = $this->ondelete($query, $key)) {
162 $query = "{$query['from']} {$query['where']}";
163 // Delete record.
164 $this->dbDriver->delete($query)->processQueue();
165 }
166 // Chaining!
167 return $this;
168 }
169
170 /**
171 * put your comment there...
172 *
173 * @param mixed $field
174 */
175 public function get($field) {
176 // Get the value of the field with E_ALL complain!
177 $value = (is_object($this->item) && property_exists($this->item, $field)) ? $this->item->{$field} : null;
178 return $this->ongetfield($value, $field);
179 }
180
181 /**
182 * put your comment there...
183 *
184 */
185 public function getData() {
186 return $this->ongetdata($this->item);
187 }
188
189 /**
190 * put your comment there...
191 *
192 * @param mixed $object
193 * @param mixed $dbDriver
194 * @return CJTxTable
195 */
196 public static function getInstance($type, $dbDriver = null, $query = null) {
197 // Filter all parameters.
198 extract(self::trigger('CJTxTable.instantiate', compact('type', 'dbDriver', 'query')));
199 // Getting dbdriver!
200 $dbDriver = !$dbDriver ? cssJSToolbox::getInstance()->getDBDriver() : $dbDriver;
201 // Import table file.
202 self::import($type);
203 // Get class name.
204 $type = str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $type)));
205 $className = "CJT{$type}Table";
206 $table = new $className($dbDriver);
207 if ($query) {
208 $table->load($query);
209 }
210 return $table;
211 }
212
213 /**
214 * put your comment there...
215 *
216 * @param mixed $tableKey
217 */
218 public function getKey($tableKey = null) {
219 if (!$tableKey) {
220 $tableKey = $this->getTableKey();
221 }
222 $key = array_intersect_key(((array) $this->item), array_flip($tableKey));
223 return $key;
224 }
225
226 /**
227 * put your comment there...
228 *
229 */
230 public function getTableKey() {
231 return $this->key;
232 }
233
234 /**
235 * put your comment there...
236 *
237 * @param mixed
238 */
239 public static function import($type) {
240 // Filtering parameters!
241 extract(self::trigger('CJTxTable.import', compact('type')));
242 // Implort table file.
243 cssJSToolbox::import("tables:{$type}.php");
244 }
245
246 /**
247 * put your comment there...
248 *
249 * @param mixed $tableKey
250 */
251 public function isValidKey($tableKey = null) {
252 $isValid = false;
253 // Get key!
254 $key = $this->getKey($tableKey);
255 // If any field has a value then its not null key!
256 foreach ($key as $field) {
257 if ($field !== null) {
258 $isValid = $key;
259 break;
260 }
261 }
262 return $isValid;
263 }
264
265 /**
266 * Load record into table!
267 *
268 * @param mixed
269 */
270 public function load($query = null) {
271 $tableKey = null;
272 $key = null;
273 // Query might be an array of keys!
274 if (is_array($query)) {
275 $tableKey = $query;
276 $query = null;
277 }
278 if (!$query) {
279 $item = (array) $this->item;
280 $query['select'] = 'SELECT *';
281 $query['from'] = "FROM {$this->table()}";
282 // Load only if key is not NULL!
283 if ($key = $this->isValidKey($tableKey)) {
284 $query['where'] = 'WHERE ' . implode(' AND ', $this->prepareQueryParameters($key));
285 if ($query = $this->onconcatquery($query)) {
286 // Read DB record!
287 $query = "{$query['select']} {$query['from']} {$query['where']}";
288 $this->setItem($this->dbDriver->getRow($this->onloadquery($query)));
289 }
290 }
291 }
292 else {
293 $this->setItem($this->dbDriver->getRow($this->onloadquery($query)));
294 }
295 return $this;
296 }
297
298 /**
299 * put your comment there...
300 *
301 * @todo Delete method and use CJTMYSQLQuery instead.
302 *
303 * @param mixed $parameters
304 */
305 protected function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) {
306 $prepared = array();
307 // For every parameter esacape name value.
308 foreach ($parameters as $name => $value) {
309 if (!$excludeNulls || ($value !== null)) {
310 if (array_key_exists($name, $this->fields) === FALSE) {
311 throw new Exception("Field:{$name} is not found!");
312 }
313 else {
314 $field = $this->fields[$name];
315 // Escape field name and value.
316 $value = $this->dbDriver->escapeValue($value, $field);
317 // Get name-value operator.
318 $operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator;
319 $prepared[] = "`{$name}`{$operator}{$value}";
320 }
321 }
322 }
323 return $prepared;
324 }
325
326 /**
327 * UPDATE/INSERT
328 *
329 * THIS METHOD STILL DOESNT SUPPORT COMPOUND KEYS!!
330 *
331 * @param mixed $forceInsert
332 * @return CJTxTable
333 */
334 public function save($forceInsert = false) {
335 $keyFieldName = $this->key[0];
336 $item = (array) $this->item;
337 // Don't update id field.
338 $fieldsList = array_diff_key($item, array_flip($this->key));
339 $fieldsList = implode(',', $this->prepareQueryParameters($fieldsList));
340 if (!$forceInsert && $this->get($keyFieldName)) { // Update
341 // Where clause.
342 $condition = implode(' AND ', $this->prepareQueryParameters($this->getKey()));
343 $query = "UPDATE {$this->table()} SET {$fieldsList} WHERE {$condition}";
344 if ($query = $this->onupdate($query)) {
345 $this->dbDriver->update($query)
346 ->processQueue();
347 $this->onupdated();
348 }
349 }
350 else { // Insert.
351 $query = "INSERT {$this->table()} SET {$fieldsList}";
352 if ($query = $this->oninsert($query)) {
353 $this->dbDriver->insert($query)
354 ->processQueue();
355 $this->item->{$keyFieldName} = $this->dbDriver->getInsertId();
356 $this->oninserted();
357 }
358 }
359 return $this;
360 }
361
362 /**
363 * put your comment there...
364 *
365 * @param mixed $prop
366 * @param mixed $value
367 */
368 public function set($prop, $value) {
369 extract($this->onsetfield(compact('prop', 'value')));
370 $this->item->{$prop} = $value;
371 return $this;
372 }
373
374 /**
375 * put your comment there...
376 *
377 * @param mixed $data
378 */
379 public function setData($data) {
380 // Cast to array.
381 $data = (array) $data;
382 // Copy values!
383 foreach ($data as $name => $value) {
384 if ($value !== null) {
385 $this->set($name, $value);
386 }
387 }
388 return $this;
389 }
390
391 /**
392 * put your comment there...
393 *
394 * @param mixed $item
395 * @return CJTxTable
396 */
397 public function setItem($item = null) {
398 // If NULL create empty stdClass, if array cast to stdClass!
399 if (!is_object($item)) {
400 $item = is_array($item) ? (object) $item : new stdClass();
401 }
402 // Set internal item object!
403 $this->item = $item;
404 // Chaining!
405 return $this;
406 }
407
408 /**
409 * put your comment there...
410 *
411 * @param mixed $key
412 */
413 public function setTableKey($key) {
414 $this->key = $key;
415 return $this;
416 }
417
418 /**
419 * put your comment there...
420 *
421 */
422 public function table() {
423 return $this->name;
424 }
425
426 } // End class.
427
428 // Hookable.
429 CJTxTable::define('CJTxTable', array('hookType' =>CJTWordpressEvents::HOOK_FILTER));
430