Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

185 lines
4.9KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\models\User;
  5. use backend\models\MailForm;
  6. use yii\data\ActiveDataProvider;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. /**
  12. * UserController implements the CRUD actions for User model.
  13. */
  14. class UserController extends Controller
  15. {
  16. public function behaviors()
  17. {
  18. return [
  19. 'verbs' => [
  20. 'class' => VerbFilter::className(),
  21. 'actions' => [
  22. 'delete' => ['post'],
  23. ],
  24. ],
  25. 'access' => [
  26. 'class' => AccessControl::className(),
  27. 'rules' => [
  28. [
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. 'matchCallback' => function ($rule, $action) {
  32. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  33. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  34. }
  35. ]
  36. ],
  37. ],
  38. ];
  39. }
  40. /**
  41. * Lists all User models.
  42. * @return mixed
  43. */
  44. public function actionIndex()
  45. {
  46. $dataProvider = new ActiveDataProvider([
  47. 'query' =>
  48. (new \yii\db\Query())
  49. ->select('*')
  50. ->from('user, user_boulangerie')
  51. ->where('user.id = user_boulangerie.id_user')
  52. ->andWhere('user_boulangerie.id_boulangerie = '.Yii::$app->user->identity->id)
  53. ]);
  54. return $this->render('index', [
  55. 'dataProvider' => $dataProvider,
  56. ]);
  57. }
  58. /**
  59. * Displays a single User model.
  60. * @param integer $id
  61. * @return mixed
  62. */
  63. public function actionView($id)
  64. {
  65. return $this->render('view', [
  66. 'model' => $this->findModel($id),
  67. ]);
  68. }
  69. /**
  70. * Creates a new User model.
  71. * If creation is successful, the browser will be redirected to the 'view' page.
  72. * @return mixed
  73. */
  74. public function actionCreate()
  75. {
  76. $model = new User();
  77. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  78. return $this->redirect(['view', 'id' => $model->id]);
  79. } else {
  80. return $this->render('create', [
  81. 'model' => $model,
  82. ]);
  83. }
  84. }
  85. /**
  86. * Updates an existing User model.
  87. * If update is successful, the browser will be redirected to the 'view' page.
  88. * @param integer $id
  89. * @return mixed
  90. */
  91. public function actionUpdate($id)
  92. {
  93. $model = $this->findModel($id);
  94. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  95. return $this->redirect(['view', 'id' => $model->id]);
  96. } else {
  97. return $this->render('update', [
  98. 'model' => $model,
  99. ]);
  100. }
  101. }
  102. /**
  103. * Deletes an existing User model.
  104. * If deletion is successful, the browser will be redirected to the 'index' page.
  105. * @param integer $id
  106. * @return mixed
  107. */
  108. public function actionDelete($id)
  109. {
  110. $this->findModel($id)->delete();
  111. return $this->redirect(['index']);
  112. }
  113. public function actionMail() {
  114. /*$model = new MailForm() ;
  115. $model->subject = '[Le Chat des Noisettes] ' ;
  116. $model->body =
  117. "Bonjour,
  118. PS : Si vous ne souhaitez plus recevoir ces emails, rendez-vous dans votre compte sur www.lechatdesnoisettes.com." ;
  119. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  120. $id_user = Yii::$app->request->post('id_user') ;
  121. $user = User::findIdentity($id_user) ;
  122. if($user)
  123. {
  124. $model->sendEmail($user->email) ;
  125. }
  126. }*/
  127. $users = (new \yii\db\Query())
  128. ->select('*')
  129. ->from('user, user_boulangerie')
  130. ->where('user.id = user_boulangerie.id_user')
  131. ->andWhere('user_boulangerie.id_boulangerie = '.Yii::$app->user->identity->id)
  132. ->all() ;
  133. $arr_users = [] ;
  134. foreach($users as $u)
  135. $arr_users[] = $u->email ;
  136. return $this->render('liste_mails', [
  137. //'model' => $model,
  138. 'users' => $arr_users
  139. ]);
  140. }
  141. /**
  142. * Finds the User model based on its primary key value.
  143. * If the model is not found, a 404 HTTP exception will be thrown.
  144. * @param integer $id
  145. * @return User the loaded model
  146. * @throws NotFoundHttpException if the model cannot be found
  147. */
  148. protected function findModel($id)
  149. {
  150. if (($model = User::findOne($id)) !== null) {
  151. return $model;
  152. } else {
  153. throw new NotFoundHttpException('The requested page does not exist.');
  154. }
  155. }
  156. }