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.

287 Zeilen
10KB

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