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.

UserController.php 9.9KB

8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_etablissement = Yii::$app->user->identity->id_etablissement ;
  187. if($credit_historique->type == CreditHistorique::TYPE_DEBIT && $credit_historique->montant > 0)
  188. $credit_historique->montant = - $credit_historique->montant ;
  189. $credit_historique->save() ;
  190. $this->redirect(['user/index']) ;
  191. }
  192. $historique = CreditHistorique::find()
  193. ->with('commande')
  194. ->where([
  195. 'id_user' => $user->id,
  196. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  197. ])
  198. ->orderBy('date DESC')
  199. ->all() ;
  200. return $this->render('credit', [
  201. 'user' => $user,
  202. 'credit_historique' => $credit_historique,
  203. 'historique' => $historique
  204. ]) ;
  205. }
  206. else {
  207. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  208. }
  209. }
  210. public function actionCommandes($id)
  211. {
  212. $user = User::findOne($id) ;
  213. $commandes = Commande::find()
  214. ->with('commandeProduits', 'pointVente', 'creditHistorique')
  215. ->joinWith('production','production.etablissement')
  216. ->where([
  217. 'id_user' => $id,
  218. 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement
  219. ])
  220. ->orderBy('production.date DESC')
  221. ->all();
  222. foreach ($commandes as $c)
  223. $c->init();
  224. return $this->render('commandes', [
  225. 'commandes' => $commandes,
  226. 'user' => $user
  227. ]) ;
  228. }
  229. /**
  230. * Finds the User model based on its primary key value.
  231. * If the model is not found, a 404 HTTP exception will be thrown.
  232. * @param integer $id
  233. * @return User the loaded model
  234. * @throws NotFoundHttpException if the model cannot be found
  235. */
  236. protected function findModel($id)
  237. {
  238. if (($model = User::findOne($id)) !== null) {
  239. return $model;
  240. } else {
  241. throw new NotFoundHttpException('The requested page does not exist.');
  242. }
  243. }
  244. }