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.

272 lines
9.5KB

  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. $model->save() ;
  103. // liaison etablissement / user
  104. $user_etablissement = new UserEtablissement() ;
  105. $user_etablissement->id_user = $model->id ;
  106. $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  107. $user_etablissement->credit = 0 ;
  108. $user_etablissement->actif = 1 ;
  109. $user_etablissement->save() ;
  110. // send mail
  111. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  112. Yii::$app->mailer->compose() ;
  113. $mail = Yii::$app->mailer->compose(
  114. ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'],
  115. ['user' => $model, 'etablissement' => $etablissement, 'password' => $password])
  116. ->setTo($model->email)
  117. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
  118. ->setSubject('[La boîte à pain] Inscription')
  119. ->send() ;
  120. return $this->redirect(['index']);
  121. } else {
  122. return $this->render('create', [
  123. 'model' => $model,
  124. ]);
  125. }
  126. }
  127. /**
  128. * Updates an existing User model.
  129. * If update is successful, the browser will be redirected to the 'view' page.
  130. * @param integer $id
  131. * @return mixed
  132. */
  133. public function actionUpdate($id)
  134. {
  135. $model = $this->findModel($id);
  136. $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one() ;
  137. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  138. if(($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  139. {
  140. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  141. return $this->redirect(['index']);
  142. } else {
  143. return $this->render('update', [
  144. 'model' => $model,
  145. ]);
  146. }
  147. }
  148. else {
  149. 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.");
  150. }
  151. }
  152. public function actionMail() {
  153. $users = (new \yii\db\Query())
  154. ->select('*')
  155. ->from('user, user_etablissement')
  156. ->where('user.id = user_etablissement.id_user')
  157. ->andWhere('user_etablissement.actif = 1')
  158. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  159. ->all() ;
  160. $arr_users = [] ;
  161. foreach($users as $u) {
  162. if(isset($u['email']))
  163. $arr_users[] = $u['email'] ;
  164. }
  165. return $this->render('liste_mails', [
  166. //'model' => $model,
  167. 'users' => $arr_users
  168. ]);
  169. }
  170. public function actionCredit($id)
  171. {
  172. $user = User::find()->with('userEtablissement')->where(['id' => $id])->one() ;
  173. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  174. if(($user_appartient_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  175. {
  176. $credit_historique = new CreditHistorique;
  177. if ($credit_historique->load(Yii::$app->request->post()) && $credit_historique->validate())
  178. {
  179. $credit_historique->id_user = $user->id ;
  180. $credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  181. $credit_historique->type = CreditHistorique::TYPE_CREDIT ;
  182. $credit_historique->save() ;
  183. $this->redirect(['user/index']) ;
  184. }
  185. $historique = CreditHistorique::find()
  186. ->with('commande')
  187. ->where([
  188. 'id_user' => $user->id,
  189. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  190. ])
  191. ->orderBy('date DESC')
  192. ->all() ;
  193. return $this->render('credit', [
  194. 'user' => $user,
  195. 'credit_historique' => $credit_historique,
  196. 'historique' => $historique
  197. ]) ;
  198. }
  199. else {
  200. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  201. }
  202. }
  203. public function actionCommandes($id)
  204. {
  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. {
  231. if (($model = User::findOne($id)) !== null) {
  232. return $model;
  233. } else {
  234. throw new NotFoundHttpException('The requested page does not exist.');
  235. }
  236. }
  237. }