- <?php
-
-
-
- namespace frontend\controllers;
-
- use Yii;
- use common\models\Product;
- use common\models\LoginForm;
- use common\models\Producer;
- use frontend\models\PasswordResetRequestForm;
- use frontend\models\ResetPasswordForm;
- use frontend\models\SignupForm;
- use common\models\ContactForm;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use yii\helpers\Html;
- use frontend\controllers\FrontendController;
- use common\models\UserProducer;
- use dosamigos\leaflet\types\LatLng;
- use dosamigos\leaflet\layers\Marker;
- use dosamigos\leaflet\layers\TileLayer;
- use dosamigos\leaflet\LeafLet;
- use dosamigos\leaflet\widgets\Map;
-
-
- class SiteController extends FrontendController
- {
-
-
-
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout', 'signup'],
- 'rules' => [
- [
- 'actions' => ['signup'],
- 'allow' => true,
- 'roles' => ['?'],
- ],
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['get'],
- ],
- ],
- ];
- }
-
-
-
- public function actions()
- {
- return [
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
-
-
-
- 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]);
- }
- }
-
-
-
- public function actionIndex()
- {
- return $this->render('index');
- }
-
-
-
- public function actionProducers()
- {
- $dataProviderProducers = new ActiveDataProvider([
- 'query' => Producer::find()
- ->where([
- 'active' => true,
- ])
- ->orderBy('name ASC'),
- 'pagination' => [
- 'pageSize' => 100,
- ],
- ]);
-
- return $this->render('producers',[
- 'dataProviderProducers' => $dataProviderProducers
- ]);
- }
-
-
-
- public function actionMentions()
- {
- return $this->render('mentions');
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- 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']);
- }
- }
- }
- }
- }
-
-
- $producersArray = Producer::getProducerPopulateDropdown() ;
- $dataProducers = $producersArray['data'];
- $optionsProducers = $producersArray['options'];
-
- return $this->render('signup', [
- 'model' => $model,
- 'dataProducers' => $dataProducers,
- 'optionsProducers' => $optionsProducers,
- ]);
- }
-
-
-
- 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,
- ]);
- }
-
-
-
- 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,
- ]);
- }
-
-
-
- public function actionCgv()
- {
- return $this->render('cgv');
- }
-
-
-
- public function actionCredit()
- {
- return $this->render('credit');
- }
-
-
-
- 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,
- ]) ;
- }
-
-
-
- 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()) {
- 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,
- ]);
- }
-
- }
|