您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

318 行
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. /**
  30. * @inheritdoc
  31. */
  32. public function behaviors()
  33. {
  34. return [
  35. 'access' => [
  36. 'class' => AccessControl::className(),
  37. 'only' => ['logout', 'signup'],
  38. 'rules' => [
  39. [
  40. 'actions' => ['signup'],
  41. 'allow' => true,
  42. 'roles' => ['?'],
  43. ],
  44. [
  45. 'actions' => ['logout'],
  46. 'allow' => true,
  47. 'roles' => ['@'],
  48. ],
  49. ],
  50. ],
  51. 'verbs' => [
  52. 'class' => VerbFilter::className(),
  53. 'actions' => [
  54. 'logout' => ['get'],
  55. ],
  56. ],
  57. ];
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function actions()
  63. {
  64. return [
  65. 'error' => [
  66. 'class' => 'yii\web\ErrorAction',
  67. ],
  68. 'captcha' => [
  69. 'class' => 'yii\captcha\CaptchaAction',
  70. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  71. ],
  72. ];
  73. }
  74. public function actionIndex()
  75. {
  76. // redirection de l'utilisateur vers le tableau de bord s'il est connecté
  77. if(!Yii::$app->user->isGuest) {
  78. return $this->redirect(['commande/index']) ;
  79. }
  80. // produits
  81. $produits = Produit::find()->orderBy('order ASC')->all() ;
  82. // contact
  83. $model = new ContactForm();
  84. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  85. if ($model->sendEmail('matthieu@lechatdesnoisettes.com')) {
  86. Yii::$app->session->setFlash('success', "Votre message a bien été envoyé, j'y répondrai dès que possible.");
  87. } else {
  88. Yii::$app->session->setFlash('error', 'There was an error sending email.');
  89. }
  90. return $this->refresh();
  91. }
  92. // map
  93. $center = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  94. $tileLayer = new TileLayer([
  95. 'map' => 'test1',
  96. 'urlTemplate' => 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
  97. 'clientOptions' => [
  98. 'attribution' => 'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> ' .
  99. '<img src="http://developer.mapquest.com/content/osm/mq_logo.png">, ' .
  100. '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>',
  101. ]
  102. ]);
  103. $map = new LeafLet([
  104. 'tileLayer' => $tileLayer,
  105. 'center' => $center
  106. ]);
  107. $point = new LatLng(['lat' => '46,9991224', 'lng' => '6,0582595']);
  108. $marker = new Marker(['latLng' => $point, 'popupContent' => Html::encode('Le Chat des Noisettes')]);
  109. $map->addLayer($marker);
  110. return $this->render('index',[
  111. 'page_principale'=>true,
  112. 'produits' => $produits,
  113. 'model' => $model,
  114. 'map' => $map
  115. ]);
  116. }
  117. public function actionMentions()
  118. {
  119. return $this->render('mentions');
  120. }
  121. public function actionCommander() {
  122. if (Yii::$app->user->isGuest) {
  123. $this->redirect(Yii::$app->urlManager->createUrl('site/login')) ;
  124. }
  125. }
  126. public function actionLogin()
  127. {
  128. if (!\Yii::$app->user->isGuest) {
  129. return Yii::$app->getResponse()->redirect(['commande/index']);
  130. }
  131. $model = new LoginForm();
  132. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  133. return $this->goBack(['commande/index']);
  134. } else {
  135. return $this->render('login', [
  136. 'model' => $model,
  137. ]);
  138. }
  139. }
  140. public function actionLogout()
  141. {
  142. Yii::$app->user->logout();
  143. return $this->goHome();
  144. }
  145. public function actionContact()
  146. {
  147. $model = new ContactForm();
  148. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  149. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  150. Yii::$app->session->setFlash('success', 'Votre message a bien été envoyé. Nous vous répondrons dès que possible.');
  151. } else {
  152. Yii::$app->session->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.');
  153. }
  154. return $this->refresh();
  155. } else {
  156. return $this->render('contact', [
  157. 'model' => $model,
  158. ]);
  159. }
  160. }
  161. public function actionAbout()
  162. {
  163. return $this->render('about');
  164. }
  165. public function actionSignup()
  166. {
  167. $model = new SignupForm();
  168. if ($model->load(Yii::$app->request->post())) {
  169. if ($user = $model->signup()) {
  170. if (Yii::$app->getUser()->login($user)) {
  171. $this->redirect(['commande/index']) ;
  172. }
  173. }
  174. }
  175. // liste des établissements disponibles
  176. $etablissements = Etablissement::getEtablissementsPopulateDropdown() ;
  177. $data_etablissements_dispos = $etablissements['data'] ;
  178. $options_etablissements_dispos = $etablissements['options'] ;
  179. return $this->render('signup', [
  180. 'model' => $model,
  181. 'data_etablissements_dispos' => $data_etablissements_dispos,
  182. 'options_etablissements_dispos' => $options_etablissements_dispos,
  183. ]);
  184. }
  185. public function actionRequestPasswordReset()
  186. {
  187. $model = new PasswordResetRequestForm();
  188. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  189. if ($model->sendEmail()) {
  190. 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.');
  191. return $this->goHome();
  192. } else {
  193. Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  194. }
  195. }
  196. return $this->render('requestPasswordResetToken', [
  197. 'model' => $model,
  198. ]);
  199. }
  200. public function actionResetPassword($token)
  201. {
  202. try {
  203. $model = new ResetPasswordForm($token);
  204. } catch (InvalidParamException $e) {
  205. throw new BadRequestHttpException($e->getMessage());
  206. }
  207. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  208. Yii::$app->getSession()->setFlash('success', 'Votre nouveau mot de passe vient d\'être sauvegardé.');
  209. return $this->goHome();
  210. }
  211. return $this->render('resetPassword', [
  212. 'model' => $model,
  213. ]);
  214. }
  215. public function actionCgv()
  216. {
  217. return $this->render('cgv') ;
  218. }
  219. public function actionCreditpain()
  220. {
  221. return $this->render('creditpain') ;
  222. }
  223. public function actionEtablissement($id_etablissement)
  224. {
  225. $model_login = new LoginForm();
  226. $model_signup = new SignupForm();
  227. $etablissement = Etablissement::findOne($id_etablissement) ;
  228. if(Yii::$app->user->isGuest)
  229. {
  230. if ($model_login->load(Yii::$app->request->post()) && $model_login->login())
  231. {
  232. // ajout de l'établissement en favoris si ce n'est pas encore le cas
  233. $user_etablissement_exist = UserEtablissement::find()
  234. ->where([
  235. 'id_user' => Yii::$app->user->id,
  236. 'id_etablissement' => $id_etablissement
  237. ])->one();
  238. if(!$user_etablissement_exist)
  239. {
  240. $etab_user = new UserEtablissement ;
  241. $etab_user->id_etablissement = $id_etablissement ;
  242. $etab_user->id_user = Yii::$app->user->id ;
  243. $etab_user->credit = 0 ;
  244. $etab_user->actif = 1 ;
  245. $etab_user->save() ;
  246. }
  247. else {
  248. if(!$user_etablissement_exist->actif)
  249. {
  250. $user_etablissement_exist->actif = 1 ;
  251. $user_etablissement_exist->save() ;
  252. }
  253. }
  254. $this->redirect(['commande/index']) ;
  255. }
  256. if ($model_signup->load(Yii::$app->request->post())) {
  257. $model_signup->id_etablissement = $id_etablissement ;
  258. $model_signup->option_client_boulanger = 'client' ;
  259. if ($user = $model_signup->signup()) {
  260. if (Yii::$app->getUser()->login($user)) {
  261. $this->redirect(['commande/index']) ;
  262. }
  263. }
  264. }
  265. }
  266. else {
  267. $this->redirect(['commande/create','id_etablissement' => $id_etablissement]) ;
  268. }
  269. return $this->render('etablissement',[
  270. 'model_login' => $model_login,
  271. 'model_signup' => $model_signup,
  272. 'etablissement' => $etablissement,
  273. ]) ;
  274. }
  275. }