%PDF- %PDF-
Direktori : /home/silvzytp/ccd-ind-code/app/Http/Controllers/Backend/ |
Current File : //home/silvzytp/ccd-ind-code/app/Http/Controllers/Backend/AgentController.php |
<?php namespace App\Http\Controllers\Backend; use DataTables; use App\Models\Role; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Hash; use App\Http\Requests\AgentRequest; use App\Http\Requests\ManagerFormRequest; class AgentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { Gate::authorize('app.agents.index'); // page title page_title('Agents'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>'']; // managers $managers = User::toBase()->where('role_id',3)->select('id','name')->get(); return view('backend.pages.agents.index',['breadcrumb'=>$breadcrumb,'managers'=>$managers]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function getData(Request $request){ if ($request->ajax()) { if (Auth::user()->role->slug == 'admin' || Auth::user()->role->slug == 'super-admin') { $users = User::with('role')->where('role_id',4)->latest('id'); }else{ $users = User::with('role')->where(['role_id'=>4,'manager_id'=>Auth::id()])->latest('id'); } return DataTables::eloquent($users) ->addIndexColumn() ->filter(function ($query) use ($request){ if (!empty($request->search_text)) { if(Auth::user()->role->slug == 'manager'){ $query->where(['role_id'=>4,'manager_id'=>Auth::id()]) ->where('name', 'LIKE',"%{$request->search_text}%") ->orWhere(function($whereQuery) use ($request){ $whereQuery->where(['role_id'=>4,'manager_id'=>Auth::id()]) ->where('phone_no','LIKE',"%{$request->search_text}%"); })->orWhere(function($whereQuery) use ($request){ $whereQuery->where(['role_id'=>4,'manager_id'=>Auth::id()]) ->where('email','LIKE',"%{$request->search_text}%"); }); }else{ $query->where('role_id',4) ->where('name', 'LIKE',"%{$request->search_text}%") ->orWhere(function($whereQuery) use ($request){ $whereQuery->where('role_id',4) ->where('phone_no','LIKE',"%{$request->search_text}%"); })->orWhere(function($whereQuery) use ($request){ $whereQuery->where('role_id',4) ->where('email','LIKE',"%{$request->search_text}%"); }); } } }) ->addColumn('role', function($user){ $roleName = $user->role ? $user->role->name : ''; return '<span class="badge badge-warning">'.$roleName.'</span>'; }) ->addColumn('bulk_check', function($user){ if ($user->manager_id) { return ' <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input"> <label class="custom-control-label"></label> </div> '; }else{ return ' <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" name="checked" value="'.$user->id.'"" id="checked-'.$user->id.'"> <label class="custom-control-label" for="checked-'.$user->id.'"></label> </div> '; } }) ->addColumn('action', function($user){ $action = ''; if (Gate::allows('app.agents.show')) { $action .= ' <a href="'.route(routeName().'.agents.show', $user->id).'" class="btn-style btn-style-view btn-view ml-1" data-id="'.$user->id.'"><i class="fa fa-eye"></i> </a>'; } if (Gate::allows('app.agents.edit')) { $action .= '<a class="btn-style btn-style-edit ml-1" href="'.route(routeName().'.agents.edit',$user->id).'"><i class="fa fa-edit"></i></a>'; } if (Gate::allows('app.agents.destroy')) { $action .= '<button class="btn-style btn-style-danger deletable-btn ml-1" data-title="Do you really want to delete this record?" data-id="'.$user->id.'"><i class="fa fa-trash"></i></button>'; } return '<div class="dropdown show text-right">'.$action.'</div>'; }) ->rawColumns(['bulk_check','profile','role','action']) ->make(true); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { Gate::authorize('app.agents.create'); // page title page_title('New Agent'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>route(routeName().'.agents.index'),'Create'=>'']; return view('backend.pages.agents.form',['breadcrumb'=>$breadcrumb]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\AgentRequest $request * @return \Illuminate\Http\Response */ public function store(AgentRequest $request) { // agent create User::create([ 'role_id' => 4, 'name' => $request->full_name, 'email' => $request->email, 'password' => Hash::make($request->password), 'password_text' => $request->password, 'phone_no' => $request->phone, 'note' => $request->description, ]); return redirect()->route(routeName().'.agents.index')->with('success',ALERT_SUCCESS); } public function show($id){ Gate::authorize('app.agents.show'); $agent = User::findOrFail($id); // page title page_title('Agent Details'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>route(routeName().'.agents.index'),'Details'=>'']; return view('backend.pages.agents.view',['breadcrumb'=>$breadcrumb,'agent'=>$agent]); } /** * User status * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function status(Request $request){ if ($request->ajax()) { User::findOrFail($request->data_id)->update(['status'=>$request->status]); $output = ['status'=>'success','message'=>'Status updated.']; return response()->json($output); } } /** * Show the form for editing the specified resource. * * @param \App\Models\User $user * @return \Illuminate\Http\Response */ public function edit($id) { Gate::authorize('app.agents.edit'); $agent = User::findOrFail($id); $managers = User::where('role_id', 3)->get(); // page title page_title('Edit Agent'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>route(routeName().'.agents.index'),'Edit'=>'']; return view('backend.pages.agents.form',['breadcrumb'=>$breadcrumb,'agent'=>$agent,'managers'=>$managers]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\AgentRequest $request * @param \App\Models\User $id * @return \Illuminate\Http\Response */ public function update(AgentRequest $request, $id) { $agent = User::find($id); // agent update $agent->update([ 'manager_id' => $request->manager, 'name' => $request->full_name, 'email' => $request->email, 'phone_no' => $request->phone, 'password' => Hash::make($request->password), 'password_text' => $request->password, 'note' => $request->description, ]); return back()->with('success', ALERT_UPDATE); } /** * Remove the specified resource from storage. * * @param \App\Models\User $user * @return \Illuminate\Http\Response */ public function destroy(Request $request) { if ($request->ajax()) { if (Gate::allows('app.agents.destroy')) { $manager = User::find($request->data_id); if ($manager == true) { file_remove($manager->avatar); // profile image remove from storage $manager->delete(); // user remove from database $output = response_alert('success',ALERT_DELETE); }else{ $output = response_alert('error',SERVER_ERROR); } } else { $output = response_alert('error',UNAUTHORIZED); } return response()->json($output); } } /** * Remove the users resource from storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function bulkDestroy(Request $request){ if ($request->ajax()) { if (!empty($request->bulk_id)) { $userId = explode(',', $request->bulk_id); $agent = User::whereIn('id', $userId); if ($agent) { if ($request->operation_type == 'delete') { $agent->delete(); $output = response_alert('success',ALERT_DELETE); }elseif($request->operation_type == 'assign'){ if (!empty($request->manager)) { $manager = User::find($request->manager); if ($manager == true) { // assign aggent by manager id $agent->update([ 'manager_id'=>$request->manager ]); $output = response_alert('success','Agent assign successfull.'); }else{ $output = response_alert('error','Somthing Wrong!'); } }else{ $output = response_alert('error','Please selected try'); } }else{ $output = response_alert('error','Operation somthing wrong!'); } }else{ $output = response_alert('error',SERVER_ERROR); } }else{ $output = response_alert('error','Please selected try'); } return response()->json($output); } } }