1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 4/25/2019 |
6
|
|
|
* Time: 8:14 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Illuminate\Support\Facades\DB; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Illuminate\Support\Facades\Cache; |
15
|
|
|
|
16
|
|
|
class ModuleGenerator |
17
|
|
|
{ |
18
|
|
|
private $table; |
19
|
|
|
private $icon; |
20
|
|
|
private $name; |
21
|
|
|
private $columns; |
22
|
|
|
private $rebuild; |
23
|
|
|
|
24
|
|
|
public function __construct($table, $name = null, $icon = "fa fa-bars", $columns, $rebuild = null) |
25
|
|
|
{ |
26
|
|
|
$this->table = $table; |
27
|
|
|
$this->icon = $icon; |
28
|
|
|
$this->name = $name; |
29
|
|
|
$this->columns = $columns; |
30
|
|
|
$this->rebuild = $rebuild; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function makeControllerName($name) { |
34
|
|
|
$controllerName = ucwords(str_replace('_', ' ', $name)); |
35
|
|
|
$controllerName = str_replace(' ', '', $controllerName).'Controller'; |
36
|
|
|
return "Admin".$controllerName; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function make() { |
40
|
|
|
$name = trim(($this->name)?:ucwords(str_replace("_"," ",$this->table))); |
41
|
|
|
|
42
|
|
|
$template = file_get_contents(__DIR__."/../templates/FooBarController.stub"); |
43
|
|
|
|
44
|
|
|
//Replace table |
45
|
|
|
$template = str_replace("{table}",'"'.$this->table.'"', $template); |
46
|
|
|
|
47
|
|
|
//Replace permalink |
48
|
|
|
$permalink = strtolower(Str::slug($name,"_")); |
49
|
|
|
$template = str_replace("{permalink}", '"'.$permalink.'"', $template); |
50
|
|
|
|
51
|
|
|
//Replace Page title |
52
|
|
|
$template = str_replace("{page_title}", '"'.$name.'"', $template); |
53
|
|
|
|
54
|
|
|
//Replace scaffolding |
55
|
|
|
$scaffold = ""; |
56
|
|
|
foreach($this->columns as $field) { |
57
|
|
|
$label = $field['column_label']; |
58
|
|
|
$column = $field['column_field']; |
59
|
|
|
$help = isset($field['column_help'])?"->help(\"".$field['column_help']."\")":""; |
60
|
|
|
$required = ($field['column_mandatory']=="on")?"":"->required(false)"; |
61
|
|
|
$indexShow = ($field['column_browse']=="on")?"":"->showIndex(false)"; |
62
|
|
|
$detailShow = ($field['column_detail']=="on")?"":"->showDetail(false)"; |
63
|
|
|
$editShow = ($field['column_edit']=="on")?"":"->showEdit(false)"; |
64
|
|
|
$addShow = ($field['column_add']=="on")?"":"->showAdd(false)"; |
65
|
|
|
$optionTable = $field['column_option_table']; |
66
|
|
|
$optionValue = $field['column_option_value']; |
67
|
|
|
$optionDisplay = $field['column_option_display']; |
68
|
|
|
$optionSqlCondition = $field['column_option_sql_condition']; |
69
|
|
|
$optionSqlCondition = str_replace('"',"'", $optionSqlCondition); |
70
|
|
|
$sqlRawQuery = $field['column_sql_query']; |
71
|
|
|
$options = $field['column_options']; |
72
|
|
|
|
73
|
|
|
$methodName = Str::studly($field['column_type']); |
74
|
|
|
if($label && $column) { |
75
|
|
|
if(in_array($field['column_type'],['radio','select_option','checkbox'])) { |
76
|
|
|
if($options) { |
77
|
|
|
$optResult = []; |
78
|
|
|
foreach($options as $opt) { |
79
|
|
|
$optResult[$opt['key']] = $opt['label']; |
80
|
|
|
} |
81
|
|
|
$scaffold .= '$this->add' . $methodName . '("' . $label . '","' . $column . '")->options('.min_var_export($optResult).')' . $required . $indexShow . $detailShow . $addShow . $editShow . $help . ';' . "\n\t\t"; |
82
|
|
|
} |
83
|
|
|
}elseif (in_array($field['column_type'],['radio_table','select_table'])) { |
84
|
|
|
if ($optionTable && $optionValue && $optionDisplay) { |
85
|
|
|
$scaffold .= '$this->add' . $methodName . '("' . $label . '","' . $column . '",["table"=>"' . $optionTable . '","value_option"=>"' . $optionValue . '","display_option"=>"' . $optionDisplay . '","sql_condition"=>"' . $optionSqlCondition . '"])' . $required . $indexShow . $detailShow . $addShow . $editShow . $help . ';' . "\n\t\t"; |
86
|
|
|
} |
87
|
|
|
}elseif ($field['column_type'] == "select_query") { |
88
|
|
|
if($sqlRawQuery && Str::contains($sqlRawQuery,["as `key`","as `label`"])) { |
89
|
|
|
$scaffold .= '$this->add' . $methodName . '("' . $label . '","' . $column . '","'.$sqlRawQuery.'")' . $required . $indexShow . $detailShow . $addShow . $editShow . $help . ';' . "\n\t\t"; |
90
|
|
|
} |
91
|
|
|
}else{ |
92
|
|
|
$scaffold .= '$this->add'.$methodName.'("'.$label.'","'.$column.'")'.$required.$indexShow.$detailShow.$addShow.$editShow.$help.';'."\n\t\t"; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
$template = str_replace("{scaffolding}", $scaffold, $template); |
97
|
|
|
|
98
|
|
|
$filename = $this->makeControllerName($name); |
99
|
|
|
|
100
|
|
|
//Replace Controller Name |
101
|
|
|
$template = str_replace("FooBarController", $filename, $template); |
102
|
|
|
|
103
|
|
|
//Create a controller file |
104
|
|
|
file_put_contents(app_path("Http/Controllers/".$filename.".php"), $template); |
105
|
|
|
|
106
|
|
|
//Save to database |
107
|
|
|
$module = []; |
108
|
|
|
$module['name'] = $name; |
109
|
|
|
$module['icon'] = $this->icon; |
110
|
|
|
$module['table_name'] = $this->table; |
111
|
|
|
$module['controller'] = $filename; |
112
|
|
|
$module['last_column_build'] = json_encode($this->columns); |
113
|
|
|
|
114
|
|
|
if($moduleData = DB::table("cb_modules")->where("name", $name)->where("table_name",$this->table)->first()) { |
115
|
|
|
DB::table("cb_modules")->where("id",$moduleData->id)->update($module); |
116
|
|
|
$id_modules = $moduleData->id; |
117
|
|
|
}else{ |
118
|
|
|
$id_modules = DB::table('cb_modules')->insertGetId($module); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
//Save menu |
122
|
|
|
$menu = []; |
123
|
|
|
$menu['name'] = $module['name']; |
124
|
|
|
$menu['type'] = 'module'; |
125
|
|
|
$menu['cb_modules_id'] = $id_modules; |
126
|
|
|
if(isset($moduleData)) { |
127
|
|
|
DB::table("cb_menus")->where("cb_modules_id",$moduleData->id)->update($menu); |
128
|
|
|
}else{ |
129
|
|
|
DB::table('cb_menus')->insertGetId($menu); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $module; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |