Ad_jobsController.php 4.32 KB
<?php

namespace App\Http\Controllers;

use App\Components\Integrations\Telegram\VacancyChannel;
use App\Mail\MailSotrudnichestvo;
use App\Models\Ad_employer;
use App\Models\Ad_jobs;
use App\Models\Category;
use App\Models\Employer;
use App\Models\Job_title;
use http\Exception\RuntimeException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Telegram\Bot\Exceptions\TelegramSDKException;

class Ad_jobsController extends Controller
{

    public function add_job_in_vac(Request $request, Ad_employer $ad_employer) {
        $id = Auth()->user()->id;

        $Positions = Category::query()->active()->get();
        /*if ($Positions->count()) {
            $jobs = Job_title::query()->orderByDesc('sort')->OrderBy('name')->
                        where('is_remove', '=', '0')->
                        where('is_bd', '=', '0')->
                        where('position_id', $Positions[0]->id)->
                        get();
        } else {
            $jobs = Job_title::query()->orderByDesc('sort')->OrderBy('name')->
                        where('is_remove', '=', '0')->
                        where('is_bd', '=', '0')->
                        where('position_id', 0)->
                        get();
        }
        */
        $jobs = Job_title::query()->orderByDesc('sort')->OrderBy('name')->
        where('is_remove', '=', '0')->
        where('is_bd', '=', '0')->
        get();

        $categories = Category::query()->get();
        $Employer = Employer::query()->with('users')->with('ads')->with('flots')->
        where('user_id', $id)->first();

        return view('employers.add_ad_jobs', compact('ad_employer',
                                                'Positions', 'categories', 'jobs',
                                                          'Employer'));
    }

    public function add_job_in_vac_save(Request $request) {
        $params = $request->all();

        Ad_jobs::create($params);
        return redirect()->route('employer.vacancy_list');
    }

    public function edit_job_in_vac(Ad_jobs $ad_job, Ad_employer $ad_employer, Request $request) {
        $id = Auth()->user()->id;
        if ($request->has('job_title_id'))
            $job_title_id= $request->get('job_title_id');
        else
            $job_title_id = '';

        $Positions = Category::query()->active()->get();
        /*if ($Positions->count()) {
            $jobs123 = Job_title::query()->
                orderByDesc('sort')->OrderBy('name')->
                where('is_remove', '=', '0')->
                where('is_bd', '=', '0')->
                where('position_id', $ad_job->job_title->position_id )->get();
        } else {
            $jobs123 = Job_title::query()->
                orderByDesc('sort')->OrderBy('name')->
                where('is_remove', '=', '0')->
                where('is_bd', '=', '0')->
                where('position_id', 0)->get();
        }*/

        $jobs123 = Job_title::query()->
        orderByDesc('sort')->OrderBy('name')->
        where('is_remove', '=', '0')->
        where('is_bd', '=', '0')->
        get();

        $Employer = Employer::query()->with('users')->with('ads')->with('flots')->
        where('user_id', $id)->first();
        return view('employers.edit_ad_jobs', compact('ad_job',
                                                        'Positions',
                                                                  'Employer','jobs123', 'ad_employer', 'job_title_id'));
    }

    public function edit_job_in_vac_save(Request $request, Ad_jobs $ad_job) {
        $params = $request->all();

        $ad_job->update($params);

        return redirect()->route('employer.vacancy_list');
    }

    public function delete_job_in_vac(Ad_jobs $ad_job) {
        $ad_job->delete();
        return redirect()->route('employer.vacancy_list');
    }

    /**
     * @throws TelegramSDKException
     */
    public function sendVacancyToSocial(string $social, Ad_employer $vacancy): RedirectResponse
    {
        switch ($social) {
            case 'tg':
                $handler =  new VacancyChannel();
                break;
            default:
                throw new RuntimeException('Unkown social');
        }

        $handler->sendVacancy($vacancy);
        return redirect()->route('employer.vacancy_list')
            ->with('success', 'Вакансия отправлена в телеграм');
    }
}