MainController.php 7.24 KB
<?php

namespace App\Http\Controllers;

use App\Models\Ad_employer;
use App\Models\Category;
use App\Models\Education;
use App\Models\Employer;
use App\Models\Job_title;
use App\Models\News;
use App\Models\reclame;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class MainController extends Controller
{
    // Главная страница публичной части
    public function index() {
        $news = News::query()->orderBy('id')->limit(6)->get();

        $categories = Category::query()->selectRaw('count(ad_employers.id) as cnt, categories.*')
            ->join('ad_employers', 'ad_employers.category_id', '=', 'categories.id')
            ->OrderByDesc('created_at')
            ->GroupBy('categories.id')
            ->get();

        $employers = Employer::query()->orderBy('id')->limit(20)->get();

        return view('index', compact('news', 'categories', 'employers'));
    }

    public function search_vacancies(Request $request) {
        if ($request->has('search')) {
            $search = $request->get('search');
            $job_titles = Job_title::query()->where('name', 'LIKE', "%$search%")->first();
            if (isset($job_titles->id))
                if ($job_titles->id > 0)
                    return redirect()->route('vacancies', ['job' => $job_titles->id]);
        }
    }

    public function vacancies(Request $request) {
        //должности
        $Job_title = Job_title::query()->orderBy('name')->get();

        $categories = Category::query()->selectRaw('count(ad_employers.id) as cnt, categories.*')
                    ->selectRaw('min(ad_employers.salary) as min_salary, max(ad_employers.salary) as max_salary')
                    ->join('ad_employers', 'ad_employers.category_id', '=', 'categories.id')
                    ->join('ad_jobs', 'ad_jobs.ad_employer_id', '=', 'ad_employers.id');

        //категории и вакансии
        if (($request->has('job')) && ($request->get('job') > 0)) {
              $categories = $categories->Where('job_title_id', '=', $request->get('job'))
                                        ->OrderByDesc('created_at')
                                        ->GroupBy('categories.id')
                                        ->get();
        } else {
                $categories = $categories->OrderByDesc('created_at')
                                           ->GroupBy('categories.id')
                                           ->get();
        }

        if ($request->ajax()) {
            return view('ajax.vacancies', compact('categories'));
        } else {
            return view('vacancies', compact('Job_title', 'categories'));
        }
    }

    //Вакансии категория детальная
    public function list_vacancies(Category $categories, Request $request) {

       $Query = Ad_employer::with('jobs')->
                             with('cat')->
                             with('employer')->
                             whereHas('jobs_code', function ($query) use ($request) {
                               if ($request->ajax()) {
                                   if (null !== ($request->get('job'))) {
                                       $query->where('job_title_id', $request->get('job'));
                                   }
                               }
                            })
                            ->select('ad_employers.*');

       if ($categories->id > 0) {
           $Query = $Query->where('category_id', '=', $categories->id);
           $Name_categori = Category::query()->where('id', '=', $categories->id)->get();
       }

       $Job_title = Job_title::query()->OrderBy('name')->get();

       $Query_count = $Query->count();

       $Query = $Query->OrderBy('updated_at')->paginate(3);

       $Reclama = reclame::query()->limit(3)->get();



       if ($request->ajax()) {
           return view('ajax.list_vacancies', compact('Query', 'Query_count', 'Name_categori', 'Reclama', 'categories'));
       } else {
           //Вернуть все
           return view('list_vacancies', compact('Query',
                                                    'Query_count',
                                                              'Reclama',
                                                              'Name_categori',
                                                              'categories',
                                                              'Job_title'));
       }
    }

    // Образование
    public function education(Request $request) {
        $educations = Education::query();
        if ($request->has('search')) {
            $search = trim($request->has('search'));
            $educations = $educations->where('name', 'LIKE', "%$search%");
        }
        $count_edu = $educations->count();
        $educations = $educations->paginate(6);
        return view('education', compact('educations', 'count_edu'));
    }

    // Контакты
    public function contacts() {
        return view('contacts');
    }

    // Вход в личный кабинет
    public function input_login(Request $request)
    {
        $rules = [
            'email' => 'required|string|email',
            'password' => 'required|string',
        ];

        $messages = [
            'required' => 'Укажите обязательное поле «:attribute»',
            'email' => 'Введите корректный email',
            'min' => [
                'string' => 'Поле «:attribute» должно быть не меньше :min символов',
                'file' => 'Файл «:attribute» должен быть не меньше :min Кбайт'
            ],
            'max' => [
                'string' => 'Поле «:attribute» должно быть не больше :max символов',
                'file' => 'Файл «:attribute» должен быть не больше :max Кбайт'
            ],
        ];

        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()) {
            return redirect()->route('index')->with('Error', "Email или пароль невалидный");
        } else {
            $credentials = $request->only('email', 'password');

            if (Auth::attempt($credentials, $request->has('remember'))) {

                if (is_null(Auth::user()->email_verified_at)) {
                    Auth::logout();
                    return json_encode(Array("ERROR" => "Адрес почты не подтвержден"));
                }

                if (Auth::user()->is_worker) {
                    return json_encode(Array("REDIRECT" => redirect()->route('worker.cabinet')->getTargetUrl()));
                } else {
                    return json_encode(Array("REDIRECT" => redirect()->route('employer.cabinet')->getTargetUrl()));
                }

                return json_encode(Array("SUCCESS" => "Вы успешно вошли в личный кабинет"));
                    //->route('index')
                    //->with('success', 'Вы вошли в личный кабинет.');
            } else {
                return json_encode(Array("ERROR" => "Неверный логин или пароль!"));
            }
        }
    }
}