Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

262 lines
9.1KB

  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. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  22. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  23. }
  24. ]
  25. ],
  26. ],
  27. ];
  28. }
  29. /**
  30. * Lists all User models.
  31. * @return mixed
  32. */
  33. public function actionIndex($id_point_vente = 0, $section_clients_inactifs = false) {
  34. $params = Yii::$app->request->queryParams;
  35. if($id_point_vente)
  36. $params['id_point_vente'] = $id_point_vente ;
  37. if($section_clients_inactifs)
  38. $params['inactifs'] = true ;
  39. $query = User::findBy($params);
  40. $dataProvider = new ActiveDataProvider([
  41. 'query' => $query,
  42. 'sort' => ['attributes' => ['nom','prenom']],
  43. ]);
  44. $etablissement = Etablissement::find()
  45. ->where(['id' => Yii::$app->user->identity->id_etablissement])
  46. ->one();
  47. $points_vente = PointVente::find()->where(['id_etablissement' => $etablissement->id])->all() ;
  48. return $this->render('index', [
  49. 'dataProvider' => $dataProvider,
  50. 'etablissement' => $etablissement,
  51. 'id_point_vente_active' => $id_point_vente,
  52. 'points_vente' => $points_vente,
  53. 'section_clients_inactifs' => $section_clients_inactifs,
  54. ]);
  55. }
  56. /**
  57. * Creates a new User model.
  58. * If creation is successful, the browser will be redirected to the 'view' page.
  59. * @return mixed
  60. */
  61. public function actionCreate() {
  62. $model = new User();
  63. if ($model->load(Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') {
  64. // save use
  65. $password = Password::generate();
  66. $model->setPassword($password);
  67. $model->generateAuthKey();
  68. $model->username = $model->email;
  69. $model->confiance = 1;
  70. if (!strlen($model->email))
  71. $model->username = 'inconnu@laboiteapain.net';
  72. $model->save();
  73. // liaison etablissement / user
  74. $user_etablissement = new UserEtablissement();
  75. $user_etablissement->id_user = $model->id;
  76. $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement;
  77. $user_etablissement->credit = 0;
  78. $user_etablissement->actif = 1;
  79. $user_etablissement->save();
  80. $model->sendMailWelcome($password) ;
  81. return $this->redirect(['index']);
  82. } else {
  83. return $this->render('create', [
  84. 'model' => $model,
  85. ]);
  86. }
  87. }
  88. /**
  89. * Updates an existing User model.
  90. * If update is successful, the browser will be redirected to the 'view' page.
  91. * @param integer $id
  92. * @return mixed
  93. */
  94. public function actionUpdate($id) {
  95. $model = $this->findModel($id);
  96. $previous_mail = $model->email ;
  97. $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one();
  98. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
  99. if (($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) {
  100. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  101. // on envoie le mail de bienvenue si le mail vient d'être défini
  102. if(!strlen($previous_mail) && strlen($model->email)) {
  103. $password = Password::generate();
  104. $model->setPassword($password);
  105. $model->username = $model->email;
  106. $model->sendMailWelcome($password) ;
  107. }
  108. return $this->redirect(['index']);
  109. } else {
  110. return $this->render('update', [
  111. 'model' => $model,
  112. ]);
  113. }
  114. } else {
  115. 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.");
  116. }
  117. }
  118. /**
  119. * Désactive l'utilisateur de l'établissement.
  120. *
  121. * @param integer $id ID de l'utilisateur
  122. */
  123. public function actionDelete($id) {
  124. $user_etablissement = UserEtablissement::findOne([
  125. 'id_user' => $id,
  126. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  127. ]) ;
  128. if($user_etablissement) {
  129. $user_etablissement->actif = 0 ;
  130. $user_etablissement->favoris = 0 ;
  131. $user_etablissement->save() ;
  132. }
  133. else {
  134. throw new \yii\web\NotFoundHttpException('L\'enregistrement UserEtablissement est introuvable', 404) ;
  135. }
  136. $this->redirect(['index']);
  137. }
  138. public function actionMail($id_point_vente = 0) {
  139. $users = User::findBy([
  140. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  141. 'id_point_vente' => $id_point_vente
  142. ])->all() ;
  143. $arr_users = [];
  144. foreach ($users as $u) {
  145. if (isset($u['email']) && strlen($u['email']))
  146. $arr_users[] = $u['email'];
  147. }
  148. $points_vente = PointVente::find()->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])->all() ;
  149. $point_vente = null ;
  150. if($id_point_vente) {
  151. $point_vente = PointVente::findOne(['id' => $id_point_vente, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  152. }
  153. return $this->render('liste_mails', [
  154. 'users' => $arr_users,
  155. 'points_vente' => $points_vente,
  156. 'point_vente' => $point_vente
  157. ]);
  158. }
  159. public function actionCredit($id) {
  160. $user = User::find()->with('userEtablissement')->where(['id' => $id])->one();
  161. $user_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
  162. if (($user_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) {
  163. $credit_form = new CreditForm;
  164. if ($credit_form->load(Yii::$app->request->post()) && $credit_form->validate()) {
  165. $credit_form->id_user = $id ;
  166. $credit_form->save();
  167. $credit_form = new CreditForm;
  168. }
  169. $historique = CreditHistorique::find()
  170. ->with(['commande', 'userAction'])
  171. ->where([
  172. 'id_user' => $user->id,
  173. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  174. ])
  175. ->orderBy('date DESC')
  176. ->all();
  177. return $this->render('credit', [
  178. 'user' => $user,
  179. 'credit_form' => $credit_form,
  180. 'historique' => $historique
  181. ]);
  182. }
  183. else {
  184. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  185. }
  186. }
  187. public function actionCommandes($id) {
  188. $user = User::findOne($id);
  189. $commandes = Commande::find()
  190. ->with('commandeProduits', 'pointVente', 'creditHistorique')
  191. ->joinWith('production', 'production.etablissement')
  192. ->where([
  193. 'id_user' => $id,
  194. 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement
  195. ])
  196. ->orderBy('production.date DESC')
  197. ->all();
  198. foreach ($commandes as $c)
  199. $c->init();
  200. return $this->render('commandes', [
  201. 'commandes' => $commandes,
  202. 'user' => $user
  203. ]);
  204. }
  205. /**
  206. * Finds the User model based on its primary key value.
  207. * If the model is not found, a 404 HTTP exception will be thrown.
  208. * @param integer $id
  209. * @return User the loaded model
  210. * @throws NotFoundHttpException if the model cannot be found
  211. */
  212. protected function findModel($id) {
  213. if (($model = User::findOne($id)) !== null) {
  214. return $model;
  215. } else {
  216. throw new NotFoundHttpException('The requested page does not exist.');
  217. }
  218. }
  219. }