|
- <?php
-
- namespace console\commands;
-
- use domain\Communication\AutomaticEmail\AutomaticEmailModule;
- use domain\Communication\Email\Email;
- use domain\Communication\Email\EmailModule;
- use domain\Producer\Producer\ProducerModule;
- use yii\console\Controller;
-
- class AutomaticEmailController extends Controller
- {
- // ./yii automatic-email/send
- public function actionSend()
- {
- $dayToday = date('N');
- $currentHour = date('G');
- $automaticEmailModule = AutomaticEmailModule::getInstance();
- $producerModule = ProducerModule::getInstance();
- $emailModule = EmailModule::getInstance();
-
- $producersArray = $producerModule->getRepository()->findProducers();
- foreach($producersArray as $producer) {
- \Yii::$app->logic->setProducerContext($producer);
- $automaticEmailsArray = $automaticEmailModule->getRepository()->findAutomaticEmails();
- foreach($automaticEmailsArray as $automaticEmail) {
- if($automaticEmail->isEnabled()) {
- $distribution = $automaticEmailModule->getResolver()->getMatchedDistribution($automaticEmail);
- if(($distribution || $automaticEmail->getSendingDay() == $dayToday) && $automaticEmail->getSendingHour() == $currentHour) {
- $email = $automaticEmailModule->getManager()->createEmailFromAutomaticEmail($automaticEmail, $distribution);
- $usersArray = $emailModule->getContactListResolver()->search(
- $producer,
- Email::TYPE_ORDER_TAKING,
- $distribution,
- $automaticEmail->getPointSale()
- );
- $emailModule->getBulkMailer()->sendEmail($email, $usersArray);
- echo 'Email automatique "'.$automaticEmail->getSubject().'" envoyé à '.count($usersArray)." utilisateur(s)\n";
- }
- }
- }
- }
- }
- }
-
- ?>
|