Kbs.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\admin\controller\workorder;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use Exception;
  7. use app\admin\model\workorder\Category;
  8. /**
  9. * 知识库管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Kbs extends Backend
  14. {
  15. /**
  16. * Kbs模型对象
  17. * @var \app\admin\model\workorder\Kbs
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\workorder\Kbs;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $filter = $this->request->get("filter", '');
  46. $filter = (array)json_decode($filter, true);
  47. $filter = $filter ? $filter : [];
  48. $category_where = [];
  49. if (array_key_exists('category_id', $filter)) {
  50. $category_kbs_ids = Category::where('id', $filter['category_id'])->value('kbs_ids');
  51. $category_where['kbs.id'] = ['in', $category_kbs_ids];
  52. }
  53. $total = $this->model->with(['category'])
  54. ->where($where)
  55. ->where($category_where)
  56. ->order($sort, $order)
  57. ->count();
  58. $list = $this->model->with(['category'])
  59. ->where($where)
  60. ->where($category_where)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. $domain = $this->request->domain();
  65. foreach ($list as $row) {
  66. $row->url = $row->url ? $row->url : $domain . '/index/workorder/kbs?id=' . $row->id;
  67. }
  68. $list = collection($list)->toArray();
  69. $result = ["total" => $total, "rows" => $list];
  70. return json($result);
  71. }
  72. $categoryList = [];
  73. $categorys = Category::order("weigh desc,id desc")->select();
  74. foreach ($categorys as $index => $category) {
  75. $categoryList[] = [
  76. 'id' => $category->id,
  77. 'parent' => $category->pid ? $category->pid : '#',
  78. 'text' => $category->name,
  79. 'state' => ['opened' => true, 'disabled' => $category->pid ? false : true],
  80. 'type' => $category->pid ? 'list' : 'none'
  81. ];
  82. }
  83. $this->assignconfig('categoryList', $categoryList);
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 真实删除
  88. */
  89. public function destroy($ids = "")
  90. {
  91. if (!$this->request->isPost()) {
  92. $this->error(__("Invalid parameters"));
  93. }
  94. $ids = $ids ? $ids : $this->request->post("ids");
  95. $pk = $this->model->getPk();
  96. $adminIds = $this->getDataLimitAdminIds();
  97. if (is_array($adminIds)) {
  98. $this->model->where($this->dataLimitField, 'in', $adminIds);
  99. }
  100. if ($ids) {
  101. $this->model->where($pk, 'in', $ids);
  102. }
  103. $count = 0;
  104. Db::startTrans();
  105. try {
  106. $list = $this->model->onlyTrashed()->select();
  107. foreach ($list as $k => $v) {
  108. $count += $v->delete(true);
  109. $this->model->catDelKbs($v->category_id, $v->id);
  110. }
  111. Db::commit();
  112. } catch (PDOException $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. } catch (Exception $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. }
  119. if ($count) {
  120. $this->success();
  121. } else {
  122. $this->error(__('No rows were deleted'));
  123. }
  124. $this->error(__('Parameter %s can not be empty', 'ids'));
  125. }
  126. }