219 lines
6.6 KiB
PHP
219 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Admins\adminEditRequest;
|
|
use App\Http\Requests\Admin\Admins\adminStoreRequest;
|
|
use App\Models\Admin;
|
|
use App\Models\AdminPermission;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$admins = Admin::query()
|
|
->when($request->filled('search'), function (Builder $query) use ($request) {
|
|
$search = $request->input('search');
|
|
|
|
$query->whereAny([
|
|
'first_name',
|
|
'last_name',
|
|
'username',
|
|
'id',
|
|
'status',
|
|
'email',
|
|
'created_at'
|
|
], 'LIKE', "%$search%");
|
|
})
|
|
->when($request->filled('sort'), function (Builder $query) use ($request) {
|
|
switch ($request->input('sort')) {
|
|
case 'newest':
|
|
$query->orderByDesc('created_at');
|
|
break;
|
|
|
|
case 'email':
|
|
$query->orderBy('email');
|
|
break;
|
|
|
|
case 'name_desc':
|
|
$query
|
|
->orderByDesc('first_name')
|
|
->orderByDesc('last_name');
|
|
break;
|
|
|
|
default:
|
|
$query
|
|
->orderBy('first_name')
|
|
->orderBy('last_name');
|
|
break;
|
|
}
|
|
})
|
|
->paginate();
|
|
|
|
return view('admin.admins.index', compact('admins'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('admin.admins.create');
|
|
}
|
|
|
|
public function store(adminStoreRequest $request)
|
|
{
|
|
$inputs = $request->validated();
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$permissions = $inputs['permissions'] ?? [];
|
|
|
|
unset($inputs['permissions']);
|
|
|
|
$inputs['password'] = Hash::make($inputs['password']);
|
|
|
|
$admin = Admin::create($inputs);
|
|
|
|
AdminPermission::create([
|
|
'admin_id' => $admin->id,
|
|
|
|
'flight_create' => $permissions['flight_create'] ?? 0,
|
|
'flight_edit' => $permissions['flight_edit'] ?? 0,
|
|
'flight_delete' => $permissions['flight_delete'] ?? 0,
|
|
|
|
'tour_create' => $permissions['tour_create'] ?? 0,
|
|
'tour_edit' => $permissions['tour_edit'] ?? 0,
|
|
'tour_delete' => $permissions['tour_delete'] ?? 0,
|
|
|
|
'booking_view' => $permissions['booking_view'] ?? 0,
|
|
'booking_edit' => $permissions['booking_edit'] ?? 0,
|
|
'booking_delete' => $permissions['booking_delete'] ?? 0,
|
|
|
|
'user_view' => $permissions['user_view'] ?? 0,
|
|
'user_edit' => $permissions['user_edit'] ?? 0,
|
|
'user_delete' => $permissions['user_delete'] ?? 0,
|
|
|
|
'admin_view' => $permissions['admin_view'] ?? 0,
|
|
'admin_create' => $permissions['admin_create'] ?? 0,
|
|
'admin_edit' => $permissions['admin_edit'] ?? 0,
|
|
'admin_delete' => $permissions['admin_delete'] ?? 0,
|
|
]);
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withErrors([
|
|
'general' => 'خطایی رخ داده است'
|
|
])
|
|
->withInput();
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.admins.index')
|
|
->withErrors([
|
|
'success' => 'مدیر با موفقیت ایجاد شد'
|
|
]);
|
|
}
|
|
|
|
public function edit(Admin $admin)
|
|
{
|
|
return view('admin.admins.edit', compact('admin'));
|
|
}
|
|
|
|
public function update(adminEditRequest $request, Admin $admin)
|
|
{
|
|
$inputs = $request->validated();
|
|
|
|
try {
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
if (!empty($inputs['password'])) {
|
|
$inputs['password'] = Hash::make($inputs['password']);
|
|
} else {
|
|
unset($inputs['password']);
|
|
}
|
|
|
|
$permissions = $inputs['permissions'] ?? [];
|
|
unset($inputs['permissions']);
|
|
|
|
|
|
$admin->update($inputs);
|
|
|
|
$permissionData = [
|
|
'flight_create' => $permissions['flight_create'] ?? 0,
|
|
'flight_edit' => $permissions['flight_edit'] ?? 0,
|
|
'flight_delete' => $permissions['flight_delete'] ?? 0,
|
|
|
|
'tour_create' => $permissions['tour_create'] ?? 0,
|
|
'tour_edit' => $permissions['tour_edit'] ?? 0,
|
|
'tour_delete' => $permissions['tour_delete'] ?? 0,
|
|
|
|
'booking_view' => $permissions['booking_view'] ?? 0,
|
|
'booking_edit' => $permissions['booking_edit'] ?? 0,
|
|
'booking_delete' => $permissions['booking_delete'] ?? 0,
|
|
|
|
'user_view' => $permissions['user_view'] ?? 0,
|
|
'user_edit' => $permissions['user_edit'] ?? 0,
|
|
'user_delete' => $permissions['user_delete'] ?? 0,
|
|
|
|
'admin_view' => $permissions['admin_view'] ?? 0,
|
|
'admin_create' => $permissions['admin_create'] ?? 0,
|
|
'admin_edit' => $permissions['admin_edit'] ?? 0,
|
|
'admin_delete' => $permissions['admin_delete'] ?? 0,
|
|
];
|
|
|
|
AdminPermission::updateOrCreate(
|
|
['admin_id' => $admin->id],
|
|
$permissionData
|
|
);
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withErrors([
|
|
'general' => 'خطایی رخ داده است'
|
|
])
|
|
->withInput();
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.admins.index')
|
|
->withErrors([
|
|
'success' => 'عملیات با موفقیت انجام شد'
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
public function destroy(Admin $admin)
|
|
{
|
|
$admin->delete();
|
|
|
|
return redirect()
|
|
->route('admin.admins.index')
|
|
->withErrors([
|
|
'success' => 'عملیات با موفقیت انجام شد'
|
|
]);
|
|
}
|
|
}
|