added xcontroller commands

pull/44/head
A1Gard 7 months ago
parent 22c6f45111
commit c7f582ed46

@ -0,0 +1,61 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class makeXcontroller extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:xcontroller {model}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'create new xContoller';
/**
* Execute the console command.
*/
public function handle()
{
//
$model = ucfirst($this->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');
}
}

@ -0,0 +1,103 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\XController;
use App\Http\Requests\UserSaveRequest;
use App\Models\Access;
use App\Models\User;
use Illuminate\Http\Request;
use App\Helper;
use function App\Helpers\hasCreateRoute;
class UserController extends XController
{
protected $cols = [];
protected $searchable = [];
protected const request = UserSaveRequest::class;
protected $buttons = [
'edit' =>
['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**/
}

@ -2,14 +2,11 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\CreateOrUpdate;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Controllers\XController; use App\Http\Controllers\XController;
use App\Http\Requests\UserSaveRequest; use App\Http\Requests\UserSaveRequest;
use App\Models\Access; use App\Models\Access;
use App\Models\Item;
use App\Models\User; use App\Models\User;
use App\SafeController;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Helper; use App\Helper;
use function App\Helpers\hasCreateRoute; use function App\Helpers\hasCreateRoute;
@ -17,9 +14,8 @@ use function App\Helpers\hasCreateRoute;
class UserController extends XController class UserController extends XController
{ {
protected $cols = ['name', 'email', 'role', 'mobile']; 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;
@ -84,10 +80,11 @@ class UserController extends XController
public function edit(User $item) 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()); // dd($request->all());
$data = explode('.', $request->input('action')); $data = explode('.', $request->input('action'));
@ -95,29 +92,29 @@ class UserController extends XController
$ids = $request->input('id'); $ids = $request->input('id');
switch ($action) { switch ($action) {
case 'delete': case 'delete':
$msg = __(':COUNT items deleted successfully',['COUNT' => count($ids)]); $msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]);
self::_MODEL_::destroy($ids); self::_MODEL_::destroy($ids);
break; break;
case 'restore': case 'restore':
$msg = __(':COUNT items restored successfully',['COUNT' => count($ids)]); $msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]);
foreach ($ids as $id) { foreach ($ids as $id) {
self::_MODEL_::withTrashed()->find($id)->restore(); self::_MODEL_::withTrashed()->find($id)->restore();
} }
break; break;
case 'role': case 'role':
foreach ($ids as $id) { foreach ($ids as $id) {
$user = User::where('id',$id)->first(); $user = User::where('id', $id)->first();
$user->role = $data[1]; $user->role = $data[1];
$user->syncRoles([strtolower($data[1])]); $user->syncRoles([strtolower($data[1])]);
$user->save(); $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; break;
default: 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) 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); return $this->bringUp($request, $item);
} }
public function restore($item) public function restore($item)
{ {
return parent::restoreing(User::withTrashed()->where('email', $item)->first());
return parent::restoreing(User::withTrashed()->where('email',$item)->first());
} }
} }

@ -15,7 +15,6 @@ abstract class XController extends Controller
protected $extra_cols = ['id']; protected $extra_cols = ['id'];
protected $listView = 'admin.users.user-list'; protected $listView = 'admin.users.user-list';
protected $formView = 'admin.users.user-form'; protected $formView = 'admin.users.user-form';
protected $filterables = [];
protected $searchable = []; protected $searchable = [];

@ -10,7 +10,7 @@ class Menu extends Model
{ {
use HasFactory,SoftDeletes; use HasFactory,SoftDeletes;
public function menuItems() public function items()
{ {
return $this->hasMany(Item::class); return $this->hasMany(Item::class);
} }

@ -1,9 +0,0 @@
<?php
namespace App;
interface SafeController
{
//
public function createOrUpdate();
}

@ -15,6 +15,10 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group(
Route::group( Route::group(
['middleware' => ['auth']], ['middleware' => ['auth']],
function () { function () {
Route::get('/',[\App\Http\Controllers\HomeController::class,'index'])->name('dash');
Route::prefix('users')->name('user.')->group( Route::prefix('users')->name('user.')->group(
function () { function () {
Route::get('', [\App\Http\Controllers\Admin\UserController::class, 'index'])->name('index'); 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::post('bulk', [\App\Http\Controllers\Admin\UserController::class, "bulk"])->name('bulk');
Route::get('trashed', [\App\Http\Controllers\Admin\UserController::class, "trashed"])->name('trashed'); 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');
});
}); });
}); });

Loading…
Cancel
Save