Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

276 lines
9.9KB

  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. }