src/Manager/ContactManager.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Normalizer\CategoryNormalizer;
  4. use App\Service\Internal\ContactService;
  5. use App\Service\Internal\LineService;
  6. use App\Service\Internal\MapService;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. class ContactManager
  9. {
  10. public const FORM_SUBJECT = [
  11. 'public_transport' => ['citea'],
  12. 'default_full' => ['vrd'],
  13. 'zou' => ['zou']
  14. ];
  15. public const SUBJECTS_INFORMATION_ZOU = [
  16. '' => '',
  17. 'form.choices.subject_information.zou_student_pass' => 'zou_student_pass',
  18. 'form.choices.subject_information.zou_student_pass_refund' => 'zou_student_pass_refund',
  19. 'form.choices.subject_information.my_trip' => 'my_trip',
  20. 'form.choices.subject_information.order_online' => 'order_online',
  21. 'form.choices.subject_information.animal_object' => 'animal_object',
  22. 'form.choices.subject_information.website' => 'website',
  23. 'form.choices.subject_information.my_zou_card' => 'my_zou_card',
  24. 'form.choices.subject_information.other' => 'other'
  25. ];
  26. public const SUBJECTS_CLAIM_ZOU = [
  27. '' => '',
  28. 'form.choices.subject_information.zou_student_pass' => 'zou_student_pass',
  29. 'form.choices.subject_information.zou_student_pass_refund' => 'zou_student_pass_refund',
  30. 'form.choices.subject_claim.object_loose' => 'object_loose',
  31. 'form.choices.subject_claim.reception' => 'reception',
  32. 'form.choices.subject_claim.late' => 'late',
  33. 'form.choices.subject_claim.taking' => 'taking',
  34. 'form.choices.subject_claim.confort' => 'confort',
  35. 'form.choices.subject_claim.security' => 'security',
  36. 'form.choices.subject_claim.driver' => 'driver',
  37. 'form.choices.subject_claim.accident' => 'accident'
  38. ];
  39. public const FORM_DEFAULT_SUBJECT = 'default';
  40. public const FORM_ZOU_SUBJECT = 'zou';
  41. public const NB_ATTACHMENTS = 3;
  42. /**
  43. * @var CategoryNormalizer
  44. */
  45. public $categoryNormalizer;
  46. /**
  47. * @var TranslatorInterface
  48. */
  49. public $translator;
  50. /**
  51. * @var ContactService
  52. */
  53. public $contactService;
  54. /**
  55. * @var MapService
  56. */
  57. public $mapService;
  58. /**
  59. * @var LineService
  60. */
  61. public $lineService;
  62. /**
  63. * @var string
  64. */
  65. public $networkId;
  66. public function __construct(
  67. CategoryNormalizer $categoryNormalizer,
  68. TranslatorInterface $translator,
  69. ContactService $contactService,
  70. MapService $mapService,
  71. LineService $lineService,
  72. string $networkId
  73. ) {
  74. $this->categoryNormalizer = $categoryNormalizer;
  75. $this->translator = $translator;
  76. $this->contactService = $contactService;
  77. $this->mapService = $mapService;
  78. $this->lineService = $lineService;
  79. $this->networkId = $networkId;
  80. }
  81. /**
  82. * Allows to know the form to call according to the subject passed in parameter
  83. *
  84. * @param string $subject
  85. *
  86. * @return string
  87. */
  88. public function getFormSubject(string $subject): ?string
  89. {
  90. if (empty($subject)) {
  91. return self::FORM_DEFAULT_SUBJECT;
  92. }
  93. foreach (self::FORM_SUBJECT as $key => $value) {
  94. if (in_array($subject, $value, false)) {
  95. return $key;
  96. }
  97. }
  98. return null;
  99. }
  100. /**
  101. * Build a journey message in contact form
  102. */
  103. public function buildMessageForJourney(string $journey): ?string
  104. {
  105. $journey = json_decode($journey, true);
  106. if (null === $journey) {
  107. return null;
  108. }
  109. $message = $this->translator->trans(
  110. 'contact.journey_new_format',
  111. ['from' => $journey['from']['value'], 'to' => $journey['to']['value']]
  112. );
  113. if (!empty($journey['via'])) {
  114. $vias = array_map(function ($v) {
  115. return $v['value'];
  116. }, $journey['via']);
  117. if (count($vias) > 0) {
  118. $message .= $this->translator->trans('contact.via') . implode(', ', $vias);
  119. }
  120. }
  121. $dateMessage = '';
  122. $at = isset($journey['arrivalDateTime']) ? $journey['arrivalDateTime'] : '';
  123. $dt = !empty($journey['departureDateTime']) ? $journey['departureDateTime'] : $at;
  124. if (!empty($dt)) {
  125. $dt = new \DateTime(str_replace(' ', '+', $dt));
  126. $dateMessage = $this->translator->trans('contact.departure_at_new_format', ['dt' => $dt->format('d/m/Y H:i')]);
  127. }
  128. $options = '';
  129. if (!empty($journey['modes'])) {
  130. $modesLabels = array_map(
  131. function ($m) {
  132. return $this->translator->trans('mode.' . strtolower($m));
  133. },
  134. $journey['modes']
  135. );
  136. $options = $this->translator->trans('contact.modes_new_format', ['modes' => implode(', ', $modesLabels)]);
  137. }
  138. $message .= " $dateMessage \n\n\n\n\n\n ________________________________________________ \n";
  139. $message .= $options . $this->translator->trans('contact.url') . $journey['currentUrl'];
  140. return $message;
  141. }
  142. /**
  143. * Build a schedule message in contact form
  144. */
  145. public function buildMessageForSchedule(string $schedule): ?string
  146. {
  147. $schedule = json_decode($schedule, true);
  148. if (null === $schedule) {
  149. return null;
  150. }
  151. $message = $this->translator->trans('contact.schedule-message');
  152. if (isset($schedule['islsn']) && ((isset($schedule['ismode']) && 'TOD' !== $schedule['ismode']) || !isset($schedule['ismode']))) {
  153. $message = $this->translator->trans(
  154. 'contact.schedule_new_format',
  155. ['line' => $schedule['islsn'], 'network' => $this->networkId]
  156. );
  157. } elseif (isset($schedule['islsn']) && isset($schedule['ismode']) && 'TOD' === $schedule['ismode']) {
  158. $message = $this->translator->trans(
  159. 'contact.schedule_tod_new_format',
  160. ['line' => $schedule['islsn'], 'network' => $this->networkId]
  161. );
  162. } elseif (isset($schedule['isspid'])) {
  163. $message = $this->translator->trans('contact.stoppoint_schedule_new_format', ['stop' => $schedule['isspv']]);
  164. }
  165. $message .= " \n\n\n\n\n\n _____________________________________________________ \n ";
  166. $message .= $this->translator->trans('contact.url') . $schedule['currentUrl'];
  167. return $message;
  168. }
  169. /**
  170. * Build categories in contact form
  171. */
  172. public function buildCategories(string $type = ''): array
  173. {
  174. $categories = $this->contactService->getCategories($type);
  175. $categories = $this->categoryNormalizer->normalize($categories);
  176. return $categories;
  177. }
  178. /**
  179. * Build an array of bike string stations
  180. */
  181. public function buildBikeSharingStation(): array
  182. {
  183. $stations = [];
  184. $bikeSharingStations = $this->mapService->getAllBikeSharingStationInfos();
  185. if (empty($bikeSharingStations['bikeSharingStations'])) {
  186. return [];
  187. }
  188. foreach ($bikeSharingStations['bikeSharingStations'] as $value) {
  189. $stations[$value['name']] = $value['name'];
  190. }
  191. asort($stations, SORT_NATURAL);
  192. return $stations;
  193. }
  194. /**
  195. * Build an array of secure bike parks
  196. */
  197. public function buildSecureBikePark(): array
  198. {
  199. $bikeParks = [];
  200. $secureBikeParks = $this->mapService->getAllSecureBikeParkInfos();
  201. if (empty($secureBikeParks['secureBikeParks'])) {
  202. return [];
  203. }
  204. foreach ($secureBikeParks['secureBikeParks'] as $value) {
  205. $bikeParks[$value['name']] = $value['name'];
  206. }
  207. asort($bikeParks, SORT_NATURAL);
  208. return $bikeParks;
  209. }
  210. /**
  211. * Build an array of lines
  212. */
  213. public function buildLines(): array
  214. {
  215. $lines = [];
  216. $allLines = $this->lineService->getAllLines();
  217. if (empty($allLines['lines'])) {
  218. return [];
  219. }
  220. foreach ($allLines['lines'] as $value) {
  221. $line = ucfirst($value['lName']);
  222. if (!empty($value['subNetwork']['name'])) {
  223. $line .= ' - ' . $value['subNetwork']['name'];
  224. }
  225. $lines[$line] = $value['id'];
  226. }
  227. ksort($lines, SORT_NATURAL);
  228. return $lines;
  229. }
  230. /**
  231. * Build an array with the directions of a line and stoppoints by direction
  232. */
  233. public function buildDirectionAndStoppoints(?string $lineId): array
  234. {
  235. $default = [
  236. [
  237. 'direction' => '',
  238. 'display' => $this->translator->trans('form.choice_direction'),
  239. 'stoppoints' => [
  240. '' => $this->translator->trans('form.choice_stoppoint'),
  241. ]
  242. ]
  243. ];
  244. if (null === $lineId) {
  245. return $default;
  246. }
  247. $directions = [];
  248. $allDirections = $this->lineService->getLineDirections($lineId);
  249. if (empty($allDirections)) {
  250. return $default;
  251. }
  252. $i = 0;
  253. foreach ($allDirections as $direction) {
  254. $directions[$i]['direction'] = $direction['direction'];
  255. $directions[$i]['display'] = $direction['display'];
  256. foreach ($direction['stopPoints'] as $stoppoint) {
  257. $stopName = ucfirst($stoppoint['name']);
  258. $directions[$i]['stoppoints'][$stopName] = $stopName;
  259. }
  260. asort($directions[$i]['stoppoints']);
  261. $i++;
  262. }
  263. $directionsDisplay = array_column($directions, 'display');
  264. array_multisort($directionsDisplay, SORT_ASC, $directions);
  265. return $directions;
  266. }
  267. /**
  268. * Build an array of directions from an array with directions and stoppoints
  269. */
  270. public function buildDirections(array $directionsWithStoppoints): array
  271. {
  272. $directions = [];
  273. foreach ($directionsWithStoppoints as $value) {
  274. $directions[$value['display']] = $value['direction'];
  275. }
  276. ksort($directions, SORT_NATURAL);
  277. return $directions;
  278. }
  279. /**
  280. * Build an array of stoppoints from an array directions and stoppoints
  281. */
  282. public function buildStoppointsByDirection(array $directionsWithStoppoints, string $directionId): array
  283. {
  284. $stoppoints = [];
  285. foreach ($directionsWithStoppoints as $direction) {
  286. if ($directionId !== $direction['direction']) {
  287. continue;
  288. }
  289. foreach ($direction['stoppoints'] as $value) {
  290. $stoppoints[$value] = $value;
  291. }
  292. }
  293. ksort($stoppoints, SORT_NATURAL);
  294. return $stoppoints;
  295. }
  296. /**
  297. * Send data post in contact form
  298. */
  299. public function normalizeContactData(string $type, array $data): array
  300. {
  301. if (!empty($type)) {
  302. $data['extra']['type'] = $type;
  303. } else {
  304. $data['extra']['type'] = 'default';
  305. }
  306. if (!isset($data['line_zou'])) {
  307. unset($data['line_zou']);
  308. } else {
  309. $data['extra']['line'] = $data['line_zou'];
  310. unset($data['line_zou']);
  311. }
  312. if (!isset($data['direction_zou'])) {
  313. unset($data['direction_zou']);
  314. } else {
  315. $data['extra']['direction'] = $data['direction_zou'];
  316. unset($data['direction_zou']);
  317. }
  318. if (!isset($data['stop_zou'])) {
  319. unset($data['stop_zou']);
  320. } else {
  321. $data['extra']['stop'] = $data['stop_zou'];
  322. unset($data['stop_zou']);
  323. }
  324. if (!isset($data['hour_zou'])) {
  325. unset($data['hour_zou']);
  326. } else {
  327. $data['extra']['hour'] = $data['hour_zou'];
  328. unset($data['hour_zou']);
  329. }
  330. if (!isset($data['date_zou'])) {
  331. unset($data['date_zou']);
  332. } else {
  333. $data['extra']['date'] = $data['date_zou'];
  334. unset($data['date_zou']);
  335. }
  336. if (isset($data['subject'])) {
  337. $data['category'] = $this->translator->trans('form.choices.subject.' . strtolower($data['subject']));
  338. unset($data['subject']);
  339. }
  340. if (isset($data['subjects'])) {
  341. $data['title'] = $data['subjects'];
  342. unset($data['subjects']);
  343. }
  344. if (isset($data['file'])) {
  345. $data['extra']['file'] = $data['file'];
  346. unset($data['file']);
  347. }
  348. if (isset($data['files'])) {
  349. $data['extra']['files'] = $data['files'];
  350. unset($data['files']);
  351. }
  352. if (isset($data['line'])) {
  353. $data['extra']['route'] = $data['line_name'];
  354. unset($data['line']);
  355. unset($data['line_name']);
  356. }
  357. if (isset($data['direction'])) {
  358. $data['extra']['direction'] = $data['direction_name'];
  359. unset($data['direction']);
  360. unset($data['direction_name']);
  361. }
  362. if (isset($data['stoppoint'])) {
  363. $data['extra']['stop'] = $data['stoppoint'];
  364. unset($data['stoppoint']);
  365. }
  366. if (isset($data['station'])) {
  367. $data['extra']['stop'] = $data['station'];
  368. unset($data['station']);
  369. }
  370. if (isset($data['storage'])) {
  371. $data['extra']['stop'] = $data['storage'];
  372. unset($data['storage']);
  373. }
  374. if (isset($data['bikeNumber'])) {
  375. $data['extra']['vehiculeNumber'] = $data['bikeNumber'];
  376. unset($data['bikeNumber']);
  377. }
  378. if (isset($data['event_date']) && isset($data['event_hour'])) {
  379. $date = $data['event_date']->format('Y-m-d');
  380. $hour = $data['event_hour']->format('H:i');
  381. unset($data['event_date']);
  382. unset($data['event_hour']);
  383. $datetime = new \DateTime($date . $hour, new \DateTimeZone(date_default_timezone_get()));
  384. $data['extra']['eventDate'] = $datetime->format('c');
  385. }
  386. if (isset($data['extra']) && count($data['extra']) <= 0) {
  387. unset($data['extra']);
  388. }
  389. return $data;
  390. }
  391. public function getSubjectsInformationZou(bool $isJson = false)
  392. {
  393. $subjects = [];
  394. foreach (self::SUBJECTS_INFORMATION_ZOU as $key => $value) {
  395. $subjects[$this->translator->trans($key)] = $this->translator->trans($key, [], null, 'fr');
  396. }
  397. if (true == $isJson) {
  398. return json_encode($subjects);
  399. }
  400. return $subjects;
  401. }
  402. public function getSubjectsClaimZou(bool $isJson = false)
  403. {
  404. $subjects = [];
  405. foreach (self::SUBJECTS_CLAIM_ZOU as $key => $value) {
  406. $subjects[$this->translator->trans($key)] = $this->translator->trans($key, [], null, 'fr');
  407. }
  408. if (true == $isJson) {
  409. return json_encode($subjects);
  410. }
  411. return $subjects;
  412. }
  413. }