Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

254 rindas
7.8KB

  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', 'Thank you for contacting us. We will respond to you as soon as possible.');
  149. } else {
  150. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  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. //return $this->goHome();
  170. $this->redirect(['commande/index']) ;
  171. }
  172. }
  173. }
  174. // liste des boulangeries disponibles
  175. $etablissements = Etablissement::getEtablissementsPopulateDropdown() ;
  176. $data_etablissements_dispos = $etablissements['data'] ;
  177. $options_etablissements_dispos = $etablissements['options'] ;
  178. return $this->render('signup', [
  179. 'model' => $model,
  180. 'data_etablissements_dispos' => $data_etablissements_dispos,
  181. 'options_etablissements_dispos' => $options_etablissements_dispos,
  182. ]);
  183. }
  184. public function actionRequestPasswordReset()
  185. {
  186. $model = new PasswordResetRequestForm();
  187. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  188. if ($model->sendEmail()) {
  189. 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.');
  190. return $this->goHome();
  191. } else {
  192. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  193. }
  194. }
  195. return $this->render('requestPasswordResetToken', [
  196. 'model' => $model,
  197. ]);
  198. }
  199. public function actionResetPassword($token)
  200. {
  201. try {
  202. $model = new ResetPasswordForm($token);
  203. } catch (InvalidParamException $e) {
  204. throw new BadRequestHttpException($e->getMessage());
  205. }
  206. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  207. Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.');
  208. return $this->goHome();
  209. }
  210. return $this->render('resetPassword', [
  211. 'model' => $model,
  212. ]);
  213. }
  214. public function actionCgv()
  215. {
  216. return $this->render('cgv') ;
  217. }
  218. public function actionCreditpain()
  219. {
  220. return $this->render('creditpain') ;
  221. }
  222. }