| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Export;
- use App\ApiResources\Export\ExportRequest;
- use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
- use App\Service\Export\Encoder\EncoderHandler;
- use App\Service\Export\Model\LicenceCmf;
- use App\Enum\Access\FunctionEnum;
- use App\Repository\Access\AccessRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Service\Storage\TemporaryFileStorage;
- use App\Service\Utils\Path;
- use Twig\Environment;
- /**
- * Exporte la licence CMF de la structure ou du ou des access, au format demandé
- */
- class LicenceCmfExporter implements ExporterInterface
- {
- const CMF_ID = 12097;
- /**
- * La couleur de la carte de licence change chaque année, de manière cyclique
- */
- const LICENCE_CMF_COLOR_START_YEAR = "2020";
- const LICENCE_CMF_COLOR = [0 => '931572', 1 => 'C2981A', 2 => '003882', 3 => '27AAE1', 4 => '2BB673'];
- public function __construct(
- private OrganizationRepository $organizationRepository,
- private AccessRepository $accessRepository,
- private Environment $twig,
- private EncoderHandler $encoderHandler
- )
- {}
- public function support($exportRequest): bool
- {
- return $exportRequest instanceof LicenceCmfOrganizationER;
- }
- /**
- *
- */
- public function export($exportRequest)
- {
- $organization = $exportRequest->getRequester()->getOrganization();
- $currentYear = (int)date('Y');
- $model = new LicenceCmf();
- $model->setId($organization->getId());
- $model->setYear($currentYear);
- $model->setOrganizationName($organization->getName());
- $model->setOrganizationIdentifier($organization->getIdentifier());
- $parentFederation = $organization->getNetworkOrganizations()[0]->getParent();
- $model->setFederationName($parentFederation->getName());
- $model->setColor(
- $this->getLicenceColor($currentYear)
- );
- $logoId = $organization->getLogo()?->getId();
- if ($logoId) {
- $model->setLogoUri(
- rtrim($_SERVER['INTERNAL_FILES_DOWNLOAD_URI'], '/') . '/' . $logoId
- );
- }
- $presidents = $this->accessRepository->findByOrganizationAndMission($organization, FunctionEnum::PRESIDENT()->getValue());
- if (count($presidents) > 0) {
- $president = $presidents[0]->getPerson();
- $model->setPersonId($president->getId());
- $model->setPersonGender($president->getGender());
- $model->setPersonFirstName($president->getGivenName());
- $model->setPersonLastName($president->getName());
- }
- $cmf = $this->organizationRepository->find(self::CMF_ID);
- $qrCodeId = $cmf->getParameters()?->getQrCode()?->getId();
- if ($qrCodeId) {
- $model->setQrCodeUri(
- rtrim($_SERVER['INTERNAL_FILES_DOWNLOAD_URI'], '/') . '/' . $qrCodeId
- );
- }
- $html = $this->twig->render(
- '@templates/export/licence_cmf.html.twig',
- [
- 'models' => [$model],
- 'isOrganizationLicence' => true
- ]
- );
- $filename = 'licence_cmf_' . $currentYear . '.pdf';
- $tempDir = TemporaryFileStorage::makeStorageDirFor($organization->getId());
- $tempPath = Path::join($tempDir, $filename);
- if (file_exists($tempPath)) {
- unlink($tempPath);
- }
- $encoder = $this->encoderHandler->getEncoderFor($exportRequest);
- $encoder->encodeToFile($html, $tempPath);
- }
- /**
- * Retourne la couleur de licence pour l'année donnée
- * @param int $year
- * @return string
- */
- protected function getLicenceColor(int $year): string {
- if (!self::LICENCE_CMF_COLOR_START_YEAR > $year) {
- return self::LICENCE_CMF_COLOR[0];
- }
- return self::LICENCE_CMF_COLOR[($year - self::LICENCE_CMF_COLOR_START_YEAR) % count(self::LICENCE_CMF_COLOR)];
- }
- }
|