200 lines
5.3 KiB
PHP
200 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\AdminStoreRequest;
|
|
use App\Models\Admin;
|
|
use App\Models\AdminPermission;
|
|
use App\Models\File;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$title = 'لیست مدیران';
|
|
|
|
$admins = Admin::query()
|
|
->when($request->filled('search'), function (Builder $query) use ($request) {
|
|
$search = $request->input('search');
|
|
|
|
$query->whereAny([
|
|
'first_name',
|
|
'last_name',
|
|
'username',
|
|
'email'
|
|
], 'LIKE', "%$search%");
|
|
|
|
})
|
|
->when($request->filled('sort'), function (Builder $query) use ($request) {
|
|
$sort = $request->input('sort');
|
|
switch ($sort) {
|
|
|
|
case 'name_desc':
|
|
{
|
|
$query
|
|
->orderByDesc('first_name');
|
|
|
|
break;
|
|
}
|
|
case 'email':
|
|
{
|
|
$query
|
|
->orderBy('email');
|
|
|
|
break;
|
|
}
|
|
case 'newest':
|
|
{
|
|
$query
|
|
->orderByDesc('created_at');
|
|
|
|
break;
|
|
}
|
|
case 'name_asc':
|
|
default:
|
|
{
|
|
$query
|
|
->orderBy('first_name');
|
|
}
|
|
|
|
}
|
|
}, function (Builder $query) {
|
|
$query->orderBy('first_name');
|
|
})
|
|
->paginate()
|
|
->withQueryString();
|
|
|
|
return view('admin.admins.index', compact('title', 'admins'));
|
|
}
|
|
|
|
public function edit(Admin $admin)
|
|
{
|
|
return view('admin.admins.edit', compact('admin'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$title = 'ایجاد کاربر جدید';
|
|
return view('admin.admins.create');
|
|
}
|
|
|
|
|
|
public function store(AdminStoreRequest $request)
|
|
{
|
|
$data = $request->validated();
|
|
|
|
$avatarPath = null;
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$admin = Admin::create([
|
|
'first_name' => $data['first_name'],
|
|
'last_name' => $data['last_name'],
|
|
'username' => $data['username'],
|
|
'email' => $data['email'] ?? null,
|
|
'mobile' => $data['mobile'] ?? null,
|
|
'password' => Hash::make($data['password']),
|
|
'role' => $data['role'],
|
|
'status' => $data['status'],
|
|
]);
|
|
|
|
if ($request->hasFile('image')) {
|
|
$image = $request->file('image');
|
|
|
|
$imageName = $admin->id
|
|
. '_'
|
|
. time()
|
|
. '_'
|
|
. rand(11111, 99999)
|
|
. '.'
|
|
. $image->extension();
|
|
|
|
$avatarPath = $image->storeAs(
|
|
'admin_avatars',
|
|
$imageName,
|
|
'public'
|
|
);
|
|
|
|
$file = File::create([
|
|
'name' => $imageName,
|
|
'size' => $image->getSize(),
|
|
'original_name' => $image->getClientOriginalName(),
|
|
'path' => $avatarPath,
|
|
'extension' => $image->extension(),
|
|
]);
|
|
|
|
// AdminAvatar::create([
|
|
// 'admin_id' => $admin->id,
|
|
// 'file_id' => $file->id,
|
|
// ]);
|
|
}
|
|
|
|
foreach ($data['permissions'] as $permission) {
|
|
AdminPermission::create([
|
|
'admin_id' => $admin->id,
|
|
'permission' => $permission,
|
|
]);
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return redirect()
|
|
->route('admin.admins.index')
|
|
->with('success', 'مدیر جدید با موفقیت ایجاد شد.');
|
|
|
|
} catch (\Throwable $exception) {
|
|
DB::rollBack();
|
|
|
|
if ($avatarPath) {
|
|
Storage::disk('public')->delete($avatarPath);
|
|
}
|
|
|
|
Log::error('خطا در ایجاد مدیر جدید', [
|
|
'message' => $exception->getMessage(),
|
|
'file' => $exception->getFile(),
|
|
'line' => $exception->getLine(),
|
|
]);
|
|
|
|
return back()
|
|
->withInput(
|
|
$request->except([
|
|
'password',
|
|
'password_confirmation',
|
|
'image',
|
|
])
|
|
)
|
|
->with('error', 'خطایی هنگام ایجاد مدیر رخ داد.');
|
|
}
|
|
}
|
|
|
|
|
|
public function destroy()
|
|
{
|
|
|
|
}
|
|
|
|
public function trash()
|
|
{
|
|
|
|
}
|
|
|
|
public function restore()
|
|
{
|
|
|
|
}
|
|
|
|
public function forceDelete()
|
|
{
|
|
|
|
}
|
|
}
|