src/Controller/BaseController.php line 94

Open in your IDE?
  1. <?php
  2. // src/Controller/BaseController.php
  3. namespace App\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Validator\ConstraintViolationList;
  9. use Orc\SaasBundle\Entity\Client;
  10. use Orc\BookingBundle\Repository\HoursOfOperationRepository;
  11. use Orc\BookingBundle\Repository\HoursChangeRepository;
  12. use Orc\BookingBundle\Entity\Booking;
  13. use Orc\BookingBundle\Booking\EndTimeCalculator;
  14. use App\Entity\BookingProcess;
  15. use App\Entity\BookingProcessBooking;
  16. use App\Entity\BookingProcessBookingNote;
  17. use App\Entity\BookingProcessBookingServiceAnswer;
  18. use App\Entity\BookingProcessLocation;
  19. use App\Model\GoogleCalendarModel;
  20. use App\Model\Google\GoogleClient;
  21. use App\Model\Google\Entity\WatchGoogleChannelParams;
  22. class BaseController extends AbstractController {
  23.     protected $client;
  24.     protected $site;
  25.     protected $entityManager;
  26.     protected function addOrUpdateGoogleEvent(
  27.         Request $requestGoogleClient $googleClient,
  28.         GoogleCalendarModel $googleCalendarModelBooking $booking
  29.     ) : void
  30.     {
  31. //        try {
  32.             $events $googleCalendarModel->getStaffCalendarsBookingEvents(
  33.                 $this->client$booking
  34.             );
  35.             foreach($events as $event) {
  36.                 $event->getGoogleCalendarEvent()->setDescription($this->renderView(
  37.                     '@OrcCalendar/Feed/feed-description.ical.twig',
  38.                     [
  39.                         'booking' => $event->getBooking(),
  40.                         'worker' => $event->getWorker(),
  41.                         'site' => $this->client->getSite()
  42.                     ]
  43.                 ));
  44.             }
  45.             $googleCalendarModel->addEvents($events);
  46. //        }
  47. //        catch (CalendarModelException $exc) {
  48. //            $this->addFlash(
  49. //                "error",
  50. //                "Unable to fully sync booking event with google calendar. "
  51. //                . "Please <a href=\"". $this->generateUrl('app_google_calendar_addstaffcalendars') ."\">re-sync with google calendar</a>."
  52. //            );
  53. //        }
  54.     }
  55.     /**
  56.      *
  57.      * @param GoogleClient $googleClient
  58.      * @return bool
  59.      */
  60.     protected function authenticate(
  61.         Request $requestGoogleClient $googleClient
  62.     ) :  bool
  63.     {
  64.         $client $this->getClient();
  65.         $this->get('session')->set('original_url'$request->getUri());
  66.         $isAuthenticated $googleClient->setRedirectUrl(
  67.             $this->getGoogleOauthRedirectUrl()
  68.         )->authenticate($this->get('session')->get('original_url'));
  69.         return $isAuthenticated;
  70.     }
  71.     function getClient() {
  72.         $hostName $this->container->get('request_stack')
  73.             ->getCurrentRequest()
  74.             ->server->get('SERVER_NAME');
  75.         if (preg_match('/^admin\./'$hostName)) {
  76.             return null;
  77.         }
  78.         
  79.         $this->entityManager $this->getDoctrine()->getManager();
  80.         $clientRepository $this->entityManager->getRepository(Client::class);
  81.         $this->client $clientRepository->findOneByDomainAdvanced($hostName);
  82.         //$this->client = $clientRepository->findOneByDomain($hostName);
  83.         if (empty($this->client)) {
  84.             throw $this->createNotFoundException('Tenant not found.');
  85.         }
  86.         $this->site $this->client->getSite();
  87.         return $this->client;
  88.     }
  89.     function getEntityManager() {
  90.         $this->entityManager $this->getDoctrine()->getManager();
  91.         return $this->entityManager;
  92.     }
  93.     function getClientFromRequest(Request $request) {
  94.         $hostName $request->server->get('SERVER_NAME');
  95.         if (preg_match('/^admin\./'$hostName)) {
  96.             return null;
  97.         }
  98.         $this->entityManager $this->getDoctrine()->getManager();
  99.         $clientRepository $this->entityManager->getRepository(Client::class);
  100.         $this->client $clientRepository->findOneByDomainAdvanced($hostName);
  101.         //$this->client = $clientRepository->findOneByDomain($hostName);
  102.         if (empty($this->client)) {
  103.             throw $this->createNotFoundException('Tenant not found.');
  104.         }
  105.         $this->site $this->client->getSite();
  106.         return $this->client;
  107.     }
  108.     function getClientFromUrl(string $url) {
  109.         $url str_replace('https://'''$url);
  110.         $arrUrl explode('/'$url);
  111.         $hostName $arrUrl[0];
  112.         if (preg_match('/^admin\./'$hostName)) {
  113.             return null;
  114.         }
  115.         $this->entityManager $this->getDoctrine()->getManager();
  116.         $clientRepository $this->entityManager->getRepository(Client::class);
  117.         $this->client $clientRepository->findOneByDomainAdvanced($hostName);
  118.         //$this->client = $clientRepository->findOneByDomain($hostName);
  119.         if (empty($this->client)) {
  120.             throw $this->createNotFoundException('Tenant not found.');
  121.         }
  122.         $this->site $this->client->getSite();
  123.         return $this->client;
  124.     }
  125.     protected function getErrorMessages($form) {
  126.         $errors = array();
  127.         foreach ($form->getErrors() as $key => $error) {
  128.             if ($form->isRoot()) {
  129.                 $errors['#'][] = $error->getMessage();
  130.             } else {
  131.                 $errors[] = $error->getMessage();
  132.             }
  133.         }
  134.         foreach ($form->all() as $child) {
  135.             if (!$child->isValid()) {
  136.                 $errors[$child->getName()] = $this->getErrorMessages($child);
  137.             }
  138.         }
  139.         return $errors;
  140.     }
  141.     /**
  142.      *
  143.      * @return string
  144.      */
  145.     protected function getGoogleOauthRedirectUrl() : string
  146.     {
  147.         $redirectUrl $this->generateUrl(
  148.             'app_google_oauthcallback', [], UrlGeneratorInterface::ABSOLUTE_URL
  149.         );
  150.         $arrUrl explode('.'$redirectUrl);
  151.         $arrUrl[0] = 'https://admin';
  152.         $redirectUrl implode('.'$arrUrl);
  153.         return $redirectUrl;
  154.     }
  155.     protected function getWatchGoogleChannelParams($client)
  156.     {
  157.         $domain $this->getParameter('domain_name');
  158.         $adminSubdomain $this->getParameter('admin_subdomain');
  159.         $googleWebHookUrl 'https://' $adminSubdomain '.' $domain .
  160.             $this->generateUrl('app_google_calendar_eventschannelwebhook');
  161.         return new WatchGoogleChannelParams(
  162.             $client,
  163.             new \Google_Service_Calendar_Channel(),
  164.             $googleWebHookUrl,
  165.             $domain
  166.         );
  167.     }
  168.     protected function determineCalendarQueryStart(
  169.         \DateTime $startBooking $booking,
  170.         HoursOfOperationRepository $hoursOfOperationRepository,
  171.         EndTimeCalculator $endTimeCalculator,
  172.         HoursChangeRepository $hoursChangeRepository
  173.     )
  174.     {
  175.         $cutofftime $booking->getService()->getCutofftime();
  176.         $start->modify($cutofftime ' hours');
  177.         $start $this->determineCalendarQueryStartRound(
  178.             $start$booking$hoursOfOperationRepository$endTimeCalculator,
  179.             $hoursChangeRepository
  180.         );
  181.         return $start;
  182.     }
  183.     protected function determineCalendarQueryStartRound(
  184.         \DateTime $startBooking $booking,
  185.         HoursOfOperationRepository $hoursOfOperationRepository,
  186.         EndTimeCalculator $endTimeCalculator,
  187.         HoursChangeRepository $hoursChangeRepository
  188.     )
  189.     {
  190.         $dayEndTime $this->getDayEndTime(
  191.             $start$hoursOfOperationRepository$hoursChangeRepository
  192.         );
  193.         $dayEndTime->setDate($start->format('Y'), $start->format('m'), $start->format('d'));
  194.         $dayEndTime->modify('-' $endTimeCalculator->getTimeRequired($booking) . ' minutes');
  195.         if ( $start $dayEndTime ) {
  196.             $start->modify('1 day');
  197.             $dayStartTime $this->getDayStartTime(
  198.                 $start$hoursOfOperationRepository$hoursChangeRepository
  199.             );
  200.             $start->setTime($dayStartTime->format('H'), $dayStartTime->format('i'));
  201.             $start $this->determineCalendarQueryStartRound(
  202.                 $start$booking$hoursOfOperationRepository,
  203.                 $endTimeCalculator$hoursChangeRepository
  204.             );
  205.         }
  206.         return $start;
  207.     }
  208.     protected function determineCalendarQueryStartForProcess(
  209.         \DateTime $startBookingProcessBooking $booking,
  210.         HoursOfOperationRepository $hoursOfOperationRepository,
  211.         EndTimeCalculator $endTimeCalculator,
  212.         HoursChangeRepository $hoursChangeRepository
  213.     )
  214.     {
  215.         $cutofftime $booking->getService()->getCutofftime();
  216.         $start->modify($cutofftime ' hours');
  217.         $start $this->determineCalendarQueryStartRoundForProcess(
  218.             $start$booking$hoursOfOperationRepository$endTimeCalculator,
  219.             $hoursChangeRepository
  220.         );
  221.         return $start;
  222.     }
  223.     protected function determineCalendarQueryStartRoundForProcess(
  224.         \DateTime $startBookingProcessBooking $booking,
  225.         HoursOfOperationRepository $hoursOfOperationRepository,
  226.         EndTimeCalculator $endTimeCalculator,
  227.         HoursChangeRepository $hoursChangeRepository
  228.     )
  229.     {
  230.         $dayEndTime $this->getDayEndTime(
  231.             $start$hoursOfOperationRepository$hoursChangeRepository
  232.         );
  233.         $olddayEndTime1 = clone($dayEndTime);
  234.         $dayEndTime->setDate($start->format('Y'), $start->format('m'), $start->format('d'));
  235.         $olddayEndTime2 = clone($dayEndTime);
  236.         $dayEndTime->modify('-' $endTimeCalculator->getTimeRequiredForProcess($booking) . ' minutes');
  237.         // if ($start->format('d') == '26') {
  238.         //     dd(['start' => $start, 'dayEndTime' => $dayEndTime, 'olddayEndTime1' => $olddayEndTime1, 'olddayEndTime2' => $olddayEndTime2, 'modify' => '-' . $endTimeCalculator->getTimeRequiredForProcess($booking) . ' minutes']);
  239.         // }
  240.         if ( $start $dayEndTime ) {
  241.             $start->modify('1 day');
  242.             $dayStartTime $this->getDayStartTime(
  243.                 $start$hoursOfOperationRepository$hoursChangeRepository
  244.             );
  245.             $start->setTime($dayStartTime->format('H'), $dayStartTime->format('i'));
  246.             $start $this->determineCalendarQueryStartRoundForProcess(
  247.                 $start$booking$hoursOfOperationRepository,
  248.                 $endTimeCalculator$hoursChangeRepository
  249.             );
  250.         }
  251.         return $start;
  252.     }
  253.     private function getDayEndTime(
  254.         \DateTime $start,
  255.         HoursOfOperationRepository $hoursOfOperationRepository,
  256.         HoursChangeRepository $hoursChangeRepository
  257.     )
  258.     {
  259.         $day = clone $start;
  260.         $day->setTime(00);
  261.         $hoursChange $hoursChangeRepository->findOneBy([
  262.             'client' => $this->client,
  263.             'day' => $day
  264.         ]);
  265.         if ($hoursChange) {
  266.             $dayEndTime = clone $hoursChange->getEndTime();
  267.         }
  268.         else {
  269.             $dayOfTheWeekNumber $start->format('N')%7;
  270.             $dayHoursOfOperation $hoursOfOperationRepository->findOneBy([
  271.                 'client' => $this->client,
  272.                 'day'    => $dayOfTheWeekNumber
  273.             ]);
  274.             $dayEndTime = clone $dayHoursOfOperation->getEndTime();
  275.         }
  276.         return $dayEndTime;
  277.     }
  278.     private function getDayStartTime(
  279.         \DateTime $start,
  280.         HoursOfOperationRepository $hoursOfOperationRepository,
  281.         HoursChangeRepository $hoursChangeRepository
  282.     )
  283.     {
  284.         $day = clone $start;
  285.         $day->setTime(00);
  286.         $hoursChange $hoursChangeRepository->findOneBy([
  287.             'client' => $this->client,
  288.             'day' => $day
  289.         ]);
  290.         if ($hoursChange) {
  291.             $dayStartTime = clone $hoursChange->getStartTime();
  292.         }
  293.         else {
  294.             $dayOfTheWeekNumber $day->format('N')%7;
  295.             $dayHoursOfOperation $hoursOfOperationRepository->findOneBy([
  296.                 'client' => $this->client,
  297.                 'day'    => $dayOfTheWeekNumber
  298.             ]);
  299.             $dayStartTime = clone $dayHoursOfOperation->getStartTime();
  300.         }
  301.         return $dayStartTime;
  302.     }
  303.     protected function toValidationErrors(ConstraintViolationList $violationListstring $prefix=null) : array
  304.     {
  305.         $errors = [];
  306.         foreach($violationList as $violation) {
  307.             $key $violation->getPropertyPath();
  308.             if ($prefix){
  309.                 $key $prefix $key;
  310.             }
  311.             $errors[$key] = $violation->getMessage();
  312.         }
  313.         return $errors;
  314.     }
  315.     protected function resize($imagePath$width$height)
  316.     {
  317.         $im = new \Imagick($imagePath);
  318.         $im $im->flattenImages();
  319.         $im->setImageFormat('jpg');
  320.         $im->resizeImage($width,$height, \imagick::FILTER_LANCZOS0.9true);
  321.         $imagePath $this->ChangeExtensionToJpg($imagePath);
  322.         $im->writeImage($imagePath);
  323.         return $imagePath;
  324.     }
  325.     protected function ChangeExtensionToJpg($filenameWithPath)
  326.     {
  327.         $path_parts pathinfo($filenameWithPath);
  328.         $fileExtension $path_parts['extension'];
  329.         unlink($filenameWithPath);
  330.         return preg_replace('/\.' $fileExtension '$/''.jpg'$filenameWithPath);
  331.     }
  332.     protected function getFileName($filenameWithPath)
  333.     {
  334.         $path_parts pathinfo($filenameWithPath);
  335.         return $path_parts['filename'] . '.' $path_parts['extension'];
  336.     }
  337. }