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ů.

205 lines
5.7KB

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