Answer.php 2.3 KB

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