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