ArticleController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace addons\TinyBlog\merchant\controllers;
  3. use Yii;
  4. use common\models\base\SearchModel;
  5. use common\traits\MerchantCurd;
  6. use common\enums\StatusEnum;
  7. use addons\TinyBlog\common\models\Article;
  8. use addons\TinyBlog\common\enums\ArticlePositionEnum;
  9. /**
  10. * Class ArticleController
  11. * @package addons\TinyBlog\merchant\controllers
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class ArticleController extends BaseController
  15. {
  16. use MerchantCurd;
  17. /**
  18. * @var Article
  19. */
  20. public $modelClass = Article::class;
  21. /**
  22. * 首页
  23. *
  24. * @return string
  25. * @throws \yii\web\NotFoundHttpException
  26. */
  27. public function actionIndex()
  28. {
  29. $searchModel = new SearchModel([
  30. 'model' => $this->modelClass,
  31. 'scenario' => 'default',
  32. 'partialMatchAttributes' => ['title'], // 模糊查询
  33. 'defaultOrder' => [
  34. 'sort' => SORT_ASC,
  35. 'id' => SORT_DESC
  36. ],
  37. 'pageSize' => $this->pageSize
  38. ]);
  39. $dataProvider = $searchModel
  40. ->search(Yii::$app->request->queryParams);
  41. $dataProvider->query
  42. ->andWhere(['status' => StatusEnum::ENABLED])
  43. ->andWhere(['merchant_id' => $this->getMerchantId()]);
  44. return $this->render($this->action->id, [
  45. 'dataProvider' => $dataProvider,
  46. 'searchModel' => $searchModel,
  47. ]);
  48. }
  49. /**
  50. * 回收站
  51. *
  52. * @return string
  53. * @throws \yii\web\NotFoundHttpException
  54. */
  55. public function actionRecycle()
  56. {
  57. $searchModel = new SearchModel([
  58. 'model' => $this->modelClass,
  59. 'scenario' => 'default',
  60. 'partialMatchAttributes' => ['title'], // 模糊查询
  61. 'defaultOrder' => [
  62. 'sort' => SORT_ASC,
  63. 'id' => SORT_DESC
  64. ],
  65. 'pageSize' => $this->pageSize
  66. ]);
  67. $dataProvider = $searchModel
  68. ->search(Yii::$app->request->queryParams);
  69. $dataProvider->query
  70. ->andWhere(['status' => StatusEnum::DISABLED])
  71. ->andWhere(['merchant_id' => $this->getMerchantId()]);
  72. return $this->render($this->action->id, [
  73. 'dataProvider' => $dataProvider,
  74. 'searchModel' => $searchModel,
  75. ]);
  76. }
  77. /**
  78. * 还原
  79. *
  80. * @param $id
  81. * @return mixed
  82. */
  83. public function actionShow($id)
  84. {
  85. $model = $this->findModel($id);
  86. $model->status = StatusEnum::ENABLED;
  87. if ($model->save()) {
  88. return $this->message("还原成功", $this->redirect(['recycle']));
  89. }
  90. return $this->message("还原失败", $this->redirect(['recycle']), 'error');
  91. }
  92. /**
  93. * 删除
  94. *
  95. * @param $id
  96. * @return mixed
  97. */
  98. public function actionHide($id)
  99. {
  100. $model = $this->findModel($id);
  101. $model->status = StatusEnum::DISABLED;
  102. if ($model->save()) {
  103. return $this->message("删除成功", $this->redirect(['index']));
  104. }
  105. return $this->message("删除失败", $this->redirect(['index']), 'error');
  106. }
  107. /**
  108. * 编辑/创建
  109. *
  110. * @return mixed
  111. */
  112. public function actionEdit()
  113. {
  114. $id = Yii::$app->request->get('id', null);
  115. $model = $this->findModel($id);
  116. // 设置选中标签
  117. $model->tagValues = $model->getTagValues(true);
  118. // 推荐位
  119. $positionExplain = ArticlePositionEnum::getMap();
  120. $keys = [];
  121. foreach ($positionExplain as $key => $value) {
  122. if (ArticlePositionEnum::checkPosition($key, $model->position)) {
  123. $keys[] = $key;
  124. }
  125. }
  126. $model->position = $keys;
  127. if ($model->load(Yii::$app->request->post())) {
  128. if ($model->save()) {
  129. return $this->referrer();
  130. }
  131. }
  132. return $this->render($this->action->id, [
  133. 'model' => $model,
  134. ]);
  135. }
  136. }
粤ICP备19079148号