Blockresource.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\common\model\ManageBlock;
  4. use app\common\controller\Backend;
  5. use app\common\model\Attachment;
  6. use app\common\model\Book;
  7. use app\main\service\SpecialService;
  8. use think\Controller;
  9. use think\Exception;
  10. use think\exception\ErrorException;
  11. use think\exception\PDOException;
  12. use think\Request;
  13. /**
  14. * 书城管理-资源管理
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Blockresource extends Backend
  19. {
  20. /**
  21. * ManageBlockResource模型对象
  22. */
  23. protected $model = null;
  24. protected $blockData = [];
  25. // protected $searchFields = 'block_id';
  26. protected $relationSearch = true;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('ManageBlockResource');
  31. $typeList = $this->model->getTypeList();
  32. $block = model('ManageBlock')->where('id', $this->request->get('block_id'))->find();
  33. if ($block['type'] != '1') {
  34. unset($typeList[3]);
  35. }
  36. $this->view->assign("typeList", $typeList);
  37. $this->view->assign("specialList", SpecialService::instance()->getSpecialList()->data);
  38. $page_id = model('ManageBlock')->where('id','in',$this->request->get('block_id'))->find();
  39. $blockList = collection(model('ManageBlock')->where('page_id', 'in', $page_id['page_id'])->select())->toArray();
  40. foreach ($blockList as $k => $v) {
  41. $this->blockData[$v['id']] = $v['name'];
  42. }
  43. $this->view->assign('block_id', $this->request->get('block_id'));
  44. $this->assignconfig('block_id', $this->request->get('block_id'));
  45. $this->assignconfig('page_id', $this->request->get('page_id'));
  46. $this->view->assign('blockData', $this->blockData);
  47. }
  48. /**
  49. * 查看
  50. */
  51. public function index()
  52. {
  53. if ($this->request->isAjax()) {
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
  55. $total = $this->model
  56. ->where($where)
  57. ->with('Book')
  58. ->order($sort, $order)
  59. ->count();
  60. $list = $this->model
  61. ->where($where)
  62. ->with('Book')
  63. ->order($sort, $order)
  64. ->limit($offset, $limit)
  65. ->select();
  66. $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]);
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /*
  72. * 新增块资源
  73. */
  74. public function add()
  75. {
  76. if($this->request->isPost()){
  77. $row = $this->request->post('row/a');
  78. if($row){
  79. try{
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = basename(str_replace('\\', '/', get_class($this->model)));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  84. $this->model->validate($validate);
  85. }
  86. $this->model->startTrans();
  87. //保存资源信息
  88. if($this->model->allowField(true)->save($row) == false){
  89. throw new Exception($this->model->getError());
  90. }
  91. $row['free_stime'] = 0;
  92. $row['free_etime'] = 0;
  93. $this->model->commit();
  94. $this->success();
  95. }catch (Exception $e){
  96. $this->model->rollback();
  97. $this->error($e->getMessage());
  98. }
  99. }
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. $this->view->assign('page_id', $this->request->get('page_id'));
  103. return $this->view->fetch();
  104. }
  105. /**
  106. * 编辑
  107. */
  108. public function edit($ids = NULL)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row)
  112. $this->error(__('No Results were found'));
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds)) {
  115. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. }
  119. if ($this->request->isPost()) {
  120. $params = $this->request->post("row/a");
  121. if ($params) {
  122. try {
  123. //是否采用模型验证
  124. if ($this->modelValidate) {
  125. $name = basename(str_replace('\\', '/', get_class($this->model)));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  127. $row->validate($validate);
  128. }
  129. $row->startTrans();
  130. $result = $row->allowField(true)->save($params);
  131. if($result === false){
  132. throw new Exception($row->getError());
  133. }
  134. $params['free_stime'] = 0;
  135. $params['free_etime'] = 0;
  136. $row->commit();
  137. $this->success();
  138. }catch (Exception $e){
  139. $row->rollback();
  140. $this->error($e->getMessage());
  141. }
  142. }
  143. $this->error(__('Parameter %s can not be empty', ''));
  144. }
  145. $this->view->assign("row", $row);
  146. return $this->view->fetch();
  147. }
  148. }