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/SupervisedThesisController.php
<?php

namespace App\Http\Controllers\Admin;

use Gate;
use App\Models\Scientist;
use Illuminate\Http\Request;
use App\Models\FinalWorkType;
use App\Models\ResearcherGroup;
use App\Models\SupervisedThesi;
use App\Models\ResearcherSubGroup;
use App\Http\Controllers\Controller;
use App\Models\FinalWorkInstitution;
use Yajra\DataTables\Facades\DataTables;
use Symfony\Component\HttpFoundation\Response;
use App\Http\Controllers\Traits\CsvImportTrait;
use App\Http\Requests\StoreSupervisedThesiRequest;
use App\Http\Requests\UpdateSupervisedThesiRequest;
use App\Http\Requests\MassDestroySupervisedThesiRequest;

class SupervisedThesisController extends Controller
{
    use CsvImportTrait;

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

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

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

            $table->editColumn('actions', function ($row) {
                $viewGate      = 'supervised_thesi_show';
                $editGate      = 'supervised_thesi_edit';
                $deleteGate    = 'supervised_thesi_delete';
                $crudRoutePart = 'supervised-thesis';
                $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('scientist', function ($row) {
                $labels = [];
                foreach ($row->scientists as $scientist) {
                    $labels[] = sprintf('<span class="label label-info label-many">%s</span>', $scientist->name);
                }

                return implode(' ', $labels);
            });

            $table->editColumn('nature', function ($row) {
                $labels = [];
                foreach ($row->natures as $nature) {
                    $labels[] = sprintf('<span class="label label-info label-many">%s</span>', $nature->name);
                }

                return implode(' ', $labels);
            });
            $table->editColumn('title', function ($row) {
                return $row->title ? $row->title : '';
            });
            $table->editColumn('person', function ($row) {
                return $row->person ? $row->person : '';
            });
            $table->editColumn('institution', function ($row) {
                $labels = [];
                foreach ($row->institutions as $institution) {
                    $labels[] = sprintf('<span class="label label-info label-many">%s</span>', $institution->name);
                }

                return implode(' ', $labels);
            });

            $table->rawColumns(['actions', 'placeholder', 'scientist', 'nature', 'institution']);

            return $table->make(true);
        }

        $scientists              = Scientist::get();
        $final_work_types        = FinalWorkType::get();
        $final_work_institutions = FinalWorkInstitution::get();
        $groups = ResearcherGroup::pluck('title', 'id');
        $subgroups = ResearcherSubGroup::pluck('title', 'id');
        $natures = FinalWorkType::pluck('name', 'id');
        $institutions = FinalWorkInstitution::pluck('name', 'id');

        return view('admin.supervisedThesis.index', compact('scientists', 'final_work_types', 'final_work_institutions', 'groups', 'subgroups', 'natures', 'institutions'));
    }

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

        $scientists = Scientist::pluck('name', 'id');

        $natures = FinalWorkType::pluck('name', 'id');

        $institutions = FinalWorkInstitution::pluck('name', 'id');

        return view('admin.supervisedThesis.create', compact('institutions', 'natures', 'scientists'));
    }

    public function store(StoreSupervisedThesiRequest $request)
    {
        $supervisedThesi = SupervisedThesi::create($request->all());
        $supervisedThesi->scientists()->sync($request->input('scientists', []));
        $supervisedThesi->natures()->sync($request->input('natures', []));
        $supervisedThesi->institutions()->sync($request->input('institutions', []));

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

        return redirect()->route('admin.supervised-thesis.index');
    }

    public function edit(SupervisedThesi $supervisedThesi)
    {
        abort_if(Gate::denies('supervised_thesi_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $scientists = Scientist::pluck('name', 'id');

        $natures = FinalWorkType::pluck('name', 'id');

        $institutions = FinalWorkInstitution::pluck('name', 'id');

        $supervisedThesi->load('scientists', 'natures', 'institutions');

        return view('admin.supervisedThesis.edit', compact('institutions', 'natures', 'scientists', 'supervisedThesi'));
    }

    public function update(UpdateSupervisedThesiRequest $request, SupervisedThesi $supervisedThesi)
    {
        $supervisedThesi->update($request->all());
        $supervisedThesi->scientists()->sync($request->input('scientists', []));
        $supervisedThesi->natures()->sync($request->input('natures', []));
        $supervisedThesi->institutions()->sync($request->input('institutions', []));

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

        return redirect()->route('admin.supervised-thesis.index');
    }

    public function destroy(SupervisedThesi $supervisedThesi)
    {
        abort_if(Gate::denies('supervised_thesi_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $supervisedThesi->delete();

        return back();
    }

    public function massDestroy(MassDestroySupervisedThesiRequest $request)
    {
        $supervisedThesis = SupervisedThesi::find(request('ids'));

        foreach ($supervisedThesis as $supervisedThesi) {
            $supervisedThesi->delete();
        }

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