src/EventSubscriber/GlobalConfigurationSerializeSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: sebastiantovar
  5.  * Date: 2019-04-08
  6.  * Time: 18:07
  7.  */
  8. namespace App\EventSubscriber;
  9. use App\Entity\App\Category;
  10. use App\Entity\App\CustomModule;
  11. use App\Entity\App\Entity\Entity;
  12. use App\Entity\App\MediaObject;
  13. use App\Entity\App\Notification;
  14. use App\Entity\App\Role;
  15. use App\Entity\App\Task\TaskStatusLog;
  16. use App\Entity\App\Ticket\IncidenceType;
  17. use App\Entity\App\Ticket\TicketPriority;
  18. use App\Entity\App\Ticket\TicketState;
  19. use App\Entity\App\Ticket\TicketType;
  20. use App\Entity\App\User;
  21. use App\MessageHandler\ChatMessage;
  22. use App\MessageHandler\LogMessage;
  23. use App\MessageHandler\Message;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use ApiPlatform\Core\EventListener\EventPriorities;
  26. use Symfony\Component\Intl\Countries;
  27. use Symfony\Component\Intl\Currencies;
  28. use Symfony\Component\Intl\Timezones;
  29. use Symfony\Component\HttpKernel\KernelEvents;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. use Symfony\Component\HttpKernel\Event\ViewEvent;
  32. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  33. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  34. use Symfony\Contracts\Translation\TranslatorInterface;
  35. class GlobalConfigurationSerializeSubscriber implements EventSubscriberInterface
  36. {
  37.     private $tokenStorage;
  38.     private $params;
  39.     private $em;
  40.     private $translator;
  41.     public function __construct(
  42.         EntityManagerInterface $entityManager,
  43.         ParameterBagInterface $params,
  44.         TokenStorageInterface $tokenStorage,
  45.         TranslatorInterface $trans
  46.     ) {
  47.         $this->em $entityManager;
  48.         $this->params $params;
  49.         $this->tokenStorage $tokenStorage;
  50.         $this->translator $trans;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public static function getSubscribedEvents() {
  56.         return [
  57.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  58.         ];
  59.     }
  60.     /**
  61.      * @param ViewEvent $event
  62.      * @throws \Exception
  63.      */
  64.     public function onKernelView(ViewEvent $event)
  65.     {
  66.         $result $event->getControllerResult();
  67.         $request $event->getRequest();
  68.         $route $request->attributes->get('_route');
  69.         //Obtenemos el idioma de la request para las traducciones del global config
  70.         $headerLangs $request->headers->get("accept-language");
  71.         if(!empty($headerLangs)){
  72.             $lang explode(",",$headerLangs)[0];
  73.         }
  74.         $routes = array(
  75.             'api_global_configurations_get_collection'
  76.         );
  77.         if (!in_array($route$routes))
  78.             return;
  79.         $localeDefault 'en';
  80.         if (isset($_GET['locale']) && in_array($_GET['locale'], ['en''es'])) {
  81.             $localeDefault $_GET['locale'];
  82.         }
  83.         $languages = [
  84.             'en' => 'english',
  85.             'es' => 'spanish'
  86.         ];
  87.         $ticketSources = ['app''web''bot''calendar'];
  88.         $ticketFrequency = ['daily','week''month''year'];
  89.         $requestStateStatic = ['on hold''rejected'];
  90.         $order = [
  91.             'on hold' => 0,
  92.             'rejected' => 1,
  93.             'in process' => 2,
  94.             'started' => 3,
  95.             'paused' => 4,
  96.             'finished' => 5,
  97.             'cancelled' => 6
  98.         ];
  99.         $requestStates = [];
  100.         $allStates = [];
  101.         $ticketStates  $this->em->getRepository(TicketState::class)->findAll();
  102.         foreach ($ticketStates as $ticketState) {
  103.             $dataState = array(
  104.                 'id' => $ticketState->getId(),
  105.                 'name' => $ticketState->getName()
  106.             );
  107.             $serializedTicketStates[] = $dataState;
  108.             $pos $order[$ticketState->getName()];
  109.             $allStates[$pos] = $dataState;
  110.             if (!in_array($ticketState->getName(), $requestStateStatic)) {
  111.                 $ticketsStates[] = $dataState;
  112.             }
  113.             if (in_array($ticketState->getName(), $requestStateStatic)) {
  114.                 $requestStates[] = $dataState;
  115.             }
  116.         }
  117.         ksort($allStates);
  118.         $priorities $this->em->getRepository(TicketPriority::class)->findAll();
  119.         foreach ($priorities as $priority) {
  120.             $serializedPriorities[] = array(
  121.                 'id' => $priority->getId(),
  122.                 'name' => $priority->getName()
  123.             );
  124.         }
  125.         $countriesNames Countries::getNames($localeDefault);
  126.         $codeCountries = require $this->params->get('SETTING_FILES_DIR').'/CodeCountries.php';
  127.         $countries = [
  128.             'ES' => [
  129.                 'id' => 'ES',
  130.                 'name' => Countries::getName('ES'$localeDefault),
  131.                 'prefix' => '+34'
  132.             ],
  133.             'US' => [
  134.                 'id' => 'US',
  135.                 'name' => Countries::getName('US'$localeDefault),
  136.                 'prefix' => '+1'
  137.             ],
  138.             'MX' => [
  139.                 'id' => 'MX',
  140.                 'name' => Countries::getName('MX',$localeDefault),
  141.                 'prefix' => '+52'
  142.             ],
  143.             'CO' => [
  144.                 'id' => 'CO',
  145.                 'name' => Countries::getName('CO'$localeDefault),
  146.                 'prefix' => '+57'
  147.             ],
  148.         ];
  149.         foreach ($countriesNames as $code => $name) {
  150.             if (!array_keys($countries$code)) {
  151.                 $prefix null;
  152.                 if (isset($codeCountries[$code])) {
  153.                     $prefix '+' $codeCountries[$code];
  154.                 }
  155.                 $countries[$code] = array(
  156.                     'id' => $code,
  157.                     'name' => $name,
  158.                     'prefix' => $prefix
  159.                 );
  160.             }
  161.         }
  162.         $taskStates[] = array(
  163.             'id' => 1,
  164.             'name' => TaskStatusLog::STATUS_TASK_IN_PROCESS
  165.         );
  166.         $taskStates[] = array(
  167.             'id' => 2,
  168.             'name' => TaskStatusLog::STATUS_TASK_STARTED
  169.         );
  170.         $taskStates[] = array(
  171.             'id' => 3,
  172.             'name' => TaskStatusLog::STATUS_TASK_PAUSED
  173.         );
  174.         $taskStates[] = array(
  175.             'id' => 4,
  176.             'name' => TaskStatusLog::STATUS_TASK_CANCELLED
  177.         );
  178.         $taskStates[] = array(
  179.             'id' => 5,
  180.             'name' => TaskStatusLog::STATUS_TASK_FINISHED
  181.         );
  182.         $customModules $this->em->getRepository(CustomModule::class)->findAll();
  183.         foreach ($customModules as $customModule) {
  184.             $serializedCustomModules[] = array(
  185.                 'id' => $customModule->getId(),
  186.                 'name' => $customModule->getName()
  187.             );
  188.         }
  189.         if (isset($_GET['platform']) && strtolower($_GET['platform']) == 'desktop') {
  190.             $result = [];
  191.             $result['dateFormats'] = [
  192.                 => [
  193.                     'label' => 'day-month-year',
  194.                     'format' => 'dd-MM-yyyy'
  195.                 ],
  196.                 => [
  197.                     'label' => 'month-day-year',
  198.                     'format' => 'MM-dd-yyyy'
  199.                 ]
  200.             ];
  201.             $result['locale'] = $localeDefault;
  202.             $result['version_web'] = $this->params->get('VERSION_WEB');
  203.             $result['ticketStates'] = $allStates ?? null;
  204.             $result['workOrderStates'] = $ticketsStates ?? null;
  205.             $result['requestStates'] = $requestStates;
  206.             $result['customModules'] = $serializedCustomModules ?? null;
  207.             $result['taskStates'] = $taskStates;
  208.             $result['ticketSources'] = $ticketSources;
  209.             $result['ticketFrequency'] = $ticketFrequency;
  210.             $result['priorities'] = $serializedPriorities ?? null;
  211.             $result['languages'] = $languages;
  212.             $result['countries'] = $countries;
  213.             $result['typeCategories'] = [
  214.                 Category::TYPE_RESOURCE => 'ResourceAndTool',
  215.                 Category::TYPE_MAINTENANCE_ELEMENT => 'Maintenance Element',
  216.                 Category::TYPE_TASK => 'Task',
  217.                 Category::TYPE_USER => 'User',
  218.                 Category::TYPE_LOCATION => 'Location'
  219.             ];
  220.             $result['chat'] = [
  221.                 'type' => ['ticket''task'],
  222.                 'state' => ['active''closed'],
  223.                 'channel' => ['private''public''presence'],
  224.                 'event' => ['NEW_CHAT''MESSAGE_READ']
  225.             ];
  226.             $result['files'] = [
  227.                 'maxSize' => MediaObject::max_size,
  228.                 'mimeTypes' => 'jpeg, jpg, png, mp4, m4v, x-m4v, JPEG, quicktime, mp4, mpeg, pdf, octet-stream, text, doc, excel, powerpoint'
  229.             ];
  230.             $currencyNames Currencies::getNames($localeDefault);
  231.             $currencies = array();
  232.             $allowedCurrencies = [
  233.                 'VES''BOB''CLE''CRC''SVC''NIO''CZK''DKK''SKK''EEK''ISK''NOK''SEK''BRR',
  234.                 'AED''AUD''CAD''BMD''KYD''TTD''USD''JMD''NZD''CVE''MZE''EUR''CHF''HNL',
  235.                 'GBP''MCF''ARS''BOP''CLP''COP''DOP''PHP''MXN''UYU''GTQ''ZAR''BRL''QAR',
  236.                 'IRR''OMR''SAR''YER''RUB''PEN''ECS''JPY''CNY'
  237.             ];
  238.             foreach ($currencyNames as $code => $name) {
  239.                 if (in_array($code$allowedCurrencies)) {
  240.                     $currencies[$code] = array(
  241.                         'id' => $code,
  242.                         'name' => $name//. $this->getRegionOfCurrencies($code)
  243.                         'symbol' => Currencies::getSymbol($code$localeDefault)
  244.                     );
  245.                 }
  246.             }
  247.             $result['currencies'] = $currencies;
  248.             $timezones = [];
  249.             $offsets = [];
  250.             $now = new \DateTime('now', new \DateTimeZone('UTC'));
  251.             foreach(Timezones::getNames($localeDefault) as $timezone => $timezoneName) {
  252.                 if ($timezone !== 'Asia/Qostanay') {
  253.                     $now->setTimezone(new \DateTimeZone($timezone));
  254.                     $offsets[] = $offset $now->getOffset();
  255.                     $timezones[$timezone] = '('.Timezones::getGmtOffset($timezone).') '.$this->format_timezone_name(
  256.                             $timezoneName
  257.                         );
  258.                 }
  259.             }
  260.             array_multisort($offsets$timezones);
  261.             $result['timezone'] = $timezones;
  262.             $result['useUnits'] = [
  263.                 => array(
  264.                     'id' => 1,
  265.                     'name' => 'Units',
  266.                     'nomenclature' => 'u'
  267.                 ),
  268.                 => array(
  269.                     'id' => 2,
  270.                     'name' => 'liters',
  271.                     'nomenclature' => 'l'
  272.                 ),
  273.                 => array(
  274.                     'id' => 3,
  275.                     'name' => 'kilograms',
  276.                     'nomenclature' => 'Kg'
  277.                 ),
  278.                 => array(
  279.                     'id' => 4,
  280.                     'name' => 'grams',
  281.                     'nomenclature' => 'g'
  282.                 ),
  283.                 => array(
  284.                     'id' => 5,
  285.                     'name' => 'milliliters',
  286.                     'nomenclature' => 'ml'
  287.                 ),
  288.                 => array(
  289.                     'id' => 6,
  290.                     'name' => 'kilometers',
  291.                     'nomenclature' => 'km'
  292.                 ),
  293.                 => array(
  294.                     'id' => 7,
  295.                     'name' => 'meters',
  296.                     'nomenclature' => 'm'
  297.                 ),
  298.                 => array(
  299.                     'id' => 8,
  300.                     'name' => 'centimeters',
  301.                     'nomenclature' => 'cm'
  302.                 ),
  303.                 => array(
  304.                     'id' => 9,
  305.                     'name' => 'square_meter',
  306.                     'nomenclature' => 'm2'
  307.                 ),
  308.                 10 => array(
  309.                     'id' => 10,
  310.                     'name' => 'hour',
  311.                     'nomenclature' => 'h'
  312.                 ),
  313.                 11 => array(
  314.                     'id' => 11,
  315.                     'name' => 'minutes',
  316.                     'nomenclature' => 'min'
  317.                 ),
  318.                 12 => array(
  319.                     'id' => 12,
  320.                     'name' => 'seconds',
  321.                     'nomenclature' => 's'
  322.                 )
  323.             ];
  324.             $entities=[];
  325.             foreach (Entity::Entities as $entity){
  326.                 $entity["label"] = $this->translator->trans('customFields.entities.'.$entity["label"],[],'messages',$lang);
  327.                 $entities[]=$entity;
  328.             };
  329.             $result['customField'] = array(
  330.                 'entities' => $entities,
  331.                 'types' =>[
  332.                     ['label' => $this->translator->trans('customFields.types.string',[],'messages',$lang), 'id' => 'string'],
  333.                     ['label' => $this->translator->trans('customFields.types.numeric',[],'messages',$lang), 'id' => 'number'],
  334.                     ['label' => $this->translator->trans('customFields.types.date',[],'messages',$lang), 'id' => 'date']]);
  335.             $result['week'] = [
  336.                         'monday' => 'Monday',
  337.                         'tuesday' => 'Tuesday',
  338.                         'wednesday' => 'Wednesday',
  339.                         'thursday' => 'Thursday',
  340.                         'friday' => 'Friday',
  341.                         'saturday' => 'Saturday',
  342.                         'sunday' => 'Sunday'
  343.                     ];
  344.             $result['hourDay'] = [
  345.                         '00:00' => '00:00',
  346.                         '01:00' => '01:00',
  347.                         '02:00' => '02:00',
  348.                         '03:00' => '03:00',
  349.                         '04:00' => '04:00',
  350.                         '05:00' => '05:00',
  351.                         '06:00' => '06:00',
  352.                         '07:00' => '07:00',
  353.                         '08:00' => '08:00',
  354.                         '09:00' => '09:00',
  355.                         '10:00' => '10:00',
  356.                         '11:00' => '11:00',
  357.                         '12:00' => '12:00',
  358.                         '13:00' => '13:00',
  359.                         '14:00' => '14:00',
  360.                         '15:00' => '15:00',
  361.                         '16:00' => '16:00',
  362.                         '17:00' => '17:00',
  363.                         '18:00' => '18:00',
  364.                         '19:00' => '19:00',
  365.                         '20:00' => '20:00',
  366.                         '21:00' => '21:00',
  367.                         '22:00' => '22:00',
  368.                         '23:00' => '23:00'
  369.                     ];
  370.             $roles  $this->em->getRepository(Role::class)->findAll();
  371.             foreach ($roles as $role) {
  372.                 $result['roles'][$role->getName()] = $role->getWeight();
  373.             }
  374.             $result json_encode($result);
  375.             $event->setControllerResult($result);
  376.             return;
  377.         }
  378.         $ticketTypes $this->em->getRepository(TicketType::class)->findAll();
  379.         foreach ($ticketTypes as $ticketType) {
  380.             $serializedTicketTypes[] = array(
  381.                 'id' => $ticketType->getId(),
  382.                 'name' => $ticketType->getName()
  383.             );
  384.         }
  385.         $incidenceTypes $this->em->getRepository(IncidenceType::class)->findAll();
  386.         foreach ($incidenceTypes as $incidenceType) {
  387.             if ($incidenceType->getEnabled()) {
  388.                 $serializedIncidenceTypes[] = array(
  389.                     'id' => $incidenceType->getId(),
  390.                     'name' => $incidenceType->getName()
  391.                 );
  392.             }
  393.         }
  394.         $resultDesnormalized json_decode($resulttrue);
  395.         $result = array();
  396.         $result['versionApp'] = [
  397.             'IOS' => $this->params->get('VERSION_APP_IOS'),
  398.             'ANDROID' => $this->params->get('VERSION_APP_ANDROID')
  399.         ];
  400.         $result['versionAppStatus'] = [
  401.             'IOS' => $this->params->get('VERSION_APP_IOS_STATUS'),
  402.             'ANDROID' => $this->params->get('VERSION_APP_ANDROID_STATUS')
  403.         ];
  404.         $result['versionAppRequest'] = [
  405.             'IOS' => $this->params->get('VERSION_APP_REQUEST_IOS'),
  406.             'ANDROID' => $this->params->get('VERSION_APP_REQUEST_ANDROID')
  407.         ];
  408.         $result['versionAppOperator'] = [
  409.             'IOS' => $this->params->get('VERSION_APP_OPERATOR_IOS'),
  410.             'ANDROID' => $this->params->get('VERSION_APP_OPERATOR_ANDROID')
  411.         ];
  412.         $result['apiVersion'] =  $resultDesnormalized[0]['apiVersion'] ?? null;
  413.         $result['priorities'] = $serializedPriorities ?? null;
  414.         $result['ticketType'] = $serializedTicketTypes ?? null;
  415.         $result['incidenceTypes'] = $serializedIncidenceTypes ?? null;
  416.         $result['ticketStates'] = $serializedTicketStates ?? null;
  417.         $result['taskStates'] = $taskStates;
  418.         $result['ticketSources'] = $ticketSources;
  419.         $result['languages'] = $languages;
  420.         $result['countries'] = $countries;
  421.         $result['dateFormats'] = [
  422.             => [
  423.                 'label' => 'day-month-year',
  424.                 'format' => 'dd-MM-yyyy'
  425.             ],
  426.             => [
  427.                 'label' => 'month-day-year',
  428.                 'format' => 'MM-dd-yyyy'
  429.             ]
  430.         ];
  431.         $result json_encode($result);
  432.         $event->setControllerResult($result);
  433.     }
  434.     protected function format_GMT_offset($offset) {
  435.         $hours intval($offset 3600);
  436.         $minutes abs(intval($offset 3600 60));
  437.         return 'GMT' . ($offset sprintf('%+03d:%02d'$hours$minutes) : '');
  438.     }
  439.     protected function format_timezone_name($name) {
  440.         $name str_replace('/'', '$name);
  441.         $name str_replace('_'' '$name);
  442.         $name str_replace('St ''St. '$name);
  443.         $name str_replace('(''- '$name);
  444.         $name str_replace(')'''$name);
  445.         return $name;
  446.     }
  447.     /**
  448.      * @param string $code
  449.      * @return mixed|string
  450.      */
  451.     protected function getRegionOfCurrencies(string $code)
  452.     {
  453.         $list = array(
  454.             'DZD' => 'Algeria',
  455.             'USD' => 'USA',
  456.             'EUR' => 'European Monetary Union',
  457.             'XCD' => 'Eastern Carribean States',
  458.             'AMD' => 'Armenia',
  459.             'AUD' => 'Australia',
  460.             'AZN' => 'Azerbaijan',
  461.             'XAF' => 'African Financial Community',
  462.             'VES' => 'Venezuela',
  463.             'BOB' => 'Bolivia',
  464.             'CRC' => 'Costa rica',
  465.             'SVC' => 'Salvador',
  466.             'NIO' => 'Nicaragua',
  467.             'CZK' => 'Czech Republic',
  468.             'DKK' => 'Denmark',
  469.             'NOK' => 'Northern Mariana Islands',
  470.             'SEK' => 'SWEDEN',
  471.             'BSD' => 'Bahamas',
  472.             'CUC' => 'Cuba',
  473.             'CUP' => 'Cuba',
  474.             'JPY' => 'Japan',
  475.             'CNY' => 'China'
  476.         );
  477.         if (isset($list[$code])) {
  478.             return ' - '.$list[$code];
  479.         }
  480.         return '';
  481.     }
  482. }