Article.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\admin\controller\ask;
  3. use app\common\controller\Backend;
  4. /**
  5. * 问答文章管理
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Article extends Backend
  10. {
  11. /**
  12. * Article模型对象
  13. * @var \app\admin\model\ask\Article
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\ask\Article;
  20. $this->view->assign("flagList", $this->model->getFlagList());
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. public function index()
  24. {
  25. $this->relationSearch = true;
  26. //设置过滤方法
  27. $this->request->filter(['strip_tags']);
  28. if ($this->request->isAjax()) {
  29. //如果发送的来源是Selectpage,则转发到Selectpage
  30. if ($this->request->request('keyField')) {
  31. return $this->selectpage();
  32. }
  33. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  34. $total = $this->model->with(['user', 'category'])
  35. ->where($where)
  36. ->order($sort, $order)
  37. ->count();
  38. $list = $this->model->with(['user', 'category'])
  39. ->where($where)
  40. ->order($sort, $order)
  41. ->limit($offset, $limit)
  42. ->select();
  43. foreach ($list as $index => $item) {
  44. $item->user->visible(['id', 'nickname']);
  45. $item->category->visible(['id', 'name']);
  46. }
  47. $list = collection($list)->toArray();
  48. $result = array("total" => $total, "rows" => $list);
  49. return json($result);
  50. }
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 还原
  55. */
  56. public function restore($ids = "")
  57. {
  58. $pk = $this->model->getPk();
  59. $adminIds = $this->getDataLimitAdminIds();
  60. if (is_array($adminIds)) {
  61. $this->model->where($this->dataLimitField, 'in', $adminIds);
  62. }
  63. if ($ids) {
  64. $this->model->where($pk, 'in', $ids);
  65. }
  66. $count = 0;
  67. $list = $this->model->onlyTrashed()->select();
  68. foreach ($list as $index => $item) {
  69. $item->deletetime = null;
  70. $item->save();
  71. $count++;
  72. }
  73. if ($count) {
  74. $this->success();
  75. }
  76. $this->error(__('No rows were updated'));
  77. }
  78. }