[ 'class' => VerbFilter::className(), 'actions' => [ ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return Yii::$app->user->identity->status == USER::STATUS_ADMIN || Yii::$app->user->identity->status == USER::STATUS_BOULANGER; } ] ], ], ]; } /** * Liste les utilisateurs. * * @return mixed */ public function actionIndex( $id_point_vente = 0, $section_clients_inactifs = false) { $params = Yii::$app->request->queryParams; if($id_point_vente) $params['id_point_vente'] = $id_point_vente ; if($section_clients_inactifs) $params['inactifs'] = true ; $query = User::findBy($params); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => ['attributes' => ['nom','prenom']], ]); $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, 'section_clients_inactifs' => $section_clients_inactifs, ]); } /** * 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(); $model->sendMailWelcome($password) ; 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); $previous_mail = $model->email ; $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()) { // on envoie le mail de bienvenue si le mail vient d'être défini if(!strlen($previous_mail) && strlen($model->email)) { $password = Password::generate(); $model->setPassword($password); $model->username = $model->email; $model->sendMailWelcome($password) ; } 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) ; } $params = Yii::$app->getRequest()->getQueryParams() ; unset($params['id']) ; $this->redirect(array_merge(['index'],$params)); } /** * Affiche la liste des emails des utilisateurs liés à un point de vente * donné. * * @param integer $id_point_vente * @return mixed */ 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 ]); } /** * Affiche les données liées au crédit d'un utilisateur (formulaire, * historique). * * @param integer $id * @return mixed * @throws UserException */ 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."); } } /** * Affiche les commandes d'un utilisateur. * * @param integer $id * @return mixed */ 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.'); } } }