1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 1/26/2019 |
6
|
|
|
* Time: 6:00 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\types; |
10
|
|
|
|
11
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\DefaultOption; |
12
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join; |
13
|
|
|
use crocodicstudio\crudbooster\models\ColumnModel; |
14
|
|
|
use crocodicstudio\crudbooster\types\select\SelectModel; |
15
|
|
|
use Illuminate\Support\Facades\DB; |
16
|
|
|
|
17
|
|
|
class Select |
18
|
|
|
{ |
19
|
|
|
use DefaultOption, Join; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $field_name |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function foreignKey($field_name) |
26
|
|
|
{ |
27
|
|
|
$data = columnSingleton()->getColumn($this->index); |
28
|
|
|
/** @var SelectModel $data */ |
29
|
|
|
$data->setForeignKey($field_name); |
30
|
|
|
columnSingleton()->setColumn($this->index, $data); |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $table string |
37
|
|
|
* @param $key_field string |
38
|
|
|
* @param $display_field string |
39
|
|
|
* @param $SQLCondition string|callable |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) { |
43
|
|
|
$data = DB::table($table); |
44
|
|
|
if($SQLCondition && is_callable($SQLCondition)) { |
45
|
|
|
$data = call_user_func($SQLCondition, $data); |
46
|
|
|
}elseif ($SQLCondition && is_string($SQLCondition)) { |
47
|
|
|
$data->whereRaw($SQLCondition); |
48
|
|
|
} |
49
|
|
|
$data = $data->get(); |
50
|
|
|
$options = []; |
51
|
|
|
foreach ($data as $d) { |
52
|
|
|
$options[ $d->$key_field ] = $d->$display_field; |
53
|
|
|
} |
54
|
|
|
$data = columnSingleton()->getColumn($this->index); |
55
|
|
|
/** @var $data SelectModel */ |
56
|
|
|
$data->setOptionsFromTable(["table"=>$table,"key_field"=>$key_field,"display_field"=>$display_field,"sql_condition"=>$SQLCondition]); |
57
|
|
|
columnSingleton()->setColumn($this->index, $data); |
58
|
|
|
|
59
|
|
|
$this->options($options); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function optionsFromQuery(callable $query) { |
65
|
|
|
$data = columnSingleton()->getColumn($this->index); |
66
|
|
|
/** @var $data SelectModel */ |
67
|
|
|
$data->setOptionsFromQuery($query); |
68
|
|
|
|
69
|
|
|
columnSingleton()->setColumn($this->index, $data); |
70
|
|
|
|
71
|
|
|
$result = call_user_func($query); |
72
|
|
|
if($result) { |
73
|
|
|
$options = []; |
74
|
|
|
foreach($result as $r) { |
75
|
|
|
$options[ $r->key ] = $r->label; |
76
|
|
|
} |
77
|
|
|
$this->options($options); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function options($data_options) { |
84
|
|
|
$data = columnSingleton()->getColumn($this->index); |
85
|
|
|
/** @var $data SelectModel */ |
86
|
|
|
$data->setOptions($data_options); |
87
|
|
|
|
88
|
|
|
columnSingleton()->setColumn($this->index, $data); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |