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\Cbmodel\Core\Model; |
12
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\DefaultOption; |
13
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join; |
14
|
|
|
use crocodicstudio\crudbooster\types\checkbox\CheckboxModel; |
15
|
|
|
use Illuminate\Support\Facades\DB; |
16
|
|
|
|
17
|
|
|
class Checkbox |
18
|
|
|
{ |
19
|
|
|
use DefaultOption, Join; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $options Array containing key and value |
23
|
|
|
* @return $this |
24
|
|
|
* @example options(['foo'=>'bar']) |
25
|
|
|
*/ |
26
|
|
|
public function options($options) { |
27
|
|
|
/** @var CheckboxModel $data */ |
28
|
|
|
$data = columnSingleton()->getColumn($this->index); |
29
|
|
|
$data->setCheckboxOptions($options); |
30
|
|
|
|
31
|
|
|
columnSingleton()->setColumn($this->index, $data); |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $table string|Model |
38
|
|
|
* @param $key_field string |
39
|
|
|
* @param $display_field string |
40
|
|
|
* @param $SQLCondition string|callable |
41
|
|
|
*/ |
42
|
|
|
public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) { |
43
|
|
|
if(strpos($table,"App\Models")!==false) { |
44
|
|
|
$table = new $table(); |
45
|
|
|
$table = $table::$tableName; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$data = DB::table($table); |
49
|
|
|
if($SQLCondition && is_callable($SQLCondition)) { |
50
|
|
|
$data = call_user_func($SQLCondition, $data); |
51
|
|
|
}elseif ($SQLCondition && is_string($SQLCondition)) { |
52
|
|
|
$data->whereRaw($SQLCondition); |
53
|
|
|
} |
54
|
|
|
$data = $data->get(); |
55
|
|
|
$options = []; |
56
|
|
|
foreach ($data as $d) { |
57
|
|
|
$options[ $d->$key_field ] = $d->$display_field; |
58
|
|
|
} |
59
|
|
|
$this->options($options); |
60
|
|
|
} |
61
|
|
|
} |