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/demo.sports-crowd.com/app/Http/Controllers/CarnetController.php
<?php

namespace App\Http\Controllers;

use App\Tag;
use App\Carnet;
use DataTables;
use App\CarnetTag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;

class CarnetController extends Controller
{

    private $util;
    private $types = [];

    public function __construct()
    {
        $this->util = new UtilController();
        $this->types[] = (object) [
            'name' => 'Todos',
            'value' => 'all'
        ];
        $this->types[] = (object) [
            'name' => 'Abonados',
            'value' => 'subscriber'
        ];
        $this->types[] = (object) [
            'name' => 'Academia',
            'value' => 'academy'
        ];
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('carnet.list');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $wordFonts = [
            'Arial',
            'Arial Black',
            'Verdana',
            'Tahoma',
            'Trebuchet MS',
            'Impact',
            'Times New Roman',
            'Didot',
            'Georgia',
            'American Typewriter',
            'Andale Mono',
            'Courier',
            'Lucida Console',
            'Monaco',
            'Bradley Hand',
            'Brush Script MT',
            'Luminari',
            'Comic Sans MS',
            'Helvetica',
            'Cambria'
        ];
        $multiselectItems = Tag::where('active', true)->get();
        return view('carnet.create')->with('multiselectItems', $multiselectItems)->with('types', $this->types)->with('wordFonts', $wordFonts);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $wordFonts = [
            'Arial',
            'Arial Black',
            'Verdana',
            'Tahoma',
            'Trebuchet MS',
            'Impact',
            'Times New Roman',
            'Didot',
            'Georgia',
            'American Typewriter',
            'Andale Mono',
            'Courier',
            'Lucida Console',
            'Monaco',
            'Bradley Hand',
            'Brush Script MT',
            'Luminari',
            'Comic Sans MS',
            'Helvetica',
            'Cambria'
        ];
        $object = Carnet::findOrFail($id);
        $multiselectItems = Tag::where('active', true)->get();
        $multiselectValues = CarnetTag::select('tag_id')->where('carnet_id', $id)->get();
        return view('carnet.edit', compact('object'))
            ->with('multiselectItems', $multiselectItems)
            ->with('multiselectValues', $multiselectValues)
            ->with('types', $this->types)
            ->with('wordFonts', $wordFonts);
    }

