helpers
1 month ago
tables
1 month ago
aIProviderInterface.php
1 month ago
assets.php
1 month ago
baseObject.php
1 month ago
builderBlock.php
1 month ago
controller.php
1 month ago
date.php
1 month ago
db.php
1 month ago
dispatcher.php
1 month ago
errors.php
1 month ago
field.php
1 month ago
fieldAdapter.php
1 month ago
frame.php
1 month ago
helper.php
1 month ago
html.php
1 month ago
installer.php
1 month ago
installerDbUpdater.php
1 month ago
integration.php
1 month ago
modInstaller.php
1 month ago
model.php
1 month ago
module.php
1 month ago
req.php
1 month ago
response.php
1 month ago
table.php
1 month ago
uri.php
1 month ago
user.php
1 month ago
utils.php
1 month ago
validator.php
1 month ago
view.php
1 month ago
table.php
533 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | abstract class WaicTable { |
| 6 | /** |
| 7 | * ID column name |
| 8 | */ |
| 9 | protected $_id =''; |
| 10 | /** |
| 11 | * Table name |
| 12 | */ |
| 13 | protected $_table = ''; |
| 14 | /** |
| 15 | * Array to store there fields for table |
| 16 | */ |
| 17 | protected $_fields = array(); |
| 18 | protected $_lists = array(); |
| 19 | /** |
| 20 | * Alias for this table, make shure that it ia unique |
| 21 | */ |
| 22 | protected $_alias = ''; |
| 23 | /** |
| 24 | * Table to be joined |
| 25 | */ |
| 26 | protected $_join = array(); |
| 27 | /** |
| 28 | * Limit |
| 29 | */ |
| 30 | protected $_limit = ''; |
| 31 | /** |
| 32 | * Order BY |
| 33 | */ |
| 34 | protected $_order = ''; |
| 35 | /** |
| 36 | * Group BY |
| 37 | */ |
| 38 | protected $_group = ''; |
| 39 | /** |
| 40 | * Table errors data |
| 41 | */ |
| 42 | protected $_errors = array(); |
| 43 | /** |
| 44 | * Escape data before action |
| 45 | */ |
| 46 | protected $_escape = false; |
| 47 | |
| 48 | protected $_limitFrom = ''; |
| 49 | protected $_limitTo = ''; |
| 50 | public function init( $params = array() ) { |
| 51 | } |
| 52 | |
| 53 | public static function getInstance( $table = '' ) { |
| 54 | static $instances = array(); |
| 55 | if (!$table) { |
| 56 | throw new Exception(esc_html('Unknown table [' . $table . ']')); |
| 57 | } |
| 58 | if (!isset($instances[$table])) { |
| 59 | $class = waicStrFirstUp(WAIC_CODE) . 'Table' . waicStrFirstUp($table); |
| 60 | if (class_exists($class)) { |
| 61 | $instances[$table] = new $class(); |
| 62 | } else { |
| 63 | $instances[$table] = null; |
| 64 | } |
| 65 | } |
| 66 | return $instances[$table]; |
| 67 | } |
| 68 | public static function _( $table = '' ) { |
| 69 | return self::getInstance($table); |
| 70 | } |
| 71 | public function innerJoin( $table, $on ) { |
| 72 | $this->_join[] = 'INNER JOIN ' . $table->getTable() . ' ' . $table->alias() . ' ON ' . $table->alias() . '.' . $table->getID() . ' = ' . $this->_alias . '.' . $on; |
| 73 | return $this; |
| 74 | } |
| 75 | public function leftJoin( $table, $on ) { |
| 76 | if ($this->haveField($on)) { |
| 77 | $this->_join[] = 'LEFT JOIN ' . $table->getTable() . ' ' . $table->alias() . ' ON ' . $table->alias() . '.' . $table->getID() . ' = ' . $this->_alias . '.' . $on; |
| 78 | } else { |
| 79 | $this->_join[] = 'LEFT JOIN ' . $table->getTable() . ' ' . $table->alias() . ' ON ' . $table->alias() . '.' . $on . ' = ' . $this->_alias . '.' . $this->getID(); |
| 80 | } |
| 81 | return $this; |
| 82 | } |
| 83 | public function arbitraryJoin( $join ) { |
| 84 | $this->_join[] = $join; |
| 85 | } |
| 86 | public function haveField( $field ) { |
| 87 | return isset($this->_fields[$field]); |
| 88 | } |
| 89 | public function addJoin( $params = array('tbl' => '', 'a' => '', 'on' => '', 'joinOnID' => true, 'joinOn' => '') ) { |
| 90 | $params['joinOnID'] = isset($params['joinOnID']) ? $params['joinOnID'] : true; |
| 91 | $params['joinOn'] = ( $params['joinOnID'] && !isset($params['joinOn']) ) ? $this->_id : $params['joinOn']; |
| 92 | $this->_join[] = 'INNER JOIN ' . $params['tbl'] . ' ' . $params['a'] . ' ON ' . $params['a'] . '.' . $params['on'] . ' = ' . $this->_alias . '.' . $params['joinOn']; |
| 93 | return $this; |
| 94 | } |
| 95 | public function fillFromDB( $id = 0, $where = '' ) { |
| 96 | $res = $this; |
| 97 | if ($id) { |
| 98 | $data = $this->getById($id); |
| 99 | } elseif ($where) { |
| 100 | $data = $this->get('*', $where); |
| 101 | } else { |
| 102 | $data = $this->getAll(); |
| 103 | } |
| 104 | |
| 105 | if ($data) { |
| 106 | if ($id) { |
| 107 | foreach ($data as $k => $v) { |
| 108 | if (isset($this->_fields[$k])) { |
| 109 | $this->_fields[$k]->setValue($v, true); |
| 110 | } |
| 111 | } |
| 112 | } else { |
| 113 | $res = array(); |
| 114 | foreach ($data as $field) { |
| 115 | $row = array(); |
| 116 | foreach ($field as $k => $v) { |
| 117 | if (isset($this->_fields[$k])) { |
| 118 | $row[$k] = waicToeCreateObj('WaicField', array( |
| 119 | $this->_fields[$k]->name, |
| 120 | $this->_fields[$k]->html, |
| 121 | $this->_fields[$k]->type, |
| 122 | $this->_fields[$k]->default, |
| 123 | $this->_fields[$k]->label, |
| 124 | $this->_fields[$k]->maxlen, |
| 125 | $this->_fields[$k]->description, |
| 126 | )); |
| 127 | $row[$k]->setValue($v, true); |
| 128 | } |
| 129 | } |
| 130 | if (!empty($row)) { |
| 131 | $res[] = $row; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return $res; |
| 137 | } |
| 138 | /** |
| 139 | * Return table name |
| 140 | * |
| 141 | * @param bool $transform need to transform to standard WP tables view or not |
| 142 | * @return string table name |
| 143 | */ |
| 144 | public function getTable( $transform = false ) { |
| 145 | if ($transform) { |
| 146 | return WaicDb::prepareQuery($this->_table); |
| 147 | } else { |
| 148 | return $this->_table; |
| 149 | } |
| 150 | } |
| 151 | public function setTable( $table ) { |
| 152 | $this->_table = $table; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get name of ID column |
| 157 | * |
| 158 | * @return string name of ID column |
| 159 | */ |
| 160 | public function getID() { |
| 161 | return $this->_id; |
| 162 | } |
| 163 | public function setID( $id ) { |
| 164 | $this->_id = $id; |
| 165 | } |
| 166 | public function getAll( $fields = '*' ) { |
| 167 | return $this->get($fields); |
| 168 | } |
| 169 | public function getById( $id, $fields = '*', $return = 'row' ) { |
| 170 | $condition = 'WHERE ' . $this->_alias . '.' . $this->_id . ' = "' . ( (int) $id ) . '"'; |
| 171 | return $this->get($fields, $condition, null, $return); |
| 172 | } |
| 173 | protected function _addJoin() { |
| 174 | $res = ''; |
| 175 | if (!empty($this->_join)) { |
| 176 | $res = ' ' . implode(' ', $this->_join); |
| 177 | $this->_join = array(); |
| 178 | } |
| 179 | return $res; |
| 180 | } |
| 181 | /** |
| 182 | * Add LIMIT to SQL |
| 183 | */ |
| 184 | public function limit( $limit = '' ) { |
| 185 | if (is_numeric($limit)) { |
| 186 | $this->_limit = $limit; |
| 187 | } else { |
| 188 | $this->_limit = ''; |
| 189 | } |
| 190 | return $this; |
| 191 | } |
| 192 | public function setLimit( $limit = '' ) { |
| 193 | $this->_limit = $limit; |
| 194 | return $this; |
| 195 | } |
| 196 | public function limitFrom( $limit = '' ) { |
| 197 | if (is_numeric($limit)) { |
| 198 | $this->_limitFrom = (int) $limit; |
| 199 | } |
| 200 | return $this; |
| 201 | } |
| 202 | public function limitTo( $limit = '' ) { |
| 203 | if (is_numeric($limit)) { |
| 204 | $this->_limitTo = (int) $limit; |
| 205 | } |
| 206 | return $this; |
| 207 | } |
| 208 | /** |
| 209 | * Add ORDER BY to SQL |
| 210 | * |
| 211 | * @param mixed $fields |
| 212 | */ |
| 213 | public function orderBy( $fields ) { |
| 214 | if (is_array($fields)) { |
| 215 | $order = implode(',', $fields); |
| 216 | } elseif ('' != $fields) { |
| 217 | $order = $fields; |
| 218 | } |
| 219 | $this->_order = $order; |
| 220 | return $this; |
| 221 | } |
| 222 | /** |
| 223 | * Add GROUP BY to SQL |
| 224 | * |
| 225 | * @param mixed $fields |
| 226 | */ |
| 227 | public function groupBy( $fields ) { |
| 228 | if (is_array($fields)) { |
| 229 | $group = implode(',', $fields); |
| 230 | } elseif ('' != $fields) { |
| 231 | $group = $fields; |
| 232 | } |
| 233 | $this->_group = $group; |
| 234 | return $this; |
| 235 | } |
| 236 | public function get( $fields = '*', $where = '', $tables = '', $return = 'all' ) { |
| 237 | if (!$tables) { |
| 238 | $tables = $this->_table . ' ' . $this->_alias; |
| 239 | } |
| 240 | if (strpos($this->_alias, $fields)) { |
| 241 | $fields = $this->_alias . '.' . $fields; |
| 242 | } |
| 243 | $query = 'SELECT ' . $fields . ' FROM ' . $tables; |
| 244 | $query .= $this->_addJoin(); |
| 245 | if ($where) { |
| 246 | $where = trim($this->_getQueryString($where, 'AND')); |
| 247 | if (!empty($where)) { |
| 248 | if (!preg_match('/^WHERE/i', $where)) { |
| 249 | $where = 'WHERE ' . $where; |
| 250 | } |
| 251 | $query .= ' ' . $where; |
| 252 | } |
| 253 | } |
| 254 | if ('' != $this->_group) { |
| 255 | $query .= ' GROUP BY ' . $this->_group; |
| 256 | $this->_group = ''; |
| 257 | } |
| 258 | if ('' != $this->_order) { |
| 259 | $query .= ' ORDER BY ' . $this->_order; |
| 260 | $this->_order = ''; |
| 261 | } |
| 262 | if ('' != $this->_limit) { |
| 263 | if (is_numeric($this->_limit)) { |
| 264 | $query .= ' LIMIT 0,' . $this->_limit; |
| 265 | } else { |
| 266 | $query .= ' LIMIT ' . $this->_limit; |
| 267 | } |
| 268 | |
| 269 | $this->_limit = ''; |
| 270 | } elseif ( ( '' !== $this->_limitFrom ) && ( '' !== $this->_limitTo ) ) { |
| 271 | $query .= ' LIMIT ' . $this->_limitFrom . ',' . $this->_limitTo; |
| 272 | $this->_limitFrom = ''; |
| 273 | $this->_limitTo = ''; |
| 274 | } |
| 275 | return WaicDb::get($query, $return); |
| 276 | } |
| 277 | public function store( $data, $method = 'INSERT', $where = '' ) { |
| 278 | $this->_clearErrors(); |
| 279 | $method = strtoupper($method); |
| 280 | if ($this->_escape) { |
| 281 | $data = WaicDb::escape($data); |
| 282 | } |
| 283 | $query = ''; |
| 284 | switch ($method) { |
| 285 | case 'INSERT': |
| 286 | $query = 'INSERT INTO '; |
| 287 | if (isset($data[$this->_id]) && empty($data[$this->_id])) { |
| 288 | unset($data[$this->_id]); |
| 289 | } |
| 290 | break; |
| 291 | case 'UPDATE': |
| 292 | $query = 'UPDATE '; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | $fields = $this->_getQueryString($data, ',', true); |
| 297 | if (empty($fields)) { |
| 298 | $this->_addError(esc_html__('Nothing to update', 'ai-copilot-content-generator')); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | $query .= $this->_table . ' SET ' . $fields; |
| 303 | |
| 304 | if (!empty($this->_errors)) { |
| 305 | return false; |
| 306 | } |
| 307 | if ( ( 'UPDATE' == $method ) && !empty($where) ) { |
| 308 | $query .= ' WHERE ' . $this->_getQueryString($where, 'AND'); |
| 309 | } |
| 310 | |
| 311 | if (WaicDb::query($query)) { |
| 312 | if ('INSERT' == $method) { |
| 313 | return WaicDb::lastID(); |
| 314 | } else { |
| 315 | return true; |
| 316 | } |
| 317 | } else { |
| 318 | $this->_addError(WAIC_TEST_MODE ? WaicDb::getError() : esc_html__('Database error. Please contact your developer.', 'ai-copilot-content-generator')); |
| 319 | } |
| 320 | return false; |
| 321 | } |
| 322 | public function insert( $data ) { |
| 323 | return $this->store($data); |
| 324 | } |
| 325 | public function update( $data, $where ) { |
| 326 | if (is_numeric($where)) { |
| 327 | $where = array($this->_id => $where); |
| 328 | } |
| 329 | return $this->store($data, 'UPDATE', $where); |
| 330 | } |
| 331 | public function alias( $alias = null ) { |
| 332 | if (!is_null($alias)) { |
| 333 | $this->_alias = $alias; |
| 334 | } |
| 335 | return $this->_alias; |
| 336 | } |
| 337 | /** |
| 338 | * Delete record(s) |
| 339 | * |
| 340 | * @param mixed $where condition to use in query, if numeric givven - use delete by ID column |
| 341 | * @return query result |
| 342 | */ |
| 343 | public function delete( $where = '' ) { |
| 344 | if (empty($where)) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | $q = 'DELETE FROM ' . $this->_table; |
| 349 | if ($where) { |
| 350 | if (is_numeric($where)) { |
| 351 | $where = array($this->_id => $where); |
| 352 | } |
| 353 | $q .= ' WHERE ' . $this->_getQueryString($where, 'AND'); |
| 354 | } |
| 355 | return WaicDb::query($q); |
| 356 | } |
| 357 | /** |
| 358 | * Convert to database query |
| 359 | * |
| 360 | * @param mixed $data if array given - convert it into string where key - is column name, value - database value to set; |
| 361 | * if key == "additionalCondition" then we will just add value to string |
| 362 | * if string givven - just return it without changes |
| 363 | * @param string $delim delimiter to use in query, recommended - ',', 'AND', 'OR' |
| 364 | * @return string query string |
| 365 | */ |
| 366 | public function _getQueryString( $data, $delim = ',', $validate = false ) { |
| 367 | $res = ''; |
| 368 | if (is_array($data) && !empty($data)) { |
| 369 | foreach ($data as $k => $v) { |
| 370 | if (array_key_exists($k, $this->_fields) || $k == $this->_id) { |
| 371 | $val = $v; |
| 372 | if (isset($this->_fields[$k]) && $this->_fields[$k]->adapt['dbTo']) { |
| 373 | $val = WaicFieldAdapter::_($val, $this->_fields[$k]->adapt['dbTo'], WaicFieldAdapter::DB); |
| 374 | } |
| 375 | if ($validate) { |
| 376 | if (isset($this->_fields[$k]) && is_object($this->_fields[$k])) { |
| 377 | $objForValidation = clone $this->_fields[$k]; |
| 378 | $objForValidation->setValue($val); |
| 379 | $errors = WaicValidator::_($objForValidation, $this); |
| 380 | if ($errors) { |
| 381 | $this->_addError($errors); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | if (isset($this->_fields[$k])) { |
| 386 | switch ($this->_fields[$k]->type) { |
| 387 | case 'int': |
| 388 | case 'tinyint': |
| 389 | $res .= '`' . $k . '` = ' . ( is_null($val) ? 'NULL' : ( (int) $val ) ) . ' ' . $delim . ' '; |
| 390 | break; |
| 391 | case 'float': |
| 392 | $res .= '`' . $k . '` = ' . ( is_null($val) ? 'NULL' : ( (float) $val ) ) . ' ' . $delim . ' '; |
| 393 | break; |
| 394 | case 'decimal': |
| 395 | $res .= '`' . $k . '` = ' . ( is_null($val) ? 'NULL' : ( (float) $val ) ) . ' ' . $delim . ' '; |
| 396 | break; |
| 397 | case 'free': //Just set it as it is |
| 398 | $res .= '`' . $k . '` = ' . ( is_null($val) ? 'NULL' : $val ) . ' ' . $delim . ' '; |
| 399 | break; |
| 400 | default: |
| 401 | $res .= '`' . $k . '` = ' . ( is_null($val) ? 'NULL' : '\'' . $val . '\'' ) . ' ' . $delim . ' '; |
| 402 | break; |
| 403 | } |
| 404 | } else { |
| 405 | $res .= '`' . $k . '` = \'' . $val . '\' ' . $delim . ' '; |
| 406 | } |
| 407 | } elseif ('additionalCondition' == $k) { //just add some string to query |
| 408 | $res .= $v . ' ' . $delim . ' '; |
| 409 | } |
| 410 | } |
| 411 | $res = substr($res, 0, -( strlen($delim) + 1 )); |
| 412 | } elseif (is_string($data)) { |
| 413 | $res = $data; |
| 414 | } |
| 415 | return $res; |
| 416 | } |
| 417 | /** |
| 418 | * Add new FieldWaicWaic for children table (@see class field) |
| 419 | * |
| 420 | * @param string $name name of a field |
| 421 | * @param string $html html type of field (text, textarea, etc. @see html class) |
| 422 | * @param string $type database type (int, varcahr, etc.) |
| 423 | * @param mixed $default default value for this field |
| 424 | * @return object $this - pointer to current object |
| 425 | */ |
| 426 | protected function _addField( $name, $html = 'text', $type = 'other', $default = '', $label = '', $maxlen = 0, $dbAdapt = '', $htmlAdapt = '', $description = '' ) { |
| 427 | $this->_fields[$name] = waicToeCreateObj('WaicField', array($name, $html, $type, $default, $label, $maxlen, $dbAdapt, $htmlAdapt, $description)); |
| 428 | return $this; |
| 429 | } |
| 430 | /** |
| 431 | * Public alias for _addField() method |
| 432 | */ |
| 433 | public function addField() { |
| 434 | $args = func_get_args(); |
| 435 | return call_user_func_array(array($this, '_addField'), $args); |
| 436 | } |
| 437 | public function getFields() { |
| 438 | return $this->_fields; |
| 439 | } |
| 440 | public function getField( $name ) { |
| 441 | return $this->_fields[$name]; |
| 442 | } |
| 443 | public function setLists( $lists ) { |
| 444 | $this->_lists = $lists; |
| 445 | } |
| 446 | public function exists( $value, $field = '' ) { |
| 447 | if (!$field) { |
| 448 | $field = $this->_id; |
| 449 | } |
| 450 | return WaicDb::get("SELECT {$this->_id} FROM {$this->_table} WHERE {$field} = %s", 'one', ARRAY_A, array($value)); |
| 451 | //return WaicDb::get('SELECT ' . $this->_id . ' FROM ' . $this->_table . ' WHERE ' . $field . ' = "' . $value . '"', 'one'); |
| 452 | } |
| 453 | protected function _addError( $error ) { |
| 454 | if (is_array($error)) { |
| 455 | $this->_errors = array_merge($this->_errors, $error); |
| 456 | } else { |
| 457 | $this->_errors[] = $error; |
| 458 | } |
| 459 | } |
| 460 | public function getErrors() { |
| 461 | return $this->_errors; |
| 462 | } |
| 463 | protected function _clearErrors() { |
| 464 | $this->_errors = array(); |
| 465 | } |
| 466 | /** |
| 467 | * Prepare data before send it to database |
| 468 | */ |
| 469 | public function prepareInput( $d = array() ) { |
| 470 | $ignore = isset($d['ignore']) ? $d['ignore'] : array(); |
| 471 | foreach ($this->_fields as $key => $f) { |
| 472 | if ('tinyint' == $f->type) { |
| 473 | if ('true' == $d[$key]) { |
| 474 | $d[$key] = 1; |
| 475 | } |
| 476 | if (empty($d[$key]) && !in_array($key, $ignore)) { |
| 477 | $d[$key] = 0; |
| 478 | } |
| 479 | } |
| 480 | if ('date' == $f->type) { |
| 481 | if (empty($d[$key]) && !in_array($key, $ignore)) { |
| 482 | $d[$key] = '0000-00-00'; |
| 483 | } elseif (!empty($d[$key])) { |
| 484 | $d[$key] = WaicDb::timeToDate($d[$key]); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | $d[$this->_id] = isset($d[$this->_id]) ? intval($d[$this->_id]) : 0; |
| 489 | return $d; |
| 490 | } |
| 491 | /** |
| 492 | * Prepare data after extracting it from database |
| 493 | */ |
| 494 | public function prepareOutput( $d = array() ) { |
| 495 | $ignore = isset($d['ignore']) ? $d['ignore'] : array(); |
| 496 | foreach ($this->_fields as $key => $f) { |
| 497 | switch ($f->type) { |
| 498 | case 'date': |
| 499 | if ('0000-00-00' == $d[$key] || empty($d[$key])) { |
| 500 | $d[$key] = ''; |
| 501 | } else { |
| 502 | $d[$key] = gmdate(WAIC_DATE_FORMAT, WaicDb::dateToTime($d[$key])); |
| 503 | } |
| 504 | break; |
| 505 | case 'int': |
| 506 | case 'tinyint': |
| 507 | if ('true' == $d[$key]) { |
| 508 | $d[$key] = 1; |
| 509 | } |
| 510 | if ('false' == $d[$key]) { |
| 511 | $d[$key] = 0; |
| 512 | } |
| 513 | $d[$key] = (int) $d[$key]; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | $d[$this->_id] = isset($d[$this->_id]) ? intval($d[$this->_id]) : 0; |
| 518 | return $d; |
| 519 | } |
| 520 | public function install( $d = array() ) { |
| 521 | } |
| 522 | public function uninstall( $d = array() ) { |
| 523 | } |
| 524 | public function activate() { |
| 525 | } |
| 526 | public function getLastInsertID() { |
| 527 | return WaicDb::get('SELECT MAX(' . $this->_id . ') FROM ' . $this->_table, 'one'); |
| 528 | } |
| 529 | public function adaptHtml( $val ) { |
| 530 | return htmlspecialchars($val); |
| 531 | } |
| 532 | } |
| 533 |