Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

281 line
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. /**
  18. * UserController implements the CRUD actions for User model.
  19. */
  20. class UserController extends BackendController
  21. {
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['post'],
  29. ],
  30. ],
  31. 'access' => [
  32. 'class' => AccessControl::className(),
  33. 'rules' => [
  34. [
  35. 'allow' => true,
  36. 'roles' => ['@'],
  37. 'matchCallback' => function ($rule, $action) {
  38. if($action->actionMethod == 'actionIndex' ||
  39. $action->actionMethod == 'actionCreate' ||
  40. $action->actionMethod == 'actionUpdate' ||
  41. $action->actionMethod == 'actionCredit')
  42. {
  43. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  44. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER ;
  45. }
  46. else {
  47. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  48. }
  49. }
  50. ]
  51. ],
  52. ],
  53. ];
  54. }
  55. /**
  56. * Lists all User models.
  57. * @return mixed
  58. */
  59. public function actionIndex()
  60. {
  61. $dataProvider = new ActiveDataProvider([
  62. 'query' =>
  63. (new \yii\db\Query())
  64. ->select('*')
  65. ->from('user, user_etablissement')
  66. ->where('user.id = user_etablissement.id_user')
  67. ->andWhere('user_etablissement.actif = 1')
  68. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  69. ]);
  70. $etablissement = Etablissement::find()
  71. ->where(['id' => Yii::$app->user->identity->id])
  72. ->one() ;
  73. return $this->render('index', [
  74. 'dataProvider' => $dataProvider,
  75. 'etablissement' => $etablissement
  76. ]);
  77. }
  78. /**
  79. * Displays a single User model.
  80. * @param integer $id
  81. * @return mixed
  82. */
  83. public function actionView($id)
  84. {
  85. return $this->render('view', [
  86. 'model' => $this->findModel($id),
  87. ]);
  88. }
  89. /**
  90. * Creates a new User model.
  91. * If creation is successful, the browser will be redirected to the 'view' page.
  92. * @return mixed
  93. */
  94. public function actionCreate()
  95. {
  96. $model = new User();
  97. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  98. // save use
  99. $password = Password::generate() ;
  100. $model->setPassword($password);
  101. $model->generateAuthKey();
  102. $model->username = $model->email ;
  103. $model->save() ;
  104. // liaison etablissement / user
  105. $user_etablissement = new UserEtablissement() ;
  106. $user_etablissement->id_user = $model->id ;
  107. $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  108. $user_etablissement->credit = 0 ;
  109. $user_etablissement->actif = 1 ;
  110. $user_etablissement->save() ;
  111. // send mail
  112. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  113. Yii::$app->mailer->compose() ;
  114. $mail = Yii::$app->mailer->compose(
  115. ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'],
  116. ['user' => $model, 'etablissement' => $etablissement, 'password' => $password])
  117. ->setTo($model->email)
  118. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
  119. ->setSubject('[La boîte à pain] Inscription')
  120. ->send() ;
  121. return $this->redirect(['index']);
  122. } else {
  123. return $this->render('create', [
  124. 'model' => $model,
  125. ]);
  126. }
  127. }
  128. /**
  129. * Updates an existing User model.
  130. * If update is successful, the browser will be redirected to the 'view' page.
  131. * @param integer $id
  132. * @return mixed
  133. */
  134. public function actionUpdate($id)
  135. {
  136. $model = $this->findModel($id);
  137. $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one() ;
  138. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  139. if(($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  140. {
  141. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  142. return $this->redirect(['index']);
  143. } else {
  144. return $this->render('update', [
  145. 'model' => $model,
  146. ]);
  147. }
  148. }
  149. else {
  150. 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.");
  151. }
  152. }
  153. /**
  154. * Deletes an existing User model.
  155. * If deletion is successful, the browser will be redirected to the 'index' page.
  156. * @param integer $id
  157. * @return mixed
  158. */
  159. /*public function actionDelete($id)
  160. {
  161. $this->findModel($id)->delete();
  162. return $this->redirect(['index']);
  163. }*/
  164. public function actionMail() {
  165. /*$model = new MailForm() ;
  166. $model->subject = '[Le Chat des Noisettes] ' ;
  167. $model->body =
  168. "Bonjour,
  169. PS : Si vous ne souhaitez plus recevoir ces emails, rendez-vous dans votre compte sur www.lechatdesnoisettes.com." ;
  170. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  171. $id_user = Yii::$app->request->post('id_user') ;
  172. $user = User::findIdentity($id_user) ;
  173. if($user)
  174. {
  175. $model->sendEmail($user->email) ;
  176. }
  177. }*/
  178. $users = (new \yii\db\Query())
  179. ->select('*')
  180. ->from('user, user_etablissement')
  181. ->where('user.id = user_etablissement.id_user')
  182. ->andWhere('user_etablissement.actif = 1')
  183. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  184. ->all() ;
  185. $arr_users = [] ;
  186. foreach($users as $u) {
  187. if(isset($u['email']))
  188. $arr_users[] = $u['email'] ;
  189. }
  190. return $this->render('liste_mails', [
  191. //'model' => $model,
  192. 'users' => $arr_users
  193. ]);
  194. }
  195. public function actionCredit($id)
  196. {
  197. $user = User::find()->with('userEtablissement')->where(['id' => $id])->one() ;
  198. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  199. if(($user_appartient_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  200. {
  201. $credit_historique = new CreditHistorique;
  202. if ($credit_historique->load(Yii::$app->request->post()) && $credit_historique->validate())
  203. {
  204. $credit_historique->id_user = $user->id ;
  205. $credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  206. $credit_historique->type = CreditHistorique::TYPE_CREDIT ;
  207. $credit_historique->save() ;
  208. $this->redirect(['user/index']) ;
  209. }
  210. $historique = CreditHistorique::find()
  211. ->with('commande')
  212. ->where([
  213. 'id_user' => $user->id,
  214. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  215. ])
  216. ->orderBy('date DESC')
  217. ->all() ;
  218. return $this->render('credit', [
  219. 'user' => $user,
  220. 'credit_historique' => $credit_historique,
  221. 'historique' => $historique
  222. ]) ;
  223. }
  224. else {
  225. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
  226. }
  227. }
  228. /**
  229. * Finds the User model based on its primary key value.
  230. * If the model is not found, a 404 HTTP exception will be thrown.
  231. * @param integer $id
  232. * @return User the loaded model
  233. * @throws NotFoundHttpException if the model cannot be found
  234. */
  235. protected function findModel($id)
  236. {
  237. if (($model = User::findOne($id)) !== null) {
  238. return $model;
  239. } else {
  240. throw new NotFoundHttpException('The requested page does not exist.');
  241. }
  242. }
  243. }