GroupsController.php 4.88 KB
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Group_user;
use App\Models\Group_works;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class GroupsController extends Controller
{
    public $limit = 15;

    // индексная страница
    public function index() {
        $groups = Group_user::with('user')->with('ingroup')->active()->paginate(15);
        return view('admin.groups.index', compact('groups'));
    }

    // форма добавления группы
    public function add(Request $request) {
        /*$editor = Auth::user()->id;
        $users = User::query()->active()->Offset(0)->limit($this->limit)->get();
        return view('admin.groups.add', compact('editor', 'users'));*/
        $hide = false;
        $total = User::query()->active()->count();
        $amt = ceil($total / $this->limit);
        if ($this->limit >= $total) $hide = true;
        if ($request->ajax()) {
            $page = intval($request->page);
            $page = (empty($page)) ? 1 : $page;
            $start = ($page != 1) ? $page * $this->limit - $this->limit : 0;
            $users = User::query()->active()->Offset($start)->limit($this->limit)->get();
            return view('admin.groups.ajax_add', compact('users', 'amt'));

        } else {
            $editor = Auth::user()->id;
            $users = User::query()->active()->Offset(0)->limit($this->limit)->get();
            return view('admin.groups.add', compact('editor', 'users', 'amt', 'hide'));
        }
    }

    // форма сохранения добавленной группы
    public function store(Request $request) {
        $rules = [
            'name_group' => 'required|min:3',
        ];
        $messages = [
            'required' => 'Укажите обязательное поле',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()) {
            return redirect()->route('admin.add-group')
                ->withErrors($validator);
        } else {
            $id_group = Group_user::create($request->all())->id;
            foreach ($request->usergroup as $user) {
                $people = new Group_works();
                $people->group_user_id = $id_group;
                $people->user_id = $user;
                $people->save();
            }
            return redirect()->route('admin.groups')
                ->with('success', 'Данные были успешно сохранены');
        }
        return redirect()->route('admin.groups');
    }

    // форма редактирования группы
    public function edit(Group_user $group, Request $request) {
        //https://snipp.ru/php/loading-scrolling
        $hide = false;
        $total = User::query()->active()->count();
        $amt = ceil($total / $this->limit);
        if ($this->limit >= $total) $hide = true;

        if ($request->ajax()) {
            $page = intval($request->page);
            $page = (empty($page)) ? 1 : $page;
            $start = ($page != 1) ? $page * $this->limit - $this->limit : 0;
            $users = User::query()->active()->Offset($start)->limit($this->limit)->get();
            return view('admin.groups.ajax', compact('users', 'group', 'amt'));

        } else {
            $editor = Auth::user()->id;
            $users = User::query()->active()->Offset(0)->limit($this->limit)->get();
            return view('admin.groups.edit', compact('editor', 'group', 'users', 'amt', 'hide'));
        }
    }

    // форма сохранения редактированной группы
    public function update(Group_user $group, Request $request) {
        $params = $request->all();
        unset($params['usergroup']);
        $rules = [
            'name_group' => 'required|min:3',
        ];
        $messages = [
            'required' => 'Укажите обязательное поле',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()) {
            return redirect()->route('admin.edit-group', ['group' => $group->id])
                ->withErrors($validator);
        } else {
            $group->update($request->all());
            $group->ingroup()->sync($request->usergroup);
            /*if ($request->usergroup->count()) {
                foreach ($request->usergroup as $us) {
                   Group_works
                }
            }*/

            return redirect()->route('admin.groups')
                ->with('success', 'Данные были успешно сохранены');
        }
        return redirect()->route('admin.groups');
    }

    public function destroy(Group_user $group) {
        $group->update(['is_remove' => 1]);

        return redirect()->route('admin.groups');
    }
}