Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

280 lines
10KB

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