Commit adfa59c1 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Modification suite réunion

parent a6582098
Loading
Loading
Loading
Loading
Loading
+12 −3
Changes for config/unicaen-mail.local.php.dist: 12 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -2,8 +2,17 @@
/**
 * Configuration locale du module UnicaenMail.
 */

use UnicaenMail\Entity\Db\Mail;

return [
    'unicaen-mail' => [

        /**
         * Classe de entité
         **/
        'mail_entity_class' => Mail::class,

        /**
         * Options concernant l'envoi de mail par l'application
         */
@@ -20,8 +29,8 @@ return [
        /**
         * Configuration de l'expéditeur
         */
        'sigle' => 'XXX',
        'expediteur' => 'XXX : XXXX XX XXXXXXX',
        'email' => 'ne-pas-repondre@XXXX.XX'
        'subject_prefix' => 'XXX',
        'from_name' => 'XXX : XXXX XX XXXXXXX',
        'from_email' => 'ne-pas-repondre@XXXX.XX'
    ],
];
 No newline at end of file
+8 −9
Changes for readme.md: 8 added lines, 9 removed lines.
Original line number Diff line number Diff line
Module Unicaen Renderer
Module Unicaen Mail
=======================
------------------------

@@ -19,7 +19,7 @@ Mail sont stocké en base avec les informations suivantes :
* date d'envoi
* status
* sujet/corps
* meta-information (entité liée + template utilisé)
* mots clefs

Mail service est en chage de faire l'envoi et l'enregistrement en base.

@@ -30,10 +30,9 @@ $mail = $this->getMailService()->sendMail('john.doe@aol.com', 'Reunion important

Exemple d'utilisation avec utilisation d'un template + entité liée
```php
$contenu = $this->getContenuService()->generateContenu('MON TEMPLATE', ['VAR1' => $var1, 'VAR2' => $var2, ...]);
$mail = $this->getMailService()->sendMail('john.doe@aol.com', $contenu->getSujet(), $contenu->getCorps());
$mail->setEntity($campagne);
$mail->setTemplateCode($contenu->getTemplate()->getCode());
$rendu = $this->getRenduService()->generateRenduByTemplateCode('MON TEMPLATE', ['VAR1' => $var1, 'VAR2' => $var2, ...]);
$mail = $this->getMailService()->sendMail('john.doe@aol.com', $rendu->getSujet(), $rendu->getCorps());
$mail->addMotsClefs(['MON_ENTITE', 'MON_TEMPLATE']);
$this->getMailService()->update($mail);
```

@@ -82,9 +81,9 @@ return [
        /**
         * Configuration de l'expéditeur
         */
        'sigle' => 'XXX',
        'expediteur' => 'XXX : XXXX XX XXXXXXX',
        'email' => 'ne-pas-repondre@XXXX.XX'
        'subjet_prefix' => 'XXX',
        'from_name' => 'XXX : XXXX XX XXXXXXX',
        'from_email' => 'ne-pas-repondre@XXXX.XX'
    ],
];
```
+37 −54
Changes for src/UnicaenMail/Entity/Db/Mail.php: 37 added lines, 54 removed lines.
Original line number Diff line number Diff line
@@ -9,17 +9,14 @@ class Mail {
    const PENDING = 'PENDING';
    const SUCCESS = 'SUCCESS';
    const FAILED  = 'FAILED';
    const MOTCLEF_SEPARATEUR = '||';

    /** @var integer */
    private $id;
    /** @var DateTime */
    private $dateEnvoi;
    /** @var int */
    private $statusEnvoi;
    /** @var string */
    private $destinataires;
    /** @var boolean */
    private $redirection;
    /** @var string|null */
    private $destinatairesInitials;

    /** Contenu *******************************************************************************************************/

@@ -27,15 +24,18 @@ class Mail {
    private $sujet;
    /** @var string */
    private $corps;
    /** @var int|null */
    private $templateCode;

    /** Attachement : Marquage pour faire des lien avec des objets  ***************************************************/

    /** @var string|null */
    private $entityClass;
    private $motsClefs;

    /** STATUT ********************************************************************************************************/

    /** @var DateTime */
    private $dateEnvoi;
    /** @var int */
    private $statusEnvoi;
    /** @var string|null */
    private $entityId;
    private $log;


    /**
@@ -101,23 +101,31 @@ class Mail {
    }

    /**
     * @return bool
     * @return string|null
     */
    public function isRedirection() : bool
    public function getDestinatairesInitials(): ?string
    {
        return $this->redirection;
        return $this->destinatairesInitials;
    }

    /**
     * @param bool|null $redirection
     * @param string|null $destinatiaresInitials
     * @return Mail
     */
    public function setRedir(?bool $redirection) : Mail
    public function setDestinatairesInitials(?string $destinatairesInitials): Mail
    {
        $this->redirection = $redirection;
        $this->destinatairesInitials = $destinatairesInitials;
        return $this;
    }

    /**
     * @return bool
     */
    public function isRedirection() : bool
    {
        return $this->destinatairesInitials !== null;
    }

    /**
     * @return string|null
     */
@@ -155,64 +163,39 @@ class Mail {
    }

    /**
     * @return string|null
     */
    public function getTemplateCode(): ?string
    {
        return $this->templateCode;
    }

    /**
     * @param string|null $templateCode
     * @return Mail
     */
    public function setTemplateCode(?string $templateCode): Mail
    {
        $this->templateCode = $templateCode;
        return $this;
    }

    /**
     * @return string|null
     * @return string
     */
    public function getEntityClass(): ?string
    public function getMotsClefs() : string
    {
        return $this->entityClass;
        return $this->motsClefs;
    }

    /**
     * @param string|null $entityClass
     * @param array $motsClefs
     * @return Mail
     */
    public function setEntityClass(?string $entityClass): Mail
    public function setMotsClefs(array $motsClefs) : Mail
    {
        $this->entityClass = $entityClass;
        $this->motsClefs = implode(Mail::MOTCLEF_SEPARATEUR, $motsClefs);
        return $this;
    }

    /**
     * @return string|null
     */
    public function getEntityId(): ?string
    public function getLog(): ?string
    {
        return $this->entityId;
        return $this->log;
    }

    /**
     * @param string|null $entityId
     * @param string|null $log
     * @return Mail
     */
    public function setEntityId(?string $entityId): Mail
    public function setLog(?string $log): Mail
    {
        $this->entityId = $entityId;
        $this->log = $log;
        return $this;
    }

    public function setEntity(Object $entity)
    {
        $this->setEntityClass(get_class($entity));
        $this->setEntityId($entity->getId());
    }


}
 No newline at end of file
+5 −6
Changes for src/UnicaenMail/Entity/Db/Mapping/UnicaenMail.Entity.Db.Mail.dcm.xml: 5 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -9,12 +9,11 @@
        <field name="dateEnvoi"             type="datetime"                   column="date_envoi"           nullable="false"/>
        <field name="statusEnvoi"           type="string"   length="256"      column="status_envoi"         nullable="false"  />
        <field name="destinataires"         type="string"   length="256"      column="destinataires"        nullable="false"  />
        <field name="redirection"       type="boolean"                    column="redirection"          nullable="false"  />
        <field name="sujet"             type="string"   length="256"      column="sujet"                nullable="true"  />
        <field name="corps"             type="string"   length="2048"     column="corps"                nullable="true"  />
        <field name="destinatairesInitials" type="string"   length="256"      column="destinataires_initials" nullable="false"  />
        <field name="sujet"                 type="string"   length="9999"     column="sujet"                nullable="true"  />
        <field name="corps"                 type="string"   length="9999"     column="corps"                nullable="true"  />
        <field name="motsClefs"             type="string"   length="9999"     column="mots_clefs"           nullable="true" />

        <field name="templateCode"      type="string"   length="256"      column="template_code"        nullable="true" />
        <field name="entityClass"       type="string"   length="1024"     column="entity_class"         nullable="true"  />
        <field name="entityId"          type="string"   length="256"      column="entity_id"            nullable="true" />
        <field name="log"                   type="string"   length="9999"     column="log"                  nullable="true"  />
    </entity>
</doctrine-mapping>
+47 −29
Changes for src/UnicaenMail/Service/Mail/MailService.php: 47 added lines, 29 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,24 @@ use Zend\Mvc\Controller\AbstractActionController;
class MailService {
    use EntityManagerAwareTrait;

    /** @var string */
    private $entityClass;

    /**
     * @param string $entityClass
     * @return MailService
     */
    public function setEntityClass(string $entityClass): MailService
    {
        $this->entityClass = $entityClass;
        return $this;
    }

    public function createMailEntity() : Object
    {
        $entity = new $this->entityClass();
        return $entity;
    }

    /** @var TransportInterface */
    private $transport;
@@ -26,18 +44,18 @@ class MailService {
    private $doNotSend;

    /** info sur l'expediteur */
    private $expediteurNom;
    private $expediteurMail;
    private $sigle;
    private $fromName;
    private $fromEmail;
    private $subjectPrefix;

    public function __construct(TransportInterface $transport, $redirectTo, $doNotSend, $expediteurNom, $expediteurMail, $sigle)
    public function __construct(TransportInterface $transport, $redirectTo, $doNotSend, $fromName, $fromEmail, $subjectPrefix)
    {
        $this->transport = $transport;
        $this->redirectTo = $redirectTo;
        $this->doNotSend = $doNotSend;
        $this->expediteurNom=$expediteurNom;
        $this->expediteurMail=$expediteurMail;
        $this->sigle=$sigle;
        $this->fromName=$fromName;
        $this->fromEmail=$fromEmail;
        $this->subjectPrefix=$subjectPrefix;
    }

    /** GESTION DES ENTITES *******************************************************************************************/
@@ -93,7 +111,7 @@ class MailService {
     */
    public function createQueryBuilder() : QueryBuilder
    {
        $qb = $this->getEntityManager()->getRepository(Mail::class)->createQueryBuilder('mail');
        $qb = $this->getEntityManager()->getRepository($this->entityClass)->createQueryBuilder('mail');
        return $qb;
    }

@@ -110,13 +128,11 @@ class MailService {
        return $result;
    }

    public function getMailsByEntity(string $entityClass, string $entityId, string $champ='dateEnvoi', string $ordre = 'DESC') : array
    public function getMailsByMotClef(string $motClef, string $champ='dateEnvoi', string $ordre = 'DESC') : array
    {
        $qb = $this->createQueryBuilder()
            ->andWhere('mail.entityClass = :entityClass')
            ->setParameter('entityClass', $entityClass)
            ->andWhere('mail.entityId = :entityId')
            ->setParameter('entityId', $entityId)
            ->andWhere('mail.motsClefs like :motclef')
            ->setParameter('motclef', '%'. $motClef . '%')
            ->orderBy('mail.' . $champ, $ordre);
        $result = $qb->getQuery()->getResult();
        return $result;
@@ -159,33 +175,37 @@ class MailService {
    {
        $message = (new Message())->setEncoding('UTF-8');

        $message->setFrom($this->expediteurMail, $this->expediteurNom);
        $message->setFrom($this->fromEmail, $this->fromName);
        if (!is_array($to)) $to = [$to];
        if ($this->doNotSend) {
            $message->addTo($this->redirectTo);
        } else {
            $to = array_unique($to);
            $message->addTo($to);
        }

        $mail = new Mail();
        /** @var Mail $mail */
        $mail = $this->createMailEntity();
        $mail->setDateEnvoi(new DateTime());
        $mail->setStatusEnvoi(Mail::PENDING);
        $mail->setDestinataires(is_array($to) ? implode(",", $to) : $to);
        $mail->setRedir($this->doNotSend);
        if ($this->doNotSend) {
            $mail->setDestinatairesInitials(implode(",",$to));
            $mail->setDestinataires(implode(",",$this->redirectTo));
        } else {
            $mail->setDestinataires(implode(",",$to));
        }
        $mail->setSujet($subject);
        $mail->setCorps($texte);
        $this->create($mail);


        $sujet = '['.$this->sigle.'] ' . $subject;


        $sujet = '['.$this->subjectPrefix.'] ' . $subject;
        if ($this->doNotSend) {
            $sujet .= ' {REDIR}';
        }
        $message->setTo( (!is_array($mail->getDestinataires())) ? [ $mail->getDestinataires()] : $mail->getDestinataires());
        $message->setSubject($sujet);


        $texte = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application ".$this->sigle.". </i></p>" . $texte;
        $texte = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application ".$this->subjectPrefix.". </i></p>" . $texte;
        $mail->setCorps($texte);
        $this->create($mail);


        if ($this->doNotSend) {
            $texte .= "<br/><br/><hr/><br/>";
@@ -233,9 +253,7 @@ class MailService {
    {
        //todo les pieces jointes
        $nMail = $this->sendMail($mail->getDestinataires(), $mail->getSujet(), $mail->getCorps());
        $nMail->setEntityClass($mail->getEntityClass());
        $nMail->setEntityId($mail->getEntityId());
        $nMail->setTemplateCode($mail->getTemplateCode());
        $nMail->setMotsClefs(explode(Mail::MOTCLEF_SEPARATEUR, $mail->getMotsClefs()));
        $this->update($nMail);
        return $mail;

Loading