Guest User

Untitled

a guest
May 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use app\models\Events;
  6. use app\models\SignupForm;
  7. use Yii;
  8. use yii\filters\AccessControl;
  9. use yii\web\Controller;
  10. use yii\web\Response;
  11. use yii\filters\VerbFilter;
  12. use app\models\LoginForm;
  13. use app\models\ContactForm;
  14.  
  15. class SiteController extends Controller
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function behaviors()
  21. {
  22. return [
  23. 'access' => [
  24. 'class' => AccessControl::className(),
  25. 'only' => ['logout', 'index'],
  26. 'rules' => [
  27. [
  28. 'actions' => ['logout', 'index'],
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. ],
  32. ],
  33. ],
  34. 'verbs' => [
  35. 'class' => VerbFilter::className(),
  36. 'actions' => [
  37. 'logout' => ['post'],
  38. ],
  39. ],
  40. ];
  41. }
  42.  
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function actions()
  47. {
  48. return [
  49. 'error' => [
  50. 'class' => 'yii\web\ErrorAction',
  51. ],
  52. 'captcha' => [
  53. 'class' => 'yii\captcha\CaptchaAction',
  54. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  55. ],
  56. ];
  57. }
  58.  
  59. /**
  60. * Displays homepage.
  61. *
  62. * @return string
  63. */
  64. public function actionIndex()
  65. {
  66. return $this->render('index');
  67. }
  68.  
  69. /**
  70. * Login action.
  71. *
  72. * @return Response|string
  73. */
  74. public function actionLogin()
  75. {
  76. if (!Yii::$app->user->isGuest) {
  77. return $this->goHome();
  78. }
  79.  
  80. $model = new LoginForm();
  81. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  82. return $this->goBack();
  83. }
  84.  
  85. $model->password = '';
  86. return $this->render('login', [
  87. 'model' => $model,
  88. ]);
  89. }
  90.  
  91. public function actionSignup()
  92. {
  93. $model = new SignupForm();
  94.  
  95. if ($model->load(Yii::$app->request->post())) {
  96. if ($user = $model->signup()) {
  97. if (Yii::$app->getUser()->login($user)) {
  98. return $this->goHome();
  99. }
  100. }
  101. }
  102.  
  103. return $this->render('signup', [
  104. 'model' => $model,
  105. ]);
  106. }
  107.  
  108. /**
  109. * Logout action.
  110. *
  111. * @return Response
  112. */
  113. public function actionLogout()
  114. {
  115. Yii::$app->user->logout();
  116.  
  117. return $this->goHome();
  118. }
  119.  
  120. public function actionJsoncalendar($start = null, $end = null, $_ = null)
  121. {
  122. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  123.  
  124. $eventModels = Events::find()->where(['user_id' => Yii::$app->user->id])->all();
  125. $events = [];
  126.  
  127. foreach ($eventModels as $eventModel) {
  128. $Event = new \yii2fullcalendar\models\Event();
  129. $Event->id = $eventModel->id;
  130. $Event->title = $eventModel->title;
  131. $Event->start = $eventModel->start;
  132. $Event->end = $eventModel->end;
  133. $events[] = $Event;
  134. }
  135.  
  136. return $events;
  137. }
  138.  
  139. public function actionAddEvent($title = '', $start = '', $end = '')
  140. {
  141. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  142. $event = new Events();
  143. $event->title = $title;
  144. $event->start = $start;
  145. $event->user_id = (string)Yii::$app->user->id;
  146. $event->end = $end;
  147. if (!$event->save()) {
  148. return $event->getErrors();
  149. }
  150. return $event->id;
  151. }
  152.  
  153. public function actionChangeEvent($title = '', $id = '', $start = '', $end = '')
  154. {
  155. If (!$id) {
  156. return '';
  157. }
  158. $event = Events::findOne($id);
  159. If (!$event) {
  160. return '';
  161. }
  162. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  163. $event->title = $title ? $title : $event->title;
  164. $event->start = $start ? $start : $event->start;
  165. $event->end = $end ? $end : $event->end;
  166. if (!$event->update()) {
  167. return $event->getErrors();
  168. }
  169. }
  170.  
  171. public function actionDeleteEvent($id = '')
  172. {
  173. If (!$id) {
  174. return '';
  175. }
  176. $event = Events::findOne($id);
  177. If (!$event) {
  178. return '';
  179. }
  180. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  181. if (!$event->delete()) {
  182. return $event->getErrors();
  183. }
  184. }
  185.  
  186. }
Add Comment
Please, Sign In to add comment