Column.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. class Column extends Base
  6. {
  7. protected $columnList = [];
  8. protected $templateList = [];
  9. protected $multiFields = ['weigh', 'isnav'];
  10. protected $searchFields = 'name';
  11. /**
  12. * Column模型对象
  13. * @var \app\admin\model\wwh\column
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\wwh\Column;
  20. $this->tree = Tree::instance();
  21. $this->tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'parent_id');
  22. $this->columnList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
  23. $this->templateList = \app\admin\model\wwh\Template::order('id asc')->select();
  24. $this->view->assign("templateList", $this->templateList);
  25. $this->view->assign("columnList", $this->columnList);
  26. $this->view->assign("typeList", $this->model->getTypeList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. $this->view->assign("classifyList", $this->model->getClassifyList());
  29. $this->assignconfig("classifyList", $this->model->getClassifyList());
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax()) {
  39. $search = $this->request->request("search");
  40. $template_id = $this->request->request("template_id");
  41. //构造父类select列表选项数据
  42. $list = [];
  43. if ($search) {
  44. foreach ($this->columnList as $k => $v) {
  45. if (stripos($v['name'], $search) !== false) {
  46. $list[] = $v;
  47. }
  48. }
  49. } else {
  50. $list = $this->columnList;
  51. }
  52. foreach ($list as $index => $item) {
  53. if ($template_id && $template_id != $item['template_id']) {
  54. unset($list[$index]);
  55. }
  56. }
  57. $list = array_values($list);
  58. $modelNameArr = [];
  59. foreach ($this->templateList as $k => $v) {
  60. $modelNameArr[$v['id']] = $v['name'];
  61. }
  62. foreach ($list as $k => &$v) {
  63. $v['template_name'] = $v['template_id'] && isset($modelNameArr[$v['template_id']]) ? $modelNameArr[$v['template_id']] : __('None');
  64. }
  65. $total = count($list);
  66. $result = array("total" => $total, "rows" => $list);
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 编辑
  73. */
  74. public function edit($ids = null)
  75. {
  76. $column = \app\admin\model\wwh\Column::get($ids);
  77. if (!$column) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $column = $column->toArray();
  81. $hasArchives = model('\app\admin\model\wwh\Archives')->where('column_id', $column['id'])->whereOr('FIND_IN_SET(:id, `column_ids`)', ['id' => $column['id']])->count();
  82. $this->view->assign('hasArchives', $hasArchives);
  83. $this->view->assign('values', $column);
  84. return parent::edit($ids);
  85. }
  86. /**
  87. * 删除
  88. */
  89. public function del($ids = "")
  90. {
  91. if (!$this->request->isPost()) {
  92. $this->error(__("Invalid parameters"));
  93. }
  94. $ids = $ids ? $ids : $this->request->post("ids");
  95. if ($ids) {
  96. $pk = $this->model->getPk();
  97. $wwh = model('\app\admin\model\wwh\Column')->where('parent_id', 'in', $ids)->select();
  98. if ($wwh) {
  99. $this->error('存在下级栏目,无法删除!');
  100. }
  101. $res = model('\app\admin\model\wwh\Column')->alias('a')->join('wwh_archives w', 'a.id = w.column_id')->where('a.id', 'in', $ids)->select();
  102. if ($res) {
  103. $this->error('栏目存在内容,无法删除!');
  104. }
  105. $count = model('\app\admin\model\wwh\Column')->where($pk, 'in', $ids)->delete();
  106. if ($count) {
  107. $this->success();
  108. } else {
  109. $this->error(__('No rows were deleted'));
  110. }
  111. }
  112. }
  113. }