Recycle.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\admin;
  10. use app\admin\controller\Admin;
  11. use app\common\builder\ZBuilder;
  12. use app\cms\model\Document as DocumentModel;
  13. use think\Db;
  14. /**
  15. * 回收站控制器
  16. * @package app\cms\admin
  17. */
  18. class Recycle extends Admin
  19. {
  20. /**
  21. * 文档列表
  22. * @param string $model 内容模型id
  23. * @author 蔡伟明 <314013107@qq.com>
  24. * @return mixed
  25. * @throws \think\Exception
  26. * @throws \think\exception\DbException
  27. */
  28. public function index($model = '')
  29. {
  30. if ($model == '') {
  31. // 查询
  32. $map = $this->getMap();
  33. $map[] = ['cms_document.trash', '=', 1];
  34. // 排序
  35. $order = $this->getOrder('update_time desc');
  36. // 数据列表
  37. $data_list = DocumentModel::getList($map, $order);
  38. // 自定义按钮
  39. $btnRestore = [
  40. 'class' => 'btn btn-xs btn-default ajax-get confirm',
  41. 'icon' => 'fa fa-fw fa-reply',
  42. 'title' => '还原',
  43. 'href' => url('restore', ['ids' => '__id__'])
  44. ];
  45. $btnRestoreAll = [
  46. 'class' => 'btn btn-success ajax-post confirm',
  47. 'icon' => 'fa fa-fw fa-reply-all',
  48. 'title' => '批量还原',
  49. 'href' => url('restore')
  50. ];
  51. // 使用ZBuilder快速创建数据表格
  52. return ZBuilder::make('table')
  53. ->setSearch(['title' => '标题', 'cms_column.name' => '栏目名称']) // 设置搜索框
  54. ->addColumns([ // 批量添加数据列
  55. ['id', 'ID'],
  56. ['title', '标题'],
  57. ['column_name', '栏目名称'],
  58. ['view', '点击量'],
  59. ['username', '发布人'],
  60. ['update_time', '更新时间', 'datetime'],
  61. ['right_button', '操作', 'btn']
  62. ])
  63. ->addTopButton('enable', $btnRestoreAll) // 批量添加顶部按钮
  64. ->addTopButton('delete', ['title' => '批量删除', 'href' => url('delete'), 'data-tips' => '删除后不可回复!']) // 批量添加顶部按钮
  65. ->addRightButton('custom', $btnRestore) // 添加右侧按钮
  66. ->addRightButton('delete', ['href' => url('delete', ['ids' => '__id__']), 'data-tips' => '删除后不可回复!']) // 添加右侧按钮
  67. ->addOrder('id,title,column_name,view,username,update_time')
  68. ->addFilter(['column_name' => 'cms_column.name', 'username' => 'admin_user'])
  69. ->setRowList($data_list) // 设置表格数据
  70. ->fetch(); // 渲染模板
  71. } else {
  72. $table_name = get_model_table($model);
  73. // 查询
  74. $map = $this->getMap();
  75. $map[] = ['trash', '=', 1];
  76. // 排序
  77. $order = $this->getOrder('update_time desc');
  78. // 数据列表
  79. $data_list = Db::view($table_name, true)
  80. ->view("cms_column", ['name' => 'column_name'], 'cms_column.id='.$table_name.'.cid', 'left')
  81. ->view("admin_user", 'username', 'admin_user.id='.$table_name.'.uid', 'left')
  82. ->where($map)
  83. ->order($order)
  84. ->paginate();
  85. // 自定义按钮
  86. $btnRestore = [
  87. 'class' => 'btn btn-xs btn-default ajax-get confirm',
  88. 'icon' => 'fa fa-fw fa-reply',
  89. 'title' => '还原',
  90. 'href' => url('restore', ['table' => $table_name, 'ids' => '__id__'])
  91. ];
  92. $btnRestoreAll = [
  93. 'class' => 'btn btn-success ajax-post confirm',
  94. 'icon' => 'fa fa-fw fa-reply-all',
  95. 'title' => '批量还原',
  96. 'href' => url('restore', ['table' => $table_name])
  97. ];
  98. // 使用ZBuilder快速创建数据表格
  99. return ZBuilder::make('table')
  100. ->setSearch(['title' => '标题', 'cms_column.name' => '栏目名称']) // 设置搜索框
  101. ->addColumns([ // 批量添加数据列
  102. ['id', 'ID'],
  103. ['title', '标题'],
  104. ['column_name', '栏目名称'],
  105. ['view', '点击量'],
  106. ['username', '发布人'],
  107. ['update_time', '更新时间', 'datetime'],
  108. ['right_button', '操作', 'btn']
  109. ])
  110. ->addTopButton('enable', $btnRestoreAll) // 添加顶部按钮
  111. ->addTopButton('delete', ['title' => '批量删除', 'href' => url('delete', ['table' => $table_name]), 'data-tips' => '删除后不可回复!']) // 添加顶部按钮
  112. ->addRightButton('custom', $btnRestore) // 添加右侧按钮
  113. ->addRightButton('delete', ['href' => url('delete', ['ids' => '__id__', 'table' => $table_name]), 'data-tips' => '删除后不可回复!']) // 添加右侧按钮
  114. ->addOrder('id,title,column_name,view,username,update_time')
  115. ->addFilter(['column_name' => 'cms_column.name', 'username' => 'admin_user'])
  116. ->setRowList($data_list) // 设置表格数据
  117. ->fetch(); // 渲染模板
  118. }
  119. }
  120. /**
  121. * 还原文档
  122. * @param null $ids 文档id
  123. * @param string $table 表名
  124. * @author 蔡伟明 <314013107@qq.com>
  125. */
  126. public function restore($ids = null, $table = '')
  127. {
  128. if ($ids === null) $this->error('请选择要操作的数据');
  129. $table = $table != '' ? substr($table, strlen(config('database.prefix'))) : 'cms_document';
  130. $document_id = is_array($ids) ? '' : $ids;
  131. $document_title = Db::name($table)->where('id', 'in', $ids)->column('title');
  132. // 还原文档
  133. if (false === Db::name($table)->where('id', 'in', $ids)->setField('trash', 0)) {
  134. $this->error('还原失败');
  135. }
  136. // 删除并记录日志
  137. action_log('document_restore', $table, $document_id, UID, implode('、', $document_title));
  138. $this->success('还原成功');
  139. }
  140. /**
  141. * 彻底删除文档
  142. * @param null $ids 文档id
  143. * @param string $table 表名
  144. * @author 蔡伟明 <314013107@qq.com>
  145. * @throws \think\Exception
  146. * @throws \think\exception\PDOException
  147. */
  148. public function delete($ids = null, $table = '')
  149. {
  150. if ($ids === null) $this->error('请选择要操作的数据');
  151. $ids = is_array($ids) ? $ids : (array)$ids;
  152. if ($table == '') {
  153. // 获取文档标题和模型id
  154. $data_list = Db::name('cms_document')->where('id', 'in', $ids)->column('id,model,title');
  155. foreach ($data_list as $document) {
  156. // 附加表名
  157. $extra_table = get_model_table($document['model']);
  158. // 删除附加表文档
  159. if (false === Db::table($extra_table)->where('aid', $document['id'])->delete()) {
  160. $this->error('删除文档:'. $document['title']. ' 失败');
  161. }
  162. // 删除主表文档
  163. if (false === Db::name('cms_document')->where('id', $document['id'])->delete()) {
  164. $this->error('删除失败');
  165. }
  166. // 记录行为
  167. action_log('document_delete', 'cms_document', $document['id'], UID, $document['title']);
  168. }
  169. } else {
  170. // 文档标题
  171. $document_title = Db::table($table)->where('id', 'in', $ids)->column('title');
  172. // 删除独立文档
  173. if (false === Db::table($table)->where('id', 'in', $ids)->delete()) {
  174. $this->error('删除失败');
  175. }
  176. // 记录行为
  177. action_log('document_delete', $table, 0, UID, '表('.$table.'),文档('.implode('、', $document_title).')');
  178. }
  179. $this->success('删除成功');
  180. }
  181. }