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. $dt = !empty($journey['departureDateTime']) ? $journey['departureDateTime'] : $journey['arrivalDateTime'];
  123. if (!empty($dt)) {
  124. $dt = new \DateTime(str_replace(' ', '+', $dt));
  125. $dateMessage = $this->translator->trans('contact.departure_at_new_format', ['dt' => $dt->format('d/m/Y H:i')]);
  126. }
  127. $options = '';
  128. if (!empty($journey['modes'])) {
  129. $modesLabels = array_map(
  130. function ($m) {
  131. return $this->translator->trans('mode.' . strtolower($m));
  132. },
  133. $journey['modes']
  134. );
  135. $options = $this->translator->trans('contact.modes_new_format', ['modes' => implode(', ', $modesLabels)]);
  136. }
  137. $message .= " $dateMessage \n\n\n\n\n\n ________________________________________________ \n";
  138. $message .= $options . $this->translator->trans('contact.url') . $journey['currentUrl'];
  139. return $message;
  140. }
  141. /**
  142. * Build a schedule message in contact form
  143. */
  144. public function buildMessageForSchedule(string $schedule): ?string
  145. {
  146. $schedule = json_decode($schedule, true);
  147. if (null === $schedule) {
  148. return null;
  149. }
  150. $message = $this->translator->trans('contact.schedule-message');
  151. if (isset($schedule['islsn']) && ((isset($schedule['ismode']) && 'TOD' !== $schedule['ismode']) || !isset($schedule['ismode']))) {
  152. $message = $this->translator->trans(
  153. 'contact.schedule_new_format',
  154. ['line' => $schedule['islsn'], 'network' => $this->networkId]
  155. );
  156. } elseif (isset($schedule['islsn']) && isset($schedule['ismode']) && 'TOD' === $schedule['ismode']) {
  157. $message = $this->translator->trans(
  158. 'contact.schedule_tod_new_format',
  159. ['line' => $schedule['islsn'], 'network' => $this->networkId]
  160. );
  161. } elseif (isset($schedule['isspid'])) {
  162. $message = $this->translator->trans('contact.stoppoint_schedule_new_format', ['stop' => $schedule['isspv']]);
  163. }
  164. $message .= " \n\n\n\n\n\n _____________________________________________________ \n ";
  165. $message .= $this->translator->trans('contact.url') . $schedule['currentUrl'];
  166. return $message;
  167. }
  168. /**
  169. * Build categories in contact form
  170. */
  171. public function buildCategories(string $type = ''): array
  172. {
  173. $categories = $this->contactService->getCategories($type);
  174. $categories = $this->categoryNormalizer->normalize($categories);
  175. return $categories;
  176. }
  177. /**
  178. * Build an array of bike string stations
  179. */
  180. public function buildBikeSharingStation(): array
  181. {
  182. $stations = [];
  183. $bikeSharingStations = $this->mapService->getAllBikeSharingStationInfos();
  184. if (empty($bikeSharingStations['bikeSharingStations'])) {
  185. return [];
  186. }
  187. foreach ($bikeSharingStations['bikeSharingStations'] as $value) {
  188. $stations[$value['name']] = $value['name'];
  189. }
  190. asort($stations, SORT_NATURAL);
  191. return $stations;
  192. }
  193. /**
  194. * Build an array of secure bike parks
  195. */
  196. public function buildSecureBikePark(): array
  197. {
  198. $bikeParks = [];
  199. $secureBikeParks = $this->mapService->getAllSecureBikeParkInfos();
  200. if (empty($secureBikeParks['secureBikeParks'])) {
  201. return [];
  202. }
  203. foreach ($secureBikeParks['secureBikeParks'] as $value) {
  204. $bikeParks[$value['name']] = $value['name'];
  205. }
  206. asort($bikeParks, SORT_NATURAL);
  207. return $bikeParks;
  208. }
  209. /**
  210. * Build an array of lines
  211. */
  212. public function buildLines(): array
  213. {
  214. $lines = [];
  215. $allLines = $this->lineService->getAllLines();
  216. if (empty($allLines['lines'])) {
  217. return [];
  218. }
  219. foreach ($allLines['lines'] as $value) {
  220. $line = ucfirst($value['lName']);
  221. if (!empty($value['subNetwork']['name'])) {
  222. $line .= ' - ' . $value['subNetwork']['name'];
  223. }
  224. $lines[$line] = $value['id'];
  225. }
  226. ksort($lines, SORT_NATURAL);
  227. return $lines;
  228. }
  229. /**
  230. * Build an array with the directions of a line and stoppoints by direction
  231. */
  232. public function buildDirectionAndStoppoints(?string $lineId): array
  233. {
  234. $default = [
  235. [
  236. 'direction' => '',
  237. 'display' => $this->translator->trans('form.choice_direction'),
  238. 'stoppoints' => [
  239. '' => $this->translator->trans('form.choice_stoppoint'),
  240. ]
  241. ]
  242. ];
  243. if (null === $lineId) {
  244. return $default;
  245. }
  246. $directions = [];
  247. $allDirections = $this->lineService->getLineDirections($lineId);
  248. if (empty($allDirections)) {
  249. return $default;
  250. }
  251. $i = 0;
  252. foreach ($allDirections as $direction) {
  253. $directions[$i]['direction'] = $direction['direction'];
  254. $directions[$i]['display'] = $direction['display'];
  255. foreach ($direction['stopPoints'] as $stoppoint) {
  256. $stopName = ucfirst($stoppoint['name']);
  257. $directions[$i]['stoppoints'][$stopName] = $stopName;
  258. }
  259. asort($directions[$i]['stoppoints']);
  260. $i++;
  261. }
  262. $directionsDisplay = array_column($directions, 'display');
  263. array_multisort($directionsDisplay, SORT_ASC, $directions);
  264. return $directions;
  265. }
  266. /**
  267. * Build an array of directions from an array with directions and stoppoints
  268. */
  269. public function buildDirections(array $directionsWithStoppoints): array
  270. {
  271. $directions = [];
  272. foreach ($directionsWithStoppoints as $value) {
  273. $directions[$value['display']] = $value['direction'];
  274. }
  275. ksort($directions, SORT_NATURAL);
  276. return $directions;
  277. }
  278. /**
  279. * Build an array of stoppoints from an array directions and stoppoints
  280. */
  281. public function buildStoppointsByDirection(array $directionsWithStoppoints, string $directionId): array
  282. {
  283. $stoppoints = [];
  284. foreach ($directionsWithStoppoints as $direction) {
  285. if ($directionId !== $direction['direction']) {
  286. continue;
  287. }
  288. foreach ($direction['stoppoints'] as $value) {
  289. $stoppoints[$value] = $value;
  290. }
  291. }
  292. ksort($stoppoints, SORT_NATURAL);
  293. return $stoppoints;
  294. }
  295. /**
  296. * Send data post in contact form
  297. */
  298. public function normalizeContactData(string $type, array $data): array
  299. {
  300. if (!empty($type)) {
  301. $data['extra']['type'] = $type;
  302. } else {
  303. $data['extra']['type'] = 'default';
  304. }
  305. if (!isset($data['line_zou'])) {
  306. unset($data['line_zou']);
  307. } else {
  308. $data['extra']['line'] = $data['line_zou'];
  309. unset($data['line_zou']);
  310. }
  311. if (!isset($data['direction_zou'])) {
  312. unset($data['direction_zou']);
  313. } else {
  314. $data['extra']['direction'] = $data['direction_zou'];
  315. unset($data['direction_zou']);
  316. }
  317. if (!isset($data['stop_zou'])) {
  318. unset($data['stop_zou']);
  319. } else {
  320. $data['extra']['stop'] = $data['stop_zou'];
  321. unset($data['stop_zou']);
  322. }
  323. if (!isset($data['hour_zou'])) {
  324. unset($data['hour_zou']);
  325. } else {
  326. $data['extra']['hour'] = $data['hour_zou'];
  327. unset($data['hour_zou']);
  328. }
  329. if (!isset($data['date_zou'])) {
  330. unset($data['date_zou']);
  331. } else {
  332. $data['extra']['date'] = $data['date_zou'];
  333. unset($data['date_zou']);
  334. }
  335. if (isset($data['subject'])) {
  336. $data['category'] = $this->translator->trans('form.choices.subject.' . strtolower($data['subject']));
  337. unset($data['subject']);
  338. }
  339. if (isset($data['subjects'])) {
  340. $data['title'] = $data['subjects'];
  341. unset($data['subjects']);
  342. }
  343. if (isset($data['file'])) {
  344. $data['extra']['file'] = $data['file'];
  345. unset($data['file']);
  346. }
  347. if (isset($data['files'])) {
  348. $data['extra']['files'] = $data['files'];
  349. unset($data['files']);
  350. }
  351. if (isset($data['line'])) {
  352. $data['extra']['route'] = $data['line_name'];
  353. unset($data['line']);
  354. unset($data['line_name']);
  355. }
  356. if (isset($data['direction'])) {
  357. $data['extra']['direction'] = $data['direction_name'];
  358. unset($data['direction']);
  359. unset($data['direction_name']);
  360. }
  361. if (isset($data['stoppoint'])) {
  362. $data['extra']['stop'] = $data['stoppoint'];
  363. unset($data['stoppoint']);
  364. }
  365. if (isset($data['station'])) {
  366. $data['extra']['stop'] = $data['station'];
  367. unset($data['station']);
  368. }
  369. if (isset($data['storage'])) {
  370. $data['extra']['stop'] = $data['storage'];
  371. unset($data['storage']);
  372. }
  373. if (isset($data['bikeNumber'])) {
  374. $data['extra']['vehiculeNumber'] = $data['bikeNumber'];
  375. unset($data['bikeNumber']);
  376. }
  377. if (isset($data['event_date']) && isset($data['event_hour'])) {
  378. $date = $data['event_date']->format('Y-m-d');
  379. $hour = $data['event_hour']->format('H:i');
  380. unset($data['event_date']);
  381. unset($data['event_hour']);
  382. $datetime = new \DateTime($date . $hour, new \DateTimeZone(date_default_timezone_get()));
  383. $data['extra']['eventDate'] = $datetime->format('c');
  384. }
  385. if (isset($data['extra']) && count($data['extra']) <= 0) {
  386. unset($data['extra']);
  387. }
  388. return $data;
  389. }
  390. public function getSubjectsInformationZou(bool $isJson = false)
  391. {
  392. $subjects = [];
  393. foreach (self::SUBJECTS_INFORMATION_ZOU as $key => $value) {
  394. $subjects[$this->translator->trans($key)] = $this->translator->trans($key, [], null, 'fr');
  395. }
  396. if (true == $isJson) {
  397. return json_encode($subjects);
  398. }
  399. return $subjects;
  400. }
  401. public function getSubjectsClaimZou(bool $isJson = false)
  402. {
  403. $subjects = [];
  404. foreach (self::SUBJECTS_CLAIM_ZOU as $key => $value) {
  405. $subjects[$this->translator->trans($key)] = $this->translator->trans($key, [], null, 'fr');
  406. }
  407. if (true == $isJson) {
  408. return json_encode($subjects);
  409. }
  410. return $subjects;
  411. }
  412. }