2024-12-02 01:18:34 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Backend;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\AdminRequest;
|
|
|
|
|
use App\Models\Admin;
|
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
use Spatie\Permission\Models\Role;
|
2024-12-13 19:34:33 +07:00
|
|
|
use App\Helpers\NextcloudHelper;
|
2024-12-23 10:42:27 +07:00
|
|
|
use App\Models\FormUpCountry;
|
|
|
|
|
use App\Models\FormEntertaimentPresentation;
|
|
|
|
|
use App\Models\FormVehicleRunningCost;
|
|
|
|
|
use App\Models\FormOthers;
|
|
|
|
|
use App\Models\FormMeetingSeminar;
|
2024-12-02 01:18:34 +07:00
|
|
|
|
|
|
|
|
class AdminsController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public function index(): Renderable
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['admin.view']);
|
|
|
|
|
|
|
|
|
|
$pageInfo = [
|
|
|
|
|
'title' => 'User Access List',
|
|
|
|
|
'sub_title' => 'List User Access System',
|
|
|
|
|
'path' => url()->current()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('backend.pages.admins.index', [
|
|
|
|
|
'admins' => Admin::all(),
|
|
|
|
|
'pageInfo' => $pageInfo
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create(): Renderable
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['admin.create']);
|
|
|
|
|
|
|
|
|
|
$pageInfo = [
|
|
|
|
|
'title' => 'Create User Access',
|
|
|
|
|
'sub_title' => 'Create new User',
|
|
|
|
|
'path' => url()->current()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('backend.pages.admins.create', [
|
|
|
|
|
'roles' => Role::all(),
|
|
|
|
|
'pageInfo' => $pageInfo
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(AdminRequest $request): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['admin.create']);
|
|
|
|
|
|
|
|
|
|
$admin = new Admin();
|
|
|
|
|
$admin->name = $request->name;
|
|
|
|
|
$admin->password = Hash::make($request->password);
|
|
|
|
|
$admin->username = $request->username;
|
|
|
|
|
$admin->email = $request->email;
|
2024-12-12 18:18:26 +07:00
|
|
|
$admin->phone = $request->phone;
|
2024-12-02 01:18:34 +07:00
|
|
|
$admin->save();
|
|
|
|
|
|
|
|
|
|
if ($request->roles) {
|
|
|
|
|
$admin->assignRole($request->roles);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 19:34:33 +07:00
|
|
|
// Create a folder in Nextcloud
|
2024-12-23 10:10:09 +07:00
|
|
|
// $response = NextcloudHelper::createUser(env('NEXT_CLOUD_URL'), env('NEXT_CLOUD_USERNAME'), env('NEXT_CLOUD_PASSWORD'), $request->username, $request->password, $request->name, $request->email);
|
|
|
|
|
|
|
|
|
|
// if ($response['success']) {
|
|
|
|
|
// session()->flash('success', __('Admin has been created.'));
|
|
|
|
|
// return redirect()->route('admin.admins.index');
|
|
|
|
|
// } else {
|
|
|
|
|
// session()->flash('error', $response['message']);
|
|
|
|
|
// return redirect()->route('admin.admins.index');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
session()->flash('success', 'Admin has been created.');
|
|
|
|
|
return redirect()->route('admin.admins.index');
|
2024-12-02 01:18:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(int $id): Renderable
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['admin.edit']);
|
|
|
|
|
|
|
|
|
|
$pageInfo = [
|
|
|
|
|
'title' => 'Edit User Access',
|
|
|
|
|
'sub_title' => 'Update User Access data',
|
|
|
|
|
'path' => url()->current()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$admin = Admin::findOrFail($id);
|
|
|
|
|
return view('backend.pages.admins.edit', [
|
|
|
|
|
'admin' => $admin,
|
|
|
|
|
'roles' => Role::all(),
|
|
|
|
|
'pageInfo' => $pageInfo
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(AdminRequest $request, int $id): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['admin.edit']);
|
|
|
|
|
|
|
|
|
|
$admin = Admin::findOrFail($id);
|
|
|
|
|
$admin->name = $request->name;
|
|
|
|
|
$admin->email = $request->email;
|
|
|
|
|
$admin->username = $request->username;
|
2024-12-12 18:18:26 +07:00
|
|
|
$admin->phone = $request->phone;
|
2024-12-02 01:18:34 +07:00
|
|
|
|
|
|
|
|
if ($request->password) {
|
|
|
|
|
$admin->password = Hash::make($request->password);
|
|
|
|
|
}
|
|
|
|
|
$admin->save();
|
|
|
|
|
$admin->roles()->detach();
|
|
|
|
|
if ($request->roles) {
|
|
|
|
|
$admin->assignRole($request->roles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session()->flash('success', 'Admin has been updated.');
|
2024-12-12 18:18:26 +07:00
|
|
|
return redirect()->route('admin.admins.index');
|
2024-12-02 01:18:34 +07:00
|
|
|
}
|
|
|
|
|
|
2024-12-23 10:42:27 +07:00
|
|
|
public function expense($user_id)
|
|
|
|
|
{
|
|
|
|
|
$formUpCountry = FormUpCountry::where('user_id', $user_id)->get();
|
|
|
|
|
$formEntertaimentPresentation = FormEntertaimentPresentation::where('user_id', $user_id)->get();
|
|
|
|
|
$formVehicleRunningCost = FormVehicleRunningCost::where('user_id', $user_id)->get();
|
|
|
|
|
$formOthers = FormOthers::where('user_id', $user_id)->get();
|
|
|
|
|
$formMeetingSeminar = FormMeetingSeminar::where('user_id', $user_id)->get();
|
|
|
|
|
|
|
|
|
|
// Combine all forms into a single array with keys for each form type
|
|
|
|
|
$forms = [
|
|
|
|
|
'form_up_country' => $formUpCountry,
|
|
|
|
|
'form_entertainment_presentation' => $formEntertaimentPresentation,
|
|
|
|
|
'form_vehicle_running_cost' => $formVehicleRunningCost,
|
|
|
|
|
'form_others' => $formOthers,
|
|
|
|
|
'form_meeting_seminar' => $formMeetingSeminar,
|
|
|
|
|
];
|
|
|
|
|
|
2024-12-26 13:28:06 +07:00
|
|
|
session()->put('redirect_url', route('admin.admins.expense', $user_id));
|
|
|
|
|
|
2024-12-23 10:42:27 +07:00
|
|
|
// Pass the forms and page information to the view
|
|
|
|
|
return view('backend.pages.admins.expense', [
|
|
|
|
|
'pageInfo' => [
|
|
|
|
|
'title' => 'Form Management',
|
|
|
|
|
],
|
|
|
|
|
'forms' => $forms,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-12-02 01:18:34 +07:00
|
|
|
public function destroy(int $id): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['admin.delete']);
|
|
|
|
|
|
|
|
|
|
$admin = Admin::findOrFail($id);
|
|
|
|
|
$admin->delete();
|
|
|
|
|
session()->flash('success', 'Admin has been deleted.');
|
2024-12-12 18:18:26 +07:00
|
|
|
return redirect()->route('admin.admins.index');
|
2024-12-02 01:18:34 +07:00
|
|
|
}
|
|
|
|
|
}
|