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.

244 line
8.6KB

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