Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

260 lines
9.2KB

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