Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

278 Zeilen
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 !== null) {
  72. return $this->render('error', ['exception' => $exception]);
  73. }
  74. if( Yii::$app->getRequest()->getQueryParam('producer_not_found')) {
  75. return $this->render('error-404-producer', ['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 actionEtablissement($id_etablissement) {
  191. $model_login = new LoginForm();
  192. $model_signup = new SignupForm();
  193. $etablissement = Etablissement::findOne($id_etablissement);
  194. if (Yii::$app->user->isGuest) {
  195. if ($model_login->load(Yii::$app->request->post()) && $model_login->login()) {
  196. // ajout de l'établissement en favoris si ce n'est pas encore le cas
  197. $user_etablissement_exist = UserEtablissement::find()
  198. ->where([
  199. 'id_user' => Yii::$app->user->id,
  200. 'id_etablissement' => $id_etablissement
  201. ])->one();
  202. if (!$user_etablissement_exist) {
  203. $etab_user = new UserEtablissement;
  204. $etab_user->id_etablissement = $id_etablissement;
  205. $etab_user->id_user = Yii::$app->user->id;
  206. $etab_user->credit = 0;
  207. $etab_user->actif = 1;
  208. $etab_user->save();
  209. } else {
  210. if (!$user_etablissement_exist->actif) {
  211. $user_etablissement_exist->actif = 1;
  212. $user_etablissement_exist->save();
  213. }
  214. }
  215. $this->redirect(['commande/index']);
  216. }
  217. if ($model_signup->load(Yii::$app->request->post())) {
  218. $model_signup->id_etablissement = $id_etablissement;
  219. $model_signup->option_client_boulanger = 'client';
  220. if ($user = $model_signup->signup()) {
  221. if (Yii::$app->getUser()->login($user)) {
  222. $this->redirect(['commande/index']);
  223. }
  224. }
  225. }
  226. } else {
  227. $this->redirect(['commande/create', 'id_etablissement' => $id_etablissement]);
  228. }
  229. return $this->render('etablissement', [
  230. 'model_login' => $model_login,
  231. 'model_signup' => $model_signup,
  232. 'etablissement' => $etablissement,
  233. ]);
  234. }
  235. }