    public function tableFilter()
    {
        DB::statement("SET sql_mode = ''");
        $obj = Carnet::select(
            'carnets.id',
            'carnets.name',
            'carnets.active',
            'carnets.image',
            'carnets.start_date',
            'carnets.end_date',
            'carnets.font_type',
            'carnets.font_color',
            DB::raw('GROUP_CONCAT(DISTINCT(tags.name)) AS segmentation')
        )
            ->leftjoin('carnet_tags', 'carnets.id', '=', 'carnet_tags.carnet_id')
            ->leftjoin('tags', function ($join) {
                $join->on('tags.id', '=', 'carnet_tags.tag_id')->where('tags.active', 1);
            })
            ->groupBy('carnets.id');

        DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");

        return DataTables::of($obj)
            ->addColumn('actions', function ($obj) {
                return '
                    <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"  style="cursor:pointer;"></i>
                ';
            })
            ->editColumn('active', function ($obj) {
                if ($obj->active == 0) {
                    return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
                } else {
                    return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label>   ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" />
                        <span></span> ' . __('messages.yes') . ' </label> </div>  </label> </div>';
                }
            })
            ->editColumn('image', function ($obj) {
                if (!$obj->image) {
                    return $this->util->generateEmptyImageColumn();
                } else {
                    return $this->util->generateImageColumn(config('filesystems.disks.s3.url') . '/carnet/' . $obj->image, 'Carné - ' . $obj->name, 'carne' . $obj->id);
                }
            })
            ->editColumn('start_date', function ($obj) {
                return $obj->start_date ? \Carbon\Carbon::parse($obj->start_date)->format('Y-m-d h:i:s A') : '';
            })
            ->editColumn('end_date', function ($obj) {
                return $obj->end_date ? \Carbon\Carbon::parse($obj->end_date)->format('Y-m-d h:i:s A') : '';
            })
            ->rawColumns(['actions', 'active', 'image'])
            ->make(true);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            if (!Carnet::where('name', $request->input('name'))->first()) {
                $tags = $request["tags"];
                $request->request->remove('tags');

                if ($model = Carnet::create($request->all())) {
                    if ($tags != null) {
                        foreach ($tags as $tagId) {
                            CarnetTag::create([
                                'tag_id' => $tagId,
                                'carnet_id' => $model->id
                            ]);
                        }
                    }
                    return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
                } else {
                    return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
                }
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('tag')]), "data" => null));
            }
        } catch (\Exception $e) {
            return response(
                array(
                    "r" => false,
                    "type" => "error",
                    "title" => "Oops...",
                    "m" => __($e->getMessage()),
                    "data" => null
                )
            );
        }
    }

    public function saveImage(Request $request)
    {
        try {
            $extension = $request->file('image')->getClientOriginalExtension();
            $filenametostore = $request->id . '.' . $extension;

            Storage::disk('s3')->put(config('s3.carnet') . $filenametostore, fopen($request->file('image'), 'r+'), 'public');

            $carnet = Carnet::find($request->id);
            $carnet->image = $filenametostore;
            $carnet->update();

            return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => null));
        } catch (\Throwable $th) {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => $th->getMessage()));
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try {
            if (!Carnet::where([['id', '!=', $id], ['name', $request->input('name')]])->first()) {
                $tags = $request["tags"];
                if (Carnet::where('id', $id)->update($request->only((new Carnet)->getFillable()))) {
                    CarnetTag::where('carnet_id', $id)->delete();
                    if ($tags != null) {
                        foreach ($tags as $tagId) {
                            CarnetTag::create([
                                'tag_id' => $tagId,
                                'carnet_id' => $id
                            ]);
                        }
                    }
                    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));
                }
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
            }
        } catch (\Exception $e) {
            return response(
                array(
                    "r" => false,
                    "type" => "error",
                    "title" => "Oops...",
                    "m" => __($e->getMessage()),
                    "data" => null
                )
            );
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            CarnetTag::where('carnet_id', $id)->delete();
            if (Carnet::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));
        }
    }

    public function activate(Request $request)
    {
        try {
            $id = $request['id'];
            $state = $request['state'];

            $carnet = Carnet::find($id);
            $carnet->active = $state;
            $carnet->update();

            return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
        } catch (\Throwable $th) {
            return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
        }
    }

    public function getBySegmentation($segmentationName)
    {
        return Carnet::where([['carnets.active', 1], ['tags.active', 1]])
            ->join('carnet_tags', 'carnet_tags.carnet_id', '=', 'carnets.id')
            ->join('tags', 'carnet_tags.tag_id', '=', 'tags.id')
            ->where('tags.name', $segmentationName)
            ->first();
    }

    public function getCarnet($seeCard = 'academy')
    {
        $card = Carnet::where('active', true)
            ->whereIn('carnets.show_in', ['all', $seeCard])
            ->first();
        if ($card) {
            $card->url = (config('filesystems.disks.s3.url') . '/carnet/' . $card->image);
            return array('r' => true, 'd' => $card);
        } else {
            return array('r' => false, 'd' => null, 'm' => 'No existen carnés de academia configurados');
        }
    }

    public function getCarnets($idCards)
    {
        $idCards = explode(',', $idCards);
        $cards = Carnet::where('active', true)->whereIn('id', $idCards)->get();
        if ($cards) {
            foreach ($cards as $card) {
                $card->url = (config('filesystems.disks.s3.url') . '/carnet/' . $card->image);
            }
            return array('r' => true, 'd' => $cards);
        } else {
            return array('r' => false, 'd' => null, 'm' => 'No existen carnés configurados');
        }
    }

    public function convertDPIImage(Request $request)
    {
        try {
            //Code to convert png to jpg image
            $dpi = $request->get('dpi') ?? 300;                     // Converting Image DPI to 300DPI for default
            $input = imagecreatefrompng($request->get('image'));
            $width = imagesx($input);
            $height = imagesy($input);

            $output = imagecreatetruecolor($width, $height);
            $white = imagecolorallocate($output, 255, 255, 255);
            imagefilledrectangle($output, 0, 0, $width, $height, $white);
            imagecopy($output, $input, 0, 0, 0, 0, $width, $height);

            ob_start();
            imagejpeg($output);
            $contents = ob_get_contents();
            $contents = substr_replace($contents, pack("cnn", 1, $dpi, $dpi), 13, 5);
            ob_end_clean();
            $image = 'data:image/jpeg;base64,' . base64_encode($contents);
            return array('r' => true, 'd' => $image);
        } catch (\Exception $ex) {
            return array('r' => false, 'm' => 'No se pudo convertir los DPI de la imagen, ' . $ex->getMessage());
        }
    }
}