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.

274 lines
9.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. use common\helpers\Password ;
  13. use common\models\UserEtablissement ;
  14. use common\models\Etablissement ;
  15. use yii\base\UserException ;
  16. use common\models\CreditHistorique;
  17. /**
  18. * UserController implements the CRUD actions for User model.
  19. */
  20. class UserController extends BackendController
  21. {
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['post'],
  29. ],
  30. ],
  31. 'access' => [
  32. 'class' => AccessControl::className(),
  33. 'rules' => [
  34. [
  35. 'allow' => true,
  36. 'roles' => ['@'],
  37. 'matchCallback' => function ($rule, $action) {
  38. if($action->actionMethod == 'actionIndex' ||
  39. $action->actionMethod == 'actionCreate' ||
  40. $action->actionMethod == 'actionUpdate')
  41. {
  42. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  43. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER ;
  44. }
  45. else {
  46. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  47. }
  48. }
  49. ]
  50. ],
  51. ],
  52. ];
  53. }
  54. /**
  55. * Lists all User models.
  56. * @return mixed
  57. */
  58. public function actionIndex()
  59. {
  60. $dataProvider = new ActiveDataProvider([
  61. 'query' =>
  62. (new \yii\db\Query())
  63. ->select('*')
  64. ->from('user, user_etablissement')
  65. ->where('user.id = user_etablissement.id_user')
  66. ->andWhere('user_etablissement.actif = 1')
  67. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  68. ]);
  69. return $this->render('index', [
  70. 'dataProvider' => $dataProvider,
  71. ]);
  72. }
  73. /**
  74. * Displays a single User model.
  75. * @param integer $id
  76. * @return mixed
  77. */
  78. public function actionView($id)
  79. {
  80. return $this->render('view', [
  81. 'model' => $this->findModel($id),
  82. ]);
  83. }
  84. /**
  85. * Creates a new User model.
  86. * If creation is successful, the browser will be redirected to the 'view' page.
  87. * @return mixed
  88. */
  89. public function actionCreate()
  90. {
  91. $model = new User();
  92. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  93. // save use
  94. $password = Password::generate() ;
  95. $model->setPassword($password);
  96. $model->generateAuthKey();
  97. $model->username = $model->email ;
  98. $model->save() ;
  99. // liaison etablissement / user
  100. $user_etablissement = new UserEtablissement() ;
  101. $user_etablissement->id_user = $model->id ;
  102. $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  103. $user_etablissement->credit = 0 ;
  104. $user_etablissement->actif = 1 ;
  105. $user_etablissement->save() ;
  106. // send mail
  107. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  108. Yii::$app->mailer->compose() ;
  109. $mail = Yii::$app->mailer->compose(
  110. ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'],
  111. ['user' => $model, 'etablissement' => $etablissement, 'password' => $password])
  112. ->setTo($model->email)
  113. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
  114. ->setSubject('[La boîte à pain] Inscription')
  115. ->send() ;
  116. return $this->redirect(['index']);
  117. } else {
  118. return $this->render('create', [
  119. 'model' => $model,
  120. ]);
  121. }
  122. }
  123. /**
  124. * Updates an existing User model.
  125. * If update is successful, the browser will be redirected to the 'view' page.
  126. * @param integer $id
  127. * @return mixed
  128. */
  129. public function actionUpdate($id)
  130. {
  131. $model = $this->findModel($id);
  132. $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one() ;
  133. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  134. if(($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  135. {
  136. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  137. return $this->redirect(['index']);
  138. } else {
  139. return $this->render('update', [
  140. 'model' => $model,
  141. ]);
  142. }
  143. }
  144. else {
  145. throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs boulangeries, soit parce qu'il n'est pas lié à la votre.");
  146. }
  147. }
  148. /**
  149. * Deletes an existing User model.
  150. * If deletion is successful, the browser will be redirected to the 'index' page.
  151. * @param integer $id
  152. * @return mixed
  153. */
  154. /*public function actionDelete($id)
  155. {
  156. $this->findModel($id)->delete();
  157. return $this->redirect(['index']);
  158. }*/
  159. public function actionMail() {
  160. /*$model = new MailForm() ;
  161. $model->subject = '[Le Chat des Noisettes] ' ;
  162. $model->body =
  163. "Bonjour,
  164. PS : Si vous ne souhaitez plus recevoir ces emails, rendez-vous dans votre compte sur www.lechatdesnoisettes.com." ;
  165. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  166. $id_user = Yii::$app->request->post('id_user') ;
  167. $user = User::findIdentity($id_user) ;
  168. if($user)
  169. {
  170. $model->sendEmail($user->email) ;
  171. }
  172. }*/
  173. $users = (new \yii\db\Query())
  174. ->select('*')
  175. ->from('user, user_etablissement')
  176. ->where('user.id = user_etablissement.id_user')
  177. ->andWhere('user_etablissement.actif = 1')
  178. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  179. ->all() ;
  180. $arr_users = [] ;
  181. foreach($users as $u) {
  182. if(isset($u['email']))
  183. $arr_users[] = $u['email'] ;
  184. }
  185. return $this->render('liste_mails', [
  186. //'model' => $model,
  187. 'users' => $arr_users
  188. ]);
  189. }
  190. public function actionCredit($id)
  191. {
  192. $user = User::find()->with('userEtablissement')->where(['id' => $id])->one() ;
  193. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  194. if(($user_appartient_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  195. {
  196. $credit_historique = new CreditHistorique;
  197. if ($credit_historique->load(Yii::$app->request->post()) && $credit_historique->validate())
  198. {
  199. $credit_historique->id_user = $user->id ;
  200. $credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  201. $credit_historique->type = CreditHistorique::TYPE_CREDIT ;
  202. $credit_historique->save() ;
  203. $this->redirect(['user/index']) ;
  204. }
  205. $historique = CreditHistorique::find()
  206. ->where([
  207. 'id_user' => $user->id,
  208. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  209. ])
  210. ->orderBy('date DESC')
  211. ->all() ;
  212. return $this->render('credit', [
  213. 'user' => $user,
  214. 'credit_historique' => $credit_historique,
  215. 'historique' => $historique
  216. ]) ;
  217. }
  218. else {
  219. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  220. }
  221. }
  222. /**
  223. * Finds the User model based on its primary key value.
  224. * If the model is not found, a 404 HTTP exception will be thrown.
  225. * @param integer $id
  226. * @return User the loaded model
  227. * @throws NotFoundHttpException if the model cannot be found
  228. */
  229. protected function findModel($id)
  230. {
  231. if (($model = User::findOne($id)) !== null) {
  232. return $model;
  233. } else {
  234. throw new NotFoundHttpException('The requested page does not exist.');
  235. }
  236. }
  237. }