Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

205 lines
5.5KB

  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use common\models\Produit;
  5. use common\models\LoginForm;
  6. use frontend\models\PasswordResetRequestForm;
  7. use frontend\models\ResetPasswordForm;
  8. use frontend\models\SignupForm;
  9. use frontend\models\ContactForm;
  10. use yii\base\InvalidParamException;
  11. use yii\web\BadRequestHttpException;
  12. use yii\web\Controller;
  13. use yii\filters\VerbFilter;
  14. use yii\filters\AccessControl;
  15. /**
  16. * Site controller
  17. */
  18. class SiteController extends Controller
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public function behaviors()
  24. {
  25. return [
  26. 'access' => [
  27. 'class' => AccessControl::className(),
  28. 'only' => ['logout', 'signup'],
  29. 'rules' => [
  30. [
  31. 'actions' => ['signup'],
  32. 'allow' => true,
  33. 'roles' => ['?'],
  34. ],
  35. [
  36. 'actions' => ['logout'],
  37. 'allow' => true,
  38. 'roles' => ['@'],
  39. ],
  40. ],
  41. ],
  42. 'verbs' => [
  43. 'class' => VerbFilter::className(),
  44. 'actions' => [
  45. 'logout' => ['get'],
  46. ],
  47. ],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function actions()
  54. {
  55. return [
  56. 'error' => [
  57. 'class' => 'yii\web\ErrorAction',
  58. ],
  59. 'captcha' => [
  60. 'class' => 'yii\captcha\CaptchaAction',
  61. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  62. ],
  63. ];
  64. }
  65. public function actionIndex()
  66. {
  67. // produits
  68. $produits = Produit::find()->orderBy('order ASC')->all() ;
  69. // contact
  70. $model = new ContactForm();
  71. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  72. if ($model->sendEmail('matthieu@lechatdesnoisettes.com')) {
  73. Yii::$app->session->setFlash('success', "Votre message a bien été envoyé, j'y répondrai dès que possible.");
  74. } else {
  75. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  76. }
  77. return $this->refresh();
  78. }
  79. return $this->render('index',[
  80. 'page_principale'=>true,
  81. 'produits' => $produits,
  82. 'model' => $model,
  83. ]);
  84. }
  85. public function actionMentions()
  86. {
  87. return $this->render('mentions');
  88. }
  89. public function actionCommander() {
  90. if (Yii::$app->user->isGuest) {
  91. $this->redirect(Yii::$app->urlManager->createUrl('site/login')) ;
  92. }
  93. }
  94. public function actionLogin()
  95. {
  96. if (!\Yii::$app->user->isGuest) {
  97. return $this->goHome();
  98. }
  99. $model = new LoginForm();
  100. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  101. return $this->goBack();
  102. } else {
  103. return $this->render('login', [
  104. 'model' => $model,
  105. ]);
  106. }
  107. }
  108. public function actionLogout()
  109. {
  110. Yii::$app->user->logout();
  111. return $this->goHome();
  112. }
  113. public function actionContact()
  114. {
  115. $model = new ContactForm();
  116. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  117. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  118. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  119. } else {
  120. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  121. }
  122. return $this->refresh();
  123. } else {
  124. return $this->render('contact', [
  125. 'model' => $model,
  126. ]);
  127. }
  128. }
  129. public function actionAbout()
  130. {
  131. return $this->render('about');
  132. }
  133. public function actionSignup()
  134. {
  135. $model = new SignupForm();
  136. if ($model->load(Yii::$app->request->post())) {
  137. if ($user = $model->signup()) {
  138. if (Yii::$app->getUser()->login($user)) {
  139. //return $this->goHome();
  140. $this->redirect(['commande/index']) ;
  141. }
  142. }
  143. }
  144. return $this->render('signup', [
  145. 'model' => $model,
  146. ]);
  147. }
  148. public function actionRequestPasswordReset()
  149. {
  150. $model = new PasswordResetRequestForm();
  151. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  152. if ($model->sendEmail()) {
  153. Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
  154. return $this->goHome();
  155. } else {
  156. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  157. }
  158. }
  159. return $this->render('requestPasswordResetToken', [
  160. 'model' => $model,
  161. ]);
  162. }
  163. public function actionResetPassword($token)
  164. {
  165. try {
  166. $model = new ResetPasswordForm($token);
  167. } catch (InvalidParamException $e) {
  168. throw new BadRequestHttpException($e->getMessage());
  169. }
  170. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  171. Yii::$app->getSession()->setFlash('success', 'New password was saved.');
  172. return $this->goHome();
  173. }
  174. return $this->render('resetPassword', [
  175. 'model' => $model,
  176. ]);
  177. }
  178. }