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.

UserController.php 9.2KB

пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. $params = Yii::$app->getRequest()->getQueryParams() ;
  137. unset($params['id']) ;
  138. $this->redirect(array_merge(['index'],$params));
  139. }
  140. public function actionMail($id_point_vente = 0) {
  141. $users = User::findBy([
  142. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  143. 'id_point_vente' => $id_point_vente
  144. ])->all() ;
  145. $arr_users = [];
  146. foreach ($users as $u) {
  147. if (isset($u['email']) && strlen($u['email']))
  148. $arr_users[] = $u['email'];
  149. }
  150. $points_vente = PointVente::find()->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])->all() ;
  151. $point_vente = null ;
  152. if($id_point_vente) {
  153. $point_vente = PointVente::findOne(['id' => $id_point_vente, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  154. }
  155. return $this->render('liste_mails', [
  156. 'users' => $arr_users,
  157. 'points_vente' => $points_vente,
  158. 'point_vente' => $point_vente
  159. ]);
  160. }
  161. public function actionCredit($id) {
  162. $user = User::find()->with('userEtablissement')->where(['id' => $id])->one();
  163. $user_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
  164. if (($user_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) {
  165. $credit_form = new CreditForm;
  166. if ($credit_form->load(Yii::$app->request->post()) && $credit_form->validate()) {
  167. $credit_form->id_user = $id ;
  168. $credit_form->save();
  169. $credit_form = new CreditForm;
  170. }
  171. $historique = CreditHistorique::find()
  172. ->with(['commande', 'userAction'])
  173. ->where([
  174. 'id_user' => $user->id,
  175. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  176. ])
  177. ->orderBy('date DESC')
  178. ->all();
  179. return $this->render('credit', [
  180. 'user' => $user,
  181. 'credit_form' => $credit_form,
  182. 'historique' => $historique
  183. ]);
  184. }
  185. else {
  186. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  187. }
  188. }
  189. public function actionCommandes($id) {
  190. $user = User::findOne($id);
  191. $commandes = Commande::find()
  192. ->with('commandeProduits', 'pointVente', 'creditHistorique')
  193. ->joinWith('production', 'production.etablissement')
  194. ->where([
  195. 'id_user' => $id,
  196. 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement
  197. ])
  198. ->orderBy('production.date DESC')
  199. ->all();
  200. foreach ($commandes as $c)
  201. $c->init();
  202. return $this->render('commandes', [
  203. 'commandes' => $commandes,
  204. 'user' => $user
  205. ]);
  206. }
  207. /**
  208. * Finds the User model based on its primary key value.
  209. * If the model is not found, a 404 HTTP exception will be thrown.
  210. * @param integer $id
  211. * @return User the loaded model
  212. * @throws NotFoundHttpException if the model cannot be found
  213. */
  214. protected function findModel($id) {
  215. if (($model = User::findOne($id)) !== null) {
  216. return $model;
  217. } else {
  218. throw new NotFoundHttpException('The requested page does not exist.');
  219. }
  220. }
  221. }