Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

304 lines
10KB

  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 common\models\UserEtablissement;
  19. use dosamigos\leaflet\types\LatLng;
  20. use dosamigos\leaflet\layers\Marker;
  21. use dosamigos\leaflet\layers\TileLayer;
  22. use dosamigos\leaflet\LeafLet;
  23. use dosamigos\leaflet\widgets\Map;
  24. /**
  25. * Site controller
  26. */
  27. class SiteController extends FrontendController {
  28. /**
  29. * @inheritdoc
  30. */
  31. public function behaviors() {
  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. 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. // redirection de l'utilisateur vers le tableau de bord s'il est connecté
  73. if (!Yii::$app->user->isGuest) {
  74. return $this->redirect(['commande/index']);
  75. }
  76. // produits
  77. $produits = Produit::find()->orderBy('order ASC')->all();
  78. // contact
  79. $model = new ContactForm();
  80. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  81. if ($model->sendEmail('matthieu@lechatdesnoisettes.com')) {
  82. Yii::$app->session->setFlash('success', "Votre message a bien été envoyé, j'y répondrai dès que possible.");
  83. } else {
  84. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  85. }
  86. return $this->refresh();
  87. }
  88. // map
  89. $center = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  90. $tileLayer = new TileLayer([
  91. 'map' => 'test1',
  92. 'urlTemplate' => 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
  93. 'clientOptions' => [
  94. 'attribution' => 'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> ' .
  95. '<img src="http://developer.mapquest.com/content/osm/mq_logo.png">, ' .
  96. '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>',
  97. ]
  98. ]);
  99. $map = new LeafLet([
  100. 'tileLayer' => $tileLayer,
  101. 'center' => $center
  102. ]);
  103. $point = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  104. $marker = new Marker(['latLng' => $point, 'popupContent' => Html::encode('Le Chat des Noisettes')]);
  105. $map->addLayer($marker);
  106. return $this->render('index', [
  107. 'page_principale' => true,
  108. 'produits' => $produits,
  109. 'model' => $model,
  110. 'map' => $map
  111. ]);
  112. }
  113. public function actionMentions() {
  114. return $this->render('mentions');
  115. }
  116. public function actionCommander() {
  117. if (Yii::$app->user->isGuest) {
  118. $this->redirect(Yii::$app->urlManager->createUrl('site/login'));
  119. }
  120. }
  121. public function actionLogin() {
  122. if (!\Yii::$app->user->isGuest) {
  123. return Yii::$app->getResponse()->redirect(['commande/index']);
  124. }
  125. $model = new LoginForm();
  126. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  127. $return_url = Yii::$app->request->get('return_url');
  128. if($return_url){
  129. return $this->redirect($return_url);
  130. }
  131. else {
  132. return $this->goBack();
  133. }
  134. } else {
  135. return $this->render('@frontend/views/site/login', [
  136. 'model' => $model,
  137. ]);
  138. }
  139. }
  140. public function actionLogout() {
  141. Yii::$app->user->logout();
  142. return $this->goHome();
  143. }
  144. public function actionContact() {
  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. return $this->render('about');
  161. }
  162. public function actionSignup() {
  163. $model = new SignupForm();
  164. if ($model->load(Yii::$app->request->post())) {
  165. if ($user = $model->signup()) {
  166. if (Yii::$app->getUser()->login($user)) {
  167. $this->redirect(['commande/index']);
  168. }
  169. }
  170. }
  171. // liste des établissements disponibles
  172. $etablissements = Etablissement::getEtablissementsPopulateDropdown();
  173. $data_etablissements_dispos = $etablissements['data'];
  174. $options_etablissements_dispos = $etablissements['options'];
  175. return $this->render('signup', [
  176. 'model' => $model,
  177. 'data_etablissements_dispos' => $data_etablissements_dispos,
  178. 'options_etablissements_dispos' => $options_etablissements_dispos,
  179. ]);
  180. }
  181. public function actionRequestPasswordReset() {
  182. $model = new PasswordResetRequestForm();
  183. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  184. if ($model->sendEmail()) {
  185. 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.');
  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. try {
  197. $model = new ResetPasswordForm($token);
  198. } catch (InvalidParamException $e) {
  199. throw new BadRequestHttpException($e->getMessage());
  200. }
  201. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  202. Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.');
  203. return $this->goHome();
  204. }
  205. return $this->render('resetPassword', [
  206. 'model' => $model,
  207. ]);
  208. }
  209. public function actionCgv() {
  210. return $this->render('cgv');
  211. }
  212. public function actionCreditpain() {
  213. return $this->render('creditpain');
  214. }
  215. public function actionEtablissement($id_etablissement) {
  216. $model_login = new LoginForm();
  217. $model_signup = new SignupForm();
  218. $etablissement = Etablissement::findOne($id_etablissement);
  219. if (Yii::$app->user->isGuest) {
  220. if ($model_login->load(Yii::$app->request->post()) && $model_login->login()) {
  221. // ajout de l'établissement en favoris si ce n'est pas encore le cas
  222. $user_etablissement_exist = UserEtablissement::find()
  223. ->where([
  224. 'id_user' => Yii::$app->user->id,
  225. 'id_etablissement' => $id_etablissement
  226. ])->one();
  227. if (!$user_etablissement_exist) {
  228. $etab_user = new UserEtablissement;
  229. $etab_user->id_etablissement = $id_etablissement;
  230. $etab_user->id_user = Yii::$app->user->id;
  231. $etab_user->credit = 0;
  232. $etab_user->actif = 1;
  233. $etab_user->save();
  234. } else {
  235. if (!$user_etablissement_exist->actif) {
  236. $user_etablissement_exist->actif = 1;
  237. $user_etablissement_exist->save();
  238. }
  239. }
  240. $this->redirect(['commande/index']);
  241. }
  242. if ($model_signup->load(Yii::$app->request->post())) {
  243. $model_signup->id_etablissement = $id_etablissement;
  244. $model_signup->option_client_boulanger = 'client';
  245. if ($user = $model_signup->signup()) {
  246. if (Yii::$app->getUser()->login($user)) {
  247. $this->redirect(['commande/index']);
  248. }
  249. }
  250. }
  251. } else {
  252. $this->redirect(['commande/create', 'id_etablissement' => $id_etablissement]);
  253. }
  254. return $this->render('etablissement', [
  255. 'model_login' => $model_login,
  256. 'model_signup' => $model_signup,
  257. 'etablissement' => $etablissement,
  258. ]);
  259. }
  260. }