HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Http/Controllers/ExperiencePlanController.php
<?php

namespace App\Http\Controllers;

use App\Models\Experience\Experience;
use Carbon\Carbon;
use App\Models\Experience\ExperiencePlan;
use App\Models\Experience\ExperiencePlanPrice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;

class ExperiencePlanController extends Controller
{

    private $types = [
        ['key' => 'Experiencia', 'value' => 'experience'],
        ['key' => 'Academia', 'value' => 'academy'],
    ];

    public function index($experience_id)
    {
        $experience = Experience::find($experience_id);
        return view('experiences.plans.list', compact('experience'));
    }

    public function indexAdd($experience_id)
    {
        $types = $this->types;
        return view('experiences.plans.create', compact('experience_id', 'types'));
    }

    public function indexEdit($id)
    {
        $object = ExperiencePlan::findOrFail($id);
        $types = $this->types;
        return view('experiences.plans.edit', compact('object', 'types'));
    }

    public function tableFilter(Request $request)
    {
        $obj = $this->tableFilterQuery($request);

        DB::enableQueryLog();
        $dataTable = DataTables::of($obj);
        $this->tableFilterCustomColumns($dataTable);

        $response = $dataTable->make(true);
        $data = $response->getData();
        $data = json_decode(json_encode($data), true);
        $queries = \DB::getQueryLog();
        $data['queries'] = $queries;
        return $data;
    }

    public function create(Request $request)
    {
        try {
            if (ExperiencePlan::where('name', $request->input('name'))->first()) {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
            }
            $data = $request->all();
            if ($model = ExperiencePlan::create($data)) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('experiences.plans.messages.create_success'), "data" => $model->id));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('experiences.plans.messages.create_error'), "data" => null));
            }
        } catch (\Exception $e) {
            return response(
                array(
                    "r" => false,
                    "type" => "error",
                    "title" => "Oops...",
                    "m" => __($e->getMessage()),
                    "data" => null
                )
            );
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $request['id'] = $id;
            if (ExperiencePlan::where([['id', '!=', $id], ['name', $request->input('name')]])->first()) {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
            }
            $data = $request->all();
            if (ExperiencePlan::where('id', $id)->update($data)) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
            }
        } catch (\Exception $e) {
            return response(
                array(
                    "r" => false,
                    "type" => "error",
                    "title" => "Oops...",
                    "m" => __($e->getMessage()),
                    "data" => null
                )
            );
        }
    }

    public function delete($id)
    {
        try {
            if (ExperiencePlan::where('id', $id)->delete()) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.deleted_successfully'), "data" => null));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_removing'), "data" => null));
            }
        } catch (\Illuminate\Database\QueryException $e) {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.delete_relation_data'), "data" => null));
        }
    }

    private function tableFilterQuery($request)
    {
        $experienceId = $request->experienceId;

        DB::statement("SET sql_mode = ''");
        $obj = ExperiencePlan::select(
            'id',
            'name',
            'description',
            'type',
            'created_at'
        )
            ->where('experience_id', $experienceId)
            ->orderBy('created_at', 'DESC');
        DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");

        return $obj;
    }

    private function tableFilterCustomColumns($dataTable)
    {
        $dataTable->addColumn('actions', function ($obj) {
            if (
                $this->validateRolePermissions('experience', 'administrator') ||
                $this->validateRolePermissions('experience', 'supervisor')
            ) {
                $actions = '
                    <i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" title="Editar" style="cursor:pointer;"></i>
                    <i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" title="Eliminar" style="cursor:pointer;"></i>
                ';

                $actions .= '<i class="fa fa-list iconMini" onClick="getExperiencePlanPrices(' . $obj->id . ')" data-id="' . $obj->id . '" title="Precios"></i>';

                return $actions;
            } else {
                return '';
            }
        })
            ->editColumn('created_at', function ($obj) {
                return Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
            })
            ->editColumn('type', function ($obj) {
                if ($obj->type) {
                    $object = collect($this->types)->where('value', $obj->type)->first();
                    if ($object)
                        return $object['key'];
                }
                return '';
            })
            ->rawColumns(['actions', 'created_at', 'type']);
    }
}