HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/ivs.kaunokolegija.lt/laravel/app/Http/Controllers/Admin/DepartmentSubsectionController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\CsvImportTrait;
use App\Http\Requests\MassDestroyDepartmentSubsectionRequest;
use App\Http\Requests\StoreDepartmentSubsectionRequest;
use App\Http\Requests\UpdateDepartmentSubsectionRequest;
use App\Models\Department;
use App\Models\DepartmentSubsection;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Yajra\DataTables\Facades\DataTables;

class DepartmentSubsectionController extends Controller
{
    use CsvImportTrait;

    public function index(Request $request)
    {
        abort_if(Gate::denies('department_subsection_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        if ($request->ajax()) {
            $query = DepartmentSubsection::with(['department'])->select(sprintf('%s.*', (new DepartmentSubsection)->table));
            $table = Datatables::of($query);

            $table->addColumn('placeholder', '&nbsp;');
            $table->addColumn('actions', '&nbsp;');

            $table->editColumn('actions', function ($row) {
                $viewGate      = 'department_subsection_show';
                $editGate      = 'department_subsection_edit';
                $deleteGate    = 'department_subsection_delete';
                $crudRoutePart = 'department-subsections';
                $modal         = true;
                $rowData       = $row;

                return view('partials.datatablesActions', compact(
                    'viewGate',
                    'editGate',
                    'deleteGate',
                    'crudRoutePart',
                    'row',
                    'modal',
                    'rowData'
                ));
            });

            $table->editColumn('id', function ($row) {
                return $row->id ? $row->id : '';
            });
            $table->editColumn('name', function ($row) {
                return $row->name ? $row->name : '';
            });
            $table->addColumn('department_name', function ($row) {
                return $row->department ? $row->department->name : '';
            });

            $table->rawColumns(['actions', 'placeholder', 'department']);

            return $table->make(true);
        }

        $departments = Department::get();

        return view('admin.departmentSubsections.index', compact('departments'));
    }

    public function create()
    {
        abort_if(Gate::denies('department_subsection_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $departments = Department::pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');

        return view('admin.departmentSubsections.create', compact('departments'));
    }

    public function store(StoreDepartmentSubsectionRequest $request)
    {
        $departmentSubsection = DepartmentSubsection::create($request->all());

        if ($request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => __('global.create_success'),
                'data' => $departmentSubsection,
            ], 201);
        }

        return redirect()->route('admin.department-subsections.index');
    }

    public function edit(DepartmentSubsection $departmentSubsection)
    {
        abort_if(Gate::denies('department_subsection_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $departments = Department::pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');

        $departmentSubsection->load('department');

        return view('admin.departmentSubsections.edit', compact('departmentSubsection', 'departments'));
    }

    public function update(UpdateDepartmentSubsectionRequest $request, DepartmentSubsection $departmentSubsection)
    {
        $departmentSubsection->update($request->all());

        if ($request->ajax()){
            return response()->json([
                 'success' => true,
                   'data' => $departmentSubsection,
             ]);
         }

        return redirect()->route('admin.department-subsections.index');
    }

    public function destroy(DepartmentSubsection $departmentSubsection)
    {
        abort_if(Gate::denies('department_subsection_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $departmentSubsection->delete();

        return back();
    }

    public function massDestroy(MassDestroyDepartmentSubsectionRequest $request)
    {
        $departmentSubsections = DepartmentSubsection::find(request('ids'));

        foreach ($departmentSubsections as $departmentSubsection) {
            $departmentSubsection->delete();
        }

        return response(null, Response::HTTP_NO_CONTENT);
    }
}