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.

SiteController.php 10.0KB

пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 $this->goBack(['commande/index']);
  128. } else {
  129. return $this->render('login', [
  130. 'model' => $model,
  131. ]);
  132. }
  133. }
  134. public function actionLogout() {
  135. Yii::$app->user->logout();
  136. return $this->goHome();
  137. }
  138. public function actionContact() {
  139. $model = new ContactForm();
  140. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  141. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  142. Yii::$app->session->setFlash('success', 'Votre message a bien été envoyé. Nous vous répondrons dès que possible.');
  143. } else {
  144. Yii::$app->session->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.');
  145. }
  146. return $this->refresh();
  147. } else {
  148. return $this->render('contact', [
  149. 'model' => $model,
  150. ]);
  151. }
  152. }
  153. public function actionAbout() {
  154. return $this->render('about');
  155. }
  156. public function actionSignup() {
  157. $model = new SignupForm();
  158. if ($model->load(Yii::$app->request->post())) {
  159. if ($user = $model->signup()) {
  160. if (Yii::$app->getUser()->login($user)) {
  161. $this->redirect(['commande/index']);
  162. }
  163. }
  164. }
  165. // liste des établissements disponibles
  166. $etablissements = Etablissement::getEtablissementsPopulateDropdown();
  167. $data_etablissements_dispos = $etablissements['data'];
  168. $options_etablissements_dispos = $etablissements['options'];
  169. return $this->render('signup', [
  170. 'model' => $model,
  171. 'data_etablissements_dispos' => $data_etablissements_dispos,
  172. 'options_etablissements_dispos' => $options_etablissements_dispos,
  173. ]);
  174. }
  175. public function actionRequestPasswordReset() {
  176. $model = new PasswordResetRequestForm();
  177. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  178. if ($model->sendEmail()) {
  179. 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.');
  180. return $this->goHome();
  181. } else {
  182. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  183. }
  184. }
  185. return $this->render('requestPasswordResetToken', [
  186. 'model' => $model,
  187. ]);
  188. }
  189. public function actionResetPassword($token) {
  190. try {
  191. $model = new ResetPasswordForm($token);
  192. } catch (InvalidParamException $e) {
  193. throw new BadRequestHttpException($e->getMessage());
  194. }
  195. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  196. Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.');
  197. return $this->goHome();
  198. }
  199. return $this->render('resetPassword', [
  200. 'model' => $model,
  201. ]);
  202. }
  203. public function actionCgv() {
  204. return $this->render('cgv');
  205. }
  206. public function actionCreditpain() {
  207. return $this->render('creditpain');
  208. }
  209. public function actionEtablissement($id_etablissement) {
  210. $model_login = new LoginForm();
  211. $model_signup = new SignupForm();
  212. $etablissement = Etablissement::findOne($id_etablissement);
  213. if (Yii::$app->user->isGuest) {
  214. if ($model_login->load(Yii::$app->request->post()) && $model_login->login()) {
  215. // ajout de l'établissement en favoris si ce n'est pas encore le cas
  216. $user_etablissement_exist = UserEtablissement::find()
  217. ->where([
  218. 'id_user' => Yii::$app->user->id,
  219. 'id_etablissement' => $id_etablissement
  220. ])->one();
  221. if (!$user_etablissement_exist) {
  222. $etab_user = new UserEtablissement;
  223. $etab_user->id_etablissement = $id_etablissement;
  224. $etab_user->id_user = Yii::$app->user->id;
  225. $etab_user->credit = 0;
  226. $etab_user->actif = 1;
  227. $etab_user->save();
  228. } else {
  229. if (!$user_etablissement_exist->actif) {
  230. $user_etablissement_exist->actif = 1;
  231. $user_etablissement_exist->save();
  232. }
  233. }
  234. $this->redirect(['commande/index']);
  235. }
  236. if ($model_signup->load(Yii::$app->request->post())) {
  237. $model_signup->id_etablissement = $id_etablissement;
  238. $model_signup->option_client_boulanger = 'client';
  239. if ($user = $model_signup->signup()) {
  240. if (Yii::$app->getUser()->login($user)) {
  241. $this->redirect(['commande/index']);
  242. }
  243. }
  244. }
  245. } else {
  246. $this->redirect(['commande/create', 'id_etablissement' => $id_etablissement]);
  247. }
  248. return $this->render('etablissement', [
  249. 'model_login' => $model_login,
  250. 'model_signup' => $model_signup,
  251. 'etablissement' => $etablissement,
  252. ]);
  253. }
  254. }