Block.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\controller\special;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. /**
  7. * 专题页-块资源
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Block extends Backend
  12. {
  13. /**
  14. * SpecialBlock模型对象
  15. */
  16. protected $model = null;
  17. protected $page_id = 0;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->page_id = Request::instance()->param("page_id", 0);
  22. $this->assign('page_id', $this->page_id);
  23. $this->assignconfig('page_id', $this->page_id);
  24. $this->model = model('SpecialBlock');
  25. $this->view->assign("blockTypeList", $this->model->getBlockTypeList());
  26. $this->view->assign("titleTypeList", $this->model->getTitleTypeList());
  27. $this->view->assign("newsTypeList", $this->model->getNewsTypeList());
  28. $this->view->assign("isAddBookList", $this->model->getIsAddBookList());
  29. $this->view->assign("isShowHotList", $this->model->getIsShowHotList());
  30. $this->view->assign("goMethodList", $this->model->getGoMethodList());
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. if (empty($this->page_id)) {
  43. $this->error("缺少专题ID");
  44. }
  45. //设置过滤方法
  46. $this->request->filter(['strip_tags']);
  47. if ($this->request->isAjax())
  48. {
  49. //如果发送的来源是Selectpage,则转发到Selectpage
  50. if ($this->request->request('pkey_name'))
  51. {
  52. return $this->selectpage();
  53. }
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55. $maps = [
  56. 'page_id' => $this->page_id,
  57. ];
  58. $total = $this->model
  59. ->where($where)
  60. ->where($maps)
  61. ->order($sort, $order)
  62. ->count();
  63. $list = $this->model
  64. ->where($where)
  65. ->where($maps)
  66. ->order($sort, $order)
  67. ->limit($offset, $limit)
  68. ->select();
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. $this->view->assign('title', '资源管理');
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 编辑
  77. */
  78. public function edit($ids = NULL)
  79. {
  80. $row = $this->model->get($ids);
  81. if (!$row)
  82. $this->error(__('No Results were found'));
  83. $adminIds = $this->getDataLimitAdminIds();
  84. if (is_array($adminIds))
  85. {
  86. if (!in_array($row[$this->dataLimitField], $adminIds))
  87. {
  88. $this->error(__('You have no permission'));
  89. }
  90. }
  91. if ($this->request->isPost())
  92. {
  93. $params = $this->request->post("row/a");
  94. if ($params)
  95. {
  96. /*
  97. * 已经弃用,如果为了兼容老版可取消注释
  98. foreach ($params as $k => &$v)
  99. {
  100. $v = is_array($v) ? implode(',', $v) : $v;
  101. }
  102. */
  103. try
  104. {
  105. //是否采用模型验证
  106. if ($this->modelValidate)
  107. {
  108. $name = basename(str_replace('\\', '/', get_class($this->model)));
  109. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  110. $row->validate($validate);
  111. }
  112. $result = $row->allowField(true)->save($params);
  113. if ($result !== false)
  114. {
  115. $this->success();
  116. }
  117. else
  118. {
  119. $this->error($row->getError());
  120. }
  121. }
  122. catch (\think\exception\PDOException $e)
  123. {
  124. $this->error($e->getMessage());
  125. }
  126. }
  127. $this->error(__('Parameter %s can not be empty', ''));
  128. }
  129. $this->view->assign("row", $row);
  130. $activityName = '';
  131. if (!empty($row['activity_id'])) {
  132. $activity = model('activity')->get($row['activity_id']);
  133. $activityName = $activity->name;
  134. }
  135. $this->view->assign("activityName", $activityName);
  136. return $this->view->fetch();
  137. }
  138. }