%PDF- %PDF-
Direktori : /home/silvzytp/calling_code/app/Http/Controllers/Backend/Member/ |
Current File : //home/silvzytp/calling_code/app/Http/Controllers/Backend/Member/ManagerController.php |
<?php namespace App\Http\Controllers\Backend\Member; 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\ManagerFormRequest; class ManagerController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { Gate::authorize('app.users.index'); // page title page_title('Users'); // breadcrumb $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Users'=>'']; return view('backend.member.pages.users.index',['breadcrumb'=>$breadcrumb]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function getData(Request $request){ if ($request->ajax()) { $users = User::with('role')->whereNotIn('role_id',[1,Auth::user()->role_id])->latest('id'); return DataTables::eloquent($users) ->addIndexColumn() ->filter(function ($query) use ($request){ if (!empty($request->status)) { $query->where('status', $request->status); } }) ->addColumn('profile', function($user){ if (file_exists($user->avatar)) { $img = '<img class="avatar-img" src="'.asset($user->avatar).'" >'; }else{ $img = '<img class="avatar-img" src="'.asset('uploads/images/user.png').'" >'; } return $img; }) ->addColumn('role', function($user){ $roleName = $user->role ? $user->role->name : ''; $badge = $user->role->name == 'Manager' ? 'badge-primary' : 'badge-warning'; return '<span class="badge '.$badge.'">'.$roleName.'</span>'; }) ->addColumn('status', function($user){ if ($user->role_id == 4) { return '--'; }else{ $checked = $user->status == 1 ? 'checked' : ''; return ' <div class="onoffswitch"> <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox user-status" data-id="'.$user->id.'" id="status-'.$user->id.'" '.$checked.'> <label class="onoffswitch-label" for="status-'.$user->id.'"> <span class="onoffswitch-inner"></span> <span class="onoffswitch-switch"></span> </label> </div>'; } }) ->addColumn('bulk_check', function($user){ return ' <label class="ui-checkbox"> <input type="checkbox" name="checked" value="'.$user->id.'"> <span class="input-span"></span> </label> '; }) ->addColumn('action', function($user){ $action = ''; if (Gate::allows('app.users.show')) { $action .= '<button class="btn-style btn-style-view btn-view mr-1" data-id="'.$user->id.'"><i class="fa fa-eye"></i> </button>'; } if(Gate::allows('app.users.destroy')) { $action .= '<a href="javascript:" class="btn-style-danger btn-style deletable-btn" '.tooltip('Delete').' data-title="Do you really want to delete this record?" data-id="'.$user->id.'"><i class="fa fa-trash"></i></a>'; } return '<div class="text-right">'.$action.'<div'; }) ->rawColumns(['bulk_check','profile','role','status','action']) ->make(true); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { Gate::authorize('app.users.create'); // page title page_title('New User'); // breadcrumb $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Users'=>route('app.users.index'),'Create'=>'']; // roles $roles = Role::where('slug',['super-admin','agent','admin'])->orderBy('name','asc')->get(); return view('backend.member.pages.users.form',['breadcrumb'=>$breadcrumb,'roles'=>$roles]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(ManagerFormRequest $request) { if ($request->ajax()) { DB::beginTransaction(); try { // new manager create $manager = User::create([ 'role_id' => 3, 'admin_id' => Auth::id(), 'name' => $request->full_name, 'email' => $request->email, 'password' => Hash::make($request->password), 'password_text' => $request->password, 'target' => $request->target, 's_target' => $request->sale_target ]); if ($request->agent) { // new agent create $data = []; foreach ($request->agent as $key => $value) { $data[] = [ 'manager_id' => $manager->id, 'role_id' => 4, 'name' => $value['name'], 'email' => $value['email'], 'password' => Hash::make(12345678), 'password_text' => 12345678, 'target' => $value['target'], 'note' => $value['description'], 'created_at' => now(), 'updated_at' => now() ]; } User::insert($data); } DB::commit(); $output = ['status' => 'success', 'message' => 'New user created successfully.']; } catch (\Exception $e) { DB::rollBack(); $output = ['status' => 'error', 'message' => $e->getMessage()]; } return response()->json($output); } } /** * 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); } } public function show(Request $request){ if ($request->ajax()) { $user = User::with('agents','manager','role')->findOrFail($request->data_id); // agent lists if (!$user->agents->isEmpty()) { $agents = ' <tr> <td><strong>Total Agents:</strong></td> <td> <ol class="agent-list">'; foreach($user->agents as $index=>$agent){ $agents .= ' <li>'.$agent->name.'</li> '; } $agents .= '</ol> </td> </tr>'; }else{ $agents = ''; } // phone number if($user->phone_no != ''){ $phone = ' <img class="country-flag" src="https://flagicons.lipis.dev/flags/4x3/'.strtolower($user->country_code).'.svg" alt=""> '.$user->phone_no.' '; }else{ $phone = '--'; } $output = ' <table class="w-100 table table-sm table-borderless border-secondary"> <tr> <td><strong>Role Name:</strong></td> <td><span class="badge badge-danger">'.$user->role->name.'</span></td> </tr> <tr> <td><strong>Full Name:</strong></td> <td>'.$user->name.'</td> </tr> <tr> <td><strong>Email:</strong></td> <td>'.$user->email.'</td> </tr> <tr> <td><strong>Phone No:</strong></td> <td> <div class="sub-heading d-flex align-items-center"> '.$phone.' </div> </td> </tr> <tr> <td><strong>Target:</strong></td> <td><strong>'.$user->target.'</strong></td> </tr> '.$agents.' <tr> <td><strong>Created At</strong></td> <td>'.date_format($user->created_at, 'd-m-Y').'</td> </tr> </table> '; return response()->json($output); } } /** * Show the form for editing the specified resource. * * @param \App\Models\User $user * @return \Illuminate\Http\Response */ public function edit(User $user) { Gate::authorize('app.users.edit'); // page title page_title('Edit User'); // breadcrumb $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Users'=>route('app.users.index'),'Edit'=>'']; // roles $roles = Role::whereNotIn('slug',['super-admin','agent','admin'])->orderBy('name','asc')->get(); return view('backend.member.pages.users.form',['breadcrumb'=>$breadcrumb,'roles'=>$roles,'user'=>$user]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\User $user * @return \Illuminate\Http\Response */ public function update(Request $request, User $user) { // validate rules $request->validate([ 'full_name' => 'required|max:100', 'email' => 'required|email|max:150|unique:users,email,'.$user->id, 'password' => 'required|min:8|max:16', 'role' => 'required|integer', 'profile' => 'nullable|image|mimes:jpg,jpeg,png' ]); // file upload $file = file_update($request->file('profile'),'profile/',$user->avatar); $user->update([ 'role_id' => $request->role, 'name' => $request->full_name, 'email' => $request->email, 'password' => Hash::make($request->password), 'password_text' => $request->password, 'avatar' => $file ]); 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.users.destroy')) { $user = User::findOrFail($request->data_id); file_remove($user->avatar); // profile image remove from storage $user->delete(); // user remove from database $output = response_alert('success',ALERT_DELETE); } else { $output = 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 (Gate::allows('app.users.bulk-destroy') || Gate::allows('app.users.bulk-status')) { if (!empty($request->bulk_id)) { $userId = explode(',', $request->bulk_id); $users = User::whereIn('id', $userId); if ($request->operation_type == 'delete') { $users->delete(); $output = response_alert('success',ALERT_DELETE); }elseif(in_array($request->operation_type, ['enabled','disabled']) == true){ $user_status = $request->operation_type == 'enabled' ? 1 : 2; $users->update(['status'=>$user_status]); $output = response_alert('success',ALERT_SUCCESS); }else{ $output = response_alert('error','Operation somthing wrong!'); } }else{ $output = reponse_alert('error','Please user selected try'); } } else { $output = UNAUTHORIZED; } return response()->json($output); } } }