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.

295 lines
10KB

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