您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

232 行
6.9KB

  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. use yii\helpers\Html;
  16. use dosamigos\leaflet\types\LatLng;
  17. use dosamigos\leaflet\layers\Marker;
  18. use dosamigos\leaflet\layers\TileLayer;
  19. use dosamigos\leaflet\LeafLet;
  20. use dosamigos\leaflet\widgets\Map;
  21. /**
  22. * Site controller
  23. */
  24. class SiteController extends Controller
  25. {
  26. /**
  27. * @inheritdoc
  28. */
  29. public function behaviors()
  30. {
  31. return [
  32. 'access' => [
  33. 'class' => AccessControl::className(),
  34. 'only' => ['logout', 'signup'],
  35. 'rules' => [
  36. [
  37. 'actions' => ['signup'],
  38. 'allow' => true,
  39. 'roles' => ['?'],
  40. ],
  41. [
  42. 'actions' => ['logout'],
  43. 'allow' => true,
  44. 'roles' => ['@'],
  45. ],
  46. ],
  47. ],
  48. 'verbs' => [
  49. 'class' => VerbFilter::className(),
  50. 'actions' => [
  51. 'logout' => ['get'],
  52. ],
  53. ],
  54. ];
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function actions()
  60. {
  61. return [
  62. 'error' => [
  63. 'class' => 'yii\web\ErrorAction',
  64. ],
  65. 'captcha' => [
  66. 'class' => 'yii\captcha\CaptchaAction',
  67. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  68. ],
  69. ];
  70. }
  71. public function actionIndex()
  72. {
  73. // produits
  74. $produits = Produit::find()->orderBy('order ASC')->all() ;
  75. // contact
  76. $model = new ContactForm();
  77. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  78. if ($model->sendEmail('matthieu@lechatdesnoisettes.com')) {
  79. Yii::$app->session->setFlash('success', "Votre message a bien été envoyé, j'y répondrai dès que possible.");
  80. } else {
  81. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  82. }
  83. return $this->refresh();
  84. }
  85. // map
  86. $center = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  87. $tileLayer = new TileLayer([
  88. 'map' => 'test1',
  89. 'urlTemplate' => 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
  90. 'clientOptions' => [
  91. 'attribution' => 'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> ' .
  92. '<img src="http://developer.mapquest.com/content/osm/mq_logo.png">, ' .
  93. 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
  94. ]
  95. ]);
  96. $map = new LeafLet([
  97. 'tileLayer' => $tileLayer,
  98. 'center' => $center
  99. ]);
  100. $point = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  101. $marker = new Marker(['latLng' => $point, 'popupContent' => Html::encode('Le Chat des Noisettes')]);
  102. $map->addLayer($marker);
  103. return $this->render('index',[
  104. 'page_principale'=>true,
  105. 'produits' => $produits,
  106. 'model' => $model,
  107. 'map' => $map
  108. ]);
  109. }
  110. public function actionMentions()
  111. {
  112. return $this->render('mentions');
  113. }
  114. public function actionCommander() {
  115. if (Yii::$app->user->isGuest) {
  116. $this->redirect(Yii::$app->urlManager->createUrl('site/login')) ;
  117. }
  118. }
  119. public function actionLogin()
  120. {
  121. if (!\Yii::$app->user->isGuest) {
  122. return $this->goHome();
  123. }
  124. $model = new LoginForm();
  125. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  126. return $this->goBack();
  127. } else {
  128. return $this->render('login', [
  129. 'model' => $model,
  130. ]);
  131. }
  132. }
  133. public function actionLogout()
  134. {
  135. Yii::$app->user->logout();
  136. return $this->goHome();
  137. }
  138. public function actionContact()
  139. {
  140. $model = new ContactForm();
  141. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  142. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  143. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  144. } else {
  145. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  146. }
  147. return $this->refresh();
  148. } else {
  149. return $this->render('contact', [
  150. 'model' => $model,
  151. ]);
  152. }
  153. }
  154. public function actionAbout()
  155. {
  156. return $this->render('about');
  157. }
  158. public function actionSignup()
  159. {
  160. $model = new SignupForm();
  161. if ($model->load(Yii::$app->request->post())) {
  162. if ($user = $model->signup()) {
  163. if (Yii::$app->getUser()->login($user)) {
  164. //return $this->goHome();
  165. $this->redirect(['commande/index']) ;
  166. }
  167. }
  168. }
  169. return $this->render('signup', [
  170. 'model' => $model,
  171. ]);
  172. }
  173. public function actionRequestPasswordReset()
  174. {
  175. $model = new PasswordResetRequestForm();
  176. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  177. if ($model->sendEmail()) {
  178. Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
  179. return $this->goHome();
  180. } else {
  181. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  182. }
  183. }
  184. return $this->render('requestPasswordResetToken', [
  185. 'model' => $model,
  186. ]);
  187. }
  188. public function actionResetPassword($token)
  189. {
  190. try {
  191. $model = new ResetPasswordForm($token);
  192. } catch (InvalidParamException $e) {
  193. throw new BadRequestHttpException($e->getMessage());
  194. }
  195. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  196. Yii::$app->getSession()->setFlash('success', 'New password was saved.');
  197. return $this->goHome();
  198. }
  199. return $this->render('resetPassword', [
  200. 'model' => $model,
  201. ]);
  202. }
  203. }