Model.php
335 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base Modal class. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category Model |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Models; |
| 15 | |
| 16 | use ReflectionClass; |
| 17 | use wpdb; |
| 18 | |
| 19 | /** |
| 20 | * Responsible for interacting with the database. |
| 21 | * |
| 22 | * Class Model |
| 23 | * |
| 24 | * @package SureTriggers\Model |
| 25 | * @psalm-consistent-constructor |
| 26 | */ |
| 27 | abstract class Model { |
| 28 | |
| 29 | /** |
| 30 | * Table name. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $table; |
| 35 | |
| 36 | /** |
| 37 | * Join table name. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $join_table; |
| 42 | |
| 43 | /** |
| 44 | * Select |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $select; |
| 49 | |
| 50 | /** |
| 51 | * Query output. |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | public $output = OBJECT; |
| 56 | |
| 57 | /** |
| 58 | * Total results count |
| 59 | * |
| 60 | * @var int |
| 61 | */ |
| 62 | public $total; |
| 63 | |
| 64 | /** |
| 65 | * Start from |
| 66 | * |
| 67 | * @var int |
| 68 | */ |
| 69 | public $start; |
| 70 | |
| 71 | /** |
| 72 | * Per page results count |
| 73 | * |
| 74 | * @var int |
| 75 | */ |
| 76 | public $per_page; |
| 77 | |
| 78 | /** |
| 79 | * Pagination Properties |
| 80 | */ |
| 81 | |
| 82 | /** |
| 83 | * Search term |
| 84 | * |
| 85 | * @var string |
| 86 | */ |
| 87 | public $search_term; |
| 88 | |
| 89 | /** |
| 90 | * Current page of the pagination |
| 91 | * |
| 92 | * @var int |
| 93 | */ |
| 94 | public $current_page; |
| 95 | |
| 96 | /** |
| 97 | * Which column should be order |
| 98 | * |
| 99 | * @var string |
| 100 | */ |
| 101 | public $order_by_col; |
| 102 | |
| 103 | /** |
| 104 | * ASC|DESC |
| 105 | * |
| 106 | * @var string |
| 107 | */ |
| 108 | public $order_by = 'DESC'; |
| 109 | |
| 110 | /** |
| 111 | * Order by Sql query |
| 112 | * |
| 113 | * @var string |
| 114 | */ |
| 115 | public $order_by_sql; |
| 116 | |
| 117 | /** |
| 118 | * Primary ID. |
| 119 | * |
| 120 | * @var string |
| 121 | */ |
| 122 | protected $primary_id = 'id'; |
| 123 | |
| 124 | /** |
| 125 | * Database. |
| 126 | * |
| 127 | * @var wpdb |
| 128 | */ |
| 129 | protected $db; |
| 130 | |
| 131 | /** |
| 132 | * It will generate Where SQL |
| 133 | * |
| 134 | * @var array |
| 135 | */ |
| 136 | protected $where = []; |
| 137 | |
| 138 | /** |
| 139 | * Constructor |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | */ |
| 143 | public function __construct() { |
| 144 | global $wpdb; |
| 145 | |
| 146 | $this->db = $wpdb; |
| 147 | |
| 148 | if ( ! empty( $this->table ) ) { |
| 149 | $this->table = $wpdb->prefix . $this->table; |
| 150 | } else { |
| 151 | $this->table = $wpdb->prefix . strtolower( ( new ReflectionClass( $this ) )->getShortName() ); |
| 152 | } |
| 153 | |
| 154 | $this->total = 0; |
| 155 | $this->start = 0; |
| 156 | $this->per_page = 0; |
| 157 | $this->search_term = ''; |
| 158 | $this->current_page = 1; |
| 159 | |
| 160 | $this->order_by_col = $this->primary_id; |
| 161 | $this->order_by_sql = " ORDER BY {$this->order_by_col} {$this->order_by} "; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Initialize query. |
| 166 | * |
| 167 | * @param string $output output. |
| 168 | * @return static|null |
| 169 | */ |
| 170 | public static function init( $output = OBJECT ) { |
| 171 | $_instance = new static(); |
| 172 | $_instance->output = $output; |
| 173 | |
| 174 | return $_instance; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get all results. |
| 179 | * |
| 180 | * @return array|object|void|null |
| 181 | */ |
| 182 | public function all() { |
| 183 | return $this->db->get_results( "SELECT * FROM {$this->table}", $this->output ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Prepare where query on given array. |
| 188 | * |
| 189 | * @param array $data where data. |
| 190 | * @return $this |
| 191 | */ |
| 192 | public function where( $data ) { |
| 193 | foreach ( $data as $key => $value ) { |
| 194 | $this->where[] = "AND {$key} = '{$value}'"; |
| 195 | } |
| 196 | return $this; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the table name. |
| 201 | * |
| 202 | * @return string |
| 203 | */ |
| 204 | public function table() { |
| 205 | return $this->table; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Add selections. |
| 210 | * |
| 211 | * @param string $select select string. |
| 212 | * @return $this |
| 213 | */ |
| 214 | public function select( $select ) { |
| 215 | $this->select = $select; |
| 216 | return $this; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get specific results. |
| 221 | * |
| 222 | * @return array|object|void|null |
| 223 | */ |
| 224 | public function get() { |
| 225 | return $this->db->get_results( "SELECT {$this->get_select()} FROM {$this->table} {$this->get_left_join()} {$this->get_where_sql()} ", $this->output ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Prepare WHERE conditions for sql. |
| 230 | * |
| 231 | * @return string |
| 232 | */ |
| 233 | public function get_where_sql() { |
| 234 | $where_clause = ''; |
| 235 | if ( count( $this->where ) ) { |
| 236 | $where_clause .= implode( ' ', $this->where ); |
| 237 | } |
| 238 | |
| 239 | return " WHERE 1 = 1 {$where_clause} "; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get var from the db. |
| 244 | * |
| 245 | * @return string|null|int |
| 246 | */ |
| 247 | public function get_var() { |
| 248 | return $this->db->get_var( "SELECT {$this->get_select()} FROM {$this->table} {$this->get_left_join()} {$this->get_where_sql()} " ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Find the result by id. |
| 253 | * |
| 254 | * @param int $id id. |
| 255 | * @return array|object|void|null |
| 256 | */ |
| 257 | public function find( $id ) { |
| 258 | return $this->db->get_row( |
| 259 | $this->db->prepare( |
| 260 | "SELECT * FROM {$this->table} WHERE {$this->primary_id} = %d", |
| 261 | $id |
| 262 | ), |
| 263 | $this->output |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get rows. |
| 269 | * |
| 270 | * @param string|null $query SQL query. |
| 271 | * @param int $y Optional. Row to return. Indexed from 0. |
| 272 | * |
| 273 | * @return array|object|void|null |
| 274 | */ |
| 275 | public function get_row( $query = null, $y = 0 ) { |
| 276 | return $this->db->get_row( $query, $this->output, $y ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Prepare query. |
| 281 | * |
| 282 | * @param string $query query string. |
| 283 | * @param array ...$args arguments. |
| 284 | * @return string|void|null |
| 285 | */ |
| 286 | public function prepare( $query, ...$args ) { |
| 287 | return $this->db->prepare( $query, ...$args ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Generate search query |
| 292 | * |
| 293 | * @param string $search_term Search Term. |
| 294 | * |
| 295 | * @return $this |
| 296 | */ |
| 297 | public function search( $search_term = '' ) { |
| 298 | $this->search_term = $search_term; |
| 299 | |
| 300 | return $this; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Set order by value DESC |
| 305 | * |
| 306 | * @param string $col Table Column. |
| 307 | * |
| 308 | * @return $this |
| 309 | */ |
| 310 | public function desc( $col = '' ) { |
| 311 | if ( ! empty( $col ) ) { |
| 312 | $this->order_by_col = $col; |
| 313 | } |
| 314 | $this->order_by = 'DESC'; |
| 315 | |
| 316 | return $this; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Set order by value ASC |
| 321 | * |
| 322 | * @param string $col Order by Column. |
| 323 | * |
| 324 | * @return $this |
| 325 | */ |
| 326 | public function asc( $col = '' ) { |
| 327 | if ( ! empty( $col ) ) { |
| 328 | $this->order_by_col = $col; |
| 329 | } |
| 330 | $this->order_by = 'ASC'; |
| 331 | |
| 332 | return $this; |
| 333 | } |
| 334 | } |
| 335 |