1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Expendable\Models; |
4
|
|
|
|
5
|
|
|
use Distilleries\Expendable\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
|
28 |
|
public static function getChoice() |
19
|
|
|
{ |
20
|
|
|
|
21
|
28 |
|
$data = self::all(); |
22
|
28 |
|
$result = []; |
23
|
28 |
|
foreach ($data as $item) { |
24
|
28 |
|
$result[$item['id']] = isset($item['libelle']) ? $item['libelle'] : $item['id']; |
25
|
|
|
} |
26
|
|
|
|
27
|
28 |
|
return $result; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function scopeSearch($query, $searchQuery) |
31
|
|
|
{ |
32
|
|
|
|
33
|
8 |
|
return $query->where(function ($query) use ($searchQuery) { |
34
|
8 |
|
$columns = $this->getAllColumnsNames(); |
35
|
|
|
|
36
|
8 |
|
foreach ($columns as $column) { |
37
|
8 |
|
$column = $this->isReserved($column) ? '"' . $column . '"' : $column; |
38
|
|
|
|
39
|
8 |
|
if ((DB::connection()->getDriverName()) == 'oracle') { |
40
|
|
|
$query->orWhereRaw('LOWER(' . $column . ') like ? ESCAPE \'\\\'', ['%' . Security::escapeLike(strtolower($searchQuery)) . '%']); |
41
|
|
|
} else { |
42
|
8 |
|
$query->orwhere($column, 'like', '%' . Security::escapeLike($searchQuery,'\\\'') . '%'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
} |
46
|
8 |
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
12 |
|
public function getAllColumnsNames() |
50
|
|
|
{ |
51
|
12 |
|
switch (DB::connection()->getDriverName()) { |
52
|
12 |
View Code Duplication |
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
|
12 |
View Code Duplication |
case 'sqlite': |
|
|
|
|
59
|
12 |
|
$query = "PRAGMA table_info('" . $this->getTable()."')"; |
60
|
12 |
|
$column_name = 'name'; |
61
|
12 |
|
$reverse = true; |
62
|
12 |
|
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
|
12 |
|
$columns = []; |
90
|
|
|
|
91
|
12 |
|
foreach (DB::select($query) as $column) { |
92
|
12 |
|
$columns[] = $column->$column_name; |
93
|
|
|
} |
94
|
|
|
|
95
|
12 |
|
if ($reverse) { |
96
|
12 |
|
$columns = array_reverse($columns); |
97
|
|
|
} |
98
|
|
|
|
99
|
12 |
|
return $columns; |
100
|
|
|
} |
101
|
|
|
|
102
|
12 |
|
public function scopeBetweenCreate($query, $start, $end) |
103
|
|
|
{ |
104
|
12 |
|
return $query->whereBetween($this->getTable() . '.created_at', [$start, $end]); |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
public function scopeBetweenUpdate($query, $start, $end) |
108
|
|
|
{ |
109
|
8 |
|
return $query->whereBetween($this->getTable() . '.updated_at', [$start, $end]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.