DefaultController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/4/8
  6. * Time: 上午11:37
  7. */
  8. namespace common\modules\user\frontend\controllers;
  9. use common\models\Favourite;
  10. use common\models\Sign;
  11. use common\modules\user\models\User;
  12. use common\models\Article;
  13. use Yii;
  14. use yii\data\ActiveDataProvider;
  15. use yii\db\Expression;
  16. use yii\filters\AccessControl;
  17. use yii\web\Controller;
  18. use yii\web\NotFoundHttpException;
  19. class DefaultController extends Controller
  20. {
  21. public function behaviors()
  22. {
  23. return [
  24. 'access' => [
  25. 'class' => AccessControl::className(),
  26. 'except' => ['index'],
  27. 'rules' => [
  28. [
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. ],
  32. ],
  33. ],
  34. ];
  35. }
  36. public function actionIndex($id)
  37. {
  38. $user = User::find()->where(['id' => $id])->one();
  39. if (empty($user)) {
  40. throw new NotFoundHttpException('用户不存在!');
  41. }
  42. $monthStart = strtotime(date('Y-m-1'));
  43. $monthEnd = strtotime("+1 month", $monthStart);
  44. $signDays = Sign::find()->where(['user_id' => $id])->andWhere(['between', 'sign_at', $monthStart, $monthEnd])->select(new Expression('FROM_UNIXTIME(sign_at, "%d")'))->column();
  45. $daysNum = date('t');
  46. $year = date('Y');
  47. $month = date('m');
  48. $weeks = [];
  49. $i = 0;
  50. foreach (range(1, $daysNum) as $day) {
  51. $w = date('w', strtotime($year . '-' . $month . '-' . $day));
  52. $weeks[$i][$w]['day'] = $day;
  53. $weeks[$i][$w]['sign'] = in_array($day, $signDays);
  54. if ($w == 6) {
  55. $i++;
  56. }
  57. }
  58. return $this->render('index', [
  59. 'user' => $user,
  60. 'weeks' => $weeks
  61. ]);
  62. }
  63. public function actionArticleList()
  64. {
  65. $query = Article::find()->my()->notTrashed();
  66. $dataProvider = new ActiveDataProvider([
  67. 'query' => $query,
  68. 'sort' => [
  69. 'defaultOrder' => [
  70. 'id' => SORT_DESC
  71. ]
  72. ]
  73. ]);
  74. $pages = $dataProvider->getPagination();
  75. $models = $dataProvider->getModels();
  76. return $this->render('article/list', [
  77. 'models' => $models,
  78. 'pages' => $pages,
  79. ]);
  80. }
  81. public function actionCreateArticle($module = 'base')
  82. {
  83. $model = new Article(['module' => $module]);
  84. $moduleClass = $model->findModuleClass();
  85. $moduleModel = new $moduleClass();
  86. if ($model->load(\Yii::$app->request->post()) && $moduleModel->load(\Yii::$app->request->post()) && $model->validate() && $moduleModel->validate() && $model->save(false)) {
  87. $moduleModel->id = $model->id;
  88. if ($moduleModel->save(false)) {
  89. \Yii::$app->session->setFlash('success', '发布成功!');
  90. return $this->redirect(['create-article']);
  91. }
  92. }
  93. return $this->render('article/create', [
  94. 'model' => $model,
  95. 'moduleModel' => $moduleModel
  96. ]);
  97. }
  98. public function actionUpdateArticle($id)
  99. {
  100. $model = Article::find()->where(['id' => $id])->my()->with('data')->one();
  101. if ($model == null) {
  102. throw new NotFoundHttpException('文章不存在或者不属于你');
  103. }
  104. $moduleModel = $model->data;
  105. if (
  106. $model->load(\Yii::$app->request->post())
  107. && $model->validate()
  108. && $moduleModel->load(\Yii::$app->request->post())
  109. && $moduleModel->validate()
  110. && $model->save(false)
  111. && $moduleModel->save(false)
  112. ) {
  113. \Yii::$app->session->setFlash('success', '修改成功!');
  114. return $this->redirect(['update-article', 'id' => $id]);
  115. }
  116. return $this->render('article/update', [
  117. 'model' => $model,
  118. 'moduleModel' => $moduleModel
  119. ]);
  120. }
  121. public function actionDeleteArticle($id)
  122. {
  123. $model = Article::find()->where(['id' => $id])->my()->one();
  124. if ($model == null) {
  125. throw new NotFoundHttpException('文章不存在或者不属于你');
  126. }
  127. if ($model->softDelete()) {
  128. \Yii::$app->session->setFlash('success', '删除成功!');
  129. return $this->redirect(['article-list']);
  130. }
  131. }
  132. public function actionFavourite()
  133. {
  134. $userId = \Yii::$app->user->id;
  135. $dataProvider = new ActiveDataProvider([
  136. 'query' => Favourite::find()->where(['user_id' => $userId]),
  137. 'sort' => [
  138. 'defaultOrder' => [
  139. 'id' => SORT_DESC
  140. ]
  141. ]
  142. ]);
  143. return $this->render('favourite/index', [
  144. 'dataProvider' => $dataProvider
  145. ]);
  146. }
  147. public function actionNotice()
  148. {
  149. Yii::$app->notify->readAll();
  150. $dataProvider = Yii::$app->notify->getDataProvider();
  151. return $this->render('notice/index', [
  152. 'dataProvider' => $dataProvider
  153. ]);
  154. }
  155. }