|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\TightORM; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\TightORM\Interfaces\ModelInterface; |
|
6
|
|
|
use HexMakina\Crudites\Interfaces\SelectInterface; |
|
7
|
|
|
|
|
8
|
|
|
class TableModelSelector |
|
9
|
|
|
{ |
|
10
|
|
|
private $model; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(ModelInterface $m) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->model = $m; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function select($filters = [], $options = []): SelectInterface |
|
18
|
|
|
{ |
|
19
|
|
|
$class = get_class($this->model); |
|
20
|
|
|
$table = $class::table(); |
|
21
|
|
|
|
|
22
|
|
|
$Query = $table->select(null, $class::table_alias()); |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if(!isset($options['eager']) || $options['eager'] !== false) |
|
26
|
|
|
{ |
|
27
|
|
|
$Query->eager(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
foreach($table->columns() as $column_name => $column) |
|
32
|
|
|
{ |
|
33
|
|
|
if(isset($filters[$column_name]) && is_string($filters[$column_name])) |
|
34
|
|
|
$Query->aw_eq($column_name, $filters[$column_name]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if(is_subclass_of($event = new $class(), '\HexMakina\kadro\Models\Interfaces\EventInterface')) |
|
38
|
|
|
{ |
|
39
|
|
|
if(!empty($filters['date_start'])) |
|
40
|
|
|
$Query->aw_gte($event->event_field(), $filters['date_start'], $Query->table_label(), ':filter_date_start'); |
|
41
|
|
|
|
|
42
|
|
|
if(!empty($filters['date_stop'])) |
|
43
|
|
|
$Query->aw_lte($event->event_field(), $filters['date_stop'], $Query->table_label(), ':filter_date_stop'); |
|
44
|
|
|
|
|
45
|
|
|
if(empty($options['order_by'])) |
|
46
|
|
|
$Query->order_by([$event->event_field(), 'DESC']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if(isset($filters['content'])) $Query->aw_filter_content($filters['content']); |
|
50
|
|
|
|
|
51
|
|
|
if(isset($filters['ids'])) |
|
52
|
|
|
{ |
|
53
|
|
|
if(empty($filters['ids'])) |
|
54
|
|
|
$Query->and_where('1=0'); // TODO: this is a new low.. find another way to cancel query |
|
55
|
|
|
else $Query->aw_numeric_in('id', $filters['ids'], $Query->table_label()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if(isset($options['order_by'])) // TODO commenting required about the array situation |
|
59
|
|
|
{ |
|
60
|
|
|
$order_by = $options['order_by']; |
|
61
|
|
|
|
|
62
|
|
|
if(is_string($order_by)) |
|
63
|
|
|
$Query->order_by($order_by); |
|
64
|
|
|
|
|
65
|
|
|
elseif(is_array($order_by)) |
|
66
|
|
|
foreach($options['order_by'] as $order_by) |
|
67
|
|
|
{ |
|
68
|
|
|
if(!isset($order_by[2])) |
|
69
|
|
|
array_unshift($order_by, ''); |
|
70
|
|
|
|
|
71
|
|
|
list($order_table, $order_field, $order_direction) = $order_by; |
|
72
|
|
|
$Query->order_by([$order_table ?? '', $order_field, $order_direction]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if(isset($options['limit']) && is_array($options['limit'])) |
|
77
|
|
|
$Query->limit($options['limit'][1], $options['limit'][0]); |
|
78
|
|
|
|
|
79
|
|
|
return $Query; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|