Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

276 Zeilen
8.7KB

  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 frontend\controllers\FrontendController;
  18. use dosamigos\leaflet\types\LatLng;
  19. use dosamigos\leaflet\layers\Marker;
  20. use dosamigos\leaflet\layers\TileLayer;
  21. use dosamigos\leaflet\LeafLet;
  22. use dosamigos\leaflet\widgets\Map;
  23. /**
  24. * Site controller
  25. */
  26. class SiteController extends FrontendController
  27. {
  28. /**
  29. * @inheritdoc
  30. */
  31. public function behaviors()
  32. {
  33. return [
  34. 'access' => [
  35. 'class' => AccessControl::className(),
  36. 'only' => ['logout', 'signup'],
  37. 'rules' => [
  38. [
  39. 'actions' => ['signup'],
  40. 'allow' => true,
  41. 'roles' => ['?'],
  42. ],
  43. [
  44. 'actions' => ['logout'],
  45. 'allow' => true,
  46. 'roles' => ['@'],
  47. ],
  48. ],
  49. ],
  50. 'verbs' => [
  51. 'class' => VerbFilter::className(),
  52. 'actions' => [
  53. 'logout' => ['get'],
  54. ],
  55. ],
  56. ];
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function actions()
  62. {
  63. return [
  64. 'error' => [
  65. 'class' => 'yii\web\ErrorAction',
  66. ],
  67. 'captcha' => [
  68. 'class' => 'yii\captcha\CaptchaAction',
  69. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  70. ],
  71. ];
  72. }
  73. public function actionIndex()
  74. {
  75. // redirection de l'utilisateur vers le tableau de bord s'il est connecté
  76. if(!Yii::$app->user->isGuest) {
  77. return $this->redirect(['commande/index']) ;
  78. }
  79. // produits
  80. $produits = Produit::find()->orderBy('order ASC')->all() ;
  81. // contact
  82. $model = new ContactForm();
  83. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  84. if ($model->sendEmail('matthieu@lechatdesnoisettes.com')) {
  85. Yii::$app->session->setFlash('success', "Votre message a bien été envoyé, j'y répondrai dès que possible.");
  86. } else {
  87. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  88. }
  89. return $this->refresh();
  90. }
  91. // map
  92. $center = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  93. $tileLayer = new TileLayer([
  94. 'map' => 'test1',
  95. 'urlTemplate' => 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
  96. 'clientOptions' => [
  97. 'attribution' => 'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> ' .
  98. '<img src="http://developer.mapquest.com/content/osm/mq_logo.png">, ' .
  99. '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>',
  100. ]
  101. ]);
  102. $map = new LeafLet([
  103. 'tileLayer' => $tileLayer,
  104. 'center' => $center
  105. ]);
  106. $point = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  107. $marker = new Marker(['latLng' => $point, 'popupContent' => Html::encode('Le Chat des Noisettes')]);
  108. $map->addLayer($marker);
  109. return $this->render('index',[
  110. 'page_principale'=>true,
  111. 'produits' => $produits,
  112. 'model' => $model,
  113. 'map' => $map
  114. ]);
  115. }
  116. public function actionMentions()
  117. {
  118. return $this->render('mentions');
  119. }
  120. public function actionCommander() {
  121. if (Yii::$app->user->isGuest) {
  122. $this->redirect(Yii::$app->urlManager->createUrl('site/login')) ;
  123. }
  124. }
  125. public function actionLogin()
  126. {
  127. if (!\Yii::$app->user->isGuest) {
  128. return Yii::$app->getResponse()->redirect(['commande/index']);
  129. }
  130. $model = new LoginForm();
  131. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  132. return $this->goBack(['commande/index']);
  133. } else {
  134. return $this->render('login', [
  135. 'model' => $model,
  136. ]);
  137. }
  138. }
  139. public function actionLogout()
  140. {
  141. Yii::$app->user->logout();
  142. return $this->goHome();
  143. }
  144. public function actionContact()
  145. {
  146. $model = new ContactForm();
  147. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  148. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  149. Yii::$app->session->setFlash('success', 'Votre message a bien été envoyé. Nous vous répondrons dès que possible.');
  150. } else {
  151. Yii::$app->session->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.');
  152. }
  153. return $this->refresh();
  154. } else {
  155. return $this->render('contact', [
  156. 'model' => $model,
  157. ]);
  158. }
  159. }
  160. public function actionAbout()
  161. {
  162. return $this->render('about');
  163. }
  164. public function actionSignup()
  165. {
  166. $model = new SignupForm();
  167. if ($model->load(Yii::$app->request->post())) {
  168. if ($user = $model->signup()) {
  169. if (Yii::$app->getUser()->login($user)) {
  170. $etablissement = null ;
  171. if($model->id_etablissement)
  172. {
  173. $etablissement = Etablissement::findOne($model->id_etablissement) ;
  174. }
  175. Yii::$app->mailer->compose(
  176. [
  177. 'html' => 'signup-html',
  178. 'text' => 'signup-text'
  179. ],
  180. [
  181. 'user' => $user,
  182. 'etablissement' => $etablissement
  183. ])
  184. ->setTo($user->email)
  185. ->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain'])
  186. ->setSubject('[La boîte à pain] Inscription')
  187. ->send();
  188. $this->redirect(['commande/index']) ;
  189. }
  190. }
  191. }
  192. // liste des boulangeries disponibles
  193. $etablissements = Etablissement::getEtablissementsPopulateDropdown() ;
  194. $data_etablissements_dispos = $etablissements['data'] ;
  195. $options_etablissements_dispos = $etablissements['options'] ;
  196. return $this->render('signup', [
  197. 'model' => $model,
  198. 'data_etablissements_dispos' => $data_etablissements_dispos,
  199. 'options_etablissements_dispos' => $options_etablissements_dispos,
  200. ]);
  201. }
  202. public function actionRequestPasswordReset()
  203. {
  204. $model = new PasswordResetRequestForm();
  205. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  206. if ($model->sendEmail()) {
  207. 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.');
  208. return $this->goHome();
  209. } else {
  210. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  211. }
  212. }
  213. return $this->render('requestPasswordResetToken', [
  214. 'model' => $model,
  215. ]);
  216. }
  217. public function actionResetPassword($token)
  218. {
  219. try {
  220. $model = new ResetPasswordForm($token);
  221. } catch (InvalidParamException $e) {
  222. throw new BadRequestHttpException($e->getMessage());
  223. }
  224. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  225. Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.');
  226. return $this->goHome();
  227. }
  228. return $this->render('resetPassword', [
  229. 'model' => $model,
  230. ]);
  231. }
  232. public function actionCgv()
  233. {
  234. return $this->render('cgv') ;
  235. }
  236. public function actionCreditpain()
  237. {
  238. return $this->render('creditpain') ;
  239. }
  240. }