'Champs obligatoire'], [['id_distribution'],'integer'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'subject' => 'Sujet', 'message' => 'Message', 'id_distribution' => 'Distribution' ]; } /** * Envoie un email aux utilisateurs d'un point de vente ou à tous les * utilisateurs d'un producteur. * * @param integer $idPointSale ID du point de vente */ public function sendEmail($usersArray) { $producer = Producer::getCurrent() ; $mj = new \Mailjet\Client( $producer->getApiKeyMailjet('public'), $producer->getApiKeyMailjet('private'), true, ['version' => 'v3.1'] ); $body = ['Messages' => []] ; $messageAutoText = '' ; $messageAutoHtml = '' ; if($this->id_distribution) { $messageAutoText = ' ' ; $messageAutoHtml = '

' ; $distribution = Distribution::searchOne(['id' => $this->id_distribution]) ; if($distribution) { $linkOrder = Yii::$app->urlManagerProducer->createAbsoluteUrl(['order/order','slug_producer' => Producer::getCurrent()->slug, 'date' => $distribution->date]) ; $dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ; $messageAutoHtml .= 'Passer ma commande du '.$dateOrder.'' ; $messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' : '.$linkOrder ; $productsArray = Product::find() ->where([ 'id_producer' => Producer::getId(), ]) ->innerJoinWith(['productDistribution' => function($query) use($distribution) { $query->andOnCondition([ 'product_distribution.id_distribution' => $distribution->id, 'product_distribution.active' => 1 ]); }]) ->orderBy('product.name ASC') ->all(); if(count($productsArray) > 1) { $messageAutoHtml .= '

Produits disponibles :
' ; } } } foreach($usersArray as $user) { $body['Messages'][] = [ 'From' => [ 'Email' => $producer->slug.'@opendistrib.net', 'Name' => $producer->name ], 'To' => [ [ 'Email' => $user['email'], 'Name' => $user['name'].' '.$user['lastname'] ] ], 'Subject' => $this->subject, 'TextPart' => $this->message.$messageAutoText, 'HTMLPart' => nl2br($this->message).$messageAutoHtml ] ; if(count($body['Messages']) == 50) { $response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]); $body['Messages'] = [] ; } } if(count($body['Messages']) > 0) { $response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]); } $response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]); return $response->success() ; } }