UsersController.php 5.74 KB
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\BaseUserRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
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 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()->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() {
        return view('admin.users.add');
    }

    public function add_store_bd(BaseUserRequest $request) {
        $params =  $request->all();
        $user = User::create($params);
        return redirect()->route('admin.worker-profile-add', ['user' => $user]);
    }

    public function edit_bd(User $user) {
        return view('admin.users.edit', compact('user'));
    }

    public function update_bd(BaseUserRequest $request, User $user) {
        $params = $request->all();
        $user->update($params);
        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 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'
                    ]
                ],
            ],
            '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);
        }

        $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');

    }
}