UsersController.php 9.25 KB
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\BaseUserRequest;
use App\Models\ContentRoles;
use App\Models\Job_title;
use App\Models\User;
use App\Models\Worker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class UsersController extends Controller
{
    public function index(Request $request) {
        $title = 'Админка - Пользователи системы';
        $id_admin = Auth::user()->id;
        if ($request->ajax()) {
            $user = User::find($request->id);
            $request->offsetUnset('id');
            $user->update($request->all());
        }

        $find_key = "";
        $users = User::query();
        if (isset($request->find)) {
           $find_key = $request->find;
           $users = $users->where('name', 'LIKE', "%$find_key%")
                        ->orWhere('email', 'LIKE', "%$find_key%");
        }

        $users = $users->paginate(15);

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

    public function user_delete(User $user) {

        $worker = $user->workers()->first();
        $employer = $user->employers()->first();

        if ($worker) {

            $worker->delete();

        } elseif ($employer) {

            $employer->delete();

        }

        $user->delete();

        return redirect()->route('admin.users')->with('Пользователь был удален из системы');
    }

    public function index_bd(Request $request) {
        $title = 'Админка - Пользователи базы данных';

        $find_key = "";
        $users = User::query();
        if (isset($request->find)) {
            $find_key = $request->find;
            $users = $users->where('name', 'LIKE', "%$find_key%")
                ->orWhere('email', 'LIKE', "%$find_key%")
                ->orWhere('telephone', 'LIKE', "%$find_key%");
        }

        $users = $users->Baseuser()
            ->orderByDesc(Worker::select('created_at')->whereColumn('workers.user_id', 'users.id'))
            ->paginate(15);

        if ($request->ajax()) {
            return view('admin.users.index_bd_ajax', compact('users'));
        } else {
            return view('admin.users.index_bd', compact('users', 'title', 'find_key'));
        }
    }

    public function add_bd() {
        $list_job_titles = Job_title::query()->active()->where('is_bd', '=' , '2')->orderBy('name', 'asc')->get();
        return view('admin.users.add', compact('list_job_titles'));
    }

    public function add_store_bd(BaseUserRequest $request) {
        $params = $request->all();

        if ($request->has('file')) {
            $params['file'] = $request->file('file')->store('basedata', 'public');
        }

        if (isset($request->name)) {
            $params['name'] = $request->surname." ".$request->name_man." ".$request->surname2;
        }

        $password = $params['password'];
        $params['password'] = Hash::make($password);
        $params['pubpassword'] = base64_encode($password);
        $params['email_verified_at'] = now();
        $user = User::create($params);
        $worker = new Worker();
        $worker->positions_work = isset($params['positions_work']) ? json_encode($params['positions_work']) : [];
        $worker->user_id = $user->id;
        $worker->comment = isset($params['comment']) ? $params['comment'] : null;
        $worker->save();

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

    public function edit_bd(User $user) {
        $list_job_titles = Job_title::query()
            ->active()
            ->where('is_bd', '=' , '2')
            ->orderByDesc('sort')
            ->orderBy('name', 'asc')
            ->get();
        return view('admin.users.edit', compact('user', 'list_job_titles'));
    }

    public function update_bd(BaseUserRequest $request, User $user) {
        $params = $request->all();
        $positions_work = $request->input('positions_work', []);

        if ($request->has('file')) {
            if (!empty($user->file)) Storage::delete($user->file);
            $params['file'] = $request->file('file')->store('basedata', 'public');
        } else {
            if (!empty($user->image)) $params['file'] = $user->file;
        }

        if (isset($request->name)) {
            $params['name'] = $request->surname." ".$request->name_man." ".$request->surname2;
        }

        $user->update($params);
        if (isset($user->workers[0]->id)) {
            $worker = Worker::find($user->workers[0]->id);
            $worker->positions_work = $positions_work;
            $worker->comment = isset($params['comment']) ? $params['comment'] : null;
            $worker->save();
        } else {
            $worker = new Worker();
            $worker->user_id = $user->id;
            $worker->positions_work = $positions_work;
            $worker->comment = isset($params['comment']) ? $params['comment'] : null;
            $worker->save();
        }

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

    public function destroy_bd(User $user) {
        $user->delete();
        return redirect()->route('admin.basedata');
    }

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

        $users = User::query()->Realuser()->paginate(15);

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

    public function admin_roles(Request $request) {

        if ($request->ajax()) {
            $id = $request->id;
            $request->offsetUnset('id');
            ContentRoles::where('id', '=', $id)->update($request->all());
        }
        $roles = ContentRoles::query()->OrderBy('id')->paginate(25);


        if ($request->ajax()) {
           return view('admin.content.roles_index_ajax', compact('roles'));
        } else {
            return view('admin.content.roles_index', compact('roles'));
        }
    }

    public function doc_bd(User $user) {
        $id = $user->id;
        $spreadsheet = new Spreadsheet();
        $activeWorksheet = $spreadsheet->getActiveSheet();
        $activeWorksheet->setCellValue('A1', 'Отчет по соискателю');
        $activeWorksheet->getStyle('A1')->applyFromArray([
            'font' => [
                'name' => 'Arial',
                'bold' => true,
                'italic' => false,
                'underline' => Font::UNDERLINE_DOUBLE,
                'strikethrough' => false,
                'color' => [
                    'rgb' => '808080'
                ]
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                    'color' => [
                        'rgb' => '808080'
                    ]
                ],
                'outline' => array(
                    'style' => Border::BORDER_THIN,
                    'color' => array('rgb' => '000000')
                ),
            ],

            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
                'vertical' => Alignment::VERTICAL_CENTER,
                'wrapText' => true,
            ]
        ]);

        $activeWorksheet->setCellValue('A2', "Псевдоним/имя: ".$user->name);
        $activeWorksheet->setCellValue('A3', "Фамилия: ".$user->surname);
        $activeWorksheet->setCellValue('A4', "Имя: ".$user->name_man);
        $activeWorksheet->setCellValue('A5', "Отчество: ".$user->surname2);
        $activeWorksheet->setCellValue('A6', "Телефон: ".$user->telephone);
        $activeWorksheet->setCellValue('A7', "Емайл: ".$user->email);

        if (isset($user->workers[0]->id)) {
            $activeWorksheet->setCellValue('A9', "Анкета: ");
            $activeWorksheet->setCellValue('A10', "Телефон: " . $user->workers[0]->telephone);
            $activeWorksheet->setCellValue('A11', "Емайл: " . $user->workers[0]->email);
        }

        if (isset($user->jobtitles[0]->id)) {
            $activeWorksheet->setCellValue('A12', "Должность: " . $user->jobtitles[0]->name);
        }
        $activeWorksheet->getColumnDimension("A")->setWidth(100);
        $writer = new Xlsx($spreadsheet);

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment; filename="user'.$id.'.xlsx"');
        header('Cache-Control: no-cache');

        $writer->save('php://output');
        $writer->save(storage_path("app/public/export/user$id.xlsx"));

        //$spreadsheet->disconnectWorksheets();
        return redirect()->route('admin.basedata');

    }
}