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.9KB

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