File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/Controller.php
<?php
namespace App\Http\Controllers;
use App\CorporateIdentity;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\UserInformation;
use App\Parameter;
use App\Module;
use App\ModuleRol;
use App\Services\LogsService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use DB;
use Exception;
use Illuminate\Support\Facades\File;
use NumberFormatter;
use Stichoza\GoogleTranslate\TranslateClient;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public $__SUPERADMIN_ROL = 1;
public $__ADMIN_ROL = 2;
public $__MESSENGER_ROL = 3;
public $__CLIENT_ROL = 4;
public $__TECHNICAL_ROL = 6; // ROL TECNICOS
public $__EXTERNAL_OPERATOR_ROL = 7; // ROL GANA
public $__TICKET_OFFICE_ROL = 8; // ROL BOLETERIA
public $__LOGISTICS_ROL = 14; // ROL LOGISTICA/ENTRENADOR/USUARIO-ESTADIO
public $__TICKET_OFFICE_ROL_INTERNAL = 15; // ROL BOLETERIA INTERNA
public $__TREASURY_TEAM = 16; // ROL TESORERIA
public $__ADMIN_TEAM = 17; // ROL ADMIN EQUIPO
public $__COMMUNICATION_ROL = 20; // ROL COMUNICACION
public $__ADMIN_2_TEAM = 22; // ROL ADMIN EQUIPO
public $__EXPERIENCE_ROL = 21; // ROL EXPERIENCIA
public function __construct()
{
ini_set('memory_limit', config('cache.memory_limit'));
}
public function registerLog($user_id, $description, $data_operation, $task, $module)
{
$module_id = $module;
if (!is_numeric($module)) {
$module_id = $this->getIdModuleByName($module);
}
try {
$logsService = new LogsService();
$logsService->log($user_id, $description, $data_operation, $task, $module_id, $module);
} catch (Exception $e) {
$error = [
'user_id' => $user_id,
'description' => $description,
'data_operation' => $data_operation,
'task' => $task,
'module' => $module,
'message' => $e->getMessage(),
'getFile' => $e->getFile(),
'getLine' => $e->getLine(),
];
$utilController = new UtilController;
$utilController->logFile(json_encode($error));
}
}
public function getIdModuleByName($name)
{
$module_id = 1;
if ($name && $name != '') {
$module = Module::select('id')->where('name', 'LIKE', '%' . $name . '%')->first();
if ($module) {
$module_id = $module->id;
}
}
return $module_id;
}
public function changeDbDefault(Request $request)
{
if (!$request->is('api/*')) {
$userOld = UserInformation::where('user_id', Auth::user()->id)->first();
Config::set('database.default', $userOld->current_db);
DB::reconnect($userOld->current_db);
$userI = UserInformation::where('user_id', Auth::user()->id)->first();
$parameters = Parameter::select('db_name')->first();
$userI->current_db = $parameters->db_name;
$userI->update();
}
}
// retorna codigo para el evento
public function codeOrder()
{
return (string) Str::uuid();
}
public function gen_uid($length = 10)
{
return substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
// funcion para retornar el valor de una celda en la importacion de archivos excel de la libreria Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
public function getValueRow($cells, $index = false)
{
if ($index !== false) {
return $cells[$index]->getValue();
}
return $cells->getValue();
}
public function translate($text, $originLanguage = 'en', $targetLanguage = 'es')
{
$tr = new TranslateClient($originLanguage, $targetLanguage);
return $tr->translate(strval($text));
}
public function requestLanguage(Request $request)
{
$language = 'es';
// REVISAR CON MAS DETALLE PORQUE NO RECUPERA IDIOMA DE LOS DISPOSITVOS
/*$headerLanguage = $request->header('accept-language');
if (str_contains($headerLanguage, 'en-US')) {
$language = 'en';
} else if (str_contains($headerLanguage, 'pt-BR')) {
$language = 'pt';
}*/
return $language;
}
public function getModule(Request $request)
{
$path = $request->getPathInfo();
$paths = explode("/", $path);
$module = null;
if (count($paths) > 1) {
$route = $paths[1];
$module = $this->getModuleForRoute($route);
}
if ($module == null) {
$route = $paths[0];
$module = $this->getModuleForRoute($route);
}
return $module ? $module->id : null;
}
private function getModuleForRoute($route)
{
$module = Module::where('route', $route)->latest('id')->first();
if (!$module)
$module = Module::where('route', 'LIKE', '%' . $route . '%')->first();
if (!$module) {
$module = $this->getSpecialRoutes($route);
}
$module = $this->validateConditionalRoutes($route, $module);
return $module;
}
public function validateRouteEnabledUser($module)
{
if (Auth::user()) {
$rol = Auth::user()->rol->id;
if (in_array($rol, [$this->__CLIENT_ROL])) {
Auth::logout();
return redirect('/password/recover-success')->send();
} elseif (in_array($rol, [$this->__SUPERADMIN_ROL])) {
return true;
} else {
$moduleRol = ModuleRol::where([['rol_id', $rol], ['module_id', $module]])->first();
return !!$moduleRol;
}
}
return false;
}
public function getRouteEnabled()
{
if (Auth::user()) {
$route = Auth::user()->rol->route;
if ($route)
return $route;
else {
$rol = Auth::user()->rol->id;
$moduleRol = ModuleRol::with('module')->where('rol_id', $rol)->first();
if (!$moduleRol) {
return '/';
}
$moduleFirst = $this->getFirstRouteEnabled($moduleRol->module);
if (!$moduleFirst) {
return '/';
}
return '/' . $moduleFirst->route;
}
}
return false;
}
public function validateRolePermissions($module, $roleType, $rol = null)
{
if (!$rol)
$rol = Auth::user()->rol->id;
switch ($module) {
case 'ticketing':
case 'complimentary_ticket':
switch ($roleType) {
case 'administrator':
if (in_array($rol, [$this->__SUPERADMIN_ROL, $this->__ADMIN_ROL]))
return true;
break;
case 'supervisor':
if (in_array($rol, [$this->__TICKET_OFFICE_ROL, $this->__TECHNICAL_ROL, $this->__ADMIN_TEAM]))
return true;
break;
case 'seller':
if (in_array($rol, [$this->__EXTERNAL_OPERATOR_ROL, $this->__TICKET_OFFICE_ROL_INTERNAL]))
return true;
break;
}
break;
case 'academy':
case 'experience':
switch ($roleType) {
case 'administrator':
if (in_array($rol, [$this->__SUPERADMIN_ROL, $this->__ADMIN_ROL]))
return true;
break;
case 'supervisor':
if (in_array($rol, [$this->__TREASURY_TEAM, $this->__ADMIN_TEAM, $this->__TICKET_OFFICE_ROL_INTERNAL, $this->__COMMUNICATION_ROL]))
return true;
break;
case 'assistant':
if (in_array($rol, [$this->__LOGISTICS_ROL, $this->__EXPERIENCE_ROL]))
return true;
break;
}
break;
case 'user':
switch ($roleType) {
case 'administrator':
if (in_array($rol, [$this->__SUPERADMIN_ROL, $this->__ADMIN_ROL]))
return true;
break;
case 'supervisor':
if (in_array($rol, [$this->__TICKET_OFFICE_ROL, $this->__TECHNICAL_ROL, $this->__TREASURY_TEAM, $this->__ADMIN_TEAM, $this->__ADMIN_2_TEAM, $this->__TICKET_OFFICE_ROL_INTERNAL]))
return true;
break;
}
break;
}
return false;
}
public function extension($mimeType)
{
$mimes = new \Mimey\MimeTypes;
return $mimes->getExtension($mimeType);
}
private function getFirstRouteEnabled($module)
{
$moduleChild = Module::where('module_id', $module->id)->first();
if ($moduleChild)
return $this->getFirstRouteEnabled($moduleChild);
else
return $module;
}
private function getSpecialRoutes($route)
{
$module = null;
switch ($route) {
// RUTAS ESPECIALES ACADEMIA
case 'academy_user_payment':
case 'validatePayments':
$module = Module::where('route', 'academy_user')->latest('id')->first();
break;
case 'academy_locations/get':
$module = Module::where('route', 'academy_schedules')->latest('id')->first();
break;
// RUTAS ESPECIALES BOLETERIA
case 'seasons':
case 'season':
case 'events':
case 'event':
case 'event_prices':
case 'blocks':
case 'matchEventStages':
$module = Module::where('route', 'tournaments')->latest('id')->first();
break;
case 'symbolic_tickets_price':
case 'symbolic_tickets_user':
$module = Module::where('route', 'symbolic_tickets')->latest('id')->first();
break;
// RUTAS ESPECIALES TIENDA
case 'productAttributes':
$module = Module::where('route', 'products')->latest('id')->first();
break;
// RUTAS ESPECIALES ENCUESTA
case 'form_fields':
$module = Module::where('route', 'forms')->latest('id')->first();
break;
// RUTAS ESPECIALES EXPERIENCIA
case 'experience_plans':
case 'experience_plan_prices':
$module = Module::where('route', 'experience_settings')->latest('id')->first();
break;
// RUTAS ESPECIALES ANALISIS
case 'document_analytics':
$module = Module::where('route', 'analytics')->latest('id')->first();
break;
case 'enable_name_banner':
case 'enable_name_popup':
$module = Module::where('route', 'app_customization/advertising_app')->latest('id')->first();
break;
}
return $module;
}
private function validateConditionalRoutes($route, $module)
{
switch ($route) {
// RUTAS ESPECIALES BOLETERIA
case 'tickets':
case 'flash_ticket':
$module = Module::where('route', 'ticket-office')->latest('id')->first();
break;
}
return $module;
}
function formatCurrency($value, $currency = null, $minimumFractionDigits = null, $maximumFractionDigits = null)
{
$corporateIdentity = CorporateIdentity::first();
if (is_null($currency)) {
$currency = $corporateIdentity->currency;
}
if (is_null($minimumFractionDigits)) {
$minimumFractionDigits = $corporateIdentity->minimum_fraction_digits;
}
if (is_null($maximumFractionDigits)) {
$maximumFractionDigits = $corporateIdentity->maximum_fraction_digits;
}
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $minimumFractionDigits);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maximumFractionDigits);
return $formatter->formatCurrency($value, $currency);
}
function formatNumberForCurrency($value, $maximumFractionDigits = null)
{
if (is_null($value)) {
return 0;
}
if (is_null($maximumFractionDigits)) {
$corporateIdentity = CorporateIdentity::first();
$maximumFractionDigits = $corporateIdentity->maximum_fraction_digits;
}
if ($maximumFractionDigits == 0) {
$value = intval(ceil($value));
} else {
$value = round($value, $maximumFractionDigits);
}
return $value;
}
public function cleanTicketFiles($path = 'tickets_tmp/')
{
if (!$path) {
return;
}
$folder = public_path($path);
$files = File::files($folder);
foreach ($files as $file) {
File::delete($file);
}
}
}