|
- <?php
-
-
-
- namespace backend\controllers;
-
- use backend\models\MailForm ;
- use yii\web\NotFoundHttpException ;
- use common\models\User ;
-
-
- class CommunicateAdminController extends BackendController
- {
-
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- ],
- ],
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::getCurrentStatus() == USER::STATUS_ADMIN;
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex($section = 'producers')
- {
- if($section == 'producers') {
- $producers = Producer::find()->where(['producer.active' => 1])->with(['contact'])->all() ;
- $usersArray = [];
- $users = [] ;
- foreach ($producers as $producer) {
- if (isset($producer->contact) && is_array($producer->contact)) {
- foreach($producer->contact as $contact) {
- $usersArray[] = $contact->email ;
- $users[] = [
- 'email' => $contact->email,
- 'name' => $contact->name,
- 'lastname' => $contact->lastname,
- ] ;
- }
- }
- }
- }
- elseif($section == 'users') {
- $users = User::find()
- ->where([
- 'user.status' => User::STATUS_ACTIVE
- ])
- ->all() ;
- $usersArray = [];
- foreach ($users as $user) {
- if (isset($user['email']) && strlen($user['email'])) {
- $usersArray[] = $user['email'];
- }
- }
- }
- else {
- throw new NotFoundHttpException('Requête incorrecte.');
- }
-
- $mailForm = new MailForm() ;
- if ($mailForm->load(Yii::$app->request->post()) && $mailForm->validate()) {
- $resultSendEmail = $mailForm->sendEmail($users, false) ;
- if($resultSendEmail) {
- Yii::$app->getSession()->setFlash('success', 'Votre email a bien été envoyé.');
- }
- else {
- Yii::$app->getSession()->setFlash('error', 'Un problème est survenu lors de l\'envoi de votre email.');
- }
- $mailForm->subject = '' ;
- $mailForm->message = '' ;
- }
-
- return $this->render('index', [
- 'section' => $section,
- 'usersArray' => $usersArray,
- 'mailForm' => $mailForm,
- ]);
- }
-
- }
|