Modelx.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\cms\library\Service;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * 内容模型表
  9. *
  10. * @icon fa fa-th
  11. */
  12. class Modelx extends Backend
  13. {
  14. /**
  15. * Model模型对象
  16. */
  17. protected $model = null;
  18. protected $searchFields = 'id,name,table,channeltpl,listtpl,showtpl';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\cms\Modelx;
  23. }
  24. /**
  25. * 复制模型
  26. */
  27. public function duplicate($ids = "")
  28. {
  29. $row = $this->model->get($ids);
  30. if (!$row) {
  31. $this->error(__('No Results were found'));
  32. }
  33. $adminIds = $this->getDataLimitAdminIds();
  34. if (is_array($adminIds)) {
  35. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  36. $this->error(__('You have no permission'));
  37. }
  38. }
  39. if ($this->request->isPost()) {
  40. $table = $this->request->request("table");
  41. $tableInfo = null;
  42. try {
  43. $tableInfo = \think\Db::name($table)->getTableInfo();
  44. } catch (\Exception $e) {
  45. }
  46. if ($tableInfo) {
  47. $this->error("模型表名已经存在");
  48. }
  49. Db::startTrans();
  50. try {
  51. $data = [
  52. 'name' => $row->getData('name') . '_copy',
  53. 'table' => $table,
  54. 'fields' => $row->fields,
  55. 'channeltpl' => $row->channeltpl,
  56. 'listtpl' => $row->listtpl,
  57. 'showtpl' => $row->showtpl,
  58. 'setting' => $row->setting,
  59. ];
  60. $modelx = \app\admin\model\cms\Modelx::create($data, true);
  61. $fieldsList = \app\admin\model\cms\Fields::where('source', 'model')->where('source_id', $row['id'])->select();
  62. foreach ($fieldsList as $index => $item) {
  63. $data = $item->toArray();
  64. $data['source_id'] = $modelx->id;
  65. unset($data['id'], $data['createtime'], $data['updatetime']);
  66. \app\admin\model\cms\Fields::create($data, true);
  67. }
  68. Db::commit();
  69. } catch (\Exception $e) {
  70. Db::rollback();
  71. $this->error("复制失败,错误:" . $e->getMessage());
  72. }
  73. $this->success();
  74. }
  75. $this->view->assign("row", $row);
  76. return $this->view->fetch();
  77. }
  78. }