post-meta-view.php
3 years ago
relation.php
3 years ago
view-base-count-trait.php
3 years ago
view-base.php
3 years ago
view-custom-table-base.php
3 years ago
view-base.php
401 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Db_Queries\Views; |
| 5 | |
| 6 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 7 | use Jet_Form_Builder\Db_Queries\Execution_Builder; |
| 8 | use Jet_Form_Builder\Db_Queries\Query_Builder; |
| 9 | use Jet_Form_Builder\Db_Queries\Query_Cache_Builder; |
| 10 | use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies; |
| 11 | use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies_Interface; |
| 12 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 13 | use spec\HubSpot\Discovery\Cms\Blogs\Tags\DiscoverySpec; |
| 14 | |
| 15 | abstract class View_Base implements Model_Dependencies_Interface { |
| 16 | |
| 17 | use Model_Dependencies; |
| 18 | |
| 19 | const FROM_HIGH_TO_LOW = 'DESC'; |
| 20 | const FROM_LOW_TO_HIGH = 'ASC'; |
| 21 | |
| 22 | protected $limit = array(); |
| 23 | protected $conditions = array(); |
| 24 | protected $order_by = array(); |
| 25 | protected $select = array( '*' ); |
| 26 | |
| 27 | /** @var Relation[] */ |
| 28 | protected $relations = array(); |
| 29 | protected $filters = array(); |
| 30 | |
| 31 | abstract public function table(): string; |
| 32 | |
| 33 | public function select_columns(): array { |
| 34 | return $this->select; |
| 35 | } |
| 36 | |
| 37 | public function always_conditions(): array { |
| 38 | return array(); |
| 39 | } |
| 40 | |
| 41 | public function custom_where(): string { |
| 42 | return ''; |
| 43 | } |
| 44 | |
| 45 | public function set_table_args( array $table_args ) { |
| 46 | $offset = $table_args['offset'] ?? 0; |
| 47 | $limit = $table_args['limit'] ?? 15; |
| 48 | $filters = $table_args['filters'] ?? array(); |
| 49 | |
| 50 | $this->set_limit( array( $offset, $limit ) ); |
| 51 | $this->set_filters( $filters ); |
| 52 | |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | public function set_filters( array $filters ) { |
| 57 | $this->filters = esc_sql( $filters ); |
| 58 | |
| 59 | return $this; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param $callable |
| 64 | * |
| 65 | * @return $this |
| 66 | * @throws Query_Builder_Exception |
| 67 | */ |
| 68 | public function with( $callable ): View_Base { |
| 69 | if ( is_string( $callable ) ) { |
| 70 | $callable = array( $this, $callable ); |
| 71 | } |
| 72 | if ( ! is_callable( $callable ) ) { |
| 73 | throw new Query_Builder_Exception( 'Relation is not callable.' ); |
| 74 | } |
| 75 | $this->relations[] = call_user_func( $callable ); |
| 76 | |
| 77 | return $this; |
| 78 | } |
| 79 | |
| 80 | public function get_prepared_join( Query_Builder $builder ) { |
| 81 | } |
| 82 | |
| 83 | public function set_conditions( array $conditions ): View_Base { |
| 84 | $this->conditions = array_merge( $this->always_conditions(), $conditions ); |
| 85 | |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | public function conditions(): array { |
| 90 | if ( empty( $this->conditions ) ) { |
| 91 | $this->set_conditions( array() ); |
| 92 | } |
| 93 | |
| 94 | return $this->conditions; |
| 95 | } |
| 96 | |
| 97 | public function set_limit( array $limit ): View_Base { |
| 98 | $this->limit = array_map( 'intval', $limit ); |
| 99 | |
| 100 | return $this; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return int[] |
| 105 | */ |
| 106 | public function limit(): array { |
| 107 | return $this->limit; |
| 108 | } |
| 109 | |
| 110 | public function set_order_by( array $order_by ): View_Base { |
| 111 | if ( ! in_array( $order_by, array( self::FROM_HIGH_TO_LOW, self::FROM_LOW_TO_HIGH ), true ) ) { |
| 112 | return $this; |
| 113 | } |
| 114 | |
| 115 | $this->order_by = $order_by; |
| 116 | |
| 117 | return $this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return string[] |
| 122 | */ |
| 123 | public function order_by(): array { |
| 124 | return $this->order_by; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the column name with table prefix |
| 129 | * |
| 130 | * @param $column |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | public function column( $column ): string { |
| 135 | if ( is_string( $column ) ) { |
| 136 | return "`{$this->table()}`.`{$column}`"; |
| 137 | } |
| 138 | |
| 139 | if ( isset( $column['as'] ) ) { |
| 140 | return $column['as']; |
| 141 | } |
| 142 | |
| 143 | $name = $column['column'] ?? false; |
| 144 | $table = empty( $column['table'] ) ? $this->table() : $column['table']; |
| 145 | |
| 146 | if ( ! $name ) { |
| 147 | wp_die( 'Please set the column name.', 'View Base Error' ); |
| 148 | } |
| 149 | |
| 150 | return "`{$table}`.`{$name}`"; |
| 151 | } |
| 152 | |
| 153 | protected function prepare_row( $row ) { |
| 154 | if ( ! $row ) { |
| 155 | return array(); |
| 156 | } |
| 157 | foreach ( $row as $column => $value ) { |
| 158 | $parts = explode( '.', $column ); |
| 159 | |
| 160 | if ( ! isset( $parts[1] ) ) { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | $row[ $parts[0] ][ $parts[1] ] = $value; |
| 165 | unset( $row[ $column ] ); |
| 166 | } |
| 167 | |
| 168 | return $row; |
| 169 | } |
| 170 | |
| 171 | protected function prepare_values( $values ) { |
| 172 | return $values; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param $row |
| 177 | * |
| 178 | * @return array |
| 179 | * @throws Query_Builder_Exception |
| 180 | */ |
| 181 | public function get_prepared_row( $row ) { |
| 182 | $prepared = $this->prepare_row( $row ); |
| 183 | |
| 184 | if ( empty( $prepared ) ) { |
| 185 | throw new Query_Builder_Exception( $this->empty_message(), $row ); |
| 186 | } |
| 187 | |
| 188 | return $prepared; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param $values |
| 193 | * |
| 194 | * @return array |
| 195 | * @throws Query_Builder_Exception |
| 196 | */ |
| 197 | public function get_prepared_values( $values ) { |
| 198 | $prepared = $this->prepare_values( $values ); |
| 199 | |
| 200 | if ( empty( $prepared ) ) { |
| 201 | throw new Query_Builder_Exception( $this->empty_message(), $values ); |
| 202 | } |
| 203 | |
| 204 | return $prepared; |
| 205 | } |
| 206 | |
| 207 | protected function empty_message(): string { |
| 208 | return __( 'Empty row.', 'jet-form-builder' ); |
| 209 | } |
| 210 | |
| 211 | public static function get_paginated_args( $args ): array { |
| 212 | $base = array_merge( |
| 213 | array( |
| 214 | 'limit' => 25, |
| 215 | 'sort' => self::FROM_HIGH_TO_LOW, |
| 216 | 'page' => 1, |
| 217 | ), |
| 218 | $args |
| 219 | ); |
| 220 | |
| 221 | $base['offset'] = static::get_offset( $args ); |
| 222 | |
| 223 | return $base; |
| 224 | } |
| 225 | |
| 226 | public static function get_offset( $args ): int { |
| 227 | return 1 === $args['page'] ? 0 : ( ( $args['page'] - 1 ) * $args['limit'] ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @param $columns |
| 232 | * |
| 233 | * @return View_Base |
| 234 | */ |
| 235 | public static function find( $columns ): View_Base { |
| 236 | $conditions = static::prepare_columns( $columns ); |
| 237 | |
| 238 | return ( new static() )->set_conditions( $conditions ); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | public static function findOne( $columns ): View_Base { |
| 243 | return static::find( $columns )->set_limit( array( 1 ) ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @param $primary_id |
| 248 | * |
| 249 | * @return array |
| 250 | * @throws Query_Builder_Exception |
| 251 | */ |
| 252 | public static function findById( $primary_id ): array { |
| 253 | return static::find( array( 'id' => $primary_id ) ) |
| 254 | ->set_limit( array( 1 ) ) |
| 255 | ->query() |
| 256 | ->query_one(); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @return array |
| 261 | * @throws Query_Builder_Exception |
| 262 | */ |
| 263 | public static function all(): array { |
| 264 | return ( new static() )->query()->query_all(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @return array |
| 269 | * @throws Query_Builder_Exception |
| 270 | */ |
| 271 | public static function one(): array { |
| 272 | return ( new static() )->query()->query_one(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return array |
| 277 | * @throws Query_Builder_Exception |
| 278 | */ |
| 279 | public static function values(): array { |
| 280 | return ( new static() )->query()->query_col(); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @param $where |
| 285 | * |
| 286 | * @return int |
| 287 | * @throws Query_Builder_Exception |
| 288 | */ |
| 289 | public static function delete( $where ): int { |
| 290 | return Execution_Builder::instance()->view_delete( static::create( $where ) ); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @param array $columns |
| 295 | * @param array $where |
| 296 | * |
| 297 | * @return int |
| 298 | * @throws Sql_Exception |
| 299 | */ |
| 300 | public static function update( array $columns, array $where ): int { |
| 301 | return Execution_Builder::instance()->view_update( $columns, static::create( $where ) ); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param array $columns |
| 306 | * |
| 307 | * @return string |
| 308 | */ |
| 309 | public function build_set( array $columns ): string { |
| 310 | $columns = $this->attach_columns( $columns ); |
| 311 | |
| 312 | return Query_Builder::build_set( $columns ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Prepare columns for build conditions |
| 317 | * |
| 318 | * @param $columns |
| 319 | * |
| 320 | * @return array |
| 321 | */ |
| 322 | public static function prepare_columns( $columns ): array { |
| 323 | $conditions = array(); |
| 324 | |
| 325 | if ( ! is_array( $columns ) ) { |
| 326 | return array( |
| 327 | array( |
| 328 | 'type' => 'equal_column', |
| 329 | 'values' => array( 'id', $columns ), |
| 330 | ), |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | foreach ( $columns as $column => $value ) { |
| 335 | if ( is_numeric( $column ) ) { |
| 336 | $conditions[] = $value; |
| 337 | } else { |
| 338 | $conditions[] = array( |
| 339 | 'type' => 'equal_column', |
| 340 | 'values' => array( $column, $value ), |
| 341 | ); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return $conditions; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @param array $columns |
| 350 | * |
| 351 | * @return array |
| 352 | */ |
| 353 | public function attach_columns( array $columns ): array { |
| 354 | foreach ( $columns as $name => $value ) { |
| 355 | if ( is_numeric( $name ) ) { |
| 356 | continue; |
| 357 | } |
| 358 | $columns[ $this->column( $name ) ] = $value; |
| 359 | unset( $columns[ $name ] ); |
| 360 | } |
| 361 | |
| 362 | return $columns; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * @param mixed $where |
| 367 | * |
| 368 | * @return View_Base |
| 369 | */ |
| 370 | public static function create( $where ): View_Base { |
| 371 | $conditions = static::prepare_columns( $where ); |
| 372 | |
| 373 | return ( new static() )->set_conditions( $conditions ); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /** |
| 378 | * @return Query_Builder |
| 379 | * @throws Query_Builder_Exception |
| 380 | */ |
| 381 | public function query(): Query_Builder { |
| 382 | $this->prepare_dependencies(); |
| 383 | |
| 384 | return ( new Query_Cache_Builder() )->set_view( $this ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @throws Query_Builder_Exception |
| 389 | */ |
| 390 | public function prepare_dependencies() { |
| 391 | foreach ( $this->get_dependencies() as $model ) { |
| 392 | $model->create(); |
| 393 | |
| 394 | foreach ( $model->get_migrations() as $migration ) { |
| 395 | throw new Query_Builder_Exception( get_class( $model ) . ' is not updated' ); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | } |
| 401 |