[ 'class' => AccessControl::className(), 'only' => ['logout', 'signup'], 'rules' => [ [ 'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['get'], ], ], ]; } /** * @inheritdoc */ public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } /** * Affiche la page d'erreur. * * @return mixed */ public function actionError() { $exception = Yii::$app->errorHandler->exception; if($exception->getMessage() == 'Producteur introuvable' || Yii::$app->getRequest()->getQueryParam('producer_not_found')) { return $this->render('error-404-producer', ['exception' => $exception]); } if ($exception !== null) { return $this->render('error', ['exception' => $exception]); } } /** * Affiche la page d'accueil. * * @return mixed */ public function actionIndex() { return $this->render('index'); } /** * Liste les producteurs utilisant la plateforme. * * @return mixed */ public function actionProducers() { $dataProviderProducers = new ActiveDataProvider([ 'query' => Producer::find() ->where([ 'active' => true, ]) ->orderBy('name ASC'), 'pagination' => [ 'pageSize' => 100, ], ]); return $this->render('producers',[ 'dataProviderProducers' => $dataProviderProducers ]); } /** * Affiche les mentions légales. * * @return mixed */ public function actionMentions() { return $this->render('mentions'); } /** * Affiche la page de connexion et traite le formulaire de connexion. * * @return mixed */ public function actionLogin() { if (!\Yii::$app->user->isGuest) { return Yii::$app->getResponse()->redirect(['site/index']); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { $returnUrl = Yii::$app->request->get('return_url'); if($returnUrl){ return $this->redirect($returnUrl); } else { $userProducerArray = UserProducer::find() ->with(['producer']) ->where(['id_user' => User::getCurrentId(), 'active' => 1, 'bookmark' => 1]) ->all() ; if($userProducerArray && is_array($userProducerArray) && count($userProducerArray) == 1) { return $this->redirect(Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index', 'slug_producer' => $userProducerArray[0]->producer->slug ])); } else { return $this->goBack(); } } } else { return $this->render('@frontend/views/site/login', [ 'model' => $model, ]); } } /** * Déconnecte l'utilisateur. * * @return mixed */ public function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } /** * Affiche la page de contact et traite le formulaire s'il est soumis. * * @return mixed */ public function actionContact() { $model = new ContactForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail(Yii::$app->params['adminEmail'])) { Yii::$app->session->setFlash('success', 'Votre message a bien été envoyé. Nous vous répondrons dès que possible.'); } else { Yii::$app->session->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.'); } return $this->refresh(); } else { return $this->render('contact', [ 'model' => $model, ]); } } /** * Affiche la page d'inscription et traite son formulaire. * * @return mixed */ public function actionSignup() { $model = new SignupForm(); if ($model->load(Yii::$app->request->post())) { if ($user = $model->signup()) { if (Yii::$app->getUser()->login($user)) { if($model->option_user_producer == 'producer') { $this->redirect(Yii::$app->urlManagerBackend->createAbsoluteUrl(['site/index'])); } else { $producer = Producer::findOne($model->id_producer) ; if($producer) { $this->redirect(Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $producer->slug])); } else { $this->redirect(['site/index']); } } } } } // liste des établissements disponibles $producersArray = Producer::getProducerPopulateDropdown() ; $dataProducers = $producersArray['data']; $optionsProducers = $producersArray['options']; return $this->render('signup', [ 'model' => $model, 'dataProducers' => $dataProducers, 'optionsProducers' => $optionsProducers, ]); } /** * Affiche la page de demande de nouveau mot de passe. * Traitement du formulaire. * * @return mixed */ public function actionRequestPasswordReset() { $model = new PasswordResetRequestForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail()) { Yii::$app->getSession()->setFlash('success', 'Un lien vous permettant de réinitialiser votre mot de passe vient d\'être envoyé sur votre boîte mail.'); return $this->goHome(); } else { Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.'); } } return $this->render('requestPasswordResetToken', [ 'model' => $model, ]); } /** * Met à jour le mot de passe de l'utilisateur. * * @param string $token * @return mixed * @throws BadRequestHttpException */ public function actionResetPassword($token) { try { $model = new ResetPasswordForm($token); } catch (InvalidParamException $e) { throw new BadRequestHttpException($e->getMessage()); } if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.'); return $this->goHome(); } return $this->render('resetPassword', [ 'model' => $model, ]); } /** * Affiche les conditions générale de service. * * @return mixed */ public function actionCgv() { return $this->render('cgv'); } /** * Affiche les précisions concernant l'utilisation du crédit. * * @return string */ public function actionCredit() { return $this->render('credit'); } /** * Affiche le formulaire de demande de code pour accéder à certains * producteurs. * * @param integer $id * @return mixed * @throws \yii\web\HttpException */ public function actionProducerCode($id) { $producer = Producer::findOne($id); if(!$producer) { throw new \yii\web\HttpException(404, 'Producteur introuvable'); } $producerCodeForm = new ProducerCodeForm ; $producerCodeForm->id_producer = $id ; if($producerCodeForm->load(Yii::$app->request->post()) && $producerCodeForm->validate()) { Producer::addUser(User::getCurrentId(), $id) ; $this->redirect(Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $producer->slug])); } return $this->render('producer_code',[ 'producer' => $producer, 'producerCodeForm' => $producerCodeForm, ]) ; } /** * Affiche la page de connexion / inscription pour accéder notamment au * formulaire de commande des producteirs. * * @param integer $id * @return mixed */ public function actionProducer($id) { $loginForm = new LoginForm(); $signupForm = new SignupForm(); $producer = Producer::searchOne([ 'id' => $id ]); $loginForm->id_producer = $id; $signupForm->id_producer = $id; $signupForm->option_user_producer = 'user'; $returnUrl = Yii::$app->request->get('returnUrl', Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $producer->slug])); if (Yii::$app->user->isGuest) { if ($loginForm->load(Yii::$app->request->post()) && $loginForm->login()) { if(!strlen($producer->code)) { Producer::addUser(User::getCurrentId(), $id) ; } $this->redirect($returnUrl); } if ($signupForm->load(Yii::$app->request->post())) { if ($user = $signupForm->signup()) { if (Yii::$app->getUser()->login($user)) { $this->redirect($returnUrl); } } } } else { $this->redirect($returnUrl); } return $this->render('producer', [ 'loginForm' => $loginForm, 'signupForm' => $signupForm, 'producer' => $producer, ]); } /** * Indique à l'utilisateur que l'espace d'un producteur est hors ligne * * @param integer $id * @return mixed */ public function actionProducerOffline($id) { $producer = Producer::searchOne([ 'id' => $id ]); return $this->render('producer_offline', [ 'producer' => $producer, ]); } }