From c7f582ed462ff512c4bc06c9405a04552088ab11 Mon Sep 17 00:00:00 2001 From: A1Gard Date: Mon, 10 Jun 2024 22:36:50 +0330 Subject: [PATCH] added xcontroller commands --- app/Console/Commands/makeXcontroller.php | 61 +++++++++++ app/Console/Commands/xcontroller.dat | 103 ++++++++++++++++++ app/Http/Controllers/Admin/UserController.php | 96 ++++++++-------- app/Http/Controllers/XController.php | 1 - app/Models/Menu.php | 2 +- app/SafeController.php | 9 -- routes/web.php | 16 +++ 7 files changed, 228 insertions(+), 60 deletions(-) create mode 100644 app/Console/Commands/makeXcontroller.php create mode 100644 app/Console/Commands/xcontroller.dat delete mode 100644 app/SafeController.php diff --git a/app/Console/Commands/makeXcontroller.php b/app/Console/Commands/makeXcontroller.php new file mode 100644 index 0000000..fddf9d7 --- /dev/null +++ b/app/Console/Commands/makeXcontroller.php @@ -0,0 +1,61 @@ +argument('model')); + $var = '$' . strtolower($this->argument('model')); + + + if (!file_exists(__DIR__.'/../../Models/'.$model.'.php')){ + $this->error("Model not found!"); + return ; + } + + $content = file_get_contents(__DIR__ . '/xcontroller.dat'); + + $content = str_replace('User', $model, $content); + $content = str_replace('$user', $var, $content); + + Artisan::call('make:request', ['name' => $model.'SaveRequest']); + Artisan::call('make:controller', ['name' => 'Admin/' . $model . 'Controller']); + + + $model_content = file_get_contents(__DIR__.'/../../Models/'.$model.'.php'); + + if (!strpos($model_content,'SoftDeletes')){ + $pattern = '/\/\*\*restore\*\/(.*?)\/\*restore\*\*\//s'; + $replacement = ''; + $content = preg_replace($pattern, $replacement, $content); + } + file_put_contents(__DIR__.'/../../Http/Controllers/Admin/' . $model . 'Controller.php',$content); + $this->info('Admin/' . $model . 'Controller created'); + $this->info( $model.'SaveRequest created'); + + + } +} diff --git a/app/Console/Commands/xcontroller.dat b/app/Console/Commands/xcontroller.dat new file mode 100644 index 0000000..ce388ac --- /dev/null +++ b/app/Console/Commands/xcontroller.dat @@ -0,0 +1,103 @@ + + ['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'], + 'show' => + ['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'], + 'destroy' => + ['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'], + ]; + + public function save($user, $request) + { + + $user->save(); + return $user; + + } + + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + return view($this->formView); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(User $item) + { + // + return view($this->formView, compact('item')); + } + + public function bulk(Request $request) + { + +// dd($request->all()); + $data = explode('.', $request->input('action')); + $action = $data[0]; + $ids = $request->input('id'); + switch ($action) { + case 'delete': + $msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]); + self::_MODEL_::destroy($ids); + break; + /**restore*/ + case 'restore': + $msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]); + foreach ($ids as $id) { + self::_MODEL_::withTrashed()->find($id)->restore(); + } + break; + /*restore**/ + default: + $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]); + } + + return $this->do_bulk($msg, $action, $ids); + } + + public function destroy(User $item) + { + return parent::delete($item); + } + + + public function update(Request $request, User $item) + { + return $this->bringUp($request, $item); + } + + /**restore*/ + public function restore($item) + { + return parent::restoreing(User::withTrashed()->where('id', $item)->first()); + } + /*restore**/ +} diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 50d1081..64d1d0a 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -2,14 +2,11 @@ namespace App\Http\Controllers\Admin; -use App\CreateOrUpdate; use App\Http\Controllers\Controller; use App\Http\Controllers\XController; use App\Http\Requests\UserSaveRequest; use App\Models\Access; -use App\Models\Item; use App\Models\User; -use App\SafeController; use Illuminate\Http\Request; use App\Helper; use function App\Helpers\hasCreateRoute; @@ -17,12 +14,11 @@ use function App\Helpers\hasCreateRoute; class UserController extends XController { protected $cols = ['name', 'email', 'role', 'mobile']; - protected $filterables = ['role']; - protected $searchable = ['name','mobile','email']; + protected $searchable = ['name', 'mobile', 'email']; - protected const request = UserSaveRequest::class; + protected const request = UserSaveRequest::class; protected $buttons = [ 'edit' => @@ -35,38 +31,38 @@ class UserController extends XController ['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'], ]; - public function save($user, $request) - { - $user->name = $request->input('name'); - $user->email = $request->input('email'); - if (trim($request->input('password')) != '') { - $user->password = bcrypt($request->input('password')); - } - $user->mobile = $request->input('mobile'); - $user->role = $request->input('role'); - $user->syncRoles($request->input('role')); - $user->save(); - if ($request->has('acl')) { - $user->accesses()->delete(); - foreach ($request->input('acl', []) as $route) { - $a = new Access(); - $a->route = $route; - $a->user_id = $user->id; - $a->save(); - $routes = explode('.', $route); - if ($routes[2] == 'store' || $routes[2] == 'update') { - $routes[2] = $routes[2] == 'store' ? 'create' : 'edit'; - $a = new Access(); - $a->route = implode('.', $routes); - $a->user_id = $user->id; - $a->save(); - } - } - - } - return $user; - - } + public function save($user, $request) + { + $user->name = $request->input('name'); + $user->email = $request->input('email'); + if (trim($request->input('password')) != '') { + $user->password = bcrypt($request->input('password')); + } + $user->mobile = $request->input('mobile'); + $user->role = $request->input('role'); + $user->syncRoles($request->input('role')); + $user->save(); + if ($request->has('acl')) { + $user->accesses()->delete(); + foreach ($request->input('acl', []) as $route) { + $a = new Access(); + $a->route = $route; + $a->user_id = $user->id; + $a->save(); + $routes = explode('.', $route); + if ($routes[2] == 'store' || $routes[2] == 'update') { + $routes[2] = $routes[2] == 'store' ? 'create' : 'edit'; + $a = new Access(); + $a->route = implode('.', $routes); + $a->user_id = $user->id; + $a->save(); + } + } + + } + return $user; + + } /** @@ -84,10 +80,11 @@ class UserController extends XController public function edit(User $item) { // - return view($this->formView,compact('item')); + return view($this->formView, compact('item')); } - public function bulk(Request $request){ + public function bulk(Request $request) + { // dd($request->all()); $data = explode('.', $request->input('action')); @@ -95,29 +92,29 @@ class UserController extends XController $ids = $request->input('id'); switch ($action) { case 'delete': - $msg = __(':COUNT items deleted successfully',['COUNT' => count($ids)]); + $msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]); self::_MODEL_::destroy($ids); break; case 'restore': - $msg = __(':COUNT items restored successfully',['COUNT' => count($ids)]); + $msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]); foreach ($ids as $id) { self::_MODEL_::withTrashed()->find($id)->restore(); } break; case 'role': foreach ($ids as $id) { - $user = User::where('id',$id)->first(); + $user = User::where('id', $id)->first(); $user->role = $data[1]; $user->syncRoles([strtolower($data[1])]); $user->save(); } - $msg = __(':COUNT user role changed to :NEWROLE successfully',['COUNT' => count($ids), 'NEWROLE' => __($data[1])]); + $msg = __(':COUNT user role changed to :NEWROLE successfully', ['COUNT' => count($ids), 'NEWROLE' => __($data[1])]); break; default: - $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action] ); + $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]); } - return $this->do_bulk($msg,$action, $ids); + return $this->do_bulk($msg, $action, $ids); } public function destroy(User $item) @@ -126,12 +123,13 @@ class UserController extends XController } - public function update(Request $request,User $item ){ + public function update(Request $request, User $item) + { return $this->bringUp($request, $item); } + public function restore($item) { - - return parent::restoreing(User::withTrashed()->where('email',$item)->first()); + return parent::restoreing(User::withTrashed()->where('email', $item)->first()); } } diff --git a/app/Http/Controllers/XController.php b/app/Http/Controllers/XController.php index 314ff47..707089b 100644 --- a/app/Http/Controllers/XController.php +++ b/app/Http/Controllers/XController.php @@ -15,7 +15,6 @@ abstract class XController extends Controller protected $extra_cols = ['id']; protected $listView = 'admin.users.user-list'; protected $formView = 'admin.users.user-form'; - protected $filterables = []; protected $searchable = []; diff --git a/app/Models/Menu.php b/app/Models/Menu.php index 2e16ffa..996743d 100644 --- a/app/Models/Menu.php +++ b/app/Models/Menu.php @@ -10,7 +10,7 @@ class Menu extends Model { use HasFactory,SoftDeletes; - public function menuItems() + public function items() { return $this->hasMany(Item::class); } diff --git a/app/SafeController.php b/app/SafeController.php deleted file mode 100644 index a24eaac..0000000 --- a/app/SafeController.php +++ /dev/null @@ -1,9 +0,0 @@ -name('admin.')->group( Route::group( ['middleware' => ['auth']], function () { + + + Route::get('/',[\App\Http\Controllers\HomeController::class,'index'])->name('dash'); + Route::prefix('users')->name('user.')->group( function () { Route::get('', [\App\Http\Controllers\Admin\UserController::class, 'index'])->name('index'); @@ -29,5 +33,17 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group( Route::post('bulk', [\App\Http\Controllers\Admin\UserController::class, "bulk"])->name('bulk'); Route::get('trashed', [\App\Http\Controllers\Admin\UserController::class, "trashed"])->name('trashed'); }); + Route::prefix('group')->name('group.')->group( + function () { + Route::get('', [\App\Http\Controllers\Admin\UserController::class, 'index'])->name('index'); + Route::get('create', [\App\Http\Controllers\Admin\UserController::class, 'create'])->name('create'); + Route::post('store', [\App\Http\Controllers\Admin\UserController::class, 'store'])->name('store'); + Route::get('edit/{item}', [\App\Http\Controllers\Admin\UserController::class, 'edit'])->name('edit'); + Route::post('update/{item}', [\App\Http\Controllers\Admin\UserController::class, 'update'])->name('update'); + Route::get('delete/{item}', [\App\Http\Controllers\Admin\UserController::class, 'destroy'])->name('destroy'); + Route::get('restore/{item}', [\App\Http\Controllers\Admin\UserController::class, 'restore'])->name('restore'); + Route::post('bulk', [\App\Http\Controllers\Admin\UserController::class, "bulk"])->name('bulk'); + Route::get('trashed', [\App\Http\Controllers\Admin\UserController::class, "trashed"])->name('trashed'); + }); }); });