1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Expendable\Models; |
4
|
|
|
|
5
|
|
|
use Distilleries\Security\Helpers\Security; |
6
|
|
|
use Distilleries\Expendable\Models\Traits\ReservedKeyWord; |
7
|
|
|
use Distilleries\Expendable\Models\Traits\ReservedKeyWordTrait; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use \DB; |
10
|
|
|
use \Exception; |
11
|
|
|
|
12
|
|
|
class BaseModel extends Model implements ReservedKeyWord |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
use ReservedKeyWordTrait; |
16
|
|
|
|
17
|
|
|
|
18
|
14 |
|
public static function getChoice() |
19
|
|
|
{ |
20
|
|
|
|
21
|
14 |
|
$data = self::all(); |
22
|
14 |
|
$result = []; |
23
|
14 |
|
foreach ($data as $item) { |
24
|
14 |
|
$result[$item['id']] = isset($item['libelle']) ? $item['libelle'] : $item['id']; |
25
|
|
|
} |
26
|
|
|
|
27
|
14 |
|
return $result; |
28
|
|
|
} |
29
|
|
|
|
30
|
4 |
|
public function scopeSearch($query, $searchQuery) |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
return $query->where(function($query) use ($searchQuery) { |
34
|
4 |
|
$columns = $this->getAllColumnsNames(); |
35
|
|
|
|
36
|
4 |
|
foreach ($columns as $column) { |
37
|
4 |
|
$column = $this->isReserved($column) ? '"'.$column.'"' : $column; |
38
|
|
|
|
39
|
4 |
|
if ((DB::connection()->getDriverName()) == 'oracle') { |
40
|
|
|
$query->orWhereRaw('LOWER('.$column.') like ? ESCAPE \'\\\'', ['%'.Security::escapeLike(strtolower($searchQuery)).'%']); |
41
|
|
|
} else { |
42
|
4 |
|
$query->orwhere($column, 'like', '%'.Security::escapeLike($searchQuery, '\\\'').'%'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
} |
46
|
4 |
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
public function getAllColumnsNames() |
50
|
|
|
{ |
51
|
6 |
|
switch (DB::connection()->getDriverName()) { |
52
|
3 |
|
case 'pgsql': |
53
|
|
|
$query = "SELECT column_name FROM information_schema.columns WHERE table_name = '".$this->getTable()."'"; |
54
|
|
|
$column_name = 'column_name'; |
55
|
|
|
$reverse = true; |
56
|
|
|
break; |
57
|
|
|
|
58
|
3 |
|
case 'sqlite': |
59
|
6 |
|
$query = "PRAGMA table_info('".$this->getTable()."')"; |
60
|
6 |
|
$column_name = 'name'; |
61
|
6 |
|
$reverse = true; |
62
|
6 |
|
break; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
case 'mysql': |
66
|
|
|
$query = 'SHOW COLUMNS FROM '.$this->getTable(); |
67
|
|
|
$column_name = 'Field'; |
68
|
|
|
$reverse = false; |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case 'sqlsrv': |
72
|
|
|
$parts = explode('.', $this->getTable()); |
73
|
|
|
$num = (count($parts) - 1); |
74
|
|
|
$table = $parts[$num]; |
75
|
|
|
$query = "SELECT column_name FROM ".DB::connection()->getConfig('database').".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'".$table."'"; |
76
|
|
|
$column_name = 'column_name'; |
77
|
|
|
$reverse = false; |
78
|
|
|
break; |
79
|
|
|
case 'oracle': |
80
|
|
|
$query = 'SELECT COLUMN_NAME from ALL_TAB_COLUMNS WHERE TABLE_NAME=\''.strtoupper($this->getTable()).'\' AND DATA_TYPE <> \'CLOB\' AND DATA_TYPE <> \'NUMBER\' AND DATA_TYPE <> \'TIMESTAMP\''; |
81
|
|
|
$column_name = 'column_name'; |
82
|
|
|
$reverse = false; |
83
|
|
|
break; |
84
|
|
|
default: |
85
|
|
|
$error = 'Database driver not supported: '.DB::connection()->getConfig('driver'); |
86
|
|
|
throw new Exception($error); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
$columns = []; |
90
|
|
|
|
91
|
6 |
|
foreach (DB::select($query) as $column) { |
92
|
6 |
|
$columns[] = $column->$column_name; |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
if ($reverse) { |
96
|
6 |
|
$columns = array_reverse($columns); |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
return $columns; |
100
|
|
|
} |
101
|
|
|
|
102
|
10 |
|
public function scopeBetweenCreate($query, $start, $end) |
103
|
|
|
{ |
104
|
10 |
|
return $query->whereBetween($this->getTable().'.created_at', [$start, $end]); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
public function scopeBetweenUpdate($query, $start, $end) |
108
|
|
|
{ |
109
|
4 |
|
return $query->whereBetween($this->getTable().'.updated_at', [$start, $end]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |