<?php
namespace App\Manager;
use App\Normalizer\CategoryNormalizer;
use App\Service\Internal\ContactService;
use App\Service\Internal\LineService;
use App\Service\Internal\MapService;
use Symfony\Contracts\Translation\TranslatorInterface;
class ContactManager
{
public const FORM_SUBJECT = [
'public_transport' => ['citea'],
'default_full' => ['vrd'],
'zou' => ['zou']
];
public const SUBJECTS_INFORMATION_ZOU = [
'' => '',
'form.choices.subject_information.zou_student_pass' => 'zou_student_pass',
'form.choices.subject_information.zou_student_pass_refund' => 'zou_student_pass_refund',
'form.choices.subject_information.my_trip' => 'my_trip',
'form.choices.subject_information.order_online' => 'order_online',
'form.choices.subject_information.animal_object' => 'animal_object',
'form.choices.subject_information.website' => 'website',
'form.choices.subject_information.my_zou_card' => 'my_zou_card',
'form.choices.subject_information.other' => 'other'
];
public const SUBJECTS_CLAIM_ZOU = [
'' => '',
'form.choices.subject_information.zou_student_pass' => 'zou_student_pass',
'form.choices.subject_information.zou_student_pass_refund' => 'zou_student_pass_refund',
'form.choices.subject_claim.object_loose' => 'object_loose',
'form.choices.subject_claim.reception' => 'reception',
'form.choices.subject_claim.late' => 'late',
'form.choices.subject_claim.taking' => 'taking',
'form.choices.subject_claim.confort' => 'confort',
'form.choices.subject_claim.security' => 'security',
'form.choices.subject_claim.driver' => 'driver',
'form.choices.subject_claim.accident' => 'accident'
];
public const FORM_DEFAULT_SUBJECT = 'default';
public const FORM_ZOU_SUBJECT = 'zou';
public const NB_ATTACHMENTS = 3;
/**
* @var CategoryNormalizer
*/
public $categoryNormalizer;
/**
* @var TranslatorInterface
*/
public $translator;
/**
* @var ContactService
*/
public $contactService;
/**
* @var MapService
*/
public $mapService;
/**
* @var LineService
*/
public $lineService;
/**
* @var string
*/
public $networkId;
public function __construct(
CategoryNormalizer $categoryNormalizer,
TranslatorInterface $translator,
ContactService $contactService,
MapService $mapService,
LineService $lineService,
string $networkId
) {
$this->categoryNormalizer = $categoryNormalizer;
$this->translator = $translator;
$this->contactService = $contactService;
$this->mapService = $mapService;
$this->lineService = $lineService;
$this->networkId = $networkId;
}
/**
* Allows to know the form to call according to the subject passed in parameter
*
* @param string $subject
*
* @return string
*/
public function getFormSubject(string $subject): ?string
{
if (empty($subject)) {
return self::FORM_DEFAULT_SUBJECT;
}
foreach (self::FORM_SUBJECT as $key => $value) {
if (in_array($subject, $value, false)) {
return $key;
}
}
return null;
}
/**
* Build a journey message in contact form
*/
public function buildMessageForJourney(string $journey): ?string
{
$journey = json_decode($journey, true);
if (null === $journey) {
return null;
}
$message = $this->translator->trans(
'contact.journey_new_format',
['from' => $journey['from']['value'], 'to' => $journey['to']['value']]
);
if (!empty($journey['via'])) {
$vias = array_map(function ($v) {
return $v['value'];
}, $journey['via']);
if (count($vias) > 0) {
$message .= $this->translator->trans('contact.via') . implode(', ', $vias);
}
}
$dateMessage = '';
$dt = !empty($journey['departureDateTime']) ? $journey['departureDateTime'] : $journey['arrivalDateTime'];
if (!empty($dt)) {
$dt = new \DateTime(str_replace(' ', '+', $dt));
$dateMessage = $this->translator->trans('contact.departure_at_new_format', ['dt' => $dt->format('d/m/Y H:i')]);
}
$options = '';
if (!empty($journey['modes'])) {
$modesLabels = array_map(
function ($m) {
return $this->translator->trans('mode.' . strtolower($m));
},
$journey['modes']
);
$options = $this->translator->trans('contact.modes_new_format', ['modes' => implode(', ', $modesLabels)]);
}
$message .= " $dateMessage \n\n\n\n\n\n ________________________________________________ \n";
$message .= $options . $this->translator->trans('contact.url') . $journey['currentUrl'];
return $message;
}
/**
* Build a schedule message in contact form
*/
public function buildMessageForSchedule(string $schedule): ?string
{
$schedule = json_decode($schedule, true);
if (null === $schedule) {
return null;
}
$message = $this->translator->trans('contact.schedule-message');
if (isset($schedule['islsn']) && ((isset($schedule['ismode']) && 'TOD' !== $schedule['ismode']) || !isset($schedule['ismode']))) {
$message = $this->translator->trans(
'contact.schedule_new_format',
['line' => $schedule['islsn'], 'network' => $this->networkId]
);
} elseif (isset($schedule['islsn']) && isset($schedule['ismode']) && 'TOD' === $schedule['ismode']) {
$message = $this->translator->trans(
'contact.schedule_tod_new_format',
['line' => $schedule['islsn'], 'network' => $this->networkId]
);
} elseif (isset($schedule['isspid'])) {
$message = $this->translator->trans('contact.stoppoint_schedule_new_format', ['stop' => $schedule['isspv']]);
}
$message .= " \n\n\n\n\n\n _____________________________________________________ \n ";
$message .= $this->translator->trans('contact.url') . $schedule['currentUrl'];
return $message;
}
/**
* Build categories in contact form
*/
public function buildCategories(string $type = ''): array
{
$categories = $this->contactService->getCategories($type);
$categories = $this->categoryNormalizer->normalize($categories);
return $categories;
}
/**
* Build an array of bike string stations
*/
public function buildBikeSharingStation(): array
{
$stations = [];
$bikeSharingStations = $this->mapService->getAllBikeSharingStationInfos();
if (empty($bikeSharingStations['bikeSharingStations'])) {
return [];
}
foreach ($bikeSharingStations['bikeSharingStations'] as $value) {
$stations[$value['name']] = $value['name'];
}
asort($stations, SORT_NATURAL);
return $stations;
}
/**
* Build an array of secure bike parks
*/
public function buildSecureBikePark(): array
{
$bikeParks = [];
$secureBikeParks = $this->mapService->getAllSecureBikeParkInfos();
if (empty($secureBikeParks['secureBikeParks'])) {
return [];
}
foreach ($secureBikeParks['secureBikeParks'] as $value) {
$bikeParks[$value['name']] = $value['name'];
}
asort($bikeParks, SORT_NATURAL);
return $bikeParks;
}
/**
* Build an array of lines
*/
public function buildLines(): array
{
$lines = [];
$allLines = $this->lineService->getAllLines();
if (empty($allLines['lines'])) {
return [];
}
foreach ($allLines['lines'] as $value) {
$line = ucfirst($value['lName']);
if (!empty($value['subNetwork']['name'])) {
$line .= ' - ' . $value['subNetwork']['name'];
}
$lines[$line] = $value['id'];
}
ksort($lines, SORT_NATURAL);
return $lines;
}
/**
* Build an array with the directions of a line and stoppoints by direction
*/
public function buildDirectionAndStoppoints(?string $lineId): array
{
$default = [
[
'direction' => '',
'display' => $this->translator->trans('form.choice_direction'),
'stoppoints' => [
'' => $this->translator->trans('form.choice_stoppoint'),
]
]
];
if (null === $lineId) {
return $default;
}
$directions = [];
$allDirections = $this->lineService->getLineDirections($lineId);
if (empty($allDirections)) {
return $default;
}
$i = 0;
foreach ($allDirections as $direction) {
$directions[$i]['direction'] = $direction['direction'];
$directions[$i]['display'] = $direction['display'];
foreach ($direction['stopPoints'] as $stoppoint) {
$stopName = ucfirst($stoppoint['name']);
$directions[$i]['stoppoints'][$stopName] = $stopName;
}
asort($directions[$i]['stoppoints']);
$i++;
}
$directionsDisplay = array_column($directions, 'display');
array_multisort($directionsDisplay, SORT_ASC, $directions);
return $directions;
}
/**
* Build an array of directions from an array with directions and stoppoints
*/
public function buildDirections(array $directionsWithStoppoints): array
{
$directions = [];
foreach ($directionsWithStoppoints as $value) {
$directions[$value['display']] = $value['direction'];
}
ksort($directions, SORT_NATURAL);
return $directions;
}
/**
* Build an array of stoppoints from an array directions and stoppoints
*/
public function buildStoppointsByDirection(array $directionsWithStoppoints, string $directionId): array
{
$stoppoints = [];
foreach ($directionsWithStoppoints as $direction) {
if ($directionId !== $direction['direction']) {
continue;
}
foreach ($direction['stoppoints'] as $value) {
$stoppoints[$value] = $value;
}
}
ksort($stoppoints, SORT_NATURAL);
return $stoppoints;
}
/**
* Send data post in contact form
*/
public function normalizeContactData(string $type, array $data): array
{
if (!empty($type)) {
$data['extra']['type'] = $type;
} else {
$data['extra']['type'] = 'default';
}
if (!isset($data['line_zou'])) {
unset($data['line_zou']);
} else {
$data['extra']['line'] = $data['line_zou'];
unset($data['line_zou']);
}
if (!isset($data['direction_zou'])) {
unset($data['direction_zou']);
} else {
$data['extra']['direction'] = $data['direction_zou'];
unset($data['direction_zou']);
}
if (!isset($data['stop_zou'])) {
unset($data['stop_zou']);
} else {
$data['extra']['stop'] = $data['stop_zou'];
unset($data['stop_zou']);
}
if (!isset($data['hour_zou'])) {
unset($data['hour_zou']);
} else {
$data['extra']['hour'] = $data['hour_zou'];
unset($data['hour_zou']);
}
if (!isset($data['date_zou'])) {
unset($data['date_zou']);
} else {
$data['extra']['date'] = $data['date_zou'];
unset($data['date_zou']);
}
if (isset($data['subject'])) {
$data['category'] = $this->translator->trans('form.choices.subject.' . strtolower($data['subject']));
unset($data['subject']);
}
if (isset($data['subjects'])) {
$data['title'] = $data['subjects'];
unset($data['subjects']);
}
if (isset($data['file'])) {
$data['extra']['file'] = $data['file'];
unset($data['file']);
}
if (isset($data['files'])) {
$data['extra']['files'] = $data['files'];
unset($data['files']);
}
if (isset($data['line'])) {
$data['extra']['route'] = $data['line_name'];
unset($data['line']);
unset($data['line_name']);
}
if (isset($data['direction'])) {
$data['extra']['direction'] = $data['direction_name'];
unset($data['direction']);
unset($data['direction_name']);
}
if (isset($data['stoppoint'])) {
$data['extra']['stop'] = $data['stoppoint'];
unset($data['stoppoint']);
}
if (isset($data['station'])) {
$data['extra']['stop'] = $data['station'];
unset($data['station']);
}
if (isset($data['storage'])) {
$data['extra']['stop'] = $data['storage'];
unset($data['storage']);
}
if (isset($data['bikeNumber'])) {
$data['extra']['vehiculeNumber'] = $data['bikeNumber'];
unset($data['bikeNumber']);
}
if (isset($data['event_date']) && isset($data['event_hour'])) {
$date = $data['event_date']->format('Y-m-d');
$hour = $data['event_hour']->format('H:i');
unset($data['event_date']);
unset($data['event_hour']);
$datetime = new \DateTime($date . $hour, new \DateTimeZone(date_default_timezone_get()));
$data['extra']['eventDate'] = $datetime->format('c');
}
if (isset($data['extra']) && count($data['extra']) <= 0) {
unset($data['extra']);
}
return $data;
}
public function getSubjectsInformationZou(bool $isJson = false)
{
$subjects = [];
foreach (self::SUBJECTS_INFORMATION_ZOU as $key => $value) {
$subjects[$this->translator->trans($key)] = $this->translator->trans($key, [], null, 'fr');
}
if (true == $isJson) {
return json_encode($subjects);
}
return $subjects;
}
public function getSubjectsClaimZou(bool $isJson = false)
{
$subjects = [];
foreach (self::SUBJECTS_CLAIM_ZOU as $key => $value) {
$subjects[$this->translator->trans($key)] = $this->translator->trans($key, [], null, 'fr');
}
if (true == $isJson) {
return json_encode($subjects);
}
return $subjects;
}
}