Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

282 lines
10.0KB

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