src/AdminBundle/Controller/EmailTemplatesAdminController.php line 210

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Controller;
  3. use Exception;
  4. use Symfony\Component\Form\FormError;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Sonata\AdminBundle\Controller\CRUDController;
  8. use AdminBundle\Controller\Traits\AdminControllerTrait;
  9. use AdminBundle\Form\Type\AdminEmailTemplateType;
  10. use AdminBundle\Model\AdminEmailTemplateModel;
  11. use AdminBundle\Entity\Account;
  12. use AdminBundle\Entity\Booking;
  13. use AdminBundle\Entity\Driver;
  14. use AdminBundle\Entity\Settings;
  15. use AdminBundle\Entity\Ticket;
  16. class EmailTemplatesAdminController extends CRUDController
  17. {
  18. use AdminControllerTrait;
  19. /**
  20. * @return Response
  21. */
  22. public function listAction(Request $request):Response
  23. {
  24. $model = new AdminEmailTemplateModel();
  25. $form = $this->createForm(AdminEmailTemplateType::class, $model);
  26. $form->handleRequest($this->getRequest());
  27. $emailContent = null;
  28. if ($form->isSubmitted() && $form->isValid()) {
  29. try {
  30. $emailContent = $this
  31. ->get('mandrill.manager')
  32. ->getEmailContent($model->getTemplate(), $this->prepareEmailTemplateVars($model))
  33. ;
  34. } catch (Exception $e) {
  35. $form->addError(new FormError($e->getMessage()));
  36. }
  37. }
  38. return $this->render('@Admin/Admin/email_templates.html.twig', [
  39. 'form' => $form->createView(),
  40. 'email_content' => $emailContent,
  41. ]);
  42. }
  43. /**
  44. * @param AdminEmailTemplateModel $model
  45. *
  46. * @return array
  47. */
  48. private function prepareEmailTemplateVars(AdminEmailTemplateModel $model)
  49. {
  50. $vars = [];
  51. if (null !== $model->getBookingKey()) {
  52. /** @var Booking|null $booking */
  53. $booking = $this
  54. ->getDoctrine()
  55. ->getRepository(Booking::class)
  56. ->findOneByKey($model->getBookingKey())
  57. ;
  58. $vars = array_replace_recursive($vars, $this->prepareEmailTemplateBookingVars($booking));
  59. }
  60. if (null !== $model->getDriverInternalName()) {
  61. /** @var Driver|null $driver */
  62. $driver = $this
  63. ->getDoctrine()
  64. ->getRepository(Driver::class)
  65. ->findOneByInternalName($model->getDriverInternalName())
  66. ;
  67. $vars = array_replace_recursive($vars, $this->prepareEmailTemplateDriverVars($driver));
  68. }
  69. if (null !== $model->getClientEmail()) {
  70. /** @var Account|null $account */
  71. $account = $this
  72. ->getDoctrine()
  73. ->getRepository(Account::class)
  74. ->findOneByEmail($model->getClientEmail())
  75. ;
  76. $vars = array_replace_recursive($vars, $this->prepareEmailTemplateAccountVars($account));
  77. }
  78. if (null !== $model->getTicketId()) {
  79. /** @var Ticket|null $ticket */
  80. $ticket = $this
  81. ->getDoctrine()
  82. ->getRepository(Ticket::class)
  83. ->find($model->getTicketId())
  84. ;
  85. $vars = array_replace_recursive($vars, $this->prepareEmailTemplateTicketVars($ticket));
  86. }
  87. return $vars;
  88. }
  89. /**
  90. * @param Booking|null $booking
  91. *
  92. * @return array
  93. */
  94. private function prepareEmailTemplateBookingVars(Booking $booking = null)
  95. {
  96. if (null === $booking) {
  97. return [];
  98. }
  99. /** @var Settings|null $distanceUnitSetting */
  100. $distanceUnitSetting = $this
  101. ->getDoctrine()
  102. ->getRepository(Settings::class)
  103. ->findOneByKey('distance_unit')
  104. ;
  105. $distanceUnit = $distanceUnitSetting ? $distanceUnitSetting->getValue() : 'Miles';
  106. $currency = $this
  107. ->get('currency.service')
  108. ->getSystemCurrency()
  109. ;
  110. $vars = [
  111. 'passenger_name' => $booking->getFullname(),
  112. 'passenger_phone' => $booking->getClientPhone(),
  113. 'passenger_email' => $booking->getClientEmail(),
  114. 'booking_key' => $booking->getKey(),
  115. 'booking_pickup_date' => $booking
  116. ->getPickUpDate()
  117. ->format('d/m/Y'),
  118. 'booking_pickup_time' => $booking->getPickUpTime(),
  119. 'booking_pickup_location' => $booking->getPickUpAddress(),
  120. 'booking_dropoff_location' => $booking->getDestinationAddress(),
  121. 'booking_vias_content' => sprintf('<ul>%s</ul>', implode('', array_map(function ($address) {
  122. return "<li>{$address}</li>";
  123. }, $booking->getViasAsArray()))),
  124. 'booking_passengers' => $booking->getPassengersNumber(),
  125. 'booking_luggage' => $booking->getCheckinLuggage(),
  126. 'booking_notes' => $booking->getNotes(),
  127. 'booking_cost' => $booking->getOverridePrice(),
  128. 'booking_duration' => $booking->getEstimatedTimePrettyFormat(),
  129. 'booking_distance' => "{$booking->getDistanceUnit()} {$distanceUnit}",
  130. 'booking_car_type' => $booking
  131. ->getCarType()
  132. ->getName(),
  133. 'booking_hand_luggage' => $booking->getHandLuggage(),
  134. 'flight_number' => !empty($booking->getFlightNumber()) ? $booking->getFlightNumber() : null,
  135. 'landing_time' => $booking->getFlightLandingTime(),
  136. 'mg_timeframe' => $booking->getMeetAndGreetTimePrettyFormat(),
  137. 'currency_symbol' => $currency,
  138. 'booking_creation_date' => $booking
  139. ->getBookingDate()
  140. ->format('d/m/Y'),
  141. 'booking_creation_time' => $booking
  142. ->getBookingDate()
  143. ->format('H:i'),
  144. 'booking_origin' => 'origin',
  145. 'booking_cancel_reason' => $booking->getCancelReason() ?? 'Office Cancellation - Payment not Processed',
  146. 'booking_cancel_charge' => $booking->getCancelRefund() == -1 ? 'TBC' : $booking->getCancelRefund(),
  147. 'booking_review_link' => "{$this->getParameter('assets_base_url')}review/{$booking->getKey()}",
  148. 'payment_link' => 'http://paymentlinkfortesting.com',
  149. 'passenger_password' => 'http://passengerpasswordurl.com',
  150. 'drivers' => '<ul><li>Id: 1</li><li>Name: Driver1</li><li>Id: 2</li><li>Name: Driver2</li></ul>',
  151. 'error_message' => 'Error message',
  152. 'bookingID' => $booking->getId(),
  153. 'message' => 'This is a test message.',
  154. 'return_booking' => false,
  155. ];
  156. $returnBooking = $booking->getReturnBooking();
  157. if (null !== $returnBooking) {
  158. $vars = array_replace_recursive($vars, [
  159. 'return_booking' => true,
  160. 'return_booking_key' => $returnBooking->getKey(),
  161. 'return_booking_pickup_date' => $returnBooking
  162. ->getPickUpDate()
  163. ->format('d/m/Y'),
  164. 'return_booking_pickup_time' => $returnBooking->getPickUpTime(),
  165. 'return_booking_pickup_location' => $returnBooking->getPickUpAddress(),
  166. 'return_booking_dropoff_location' => $returnBooking->getDestinationAddress(),
  167. 'return_booking_vias_content' => sprintf('<ul>%s</ul>', implode('', array_map(function ($address) {
  168. return "<li>{$address}</li>";
  169. }, $returnBooking->getViasAsArray()))),
  170. 'return_booking_notes' => $returnBooking->getNotes(),
  171. 'return_booking_duration' => $returnBooking->getEstimatedTimePrettyFormat(),
  172. 'return_booking_distance' => "{$returnBooking->getDistanceUnit()} {$distanceUnit}",
  173. ]);
  174. }
  175. return array_replace_recursive($vars, $this->prepareEmailTemplateDriverVars($booking->getDriver()));
  176. }
  177. /**
  178. * @param Driver|null $driver
  179. *
  180. * @return array
  181. */
  182. private function prepareEmailTemplateDriverVars(Driver $driver = null)
  183. {
  184. if (null === $driver) {
  185. return [];
  186. }
  187. $car = $driver->getCar();
  188. return [
  189. 'driver_ID' => $driver->getInternalName(),
  190. 'driver_internal_name' => $driver->getInternalName(),
  191. 'driver_email' => $driver
  192. ->getUser()
  193. ->getEmail(),
  194. 'driver_name' => $driver
  195. ->getUser()
  196. ->getFullname(),
  197. 'driver_PCO' => $driver->getPublicCarriageOfficeLicenceNumber(),
  198. 'driver_HACK' => $driver->getHackLicenseNumber(),
  199. 'driver_SSN' => $driver->getSocialSecurityNumber(),
  200. 'driver_phone' => $driver->getPhone(),
  201. 'driver_vehicle_name' => $car ? "{$car->getMaker()} {$car->getModel()}" : null,
  202. 'driver_vehicle_plate' => $car ? $car->getPlateNumber() : null,
  203. 'driver_vehicle_PHV' => $car ? $car->getPhvLicenseNumber() : null,
  204. 'driver_vehicle_LIMO' => $car ? $car->getLimoLicenseNumber() : null,
  205. 'driver_vehicle_color' => $car ? $car->getColor() : null,
  206. 'driver_password' => 'http://driverpasswordurl.com',
  207. 'week_number' => 3,
  208. 'week_period' => '05.05.2020-10.05.2020',
  209. 'current_week_saturday' => '08.05.2020',
  210. ];
  211. }
  212. /**
  213. * @param Account|null $account
  214. *
  215. * @return array
  216. */
  217. private function prepareEmailTemplateAccountVars(Account $account = null)
  218. {
  219. if (null === $account) {
  220. return [];
  221. }
  222. $user = $account->getUser();
  223. return [
  224. 'passenger_name' => $account->getName(),
  225. 'passenger_email' => $user ? $user->getEmail() : null,
  226. 'passenger_phone' => $account->getPhone(),
  227. 'department' => $account->getDepartment(),
  228. 'first_name' => $user ? $user->getFirstname() : null,
  229. 'last_name' => $user ? $user->getLastname() : null,
  230. 'user_email' => $user ? $user->getEmail() : null,
  231. 'user_phone' => $user ? $user->getPhone() : null,
  232. ];
  233. }
  234. /**
  235. * @param Ticket|null $ticket
  236. *
  237. * @return array
  238. */
  239. private function prepareEmailTemplateTicketVars(Ticket $ticket = null)
  240. {
  241. if (null === $ticket) {
  242. return [];
  243. }
  244. return [
  245. 'ticket_id' => $ticket->getId(),
  246. 'ticket_subject' => $ticket->getSubject(),
  247. 'ticket_description' => $ticket->getDescription(),
  248. 'ticket_creation_date' => $ticket
  249. ->getCreatedDate()
  250. ->format('Y-m-d'),
  251. 'ticket_creation_time' => $ticket
  252. ->getCreatedDate()
  253. ->format('H:i:s'),
  254. 'booking_key' => $ticket->getBooking(),
  255. 'ticket_department' => $ticket->getDepartmentNames(),
  256. 'ticket_message' => $ticket->getTicketMessages(),
  257. ];
  258. }
  259. }