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.

280 lines
8.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 common\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. 'captcha' => [
  63. 'class' => 'yii\captcha\CaptchaAction',
  64. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  65. ],
  66. ];
  67. }
  68. public function actionError()
  69. {
  70. $exception = Yii::$app->errorHandler->exception;
  71. if($exception->getMessage() == 'Établissement introuvable' || Yii::$app->getRequest()->getQueryParam('producer_not_found')) {
  72. return $this->render('error-404-producer', ['exception' => $exception]);
  73. }
  74. if ($exception !== null) {
  75. return $this->render('error', ['exception' => $exception]);
  76. }
  77. }
  78. public function actionIndex() {
  79. return $this->render('index');
  80. }
  81. public function actionProducers() {
  82. $data_provider_producers = new ActiveDataProvider([
  83. 'query' => Etablissement::find()
  84. ->where([
  85. 'actif' => true,
  86. ])
  87. ->orderBy('nom ASC'),
  88. 'pagination' => [
  89. 'pageSize' => 100,
  90. ],
  91. ]);
  92. return $this->render('producers',[
  93. 'data_provider_producers' => $data_provider_producers
  94. ]);
  95. }
  96. public function actionMentions() {
  97. return $this->render('mentions');
  98. }
  99. public function actionLogin() {
  100. if (!\Yii::$app->user->isGuest) {
  101. return Yii::$app->getResponse()->redirect(['commande/index']);
  102. }
  103. $model = new LoginForm();
  104. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  105. $return_url = Yii::$app->request->get('return_url');
  106. if($return_url){
  107. return $this->redirect($return_url);
  108. }
  109. else {
  110. return $this->goBack();
  111. }
  112. } else {
  113. return $this->render('@frontend/views/site/login', [
  114. 'model' => $model,
  115. ]);
  116. }
  117. }
  118. public function actionLogout() {
  119. Yii::$app->user->logout();
  120. return $this->goHome();
  121. }
  122. public function actionContact() {
  123. $model = new ContactForm();
  124. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  125. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  126. Yii::$app->session->setFlash('success', 'Votre message a bien été envoyé. Nous vous répondrons dès que possible.');
  127. } else {
  128. Yii::$app->session->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.');
  129. }
  130. return $this->refresh();
  131. } else {
  132. return $this->render('contact', [
  133. 'model' => $model,
  134. ]);
  135. }
  136. }
  137. public function actionSignup() {
  138. $model = new SignupForm();
  139. if ($model->load(Yii::$app->request->post())) {
  140. if ($user = $model->signup()) {
  141. if (Yii::$app->getUser()->login($user)) {
  142. $this->redirect(['commande/index']);
  143. }
  144. }
  145. }
  146. // liste des établissements disponibles
  147. $etablissements = Etablissement::getEtablissementsPopulateDropdown();
  148. $data_etablissements_dispos = $etablissements['data'];
  149. $options_etablissements_dispos = $etablissements['options'];
  150. return $this->render('signup', [
  151. 'model' => $model,
  152. 'data_etablissements_dispos' => $data_etablissements_dispos,
  153. 'options_etablissements_dispos' => $options_etablissements_dispos,
  154. ]);
  155. }
  156. public function actionRequestPasswordReset() {
  157. $model = new PasswordResetRequestForm();
  158. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  159. if ($model->sendEmail()) {
  160. 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.');
  161. return $this->goHome();
  162. } else {
  163. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  164. }
  165. }
  166. return $this->render('requestPasswordResetToken', [
  167. 'model' => $model,
  168. ]);
  169. }
  170. public function actionResetPassword($token) {
  171. try {
  172. $model = new ResetPasswordForm($token);
  173. } catch (InvalidParamException $e) {
  174. throw new BadRequestHttpException($e->getMessage());
  175. }
  176. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  177. Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.');
  178. return $this->goHome();
  179. }
  180. return $this->render('resetPassword', [
  181. 'model' => $model,
  182. ]);
  183. }
  184. public function actionCgv() {
  185. return $this->render('cgv');
  186. }
  187. public function actionCreditpain() {
  188. return $this->render('creditpain');
  189. }
  190. public function actionProducerCode($id) {
  191. $producer = Etablissement::findOne($id);
  192. if(!$producer)
  193. throw new \yii\web\HttpException(404, 'Établissement introuvable');
  194. $model_producer_code = new ProducerCodeForm ;
  195. $model_producer_code->id_producer = $id ;
  196. if($model_producer_code->load(Yii::$app->request->post()) && $model_producer_code->validate()) {
  197. Etablissement::addUser(Yii::$app->user->id, $id) ;
  198. $this->redirect(Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $producer->slug]));
  199. }
  200. return $this->render('producer_code',[
  201. 'producer' => $producer,
  202. 'model_producer_code' => $model_producer_code,
  203. ]) ;
  204. }
  205. public function actionProducer($id) {
  206. $model_login = new LoginForm();
  207. $model_signup = new SignupForm();
  208. $etablissement = Etablissement::findOne($id);
  209. $model_login->id_etablissement = $id;
  210. $model_signup->id_etablissement = $id;
  211. $model_signup->option_client_boulanger = 'client';
  212. $return_url = Yii::$app->request->get('return_url', Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $etablissement->slug]));
  213. if (Yii::$app->user->isGuest) {
  214. if ($model_login->load(Yii::$app->request->post()) && $model_login->login()) {
  215. Etablissement::addUser(Yii::$app->user->id, $id) ;
  216. $this->redirect($return_url);
  217. }
  218. if ($model_signup->load(Yii::$app->request->post())) {
  219. if ($user = $model_signup->signup()) {
  220. if (Yii::$app->getUser()->login($user)) {
  221. $this->redirect($return_url);
  222. }
  223. }
  224. }
  225. } else {
  226. $this->redirect($return_url);
  227. }
  228. return $this->render('producer', [
  229. 'model_login' => $model_login,
  230. 'model_signup' => $model_signup,
  231. 'etablissement' => $etablissement,
  232. ]);
  233. }
  234. }