Media.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\controller\booklist;
  3. use app\common\controller\Backend;
  4. use think\Collection;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 书单素材库
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Media extends Backend
  13. {
  14. /**
  15. * BookListMedia模型对象
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('BookListMedia');
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. }
  24. /**
  25. * 素材选择列表
  26. */
  27. public function select()
  28. {
  29. if ($this->request->isAjax())
  30. {
  31. //如果发送的来源是Selectpage,则转发到Selectpage
  32. if ($this->request->request('pkey_name'))
  33. {
  34. return $this->selectpage();
  35. }
  36. $map = [];
  37. $map['status'] = ['EQ', 'normal'];
  38. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. $total = $this->model
  40. ->where($where)
  41. ->where($map)
  42. ->order($sort, $order)
  43. ->count();
  44. $list = $this->model
  45. ->where($where)
  46. ->where($map)
  47. ->order($sort, $order)
  48. ->limit($offset, $limit)
  49. ->select();
  50. $result = array("total" => $total, "rows" => $list);
  51. return json($result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 添加
  57. */
  58. public function add()
  59. {
  60. $from = $this->request->get('from_window', 'list');
  61. if ($this->request->isPost())
  62. {
  63. $params = $this->request->post("row/a");
  64. if ($params)
  65. {
  66. /*
  67. * 已经弃用,如果为了兼容老版可取消注释
  68. foreach ($params as $k => &$v)
  69. {
  70. $v = is_array($v) ? implode(',', $v) : $v;
  71. }
  72. */
  73. if ($this->dataLimit)
  74. {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. try
  78. {
  79. //是否采用模型验证
  80. if ($this->modelValidate)
  81. {
  82. $name = basename(str_replace('\\', '/', get_class($this->model)));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  84. $this->model->validate($validate);
  85. }
  86. $result = $this->model->allowField(true)->save($params);
  87. if ($result !== false)
  88. {
  89. $this->success('', null, $this->model->getData());
  90. }
  91. else
  92. {
  93. $this->error($this->model->getError());
  94. }
  95. }
  96. catch (\think\exception\PDOException $e)
  97. {
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. $this->assignconfig('from_window', $from);
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = NULL)
  110. {
  111. $from = $this->request->get('from_window', 'list');
  112. $row = $this->model->get($ids);
  113. if (!$row)
  114. $this->error(__('No Results were found'));
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds))
  117. {
  118. if (!in_array($row[$this->dataLimitField], $adminIds))
  119. {
  120. $this->error(__('You have no permission'));
  121. }
  122. }
  123. if ($this->request->isPost())
  124. {
  125. $params = $this->request->post("row/a");
  126. if ($params)
  127. {
  128. /*
  129. * 已经弃用,如果为了兼容老版可取消注释
  130. foreach ($params as $k => &$v)
  131. {
  132. $v = is_array($v) ? implode(',', $v) : $v;
  133. }
  134. */
  135. try
  136. {
  137. //是否采用模型验证
  138. if ($this->modelValidate)
  139. {
  140. $name = basename(str_replace('\\', '/', get_class($this->model)));
  141. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  142. $row->validate($validate);
  143. }
  144. $result = $row->allowField(true)->save($params);
  145. if ($result !== false)
  146. {
  147. $this->success('', null, $row->getData());
  148. }
  149. else
  150. {
  151. $this->error($row->getError());
  152. }
  153. }
  154. catch (\think\exception\PDOException $e)
  155. {
  156. $this->error($e->getMessage());
  157. }
  158. }
  159. $this->error(__('Parameter %s can not be empty', ''));
  160. }
  161. $this->view->assign("row", $row);
  162. $this->assignconfig('from_window', $from);
  163. return $this->view->fetch();
  164. }
  165. }