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

240 行
7.3KB

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