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/qas.sports-crowd.com/app/Http/Controllers/Api/ExternalAppApiController.php
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Form;
use App\HeaderValue;
use App\HeaderValueDetail;

class ExternalAppApiController extends Controller
{
    private $__UNRESTRICTED_USER = ['invitado@invitado.com.co'];
    public function createRequest(Request $request)
    {
        $formId = $request->form_id;
        $userId = Auth::user()->id;
        $previousHeaderValue = HeaderValue::where('user_id', $userId)->where('form_id', $formId)->first();
        $form = Form::find($formId);

        if ($previousHeaderValue && !$form->multiple_answers) {
            return response(array(
                "status" => true,
                "type" => "success",
                "title" => "",
                "message" => __('messages.external_app.exist_answer'),
                "data" => array('request_id' => $previousHeaderValue->id)
            ));
        }

        $nhv = new HeaderValue;
        $nhv->form_id = $formId;
        $nhv->user_id = $userId;
        $nhv->save();

        $requestData = $request->all();

        foreach ($requestData['specific_data'] as $field) {
            $nhvd = new HeaderValueDetail;
            $nhvd->header_value_id = $nhv->id;
            $nhvd->field_id = $field['field_id'];

            if ($field['is_multiple']) {
                $nhvd->value = $this->getMultipleValue($field['value']);
            } else {
                $nhvd->value = $field['value'];
            }
            $nhvd->save();
        }

        return response(array("status" => true, "type" => "success", "title" => __('messages.external_app.title_create'), "message" => __('messages.external_app.ok_create'), "data" => array('request_id' => $nhv->id)));
    }

    public function getMultipleValue($values)
    {
        $cont = 0;
        $ret = '';

        if (!$values) {
            return '';
        }

        foreach ($values as $v) {
            if ($cont == 0) {
                $ret = $v;
            } else {
                $ret = $ret . ',' . $v;
            }
            $cont++;
        }

        return $ret;
    }

    public function getPolls()
    {
        $o = Form::where('active', true)->get();
        return response(array("status" => true, "type" => "success", "title" => "ok", "message" => "", "data" => $o));
    }

    public function getInfoForm($form_id)
    {
        $o = Form::where('id', $form_id)->with('fields')->first();
        return response(array("status" => true, "type" => "success", "title" => "ok", "message" => "", "data" => $o));
    }

    public function getForms(Request $request)
    {
        \DB::statement("SET sql_mode = ''");
        $userId = Auth::user()->id;
        if (!in_array(Auth::user()->email, $this->__UNRESTRICTED_USER)) {
            $query = Form::select('forms.*')
                ->leftjoin('header_values as hv', function ($join) use ($userId) {
                    $join->on('forms.id', '=', 'hv.form_id')->where('hv.user_id', '=', $userId);
                })
                ->leftjoin('form_tags as ft', 'forms.id', '=', 'ft.form_id')
                ->leftjoin('user_tags as ut', 'ut.tag_id', '=', 'ft.tag_id')
                ->leftjoin('tags', function ($join) {
                    $join->on('tags.id', '=', 'ut.tag_id')->where('tags.active', 1);
                })
                ->join('form_fields as ff', 'ff.form_id', '=', 'forms.id')
                ->where(function ($query) use ($userId) {
                    $query->where('user_tags.user_id', '=', $userId)->orWhereNull('user_tags.user_id');
                })
                ->whereNull('forms.deleted_at')
                ->where('forms.active', true)
                ->where(function ($query) use ($userId) {
                    $query->where(function ($q) {
                        $q->where('forms.multiple_answers', 0)
                            ->whereNull('hv.id');
                    })
                        ->orWhere(function ($q) use ($userId) {
                            $q->where('ut.user_id', $userId)
                                ->orWhereNull('ft.id');
                        });
                });

            if ($request->show_in_home) {
                $query->where('forms.show_in_home', true)
                    ->whereNotNull('forms.image');
            }

            if ($request->show_in_setting) {
                $query->where('forms.show_in_setting', true);
            }

            $query->groupBy('forms.id');

            // To get the results
            $query = $query->get();
        } else {
            $query = [];
        }
        \DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");

        return response(array(
            "status" => true,
            "type" => "success",
            "title" => "ok",
            "message" => "",
            "data" => $query
        ));
    }
}