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.

268 Zeilen
8.5KB

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