[ 'class' => VerbFilter::className(), 'actions' => [ ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { if ($action->actionMethod == 'actionIndex' || $action->actionMethod == 'actionCreate' || $action->actionMethod == 'actionUpdate' || $action->actionMethod == 'actionCredit' || $action->actionMethod == 'actionMail' || $action->actionMethod == 'actionCommandes') { return Yii::$app->user->identity->status == User::STATUS_ADMIN || Yii::$app->user->identity->status == User::STATUS_BOULANGER; } else { return Yii::$app->user->identity->status == User::STATUS_ADMIN; } } ] ], ], ]; } /** * Lists all User models. * @return mixed */ public function actionIndex($id_point_vente = 0) { $params = Yii::$app->request->queryParams; if($id_point_vente) $params['id_point_vente'] = $id_point_vente ; $query = User::findBy($params); $dataProvider = new ActiveDataProvider([ 'query' => $query ]); $etablissement = Etablissement::find() ->where(['id' => Yii::$app->user->identity->id_etablissement]) ->one(); $points_vente = PointVente::find()->where(['id_etablissement' => $etablissement->id])->all() ; return $this->render('index', [ 'dataProvider' => $dataProvider, 'etablissement' => $etablissement, 'id_point_vente_active' => $id_point_vente, 'points_vente' => $points_vente ]); } /** * Displays a single User model. * @param integer $id * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') { // save use $password = Password::generate(); $model->setPassword($password); $model->generateAuthKey(); $model->username = $model->email; $model->confiance = 1; if (!strlen($model->email)) $model->username = 'inconnu@laboiteapain.net'; $model->save(); // liaison etablissement / user $user_etablissement = new UserEtablissement(); $user_etablissement->id_user = $model->id; $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement; $user_etablissement->credit = 0; $user_etablissement->actif = 1; $user_etablissement->save(); // send mail if (strlen($model->email)) { $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement); Yii::$app->mailer->compose(); $mail = Yii::$app->mailer->compose( ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $model, 'etablissement' => $etablissement, 'password' => $password]) ->setTo($model->email) ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain']) ->setSubject('[La boîte à pain] Inscription') ->send(); } return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * Updates an existing User model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one(); $user_appartient_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]); if (($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) { if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('update', [ 'model' => $model, ]); } } else { throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs établissements, soit parce qu'il n'est pas lié au votre."); } } /** * Désactive l'utilisateur de l'établissement. * * @param integer $id ID de l'utilisateur */ public function actionDelete($id) { $user_etablissement = UserEtablissement::findOne([ 'id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement ]) ; if($user_etablissement) { $user_etablissement->actif = 0 ; $user_etablissement->favoris = 0 ; $user_etablissement->save() ; } else { throw new \yii\web\NotFoundHttpException('L\'enregistrement UserEtablissement est introuvable', 404) ; } $this->redirect(['index']); } public function actionMail($id_point_vente = 0) { $users = User::findBy([ 'id_etablissement' => Yii::$app->user->identity->id_etablissement, 'id_point_vente' => $id_point_vente ])->all() ; $arr_users = []; foreach ($users as $u) { if (isset($u['email']) && strlen($u['email'])) $arr_users[] = $u['email']; } $points_vente = PointVente::find()->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])->all() ; $point_vente = null ; if($id_point_vente) { $point_vente = PointVente::findOne(['id' => $id_point_vente, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ; } return $this->render('liste_mails', [ 'users' => $arr_users, 'points_vente' => $points_vente, 'point_vente' => $point_vente ]); } public function actionCredit($id) { $user = User::find()->with('userEtablissement')->where(['id' => $id])->one(); $user_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]); if (($user_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) { $credit_form = new CreditForm; if ($credit_form->load(Yii::$app->request->post()) && $credit_form->validate()) { $credit_form->id_user = $id ; $credit_form->save(); $credit_form = new CreditForm; } $historique = CreditHistorique::find() ->with(['commande', 'userAction']) ->where([ 'id_user' => $user->id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement, ]) ->orderBy('date DESC') ->all(); return $this->render('credit', [ 'user' => $user, 'credit_form' => $credit_form, 'historique' => $historique ]); } else { throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie."); } } public function actionCommandes($id) { $user = User::findOne($id); $commandes = Commande::find() ->with('commandeProduits', 'pointVente', 'creditHistorique') ->joinWith('production', 'production.etablissement') ->where([ 'id_user' => $id, 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement ]) ->orderBy('production.date DESC') ->all(); foreach ($commandes as $c) $c->init(); return $this->render('commandes', [ 'commandes' => $commandes, 'user' => $user ]); } /** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }