You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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