File: /home/imensosw/.trash/app.2/Http/Controllers/Admin/VolunteerController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreConsultancyRequest;
use App\Http\Requests\UpdateConsultancyRequest;
use App\Role;
use App\User;
use App\Partner;
use App\Volunteer;
use App\Country;
use App\Organization;
use App\VolunteerOpportunity;
use Validator;
use Gate;
use URL;
use Auth;
use File;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Notifications\NewUserEmailNotification;
use App\Notifications\NewUser;
class VolunteerController extends Controller
{
public function index()
{
abort_if(Gate::denies('volunteer_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role)){
$volunteers = User::join('role_user', 'users.id', '=', 'role_user.user_id')->where('role_user.role_id',4)->get();
return view('admin.volunteers.index', compact('volunteers'));
}else{
return redirect()->back()->with('error','Wrong access!');
}
}
public function create()
{
abort_if(Gate::denies('volunteer_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$roles = Role::whereIn('id',[4])->pluck('title', 'id');
$countries = Country::all();
//$organizations = Organization::all();
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role)){
return view('admin.volunteers.create', compact('roles'));
}else{
return redirect()->back()->with('error','Wrong access!');
}
}
public function store(Request $request)
{
abort_if(Gate::denies('volunteer_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$validator = Validator::make($request->all(),
[
'name' => [
'required',
],
'email' => [
'required',
'unique:users',
],
'password' => [
'required',
],
'roles.*' => [
'integer',
],
'roles' => [
'required',
'array',
],
]
);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$client_role = array(0=>4);
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
$add_array = array();
if(in_array('Admin',$role)){
$add_array['user_status'] = 1;
$add_array['email_verified_at'] = date('Y-m-d H:i:s');
$add_array['approved_at'] = date('Y-m-d H:i:s');
$request->merge($add_array);
$user = Volunteer::create($request->all());
$user->roles()->sync($client_role);
$mailData = [
'greeting' => 'Hi '.$user->name.',',
'body' => 'Your Validator Account is Created on LiftCincy by Admin.
your Login detail are :
username - '.$user->email.'
Password - '.$request->password,
'thanks' => 'Thank you to connect with LiftCincy,',
'actionText' => 'View Account',
'actionURL' => url('/'),
];
//$user->notify(new NewUserEmailNotification($mailData));
$this_user = User::find($user->id);
$admin = User::find(1);
//$admin->notify(new NewUser($this_user));
return redirect()->route('admin.volunteers.index')->with('success','Volunteer Created successfully!');
}else{
return redirect()->back()->with('error','Wrong access!');
}
}
public function edit(User $user,$id)
{
abort_if(Gate::denies('volunteer_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(User::where('id',$id)->count()){
$user = User::find($id);
$roles = Role::whereIn('id',[4])->pluck('title', 'id');
$user->load('roles');
$countries = Country::all();
$user_c = Auth::user();
$role = $user_c->roles->pluck('title','id')->toArray();
$organizations = Organization::all();
if(in_array('Admin',$role)){
return view('admin.volunteers.edit', compact('user','roles','countries','organizations'));
}else{
return redirect()->back()->with('error','Wrong access');
}
}
}
public function update(Request $request, $id)
{
abort_if(Gate::denies('volunteer_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$validator = Validator::make($request->all(),
[
'name' => [
'required',
],
'email' => [
'required',
'unique:users,email,'.$id,
],
'org_contact' => [
'required',
'regex:/^([0-9\s\-\+\(\)]*)$/',
'min:9',
],
'roles.*' => [
'integer',
],
'roles' => [
'required',
'array',
],
],[
'org_contact.required' => 'Contact number is required!',
'org_contact.regex' => 'The Contact number format is invalid!',
'org_contact.min' => 'The Contact number must be at least 9 characters!',
]
);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
if(User::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',4)->where('users.id',$id)->count()){
$user = Volunteer::find($id);
$client_role = array(0=>4);
$c_user = Auth::user();
$role = $c_user->roles->pluck('title','id')->toArray();
$add_array = array();
if(in_array('Admin',$role)){
//$add_array['company_id'] = $c_user->id;
//$add_array['parent_id'] = $c_user->id;
//$request->merge($add_array);
$user->update($request->all());
return redirect()->route('admin.volunteers.index')->with('success','Volunteer updated successfully!');
}else{
return redirect()->route('admin.volunteers.edit',[$id])->with('error','Wrong access!');
}
}else{
return redirect()->route('admin.volunteers.edit',[$id])->with('error','Wrong access!');
}
}
public function show(Request $user,$id)
{
abort_if(Gate::denies('volunteer_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(User::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',4)->where('users.id',$id)->count()){
$user = User::find($id);
$roles = Role::whereIn('id',[4])->pluck('title', 'id');
$user->load('roles');
$countries = Country::all();
$user_profile_img = URL::asset('images/profile.webp');
$organizations = Organization::all();
//$user_docs = UserDoc::where('user_id',$id)->get();
if(!empty($user->user_profile_img)){
$user_profile_img = URL::asset('Profile/'.$user->id.'/'.$user->user_profile_img);
}
return view('admin.volunteers.show', compact('user','roles','countries','user_profile_img','organizations'));
}
}
public function destroy(Request $request,$id)
{
abort_if(Gate::denies('volunteer_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(User::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',4)->where('users.id',$id)->count()){
$user_c = Auth::user();
$role = $user_c->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role)){
/*if(user::where('parent_id',$id)->count() || user::where('company_id',$id)->count()){
return back()->with('error','User Associated with other users, Can not delete!');
}else{
$user = User::find($id);
$user->delete();
return back()->with('success','Partner deleted successfully!');
}*/
$user = User::find($id);
$user->delete();
return back()->with('success','Volunteer deleted successfully!');
}
}
return back()->with('error','Something Wrong!');
}
}