![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mcoil.corals.io/app/Http/Controllers/Admin/ |
<?php namespace App\Http\Controllers\Admin; use App\Shop\Employees\Requests\CreateEmployeeRequest; use App\Shop\Employees\Requests\UpdateEmployeeRequest; use App\Shop\Employees\Repositories\EmployeeRepository; use App\Shop\Employees\Employee; use App\Shop\Employees\Repositories\Interfaces\EmployeeRepositoryInterface; use App\Shop\Roles\Repositories\RoleRepositoryInterface; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Hash; class EmployeeController extends Controller { /** * @var EmployeeRepositoryInterface */ private $employeeRepo; /** * @var RoleRepositoryInterface */ private $roleRepo; /** * EmployeeController constructor. * * @param EmployeeRepositoryInterface $employeeRepository * @param RoleRepositoryInterface $roleRepository */ public function __construct( EmployeeRepositoryInterface $employeeRepository, RoleRepositoryInterface $roleRepository ) { $this->employeeRepo = $employeeRepository; $this->roleRepo = $roleRepository; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $list = $this->employeeRepo->listEmployees('name', 'DESC'); return view('admin.employees.list', [ 'employees' => $list->all() ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $roles = $this->roleRepo->listRoles(); return view('admin.employees.create', compact('roles')); } /** * Store a newly created resource in storage. * * @param CreateEmployeeRequest $request * * @return \Illuminate\Http\Response */ public function store(CreateEmployeeRequest $request) { $data = $request->except('_token', '_method'); $data['name'] = $data['fname'] . ' ' . $data['lname']; $employee = $this->employeeRepo->createEmployee($data); if ($request->has('role')) { $employeeRepo = new EmployeeRepository($employee); $employeeRepo->syncRoles([$request->input('role')]); } return redirect()->route('admin.employees.index')->with('message', 'Employee added successfully.'); } /** * Display the specified resource. * * @param int $id * * @return \Illuminate\Http\Response */ public function show(int $id) { $employee = $this->employeeRepo->findEmployeeById($id); return view('admin.employees.show', ['employee' => $employee]); } /** * Show the form for editing the specified resource. * * @param int $id * * @return \Illuminate\Http\Response */ public function edit(int $id) { $employee = $this->employeeRepo->findEmployeeById($id); $roles = $this->roleRepo->listRoles('name', 'DESC'); $isCurrentUser = $this->employeeRepo->isAuthUser($employee); return view( 'admin.employees.edit', [ 'employee' => $employee, 'roles' => $roles, 'isCurrentUser' => $isCurrentUser, 'selectedIds' => $employee->roles()->pluck('role_id')->all() ] ); } /** * Update the specified resource in storage. * * @param UpdateEmployeeRequest $request * @param int $id * * @return \Illuminate\Http\Response */ public function update(UpdateEmployeeRequest $request, $id) { $employee = $this->employeeRepo->findEmployeeById($id); $isCurrentUser = $this->employeeRepo->isAuthUser($employee); $empRepo = new EmployeeRepository($employee); $data = $request->except('_token', '_method', 'password'); $data['name'] = $data['fname'] . ' ' . $data['lname']; $empRepo->updateEmployee($data); if ($request->has('password') && !empty($request->input('password'))) { $employee->password = Hash::make($request->input('password')); $employee->save(); } if ($request->has('role') and !$isCurrentUser) { $employee->roles()->sync($request->input('role')); } elseif (!$isCurrentUser) { $employee->roles()->detach(); } //return redirect()->route('admin.employees.edit', $id)->with('message', 'Update successful'); return redirect()->route('admin.employees.index')->with('message', 'Employee updated successfully.'); } /** * Remove the specified resource from storage. * * @param int $id * * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(int $id) { $employee = $this->employeeRepo->findEmployeeById($id); $employeeRepo = new EmployeeRepository($employee); //$employeeRepo->deleteEmployee(); $emp = Employee::find($id); if($emp){ $emp->forceDelete(); $employee->roles()->detach(); } return redirect()->route('admin.employees.index')->with('message', 'Employee delete successful'); } /** * @param $id * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function getProfile($id) { $employee = $this->employeeRepo->findEmployeeById($id); return view('admin.employees.profile', ['employee' => $employee]); } /** * @param UpdateEmployeeRequest $request * @param $id * * @return \Illuminate\Http\RedirectResponse */ public function updateProfile(UpdateEmployeeRequest $request, $id) { $employee = $this->employeeRepo->findEmployeeById($id); $update = new EmployeeRepository($employee); $update->updateEmployee($request->except('_token', '_method', 'password')); if ($request->has('password') && $request->input('password') != '') { $update->updateEmployee($request->only('password')); } return redirect()->route('admin.employee.profile', $id)->with('message', 'Employee update successful'); } }