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.

UserController.php 10KB

vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\models\User;
  5. use backend\models\MailForm;
  6. use yii\data\ActiveDataProvider;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. use common\helpers\Upload ;
  12. use common\helpers\Password ;
  13. use common\models\UserEtablissement ;
  14. use common\models\Etablissement ;
  15. use yii\base\UserException ;
  16. use common\models\CreditHistorique;
  17. use common\models\Commande;
  18. /**
  19. * UserController implements the CRUD actions for User model.
  20. */
  21. class UserController extends BackendController
  22. {
  23. public function behaviors()
  24. {
  25. return [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['post'],
  30. ],
  31. ],
  32. 'access' => [
  33. 'class' => AccessControl::className(),
  34. 'rules' => [
  35. [
  36. 'allow' => true,
  37. 'roles' => ['@'],
  38. 'matchCallback' => function ($rule, $action) {
  39. if($action->actionMethod == 'actionIndex' ||
  40. $action->actionMethod == 'actionCreate' ||
  41. $action->actionMethod == 'actionUpdate' ||
  42. $action->actionMethod == 'actionCredit' ||
  43. $action->actionMethod == 'actionMail' ||
  44. $action->actionMethod == 'actionCommandes')
  45. {
  46. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  47. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER ;
  48. }
  49. else {
  50. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  51. }
  52. }
  53. ]
  54. ],
  55. ],
  56. ];
  57. }
  58. /**
  59. * Lists all User models.
  60. * @return mixed
  61. */
  62. public function actionIndex()
  63. {
  64. $params = Yii::$app->request->queryParams;
  65. $query = (new \yii\db\Query())
  66. ->select('*')
  67. ->from('user, user_etablissement')
  68. ->where('user.id = user_etablissement.id_user')
  69. ->andWhere('user_etablissement.actif = 1')
  70. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement) ;
  71. if(isset($params['nom']))
  72. $query->andFilterWhere(['like', 'nom', $params['nom']]);
  73. if(isset($params['prenom']))
  74. $query->andFilterWhere(['like', 'prenom', $params['prenom']]) ;
  75. if(isset($params['email']))
  76. $query->andFilterWhere(['like', 'email', $params['email']]) ;
  77. if(isset($params['telephone']))
  78. $query->andFilterWhere(['like', 'telephone', $params['telephone']]) ;
  79. $dataProvider = new ActiveDataProvider([
  80. 'query' => $query
  81. ]);
  82. $etablissement = Etablissement::find()
  83. ->where(['id' => Yii::$app->user->identity->id])
  84. ->one() ;
  85. return $this->render('index', [
  86. 'dataProvider' => $dataProvider,
  87. 'etablissement' => $etablissement
  88. ]);
  89. }
  90. /**
  91. * Displays a single User model.
  92. * @param integer $id
  93. * @return mixed
  94. */
  95. public function actionView($id)
  96. {
  97. return $this->render('view', [
  98. 'model' => $this->findModel($id),
  99. ]);
  100. }
  101. /**
  102. * Creates a new User model.
  103. * If creation is successful, the browser will be redirected to the 'view' page.
  104. * @return mixed
  105. */
  106. public function actionCreate()
  107. {
  108. $model = new User();
  109. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  110. // save use
  111. $password = Password::generate() ;
  112. $model->setPassword($password);
  113. $model->generateAuthKey();
  114. $model->username = $model->email ;
  115. $model->save() ;
  116. // liaison etablissement / user
  117. $user_etablissement = new UserEtablissement() ;
  118. $user_etablissement->id_user = $model->id ;
  119. $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  120. $user_etablissement->credit = 0 ;
  121. $user_etablissement->actif = 1 ;
  122. $user_etablissement->save() ;
  123. // send mail
  124. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  125. Yii::$app->mailer->compose() ;
  126. $mail = Yii::$app->mailer->compose(
  127. ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'],
  128. ['user' => $model, 'etablissement' => $etablissement, 'password' => $password])
  129. ->setTo($model->email)
  130. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
  131. ->setSubject('[La boîte à pain] Inscription')
  132. ->send() ;
  133. return $this->redirect(['index']);
  134. } else {
  135. return $this->render('create', [
  136. 'model' => $model,
  137. ]);
  138. }
  139. }
  140. /**
  141. * Updates an existing User model.
  142. * If update is successful, the browser will be redirected to the 'view' page.
  143. * @param integer $id
  144. * @return mixed
  145. */
  146. public function actionUpdate($id)
  147. {
  148. $model = $this->findModel($id);
  149. $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one() ;
  150. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  151. if(($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  152. {
  153. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  154. return $this->redirect(['index']);
  155. } else {
  156. return $this->render('update', [
  157. 'model' => $model,
  158. ]);
  159. }
  160. }
  161. else {
  162. 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.");
  163. }
  164. }
  165. public function actionMail() {
  166. $users = (new \yii\db\Query())
  167. ->select('*')
  168. ->from('user, user_etablissement')
  169. ->where('user.id = user_etablissement.id_user')
  170. ->andWhere('user_etablissement.actif = 1')
  171. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  172. ->all() ;
  173. $arr_users = [] ;
  174. foreach($users as $u) {
  175. if(isset($u['email']))
  176. $arr_users[] = $u['email'] ;
  177. }
  178. return $this->render('liste_mails', [
  179. //'model' => $model,
  180. 'users' => $arr_users
  181. ]);
  182. }
  183. public function actionCredit($id)
  184. {
  185. $user = User::find()->with('userEtablissement')->where(['id' => $id])->one() ;
  186. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  187. if(($user_appartient_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  188. {
  189. $credit_historique = new CreditHistorique;
  190. if ($credit_historique->load(Yii::$app->request->post()) && $credit_historique->validate())
  191. {
  192. $credit_historique->id_user = $user->id ;
  193. $credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  194. $credit_historique->type = CreditHistorique::TYPE_CREDIT ;
  195. $credit_historique->save() ;
  196. $this->redirect(['user/index']) ;
  197. }
  198. $historique = CreditHistorique::find()
  199. ->with('commande')
  200. ->where([
  201. 'id_user' => $user->id,
  202. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  203. ])
  204. ->orderBy('date DESC')
  205. ->all() ;
  206. return $this->render('credit', [
  207. 'user' => $user,
  208. 'credit_historique' => $credit_historique,
  209. 'historique' => $historique
  210. ]) ;
  211. }
  212. else {
  213. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  214. }
  215. }
  216. public function actionCommandes($id)
  217. {
  218. $user = User::findOne($id) ;
  219. $commandes = Commande::find()
  220. ->with('commandeProduits', 'pointVente', 'creditHistorique')
  221. ->joinWith('production','production.etablissement')
  222. ->where([
  223. 'id_user' => $id,
  224. 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement
  225. ])
  226. ->orderBy('production.date DESC')
  227. ->all();
  228. foreach ($commandes as $c)
  229. $c->init();
  230. return $this->render('commandes', [
  231. 'commandes' => $commandes,
  232. 'user' => $user
  233. ]) ;
  234. }
  235. /**
  236. * Finds the User model based on its primary key value.
  237. * If the model is not found, a 404 HTTP exception will be thrown.
  238. * @param integer $id
  239. * @return User the loaded model
  240. * @throws NotFoundHttpException if the model cannot be found
  241. */
  242. protected function findModel($id)
  243. {
  244. if (($model = User::findOne($id)) !== null) {
  245. return $model;
  246. } else {
  247. throw new NotFoundHttpException('The requested page does not exist.');
  248. }
  249. }
  250. }