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.

278 lines
9.7KB

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