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.

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