|
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; |
|
15
|
|
|
public $route; |
|
16
|
|
|
public $models; |
|
17
|
|
|
public $dropdown; |
|
18
|
|
|
|
|
19
|
|
|
protected $rules = [ |
|
20
|
|
|
'model' => 'required|min:8|unique:cruds', |
|
21
|
|
|
'route' => 'required|min:2|unique:cruds', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public function setModel() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->models = $this->getModels(); |
|
27
|
|
|
$this->showDropdown(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setSuggestedModel($key) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->model = $this->models[$key]; |
|
33
|
|
|
$this->route = Str::lower($this->getModelName($this->model)); |
|
34
|
|
|
$this->hideDropdown(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function updatedModel($value) |
|
38
|
|
|
{ |
|
39
|
|
|
$value = $value == '' ? null : $value; |
|
40
|
|
|
$this->models = $this->getModels($value); |
|
41
|
|
|
$this->showDropdown(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function hideDropdown() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->dropdown = false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function showDropdown() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->dropdown = true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function create() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->validate(); |
|
57
|
|
|
|
|
58
|
|
|
if (!class_exists($this->model) or ! app()->make($this->model) instanceof Model){ |
|
59
|
|
|
$this->addError('model', __('Namespace is invalid')); |
|
60
|
|
|
|
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!preg_match('/^([a-z]|[0-9])+/', $this->route)){ |
|
65
|
|
|
$this->addError('route', __('Route is invalid')); |
|
66
|
|
|
|
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
try{ |
|
71
|
|
|
$name = strtolower($this->getModelName($this->model)); |
|
72
|
|
|
CRUD::create([ |
|
73
|
|
|
'model' => trim($this->model, '\\'), |
|
74
|
|
|
'name' => $name, |
|
75
|
|
|
'route' => trim($this->route, '\\'), |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
Artisan::call('panel:config', [ |
|
79
|
|
|
'name' => $name, |
|
80
|
|
|
'--force' => true |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
$this->dispatchBrowserEvent('show-message', ['type' => 'success', 'message' => __('CreatedMessage', ['name' => __('CRUD') ])]); |
|
84
|
|
|
} catch(\Exception $exception){ |
|
85
|
|
|
$this->dispatchBrowserEvent('show-message', ['type' => 'error', 'message' => __('UnknownError') ]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$this->emit('crudUpdated'); |
|
90
|
|
|
$this->reset(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function render() |
|
94
|
|
|
{ |
|
95
|
|
|
return view('admin::livewire.crud.create') |
|
96
|
|
|
->layout('admin::layouts.app'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function getModelName($model){ |
|
100
|
|
|
$model = explode('\\', $model); |
|
101
|
|
|
|
|
102
|
|
|
return end($model); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function getModels($query = null) |
|
106
|
|
|
{ |
|
107
|
|
|
$files = File::exists(app_path('/Models')) ? File::files(app_path('/Models')) : File::allFiles(app_path('/')); |
|
108
|
|
|
$array = []; |
|
109
|
|
|
foreach ($files as $file) { |
|
110
|
|
|
if (!Str::contains($file->getFilename(), '.php') or (!is_null($query) and !Str::contains(Str::lower($file->getFilename()), Str::lower($query)))){ |
|
111
|
|
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$namespace = File::exists(app_path('/Models')) ? "App\\Models" : "\\App"; |
|
115
|
|
|
$fileName = str_replace('.php', null, $file->getFilename()); |
|
116
|
|
|
$fullNamespace = $namespace."\\{$fileName}"; |
|
117
|
|
|
|
|
118
|
|
|
if (app()->make($fullNamespace) instanceof Model) { |
|
119
|
|
|
$array[] = $fullNamespace; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $array; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|