Casescategory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Casescategory extends Backend
  10. {
  11. /**
  12. * WwhCasescategory模型对象
  13. * @var \app\admin\model\wwh\Casescategory
  14. */
  15. protected $noNeedRight = ['getjsTree', 'category'];
  16. protected $model = null;
  17. protected $categoryList = [];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('\app\admin\model\wwh\Casescategory');
  22. $casesCategoryList = collection($this->model->select())->toArray();
  23. Tree::instance()->init($casesCategoryList);
  24. $casesCategory = [];
  25. $this->categoryList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  26. $primary = array(array('name' => '无', 'id' => '0'));
  27. $result = array_merge_recursive($primary, $this->categoryList);
  28. foreach ($result as $k => $v) {
  29. $casesCategory[$v['id']] = $v['name'];
  30. }
  31. $this->view->assign("casesCategory", $casesCategory);
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. if ($this->request->isAjax()) {
  44. $list = $this->categoryList;
  45. $total = count($this->categoryList);
  46. $result = array("total" => $total, "rows" => $list);
  47. return json($result);
  48. }
  49. return $this->view->fetch();
  50. }
  51. /**
  52. * 添加
  53. */
  54. public function add()
  55. {
  56. if ($this->request->isPost()) {
  57. $params = $this->request->post("row/a");
  58. if ($params) {
  59. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  60. $params[$this->dataLimitField] = $this->auth->id;
  61. }
  62. try {
  63. //是否采用模型验证
  64. if ($this->modelValidate) {
  65. $name = basename(str_replace('\\', '/', get_class($this->model)));
  66. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  67. $this->model->validate($validate);
  68. }
  69. $result = $this->model->allowField(true)->save($params);
  70. if ($result !== false) {
  71. $this->success();
  72. } else {
  73. $this->error($this->model->getError());
  74. }
  75. } catch (\think\exception\PDOException $e) {
  76. $this->error($e->getMessage());
  77. }
  78. }
  79. $this->error(__('Parameter %s can not be empty', ''));
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 删除
  85. */
  86. public function del($ids = "")
  87. {
  88. if (!$this->request->isPost()) {
  89. $this->error(__("Invalid parameters"));
  90. }
  91. $ids = $ids ? $ids : $this->request->post("ids");
  92. if ($ids) {
  93. $pk = $this->model->getPk();
  94. $wwh = model('\app\admin\model\wwh\Casescategory')->where('pid', 'in', $ids)->select();
  95. if ($wwh) {
  96. $this->error('存在下级分类,无法删除!');
  97. }
  98. $res = model('\app\admin\model\wwh\Casescategory')->alias('a')->join('wwh_cases w', 'a.id = w.casescategoryid')->where('a.id', 'in', $ids)->select();
  99. if ($res) {
  100. $this->error('分类存在方案,无法删除!');
  101. }
  102. $count = model('\app\admin\model\wwh\Casescategory')->where($pk, 'in', $ids)->delete();
  103. if ($count) {
  104. $this->success();
  105. } else {
  106. $this->error(__('No rows were deleted'));
  107. }
  108. }
  109. }
  110. /**
  111. * 获取类别树
  112. */
  113. public function getjsTree()
  114. {
  115. $categoryList = collection($this->model->select())->toArray();
  116. $result = [];
  117. foreach ($categoryList as $k => $v) {
  118. $n = [];
  119. $n['id'] = $v['id'];
  120. $n['parent'] = $v['pid'] == 0 ? "#" : $v['pid'];
  121. $n['text'] = $v['name'];
  122. $n['type'] = $v['id'];
  123. $n['data'] = $v;
  124. $n['state'] = ["opened" => true, "disabled" => false];
  125. $result[]=$n;
  126. }
  127. return json($result);
  128. }
  129. /**
  130. * 获取类别名称
  131. */
  132. public function category()
  133. {
  134. $result = collection($this->model->select())->toArray();
  135. return json($result);
  136. }
  137. }