Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

260 linhas
9.3KB

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