UsersController.php 8.15 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\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 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() {
        $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();
        $position_work = $request->position_work;

        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;
        }

        $user = User::create($params);
        $user_id = $user->id;
        $worker = new Worker();
        $worker->position_work = $position_work;
        $worker->user_id = $user_id;
        $worker->save();

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

    public function edit_bd(User $user) {
        $list_job_titles = Job_title::query()->active()->where('is_bd', '=' , '2')->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();
        $position_work = $request->position_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->position_work = $position_work;
            $worker->save();
        } else {
            $worker = new Worker();
            $worker->user_id = $user->id;
            $worker->position_work = $position_work;
            $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');

    }
}