/
Model.php
177 lines (143 loc) · 4.17 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php namespace api\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
/**
* Parent model inherited by others models. Contains common object's instantiations and property init
*
* @author Luca Brognara
* @date December 2015
*/
abstract class Model extends EloquentModel {
protected $constants;
private $_database;
public function __construct() {
parent::__construct();
$this->constants = require __DIR__ .'/../../config/constants.php';
$this->_database = require __DIR__ .'/../../config/database.php';
}
/**
* Filter for the join tables in read().
* TODO: manage the field datetime
*
* @author Luca Brognara
* @date December 2015
*
* @param Illuminate\Database\Eloquent\Builder $query
* @param array $filters
* @param array $columns
*
* @return array
*/
protected function filter($query, $filters, $columns) {
foreach ($filters as $key => $filter) {
$fields_exploded = explode('.', $filter['field']);
if (count($fields_exploded) > 1) {
$field_name = $fields_exploded[1]; // explode() necessary, because value in the form "table.field"
}
else {
$field_name = $filter['field'];
}
$field_name = explode('.', $filter['field'])[1]; // explode() necessary, because value in the form "table.field"
// if the field exist in the table
if (in_array($field_name, $columns)) {
switch ((isset($filter['like'])) ? $filter['like'] : 0) {
case 0:
default: // field == value
$query->where($field_name, $filter['value']);
break;
case 1: // field == value%
$query->where($field_name, 'LIKE', $filter['value'].'%');
break;
case 2: // field == %value
$query->where($field_name, 'LIKE', '%'.$filter['value']);
break;
case 3: // field == %value%
$query->where($field_name, 'LIKE', '%'.$filter['value'].'%');
break;
}
}
else {
throw new \Exception('invalidFilters', 400);
}
}
return true;
}
/**
* Get the columns of a db's table
*
* @author Luca Brognara
* @date December 2015
*
* @param string $table
* @param string $requestType
* @param string $requestContent
*
* @return array
*/
protected function get_table_columns($table) {
$conn_str = 'pgsql:host='.$this->_database['host'];
$conn_str.= ';dbname='.$this->_database['database'];
$conn = new \PDO($conn_str, $this->_database['username'], $this->_database['password']);
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$sql = "SELECT column_name
FROM information_schema.columns
WHERE table_name = :table;";
$stmt = $conn->prepare($sql);
$stmt->execute(array('table' => $table));
$struttura = array();
while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$struttura[] = $row['column_name'];
}
return $struttura;
}
/**
* Filter the main table of the query by the value passed in $filters
*
* @author Luca Brognara
* @date December 2015
*
* @param Illuminate\Database\Query\Builder $query
* @param array $filters
* @param string $table
*
* @return array
*/
public function scopeFilter($query, $filters, $table) {
if (count($filters) == 0) {
return $query;
}
return $query->where(function($query) use ($filters, $table) {
try {
$this->filter($query, $filters, $this->get_table_columns($table));
}
catch(\Exception $e) {
throw new \Exception($e->getMessage(), $e->getCode());
}
});
}
/**
* Filter the joined table of the query by the value passed in $filters
*
* @author Luca Brognara
* @date December 2015
*
* @param Illuminate\Database\Query\Builder $query
* @param string $model
* @param array $filters
* @param string $filters
*
* @return array
*/
public function scopeFilterBy($query, $model, $filters, $table) {
if (count($filters) == 0) {
return $query;
}
return $query->whereHas($model, function($query) use($filters, $table) {
try {
$this->filter($query, $filters, $this->get_table_columns($table));
}
catch(\Exception $e) {
throw new \Exception($e->getMessage(), $e->getCode());
}
});
}
}