Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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