File: /home/imensosw/.trash/app.2/Http/Controllers/Admin/OpportunityController.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\Opportunity;
use App\OpportunityVolunteer;
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;
use App\Notifications\NewVolunteerApplyEmailNotification;
use Illuminate\Support\Str;
class OpportunityController extends Controller
{
public function index()
{
abort_if(Gate::denies('opportunity_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Partner',$role)){
$user_type = 'Partner';
$opportunities = Opportunity::join('users', 'opportunities.partner_id', '=', 'users.id')->where('opportunities.partner_id',$user->id)->select('opportunities.*','users.name as user_name','opportunities.id as opportunity_id')->get();
return view('admin.opportunities.index', compact('opportunities','user_type'));
}else if(in_array('Admin',$role)){
$user_type = 'Admin';
$opportunities = Opportunity::join('users', 'opportunities.partner_id', '=', 'users.id')->select('opportunities.*','users.name as user_name','opportunities.id as opportunity_id')->get();
return view('admin.opportunities.index', compact('opportunities','user_type'));
}else{
return redirect()->back()->with('error','Wrong access!');
}
}
public function create()
{
abort_if(Gate::denies('opportunity_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role)){
$user_type = 'Admin';
$partners = Partner::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',3)->get();
return view('admin.opportunities.create', compact('partners','user_type'));
}else if(in_array('Partner',$role)){
$user_type = 'Partner';
$partners = Partner::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',3)->where('users.id',$user->id)->get();
return view('admin.opportunities.create', compact('partners','user_type'));
}else{
return redirect()->back()->with('error','Wrong access!');
}
}
public function store(Request $request)
{
abort_if(Gate::denies('opportunity_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$valuesToAdd = array();
if(isset($request->slug) && !empty($request->slug)){
$valuesToAdd['slug'] = Str::slug($request->slug, '-');
}else{
$valuesToAdd['slug'] = Str::slug($request->name, '-');
}
$request->merge($valuesToAdd);
$validator = Validator::make($request->all(),
[
'partner_id' => [
'required',
],
'opportunity_title' => [
'required',
'unique:opportunities',
],
'slug' => [
'required',
'unique:opportunities,slug',
],
'opportunity_hours' => [
'required',
],
'no_of_volunteers' => [
'required',
'integer',
],
'start_date' => [
'required',
],
'start_time' => [
'required',
],
'end_time' => [
'required',
]
]
);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
if(!Partner::find($request->partner_id)){
return redirect()->back()->withError('Partner not found!');
}
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
$add_array = array();
if(in_array('Admin',$role)){
$add_array['opportubity_status'] = 1;
$request->merge($add_array);
$user = Opportunity::create($request->all());
/*$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));*/
//$admin->notify(new NewUser($this_user));
return redirect()->route('admin.opportunities.edit',$user->id)->with('success','Opportunity Created successfully! Please and remaining details');
}else if(in_array('Partner',$role) && $request->partner_id == $user->id){
$add_array['opportubity_status'] = 0;
$request->merge($add_array);
$user = Opportunity::create($request->all());
return redirect()->route('admin.opportunities.edit',$user->id)->with('success','Opportunity Created successfully! Please and remaining details');
}else{
return redirect()->back()->with('error','Wrong access!');
}
}
public function edit(Opportunity $opportunity)
{
$id = $opportunity->id;
abort_if(Gate::denies('opportunity_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(Opportunity::where('id',$id)->count()){
$opportunity = Opportunity::find($id);
if($opportunity->opportunity_status == 1){
//return redirect()->back()->with('error','Opportunity is approved by admin you can not do changes now.');
}
$countries = Country::all();
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
$add_array = array();
if(in_array('Admin',$role)){
$user_type = 'Admin';
$partners = Partner::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',3)->get();
}else if(in_array('Partner',$role) && $opportunity->partner_id == $user->id){
$user_type = 'Partner';
$partners = Partner::where('id',$user->id)->get();
}else{
return redirect()->back()->with('error','Wrong access');
}
return view('admin.opportunities.edit', compact('opportunity','partners','user_type','countries'));
}else{
return redirect()->back()->with('error','Wrong access');
}
}
public function update(Request $request, $id)
{
abort_if(Gate::denies('opportunity_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$valuesToAdd = array();
if(isset($request->slug) && !empty($request->slug)){
$valuesToAdd['slug'] = Str::slug($request->slug, '-');
}else{
$valuesToAdd['slug'] = Str::slug($request->name, '-');
}
$request->merge($valuesToAdd);
$validator = Validator::make($request->all(),
[
'partner_id' => [
'required',
],
'opportunity_title' => [
'required',
'unique:opportunities,opportunity_title,'.$id,
],
'slug' => [
'required',
'unique:opportunities,slug,'.$id,
],
'opportunity_desc' => [
'required',
],
'opportunity_hours' => [
'required',
],
'no_of_volunteers' => [
'required',
'integer',
],
'start_date' => [
'required',
],
'start_time' => [
'required',
],
'end_time' => [
'required',
]
]
);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
if(Opportunity::where('id',$id)->count()){
$opportunity = Opportunity::find($id);
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
$add_array = array();
if(in_array('Admin',$role)){
$opportunity->update($request->all());
return redirect()->route('admin.opportunities.index')->with('success','Opportunity updated successfully!');
}else if(in_array('Partner',$role) && $opportunity->partner_id == $user->id &&$opportunity->partner_id == $request->partner_id){
$opportunity->update($request->all());
return redirect()->route('admin.opportunities.index')->with('success','Opportunity updated successfully!');
}else{
return redirect()->back()->with('error','Wrong access')->withInput();
}
}else{
return redirect()->back()->with('error','Wrong Opportunity access')->withInput();
}
}
public function show(Request $request,$id)
{
abort_if(Gate::denies('opportunity_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(Opportunity::where('id',$id)->count()){
$opportunity = Opportunity::find($id);
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
$add_array = array();
if(in_array('Admin',$role)){
$user_type = 'Admin';
$partners = Partner::join('role_user','users.id','=','role_user.user_id')->where('role_user.role_id',3)->get();
}else if(in_array('Partner',$role) && $opportunity->partner_id == $user->id){
$user_type = 'Partner';
$partners = Partner::where('id',$user->id)->get();
}else{
return redirect()->back()->with('error','Wrong access');
}
$volunteers = OpportunityVolunteer::join('users','opportunity_volunteers.volunteer_id','=','users.id')->select('opportunity_volunteers.*','users.name','users.user_profile_img','users.email')->where('opportunity_id',$id)->get();
return view('admin.opportunities.show', compact('opportunity','partners','volunteers','user','user_type'));
}else{
return redirect()->back()->with('error','Wrong access');
}
}
public function destroy(Request $request,$id)
{
abort_if(Gate::denies('opportunity_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(Opportunity::where('id',$id)->count()){
$opportunity = Opportunity::find($id);
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
$delete_permission = true;
$add_array = array();
if(in_array('Admin',$role)){
//$opportunity->delete();
$delete_permission = true;
}else if(in_array('Partner',$role) && $opportunity->partner_id == $user->id){
$delete_permission = true;
}else{
$delete_permission = false;
return redirect()->back()->with('error','Wrong access');
}
$old_dir = public_path().'/Opportunity/'.$opportunity->partner_id.'/'.$opportunity->id;
if(File::exists($old_dir)){
File::deleteDirectory($old_dir);
}
$opportunity->delete();
return back()->with('success','Opportunity deleted successfully!');
}else{
return redirect()->back()->with('error','Wrong access');
}
}
public function upload_feature_image(Request $request)
{
if($request->ajax()){
$field_value = $request->field_value;
$field_name = $request->field_name;
$user_id = $request->partner_id;
$opportunity_id = $request->opportunity_id;
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(empty($field_name)){
return json_encode(array('success' => 'error','msg'=>'Please add Field Name!'));
}
if(empty($user_id)){
return json_encode(array('success' => 'error','msg'=>'Please add partner!'));
}
if(empty($opportunity_id) || !Opportunity::where('id',$opportunity_id)->get()->count()){
return json_encode(array('success' => 'error','msg'=>'Please add Opportunity!'));
}
if(!Opportunity::where('id',$opportunity_id)->where('partner_id',$user_id)->get()->count()){
return json_encode(array('success' => 'error','msg'=>'Wrong Opportunity access!'));
}
if(in_array('Admin',$role) || in_array('Partner',$role)) {
$folder= public_path().'/Opportunity/'.$user_id.'/'.$opportunity_id;
if(!is_dir($folder))
{
//File::makeDirectory($folder);
File::makeDirectory($folder,0777,true);
}
if($request->hasFile('field_value')) {
$opportunity = Opportunity::find($opportunity_id);
if(!empty($opportunity->feature_image)){
$old_feature_image = public_path().'/Opportunity/'.$user_id.'/'.$opportunity_id.'/'.$opportunity->feature_image;
if(File::exists($old_feature_image)){
File::delete($old_feature_image);
$opportunity->feature_image = '';
$opportunity->save();
}
}
$file = $request->file('field_value');
$ext = $file->getClientOriginalExtension();
$filename = 'feature_image_'.$user_id.'_'.$opportunity_id.'_'.time().'.' . $ext;
$destinationPath = $folder;
$upload_success = $file->move($destinationPath, $filename);
if($upload_success){
$opportunity->feature_image = $filename;
$opportunity->save();
$doc_url = URL::asset('Opportunity').'/'.$user_id.'/'.$opportunity_id.'/'.$filename;
$ddd = array('success' => 'done','doc_url'=>$doc_url,'msg'=>'Feature image uploaded successfully!');
return json_encode($ddd);
}else{
return json_encode(array('success' => 'error','doc_name'=>'kk'));
}
}
return json_encode(array('success' => 'error','doc_name'=>'kk'));
}
}
}
public function delete_feature_image(Request $request)
{
if($request->ajax()){
$data_col = $request->data_col;
$opportunity_id = $request->opportunity_id;
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(empty($opportunity_id) || !Opportunity::where('id',$opportunity_id)->get()->count()){
return json_encode(array('success' => 'error','msg'=>'Opportunity not Found!'));
}
if(in_array('Admin',$role) || (in_array('Partner',$role) && Opportunity::where('partner_id',$user->id)->where('id',$opportunity_id)->get()->count())) {
$opportunity = Opportunity::find($opportunity_id);
if(in_array('Admin',$role)){
$user_id = $opportunity->partner_id;
}else{
$user_id = $user->id;
}
$folder= public_path().'/Opportunity/'.$user_id.'/'.$opportunity_id;
if(isset($opportunity->$data_col) && !empty($opportunity->$data_col)){
$old_feature_image = public_path().('/Opportunity').'/'.$user_id.'/'.$opportunity_id.'/'.$opportunity->$data_col;
if(File::exists($old_feature_image)){
File::delete($old_feature_image);
}
$opportunity->$data_col = '';
$opportunity->save();
$doc_url = URL::asset('images/feature_image.png');
$ddd = array('success' => 'done','doc_url'=>$doc_url,'msg'=>'Feature Image deleted Successfully!');
return json_encode($ddd);
}else{
return json_encode(array('success' => 'error','msg'=>'Field Value Null!'));
}
}else{
return json_encode(array('success' => 'error','msg'=>'Opportunity not Found!'));
}
}
return json_encode(array('success' => 'error','msg'=>'Wrong access'));
}
public function change_opportunity_status(Request $request){
$id = $request->opportunity_id;
if($request->ajax()){
if(empty($request->opportunity_id) && !Opportunity::where('id',$id)->get()->count()){
return json_encode(array('status'=>'error','msg'=>'Opportunity not Found!'));
}
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role)){
$opportunity = Opportunity::find($id);
$opportunity->opportunity_status = $request->opportunity_status;
$opportunity->updated_at = now();
$opportunity->save();
/*$mailData = [
'greeting' => 'Hi '.$user->name.',',
'body' => 'Your Account on LiftCincy is Approved.',
'thanks' => 'Thank you,',
'actionText' => 'View Account',
'actionURL' => url('/'),
];
$user->notify(new NewUserEmailNotification($mailData));*/
return json_encode(array('status'=>'success','msg'=>'Opportunity status changed successfully!'));
}
}
return json_encode(array('status'=>'error','msg'=>'Request Failed!'));
}
public function volunteer_approve($opportunity_id,$volunteer_id){
abort_if(Gate::denies('opportunity_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(empty($opportunity_id)){
return redirect()->back()->with('error','Opportunity not selected.');
}
if(empty($volunteer_id)){
return redirect()->back()->with('error','Volunteer not provided.');
}
if(Opportunity::where('id',$opportunity_id)->count()){
$opportunity = Opportunity::find($opportunity_id);
if($opportunity->opportunity_status != 1){
return redirect()->back()->with('error','Opportunity is not approved by admin you can not do changes now.');
}
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role) || (in_array('Partner',$role) && $opportunity->partner_id == $user->id)){}else{
return redirect()->back()->with('error','Wrong access');
}
if(empty($volunteer_id)){
return redirect()->back()->with('error','Volunteer not provided.');
}
if(!User::where('id',$volunteer_id)->get()->count()){
return redirect()->back()->with('error','Volunteer not found');
}
if(!OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_id',$volunteer_id)->get()->count()){
return redirect()->back()->with('error','Volunter not applied for this opportunity');
}
if(in_array('Admin',$role) || (in_array('Partner',$role) && $opportunity->partner_id == $user->id)){
if(OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_id',$volunteer_id)->get()->count()){
$approve = OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_id',$volunteer_id)->first();
$volunteers_needed = $opportunity->no_of_volunteers;
$volunteers_approved = OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_status',1)->get()->count();
if($approve->volunteer_status != 1){
if($volunteers_needed > $volunteers_approved){
$approve->volunteer_status = 1;
$approve->save();
/*$user = User::find($volunteer_id);
$mailData = [
'greeting' => 'Hi '.$user->name.',',
'body' => 'Your are approved as a Volunteer for Opportunity :'.$opportunity->opportunity_title,
'thanks' => 'Thank you to connect with LiftCincy,',
'actionText' => 'View Account',
'actionURL' => url('/'),
];
//$user->notify(new NewVolunteerApplyEmailNotification($mailData));*/
return redirect()->back()->with('success','Volunter approved!');
}
}else{
return redirect()->back()->with('error','Volunteer already approved.');
}
}
}
return redirect()->back()->with('error','Wrong access');
}else{
return redirect()->back()->with('error','Opportunity not selected');
}
}
public function volunteer_reject($opportunity_id,$volunteer_id){
abort_if(Gate::denies('opportunity_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
if(empty($opportunity_id)){
return redirect()->back()->with('error','Opportunity not selected.');
}
if(empty($volunteer_id)){
return redirect()->back()->with('error','Volunteer not provided.');
}
if(Opportunity::where('id',$opportunity_id)->count()){
$opportunity = Opportunity::find($opportunity_id);
if($opportunity->opportunity_status != 1){
return redirect()->back()->with('error','Opportunity is not approved by admin you can not do changes now.');
}
$user = Auth::user();
$role = $user->roles->pluck('title','id')->toArray();
if(in_array('Admin',$role) || (in_array('Partner',$role) && $opportunity->partner_id == $user->id)){}else{
return redirect()->back()->with('error','Wrong access');
}
if(empty($volunteer_id)){
return redirect()->back()->with('error','Volunteer not provided.');
}
if(!User::where('id',$volunteer_id)->get()->count()){
return redirect()->back()->with('error','Volunteer not found');
}
if(!OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_id',$volunteer_id)->get()->count()){
return redirect()->back()->with('error','Volunter not applied for this opportunity');
}
if(in_array('Admin',$role) || (in_array('Partner',$role) && $opportunity->partner_id == $user->id)){
if(OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_id',$volunteer_id)->get()->count()){
$approve = OpportunityVolunteer::where('opportunity_id',$opportunity_id)->where('volunteer_id',$volunteer_id)->first();
$approve->volunteer_status = 2;
$approve->save();
return redirect()->back()->with('success','Volunter rejected!');
}
}
return redirect()->back()->with('error','Wrong access');
}else{
return redirect()->back()->with('error','Opportunity not selected');
}
}
}