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.

276 lines
9.5KB

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