Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

186 lines
5.0KB

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