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; |
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 = strtolower($this->getModelName($this->model)); |
78
|
|
|
CRUD::create([ |
79
|
|
|
'model' => trim($this->model, '\\'), |
80
|
|
|
'name' => $name, |
81
|
|
|
'route' => trim($this->route, '\\'), |
82
|
|
|
'icon' => $this->icon ?? 'fas fa-bars' |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
Artisan::call('panel:config', [ |
86
|
|
|
'name' => $name, |
87
|
|
|
'--force' => true |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$this->dispatchBrowserEvent('show-message', ['type' => 'success', 'message' => __('CreatedMessage', ['name' => __('CRUD') ])]); |
91
|
|
|
} catch(\Exception $exception){ |
92
|
|
|
$this->dispatchBrowserEvent('show-message', ['type' => 'error', 'message' => __('UnknownError') ]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->emit('crudUpdated'); |
97
|
|
|
$this->reset(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function render() |
101
|
|
|
{ |
102
|
|
|
return view('admin::livewire.crud.create') |
103
|
|
|
->layout('admin::layouts.app'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getModelName($model){ |
107
|
|
|
$model = explode('\\', $model); |
108
|
|
|
|
109
|
|
|
return end($model); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function getModels($query = null) |
113
|
|
|
{ |
114
|
|
|
$files = File::exists(app_path('/Models')) |
115
|
|
|
? File::files(app_path('/Models')) |
116
|
|
|
: File::allFiles(app_path('/')); |
117
|
|
|
|
118
|
|
|
$array = []; |
119
|
|
|
|
120
|
|
|
foreach ($files as $file) { |
121
|
|
|
|
122
|
|
|
if ($this->fileCanNotBeListed($file->getFilename(), $query)){ |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$namespace = $this->fileNamespace($file->getFilename()); |
127
|
|
|
|
128
|
|
|
if (app()->make($namespace) instanceof Model) { |
129
|
|
|
$array[] = $namespace; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $array; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function fileCanNotBeListed($fileName, $searchedValued = null): bool |
137
|
|
|
{ |
138
|
|
|
return !Str::contains($fileName, '.php') or (!is_null($searchedValued) and !Str::contains(Str::lower($fileName), Str::lower($searchedValued))); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function fileNamespace($fileName): string |
142
|
|
|
{ |
143
|
|
|
$namespace = File::exists(app_path('/Models')) ? "App\\Models" : "\\App"; |
144
|
|
|
$fileName = str_replace('.php', null, $fileName); |
145
|
|
|
return $namespace."\\{$fileName}"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|