Modelx.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (strlen($table) < 5 || strlen($table) > 20) {
  42. $this->error(__('Please fill in 5 to 20 characters'));
  43. }
  44. $tableInfo = null;
  45. try {
  46. $tableInfo = \think\Db::name($table)->getTableInfo();
  47. } catch (\Exception $e) {
  48. }
  49. if ($tableInfo) {
  50. $this->error("模型表名已经存在");
  51. }
  52. Db::startTrans();
  53. try {
  54. $data = [
  55. 'name' => $row->getData('name') . '_copy',
  56. 'table' => $table,
  57. 'fields' => $row->fields,
  58. 'channeltpl' => $row->channeltpl,
  59. 'listtpl' => $row->listtpl,
  60. 'showtpl' => $row->showtpl,
  61. 'setting' => $row->setting,
  62. ];
  63. $modelx = \app\admin\model\cms\Modelx::create($data, true);
  64. $fieldsList = \app\admin\model\cms\Fields::where('source', 'model')->where('source_id', $row['id'])->select();
  65. foreach ($fieldsList as $index => $item) {
  66. $data = $item->toArray();
  67. $data['source_id'] = $modelx->id;
  68. unset($data['id'], $data['createtime'], $data['updatetime']);
  69. \app\admin\model\cms\Fields::create($data, true);
  70. }
  71. Db::commit();
  72. } catch (\Exception $e) {
  73. Db::rollback();
  74. $this->error("复制失败,错误:" . $e->getMessage());
  75. }
  76. $this->success();
  77. }
  78. $this->view->assign("row", $row);
  79. return $this->view->fetch();
  80. }
  81. }