EmployersController.php 7.52 KB
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Ad_employer;
use App\Models\Answer;
use App\Models\CategoryEmp;
use App\Models\Employer;
use App\Models\Static_ad;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class EmployersController extends Controller
{
    public function index(Request $request) {
        //$all_employer = User::where('is_worker', '0')->count();

        if ($request->ajax()) {
            $user = User::find($request->id);
            $request->offsetUnset('id');
            $user->update($request->all());
        }

        $users = User::with('employers')->select(['users.*','users.id as usr_id', 'emp.id as emp_id', 'emp.logo as emp_logo', 'emp.*'])
            ->join('employers as emp','emp.user_id','users.id')
            ->where('users.is_worker', '0')->Realuser();
        $all_employer = $users->count();

        $find_cat = "";
        if (isset($request->category)) {
            if ($request->category != 'Все категории') {
                $users = $users->where('category', '=', $request->category);
                $find_cat = $request->category;
            }
        }
        $find_key = "";

        if (isset($request->find)) {
            $find_key = $request->find;
            $users = $users->where(function($query) use($find_key) {
                $query->Where('users.name', 'LIKE', "%$find_key%")
                    ->orWhere('emp.email', 'LIKE', "%$find_key%")
                    ->orWhere('emp.telephone', 'LIKE', "%$find_key%");
            });
        }

        //DB::enableQueryLog();
        $users = $users->paginate(15);
        //dd(DB::getQueryLog());

        $select_category = CategoryEmp::query()->active()->get();

        if ($request->ajax()) {
            return view('admin.employer.index_ajax', compact('users'));
        } else {
            return view('admin.employer.index', compact('users', 'find_key', 'find_cat', 'all_employer', 'select_category'));
        }
    }

    public function comment_read(Employer $employer) {
        return view('admin.employer.comment', compact('employer'));
    }

    public function form_update_employer(Employer $employer) {
        $select_category = CategoryEmp::query()->active()->get();
        return view('admin.employer.edit', compact('employer', 'select_category'));
    }

    public function update_employer(Employer $employer, Request $request)
    {
        $params = $request->all();
        unset($params['logo']);
        unset($params['telephone']);
        unset($params['email']);
        unset($params['address']);
        unset($params['site']);
        unset($params['status_hidden']);
        unset($params['oficial_status']);
        unset($params['social_is']);
        unset($params['sending_is']);
        unset($params['category']);
        unset($params['comment_admin']);

        $rules = [
            'name' => 'required|string|max:255',
        ];

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

        $validator = Validator::make($params, $rules, $messages);

        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();                    //->route('admin.register')

        } else {

            //$user = User::find($employer->user_id);
            $user_id = $employer->user_id;
            $employer->telephone = $request->telephone;
            $employer->email = $request->email;
            $employer->address = $request->address;
            $employer->site = $request->site;
            $employer->text = $request->text;
            $employer->status_hidden = $request->status_hidden;
            $employer->oficial_status = $request->oficial_status;
            $employer->social_is = $request->social_is;
            $employer->sending_is = $request->sending_is;
            $employer->category = $request->category;
            $employer->comment_admin = $request->comment_admin;

            if ($request->has('logo')) {
                if (!empty($employer->logo)) {
                    Storage::delete($employer->logo);
                }
                $employer->logo = $request->file('logo')->store("employer/$user_id", 'public');
            }
            $employer->save();

            $user = User::find($user_id);
            $user->update($params);

            return redirect()->route('admin.employer-profile', ['employer' => $employer->id])
                ->with('success', 'Данные были успешно сохранены');
        }
    }

    // Удаление работодателя, вакансий и профиля юзера
    public function delete_employer(Employer $employer, User $user) {
        try {
            if (!empty($employer)) {
                $employer->ads()->delete();
                if (!empty($employer->logo)) {
                    Storage::delete($employer->logo);
                }
                $employer->delete();
            }
        } finally {
            $user->delete();
        }

        return redirect()->route('admin.employers')->with('success', 'Данные были удалены о работодателе');
    }

    // кабинет - отзывы о работодателе для модерации
    public function answers(Request $request) {
        if ($request->ajax()) {
            $user = Answer::find($request->id);
            $request->offsetUnset('id');
            $user->update($request->all());
        }

        $answers = Answer::query()->orderByDesc('id')->paginate(15);

        if ($request->ajax()) {
            return view('admin.answers.index_ajax', compact('answers'));
        } else {
            return view('admin.answers.index', compact('answers'));
        }
    }

    // кабинет - статистика вакансий работодателя
    public function static_ads(Request $request) {
        $stat = Static_ad::with('ads');
        $ads = Ad_employer::query()->active()->OrderBy('id')->get();
        $periods = Static_ad::query()->distinct('year_month')->select('year_month')->get();
        if ($request->ajax()) {
            if (isset($request->ad_employer_id))
                if (!$request->ad_employer_id == "0")
                    $stat = $stat->Where('ad_employer_id', '=', $request->ad_employer_id);
            if (isset($request->year_month)) {
                if (!$request->year_month == "0")
                    $stat = $stat->Where('year_month', '=', $request->year_month);
            }
        }

        $stat = $stat->OrderByDesc('year_month');
        $stat = $stat->paginate(15);

        if ($request->ajax())
            return view('admin.static.index_ads_ajax', compact('stat'));
        else
            return view('admin.static.index_ads', compact('stat', 'ads', 'periods'));

    }


}