1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyPanel\Http\Livewire\CRUD; |
4
|
|
|
|
5
|
|
|
use Livewire\Component; |
6
|
|
|
use EasyPanel\Models\CRUD; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Facades\Artisan; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
|
12
|
|
|
class Create extends Component |
13
|
|
|
{ |
14
|
|
|
public $model, $route, $icon, $withAcl, $withPolicy; |
15
|
|
|
public $models; |
16
|
|
|
public $dropdown; |
17
|
|
|
protected $listeners = ['closeModal']; |
18
|
|
|
|
19
|
|
|
protected $rules = [ |
20
|
|
|
'model' => 'required|min:8|unique:cruds', |
21
|
|
|
'route' => 'required|min:2|unique:cruds', |
22
|
|
|
'icon' => 'nullable|min:5', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function closeModal() |
26
|
|
|
{ |
27
|
|
|
$this->hideDropdown(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setModel() |
31
|
|
|
{ |
32
|
|
|
$this->models = $this->getModels(); |
33
|
|
|
$this->showDropdown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setSuggestedModel($key) |
37
|
|
|
{ |
38
|
|
|
$this->model = $this->models[$key]; |
39
|
|
|
$this->route = Str::lower($this->getModelName($this->model)); |
40
|
|
|
$this->hideDropdown(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function updatedModel($value) |
44
|
|
|
{ |
45
|
|
|
$value = $value == '' ? null : $value; |
46
|
|
|
$this->models = $this->getModels($value); |
47
|
|
|
$this->showDropdown(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function hideDropdown() |
51
|
|
|
{ |
52
|
|
|
$this->dropdown = false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function showDropdown() |
56
|
|
|
{ |
57
|
|
|
$this->dropdown = true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function create() |
61
|
|
|
{ |
62
|
|
|
$this->validate(); |
63
|
|
|
|
64
|
|
|
if (!class_exists($this->model) or ! app()->make($this->model) instanceof Model){ |
65
|
|
|
$this->addError('model', __('Namespace is invalid')); |
66
|
|
|
|
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!preg_match('/^([a-z]|[0-9])+/', $this->route)){ |
71
|
|
|
$this->addError('route', __('Route is invalid')); |
72
|
|
|
|
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
try{ |
77
|
|
|
$name = $this->getModelName($this->model); |
78
|
|
|
|
79
|
|
|
CRUD::create([ |
80
|
|
|
'model' => trim($this->model, '\\'), |
81
|
|
|
'name' => $name, |
82
|
|
|
'route' => trim($this->route, '\\'), |
83
|
|
|
'icon' => $this->icon ?? 'fas fa-bars', |
84
|
|
|
'with_acl' => $this->withAcl == 1, |
85
|
|
|
'with_policy' => $this->withAcl == 1 && $this->withPolicy == 1 |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
Artisan::call('panel:config', [ |
89
|
|
|
'name' => $name, |
90
|
|
|
'--force' => true |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$this->dispatchBrowserEvent('show-message', ['type' => 'success', 'message' => __('CreatedMessage', ['name' => __('CRUD') ])]); |
94
|
|
|
} catch(\Exception $exception){ |
95
|
|
|
$this->dispatchBrowserEvent('show-message', ['type' => 'error', 'message' => __('UnknownError') ]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->emit('crudUpdated'); |
100
|
|
|
$this->reset(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function render() |
104
|
|
|
{ |
105
|
|
|
return view('admin::livewire.crud.create') |
106
|
|
|
->layout('admin::layouts.app', ['title' => __('CreateTitle', ['name' => __('CRUD') ])]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getModelName($model){ |
110
|
|
|
$model = explode('\\', $model); |
111
|
|
|
|
112
|
|
|
return end($model); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getModels($query = null) |
116
|
|
|
{ |
117
|
|
|
$files = File::exists(app_path('/Models')) |
118
|
|
|
? File::files(app_path('/Models')) |
119
|
|
|
: File::allFiles(app_path('/')); |
120
|
|
|
|
121
|
|
|
$array = []; |
122
|
|
|
|
123
|
|
|
foreach ($files as $file) { |
124
|
|
|
|
125
|
|
|
if ($this->fileCanNotBeListed($file->getFilename(), $query)){ |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$namespace = $this->fileNamespace($file->getFilename()); |
130
|
|
|
|
131
|
|
|
if (app()->make($namespace) instanceof Model) { |
132
|
|
|
$array[] = $namespace; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $array; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function fileCanNotBeListed($fileName, $searchedValued = null): bool |
140
|
|
|
{ |
141
|
|
|
return !Str::contains($fileName, '.php') or (!is_null($searchedValued) and !Str::contains(Str::lower($fileName), Str::lower($searchedValued))); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function fileNamespace($fileName): string |
145
|
|
|
{ |
146
|
|
|
$namespace = File::exists(app_path('/Models')) ? "App\\Models" : "\\App"; |
147
|
|
|
$fileName = str_replace('.php', null, $fileName); |
148
|
|
|
return $namespace."\\{$fileName}"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|