| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base Model |
| 4 |
* |
| 5 |
* @package Tutor\Models |
| 6 |
* @author Themeum <support@themeum.com> |
| 7 |
* @link https://themeum.com |
| 8 |
* @since 3.7.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Tutor\Models; |
| 12 |
|
| 13 |
use Tutor\Helpers\QueryHelper; |
| 14 |
|
| 15 |
/** |
| 16 |
* BaseModel Class. |
| 17 |
* |
| 18 |
* @since 3.7.0 |
| 19 |
*/ |
| 20 |
abstract class BaseModel { |
| 21 |
/** |
| 22 |
* WP db instance |
| 23 |
* |
| 24 |
* @var \wpdb |
| 25 |
*/ |
| 26 |
protected $db; |
| 27 |
|
| 28 |
/** |
| 29 |
* Table name |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $table_name; |
| 34 |
|
| 35 |
/** |
| 36 |
* Primary key of the table |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $primary_key = 'id'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Guard which fields can not be filled |
| 44 |
* |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
protected $guarded = array( 'id' ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Set which fields can be filled |
| 51 |
* |
| 52 |
* @var string|array star(*) means all are fillable. |
| 53 |
*/ |
| 54 |
protected $fillable = '*'; |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor |
| 58 |
*/ |
| 59 |
public function __construct() { |
| 60 |
global $wpdb; |
| 61 |
$this->db = $wpdb; |
| 62 |
|
| 63 |
if ( $this->is_set_table() && ! $this->has_table_prefix() ) { |
| 64 |
$this->table_name = $this->db->prefix . $this->table_name; |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Check model is associated with a db table. |
| 71 |
* |
| 72 |
* @return boolean |
| 73 |
*/ |
| 74 |
public function is_set_table() { |
| 75 |
return isset( $this->table_name ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Check table as table prefix. |
| 80 |
* |
| 81 |
* @return boolean |
| 82 |
*/ |
| 83 |
public function has_table_prefix() { |
| 84 |
return strpos( $this->table_name, $this->db->prefix ) === 0; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get table name. |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function get_table_name() { |
| 93 |
return $this->table_name; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get table fields |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public function get_fields() { |
| 102 |
$fields = array(); |
| 103 |
if ( isset( $this->table_name ) ) { |
| 104 |
foreach ( $this->db->get_col( 'DESC ' . $this->table_name, 0 ) as $column_name ) { |
| 105 |
$fields[] = $column_name; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return $fields; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Filter inputs. |
| 114 |
* |
| 115 |
* @param array $inputs user inputs. |
| 116 |
* |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
private function filter_inputs( $inputs ) { |
| 120 |
$table_fields = $this->get_fields(); |
| 121 |
|
| 122 |
// Remove unwanted user input key. |
| 123 |
$inputs = array_intersect_key( $inputs, array_flip( $table_fields ) ); |
| 124 |
|
| 125 |
// Handle fillable fields. |
| 126 |
if ( is_array( $this->fillable ) ) { |
| 127 |
$inputs = array_intersect_key( $inputs, array_flip( $this->fillable ) ); |
| 128 |
} |
| 129 |
|
| 130 |
// Handle guarded fields. |
| 131 |
if ( is_array( $this->guarded ) ) { |
| 132 |
$inputs = array_diff_key( $inputs, array_flip( $this->guarded ) ); |
| 133 |
} |
| 134 |
|
| 135 |
return $inputs; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get a record. |
| 140 |
* |
| 141 |
* @param array $where where clause. |
| 142 |
* |
| 143 |
* @return object|false object or false when not found. |
| 144 |
*/ |
| 145 |
public function get_row( $where ) { |
| 146 |
$result = false; |
| 147 |
if ( $this->is_set_table() ) { |
| 148 |
$result = QueryHelper::get_row( $this->table_name, $where, $this->primary_key ); |
| 149 |
} |
| 150 |
|
| 151 |
return $result; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get all record from table. |
| 156 |
* |
| 157 |
* @param array $where where clause. |
| 158 |
* @param string $order_by order by. |
| 159 |
* @param string $order order. |
| 160 |
* @param int $limit limit. |
| 161 |
* |
| 162 |
* @return array|false list of rows or false when table not set. |
| 163 |
*/ |
| 164 |
public function get_all( $where, $order_by = 'id', $order = 'DESC', $limit = -1 ) { |
| 165 |
$results = false; |
| 166 |
$order_by_column = $order_by === $this->primary_key ? $this->primary_key : $order_by; |
| 167 |
if ( $this->is_set_table() ) { |
| 168 |
$results = QueryHelper::get_all( $this->table_name, $where, $order_by_column, $limit, $order ); |
| 169 |
} |
| 170 |
|
| 171 |
return $results; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get column values |
| 176 |
* |
| 177 |
* @param string $column_name column name. |
| 178 |
* @param array $where where. |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public function get_col( $column_name, $where ) { |
| 183 |
$where_clause = count( $where ) ? 'WHERE ' . QueryHelper::prepare_where_clause( $where ) : ''; |
| 184 |
$query = $this->db->prepare( "SELECT {$column_name} FROM {$this->table_name} {$where_clause}" ); |
| 185 |
|
| 186 |
return $this->db->get_col( $query ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get table record with pagination. |
| 191 |
* |
| 192 |
* @param integer $per_page per page record. |
| 193 |
* @param integer $page current page number. |
| 194 |
* @param array $args options like alias, select, where, search, joins, groupby, having, orderby. |
| 195 |
* |
| 196 |
* @return object|false object contains total_record, total_page, per_page, current_page, data. |
| 197 |
*/ |
| 198 |
public function paginate( $per_page = 10, $page = 1, $args = array() ) { |
| 199 |
if ( ! $this->is_set_table() ) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
$alias = $args['alias'] ?? 'main'; |
| 204 |
$table_with_alias = "{$this->table_name} AS {$alias}"; |
| 205 |
|
| 206 |
$select_clause = '*'; |
| 207 |
$where_clause = ''; |
| 208 |
|
| 209 |
if ( isset( $args['select'] ) ) { |
| 210 |
$select = $args['select']; |
| 211 |
if ( is_array( $select ) && count( $select ) ) { |
| 212 |
$select_clause = implode( ',', $args['select'] ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
$join_clause = ''; |
| 217 |
if ( isset( $args['joins'] ) && is_array( $args['joins'] ) ) { |
| 218 |
foreach ( $args['joins'] as $join ) { |
| 219 |
$type = isset( $join['type'] ) ? strtoupper( $join['type'] ) : 'LEFT'; |
| 220 |
$table = $join['table']; |
| 221 |
$on = $join['on']; |
| 222 |
$join_clause .= " {$type} JOIN {$table} ON {$on} "; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
$groupby_clause = ''; |
| 227 |
if ( isset( $args['groupby'] ) && $args['groupby'] ) { |
| 228 |
$groupby_clause = 'GROUP BY ' . $args['groupby']; |
| 229 |
} |
| 230 |
|
| 231 |
$having_clause = ''; |
| 232 |
if ( isset( $args['having'] ) && $args['having'] ) { |
| 233 |
$having_clause = 'HAVING ' . $args['having']; |
| 234 |
} |
| 235 |
|
| 236 |
if ( isset( $args['where'] ) && is_array( $args['where'] ) ) { |
| 237 |
$where = $args['where']; |
| 238 |
$where_clause = count( $where ) ? 'WHERE ' . QueryHelper::prepare_where_clause( $where ) : ''; |
| 239 |
} |
| 240 |
|
| 241 |
if ( isset( $args['search'] ) && is_array( $args['search'] ) ) { |
| 242 |
if ( count( $args['search'] ) ) { |
| 243 |
$where_clause .= ( empty( $where_clause ) ? 'WHERE ' : ' AND ' ) . QueryHelper::prepare_like_clause( $args['search'], 'AND' ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
$orderby = $args['orderby'] ?? $this->primary_key; |
| 248 |
$order = 'DESC' === strtoupper( $args['order'] ?? 'DESC' ) ? 'DESC' : 'ASC'; |
| 249 |
$orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" ); |
| 250 |
|
| 251 |
$page = max( $page, 1 ); |
| 252 |
$offset = ( $page - 1 ) * $per_page; |
| 253 |
if ( $offset < 0 ) { |
| 254 |
$offset = 0; |
| 255 |
} |
| 256 |
|
| 257 |
$sql_query = $this->db->prepare( |
| 258 |
"SELECT SQL_CALC_FOUND_ROWS {$select_clause} |
| 259 |
FROM {$table_with_alias} {$join_clause} {$where_clause} {$groupby_clause} {$having_clause} |
| 260 |
ORDER BY {$orderby_sql} |
| 261 |
LIMIT %d, %d", |
| 262 |
$offset, |
| 263 |
$per_page |
| 264 |
); |
| 265 |
|
| 266 |
$rows = $this->db->get_results( $sql_query ); |
| 267 |
|
| 268 |
$has_records = is_array( $rows ) && count( $rows ); |
| 269 |
$total_record = (int) $has_records ? $this->db->get_var( 'SELECT FOUND_ROWS()' ) : 0; |
| 270 |
$total_page = (int) ceil( $total_record / $per_page ); |
| 271 |
$pagination = array( |
| 272 |
'total_record' => (int) $total_record, |
| 273 |
'per_page' => $per_page, |
| 274 |
'current_page' => $page, |
| 275 |
'total_page' => $total_page, |
| 276 |
'data' => $rows, |
| 277 |
); |
| 278 |
|
| 279 |
return (object) $pagination; |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Count record. |
| 285 |
* |
| 286 |
* @param array $where where clause. |
| 287 |
* |
| 288 |
* @return int |
| 289 |
*/ |
| 290 |
public function count( $where ) { |
| 291 |
if ( ! $this->is_set_table() ) { |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
return QueryHelper::get_count( $this->table_name, $where, array(), $this->primary_key ); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Create record |
| 300 |
* |
| 301 |
* @since 3.0.0 |
| 302 |
* |
| 303 |
* @param array $data data. |
| 304 |
* |
| 305 |
* @return int|false id or false when creation failed. |
| 306 |
*/ |
| 307 |
public function create( $data ) { |
| 308 |
$insert = false; |
| 309 |
$filtered = $this->filter_inputs( $data ); |
| 310 |
|
| 311 |
if ( $this->is_set_table() && count( $filtered ) ) { |
| 312 |
$insert = $this->db->insert( $this->table_name, $filtered ); |
| 313 |
if ( $insert ) { |
| 314 |
return $this->db->insert_id; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
return $insert; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Update record |
| 323 |
* |
| 324 |
* @since 3.0.0 |
| 325 |
* |
| 326 |
* @param int $id id. |
| 327 |
* @param array $data data. |
| 328 |
* |
| 329 |
* @return int|false id or false when failed. |
| 330 |
*/ |
| 331 |
public function update( $id, $data ) { |
| 332 |
$update = false; |
| 333 |
$filtered = $this->filter_inputs( $data ); |
| 334 |
|
| 335 |
if ( $this->is_set_table() && count( $filtered ) ) { |
| 336 |
$this->db->update( $this->table_name, $filtered, array( $this->primary_key => $id ) ); |
| 337 |
$update = $this->db->last_error ? false : $id; |
| 338 |
} |
| 339 |
|
| 340 |
return $update; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Delete a record. |
| 345 |
* |
| 346 |
* @since 3.0.0 |
| 347 |
* |
| 348 |
* @param int $id id. |
| 349 |
* |
| 350 |
* @return bool |
| 351 |
*/ |
| 352 |
public function delete( $id ) { |
| 353 |
$deleted = false; |
| 354 |
if ( $this->is_set_table() ) { |
| 355 |
$deleted = (bool) $this->db->delete( $this->table_name, array( $this->primary_key => $id ) ); |
| 356 |
} |
| 357 |
|
| 358 |
return $deleted; |
| 359 |
} |
| 360 |
} |
| 361 |
|