관리-도구
편집 파일: UserController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Validation\Rule; use App\User; use App\Models\JobPost; use App\Models\Company; use Validator; use Hash; class UserController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { date_default_timezone_set(get_option('timezone','Asia/Dhaka')); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // $users = User::where('user_type','user') // ->orWhere('user_type','admin') // ->orderBy('name','asc') // ->get(); $users = User::orderBy('name','asc')->get(); return view('backend.user.list',compact('users')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { if( ! $request->ajax()){ return view('backend.user.create'); }else{ return view('backend.user.modal.create'); } } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|max:255', 'email' => 'required|email|unique:users|max:255', 'user_type' => 'required', 'status' => 'required', 'profile_picture' => 'nullable|image', 'password' => 'required|min:6', ]); if ($validator->fails()) { if($request->ajax()){ return response()->json(['result'=>'error','message'=>$validator->errors()->all()]); }else{ return redirect()->route('users.create') ->withErrors($validator) ->withInput(); } } $profile_picture = "default.png"; if($request->hasfile('profile_picture')) { $file = $request->file('profile_picture'); $profile_picture = time().$file->getClientOriginalName(); $file->move(public_path()."/uploads/profile/", $profile_picture); } $user = new User(); $user->name = $request->input('name'); $user->email = $request->input('email'); $user->user_type = $request->input('user_type'); $user->employer_spotlight = $request->input('employer_spotlight'); $user->candidate_spotlight = $request->input('candidate_spotlight'); $user->role_id = $request->input('role_id'); $user->status = $request->input('status'); $user->profile_picture = $profile_picture; $user->email_verified_at = date('Y-m-d H:i:s'); $user->password = Hash::make($request->password); $user->save(); //Prefix Output $user->status = status($user->status); $user->user_type = ucwords($user->user_type); $user->profile_picture = '<img src="' . profile_picture($user->profile_picture) . '" class="thumb-sm rounded-circle mr-2">'; if(! $request->ajax()){ return redirect()->route('users.create')->with('success', _lang('Saved Sucessfully')); }else{ return response()->json(['result'=>'success','action'=>'store','message'=>_lang('Saved Sucessfully'),'data'=>$user, 'table' => '#users_table']); } } public function storeUser(Request $request) { // dd($request->all()); $validator = Validator::make($request->all(), [ 'name' => 'required|max:255', 'email' => 'required|email|max:255', // 'user_type' => 'required', // 'status' => 'required', 'profile_picture' => 'nullable|image', 'password' => 'required|min:6', ]); if ($validator->fails()) { // dd("working"); return back()->with('error','Kindly Add All Required Field, i-e: Name,Eamil,password'); } $profile_picture = "default.png"; if($request->hasfile('profile_picture')) { $file = $request->file('profile_picture'); $profile_picture = time().$file->getClientOriginalName(); $file->move(public_path()."/uploads/profile/", $profile_picture); } if (User::where('email',$request->input('email'))->first()) { return back()->with('error','This Email Is Already Registered'); }else{ $user = new User(); $user->name = $request->input('name'); $user->email = $request->input('email'); $user->job_title = $request->input('job_title')?? ''; $user->job_location = $request->input('job_location')??''; $user->desired_pay = $request->input('desired_pay')??''; $user->minimum_pay = $request->input('minimum_pay')??''; $user->workplace_type = $request->input('workplace_type')??''; $user->purpose = $request->input('purpose')??''; $user->user_type = 'user'; $user->role_id = ''; $user->status = 1; $user->profile_picture = $profile_picture; $user->email_verified_at = date('Y-m-d H:i:s'); $user->password = Hash::make($request->password); $save = $user->save(); } //Prefix Output $user->status = status($user->status); $user->user_type = ucwords($user->user_type); $user->profile_picture = '<img src="' . profile_picture($user->profile_picture) . '" class="thumb-sm rounded-circle mr-2">'; if($save){ return redirect('user-signup')->with('success', _lang('Registered Sucessfully')); }else{ return redirect('user-signup')->with('error', _lang('registration Failed')); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show(Request $request,$id) { $user = User::find($id); if(! $request->ajax()){ return view('backend.user.view',compact('user','id')); }else{ return view('backend.user.modal.view',compact('user','id')); } } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit(Request $request,$id) { $user = User::find($id); if(! $request->ajax()){ return view('backend.user.edit',compact('user','id')); }else{ return view('backend.user.modal.edit',compact('user','id')); } } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $validator = Validator::make($request->all(), [ 'name' => 'required|max:255', 'email' => [ 'required', 'email', Rule::unique('users')->ignore($id), ], 'user_type' => 'required', 'status' => 'required', 'profile_picture' => 'nullable|image', 'password' => 'nullable|min:6', ]); if ($validator->fails()) { if($request->ajax()){ return response()->json(['result'=>'error','message'=>$validator->errors()->all()]); }else{ return redirect()->route('users.edit', $id) ->withErrors($validator) ->withInput(); } } // Fetch the current number of spotlight employers $spotlightEmployerCount = User::where('employer_spotlight', 1)->count(); // Check if trying to add a fourth spotlight employer if ($spotlightEmployerCount >= 3 && $request->input('employer_spotlight') == 1 && User::find($id)->employer_spotlight != 1) { return redirect()->route('users.edit', $id)->with('error', 'You can only spotlight up to 3 employers.'); } if($request->hasfile('profile_picture')) { $file = $request->file('profile_picture'); $profile_picture = time().$file->getClientOriginalName(); $file->move(public_path()."/uploads/profile/", $profile_picture); } $user = User::find($id); $user->name = $request->input('name'); $user->email = $request->input('email'); $user->user_type = $request->input('user_type'); $user->employer_spotlight = $request->input('employer_spotlight'); $user->candidate_spotlight = $request->input('candidate_spotlight'); $user->role_id = $request->input('role_id'); $user->status = $request->input('status'); if($request->hasfile('profile_picture')){ $user->profile_picture = $profile_picture; } if($request->password){ $user->password = Hash::make($request->password); } $user->save(); //Prefix Output $user->status = status($user->status); $user->user_type = ucwords($user->user_type); $user->profile_picture = '<img src="' . profile_picture($user->profile_picture) . '" class="thumb-sm rounded-circle mr-2">'; if(! $request->ajax()){ return redirect()->route('users.index')->with('success', _lang('Updated Sucessfully')); }else{ return response()->json(['result'=>'success','action'=>'update', 'message'=>_lang('Updated Sucessfully'),'data'=>$user, 'table' => '#users_table']); } } public function updateUserDetails(Request $request) { // dd($request->all()); $user = User::find($request->extra); if($request->hasfile('profile_picture')) { $file = $request->file('profile_picture'); $profile_picture = time().$file->getClientOriginalName(); $file->move(public_path()."/uploads/profile/", $profile_picture); $user->profile_picture = $profile_picture; } if($request->has('_name')) { $user->name = $request->input('_name'); $user->user_headline = $request->input('headline'); $user->user_location = $request->input('location'); $user->user_zip = $request->input('zip'); } if($request->has('phone')) { $user->phone = $request->input('phone'); } if($request->has('website_url')) { $user->user_website_url = $request->input('website_url'); $user->user_twitter_url = $request->input('twitter_url'); $user->user_facebook_url = $request->input('facebook_url'); $user->user_linkedin_url = $request->input('linkedin_url'); } if($request->hasfile('resume')) { $file = $request->file('resume'); $resume = time().$file->getClientOriginalName(); $file->move(public_path()."/uploads/resume/", $resume); $user->user_resume = $resume; } if($request->hasfile('certificate')) { $file = $request->file('certificate'); $certificate = time().$file->getClientOriginalName(); $file->move(public_path()."/uploads/certificates/", $certificate); $user->user_certificate = $certificate; } if($request->has('user_executive_summary')) { $user->user_executive_summary = $request->input('user_executive_summary'); } if($request->has('user_objectives')) { $user->user_objective = $request->input('user_objectives'); } if($request->has('user_achievements')) { $user-> user_achievements = $request->input('user_achievements'); } if($request->has('user_association')) { $user->user_associations = $request->input('user_association'); } if($request->has('user_skills')) { $user->user_skills = $request->input('user_skills'); } if($request->has('desired_salary')) { $user->desired_pay = $request->input('desired_salary'); $user->user_experience = $request->input('user_experience'); $user->user_highest_degree = $request->input('user_highest_degree'); $user->user_industry = $request->input('user_industry'); $user->user_veteran = $request->input('user_veteran'); } if($request->has('is_private')) { if($request->input('is_private') == 'yes'){ $user->is_private = 'yes'; }else{ $user->is_private = 'no'; } } // $user->name = $request->input('name'); // $user->email = $request->input('email'); // $user->user_type = $request->input('user_type'); // $user->role_id = $request->input('role_id'); // $user->status = $request->input('status'); // if($request->hasfile('profile_picture')){ // $user->profile_picture = $profile_picture; // } // if($request->password){ // $user->password = Hash::make($request->password); // } $save = $user->save(); //Prefix Output // $user->status = status($user->status); // $user->user_type = ucwords($user->user_type); // $user->profile_picture = '<img src="' . profile_picture($user->profile_picture) . '" class="thumb-sm rounded-circle mr-2">'; if($save){ return redirect()->back()->with('success', _lang('Updated Sucessfully')); }else{ return redirect()->back()->with('error', _lang("Couldn't Update,Try Again!")); } } public function showUserDashboard($id) { if(Auth::check()){ $user = User::where('status',1)->where('id',$id)->first(); return view("theme.saintsaccessories.user-dashboard")->with('data',$user); }else{ return redirect('/')->with('error','Please Login To Continue!'); } } public function showHomePage() { // Fetch Spotlight Employers $spotlightEmployers = User::where('employer_spotlight', 1)->orderBy('created_at', 'desc')->take(3)->get(); // Pass the data to the view return view('theme.saintsaccessories.index', compact('spotlightEmployers')); } // public function showUserDetails($id) // { // $data = User::findOrFail($id); // return view('theme.saintsaccessories.employer_details', compact('data')); // } public function showEmployerDetails($id) { // Fetch the user by their ID $data = User::findOrFail($id); // Fetch the company details where the company's email matches the user's company email $company = Company::where('company_email', $data->email)->first(); // Pass both the user and company data to the view return view('theme.saintsaccessories.employer_details', compact('data', 'company')); } public function showEmployerPage() { // Fetch the 3 most recent Spotlight Candidates $spotlightCandidates = User::where('candidate_spotlight', 1)->orderBy('created_at', 'desc')->take(3)->get(); // Pass the data to the view return view('theme.saintsaccessories.employeer', compact('spotlightCandidates')); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function deleteUserAndPosts($id) { // Find the user by ID $user = User::findOrFail($id); // Delete job posts associated with the user's company name JobPost::where('company_name', $user->company_name)->delete(); // Delete the user $user->delete(); return response()->json(['message' => 'User and their job posts deleted successfully.']); } public function destroy($id) { $user = User::find($id); $user->delete(); return redirect()->route('users.index')->with('success',_lang('Deleted Sucessfully')); } }