Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

194 lines
5.4KB

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