You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserController.php 5.4KB

8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. 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)
  139. ->all() ;
  140. $arr_users = [] ;
  141. foreach($users as $u)
  142. $arr_users[] = $u->email ;
  143. return $this->render('liste_mails', [
  144. //'model' => $model,
  145. 'users' => $arr_users
  146. ]);
  147. }
  148. /**
  149. * Finds the User model based on its primary key value.
  150. * If the model is not found, a 404 HTTP exception will be thrown.
  151. * @param integer $id
  152. * @return User the loaded model
  153. * @throws NotFoundHttpException if the model cannot be found
  154. */
  155. protected function findModel($id)
  156. {
  157. if (($model = User::findOne($id)) !== null) {
  158. return $model;
  159. } else {
  160. throw new NotFoundHttpException('The requested page does not exist.');
  161. }
  162. }
  163. }