Diydata.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\cms\library\Service;
  4. use app\common\controller\Backend;
  5. /**
  6. * 自定义表单数据表
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Diydata extends Backend
  11. {
  12. /**
  13. * 自定义表单模型对象
  14. */
  15. protected $diyform = null;
  16. /**
  17. * 定义表单数据表模型
  18. * @var null
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $diyform_id = $this->request->param('diyform_id');
  25. $this->diyform = \app\admin\model\cms\Diyform::get($diyform_id);
  26. if (!$this->diyform) {
  27. $this->error('未找到对应自定义表单');
  28. }
  29. $this->model = new \addons\cms\model\Diydata([], $this->diyform);
  30. $this->assignconfig('diyform_id', $diyform_id);
  31. }
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. $fieldsList = \app\admin\model\cms\Fields::where('source', 'diyform')->where('source_id', $this->diyform['id'])->where('type', '<>', 'text')->select();
  38. $fields = [];
  39. foreach ($fieldsList as $index => $item) {
  40. $fields[] = ['field' => $item['name'], 'title' => $item['title'], 'type' => $item['type'], 'content' => $item['content_list']];
  41. }
  42. $this->assignconfig('fields', $fields);
  43. $where = [];
  44. $config = get_addon_config('cms');
  45. if ($config['diyformdatalimit'] != 'all') {
  46. $this->dataLimit = $config['diyformdatalimit'];
  47. $adminIds = $this->getDataLimitAdminIds();
  48. if (is_array($adminIds)) {
  49. $where[$this->dataLimitField] = ['in', $adminIds];
  50. }
  51. $this->dataLimit = false;
  52. }
  53. $diyformList = \app\admin\model\cms\Diyform::where($where)->select();
  54. $this->view->assign('diyform', $this->diyform);
  55. $this->view->assign('diyformList', $diyformList);
  56. return parent::index();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. $this->assignFields();
  64. if ($this->request->isPost()) {
  65. $params = $this->request->post("row/a");
  66. if ($params) {
  67. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  68. $params[$this->dataLimitField] = $this->auth->id;
  69. }
  70. try {
  71. $result = $this->model->save($params);
  72. if ($result !== false) {
  73. $this->success();
  74. } else {
  75. $this->error($this->model->getError());
  76. }
  77. } catch (\think\exception\PDOException $e) {
  78. $this->error($e->getMessage());
  79. } catch (\think\Exception $e) {
  80. $this->error($e->getMessage());
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 编辑
  89. */
  90. public function edit($ids = null)
  91. {
  92. $row = $this->model->where('id', $ids)->find();
  93. if (!$row) {
  94. $this->error(__('No Results were found'));
  95. }
  96. $adminIds = $this->getDataLimitAdminIds();
  97. if (is_array($adminIds)) {
  98. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  99. $this->error(__('You have no permission'));
  100. }
  101. }
  102. if ($this->request->isPost()) {
  103. $params = $this->request->post("row/a");
  104. if ($params) {
  105. try {
  106. $result = $this->model->update($params, ['id' => $ids]);
  107. if ($result !== false) {
  108. $this->success();
  109. } else {
  110. $this->error($row->getError());
  111. }
  112. } catch (\think\exception\PDOException $e) {
  113. $this->error($e->getMessage());
  114. } catch (\think\Exception $e) {
  115. $this->error($e->getMessage());
  116. }
  117. }
  118. $this->error(__('Parameter %s can not be empty', ''));
  119. }
  120. $this->assignFields($ids);
  121. $this->view->assign("row", $row);
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 删除
  126. */
  127. public function del($ids = "")
  128. {
  129. if (!$this->request->isPost()) {
  130. $this->error(__("Invalid parameters"));
  131. }
  132. $ids = $ids ? $ids : $this->request->post("ids");
  133. if ($ids) {
  134. $pk = $this->model->getPk();
  135. $adminIds = $this->getDataLimitAdminIds();
  136. if (is_array($adminIds)) {
  137. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  138. }
  139. $count = $this->model->where($pk, 'in', $ids)->delete();
  140. if ($count) {
  141. $this->success();
  142. } else {
  143. $this->error(__('No rows were deleted'));
  144. }
  145. }
  146. $this->error(__('Parameter %s can not be empty', 'ids'));
  147. }
  148. private function assignFields($diydata_id = 0)
  149. {
  150. $values = [];
  151. if ($diydata_id) {
  152. $values = db($this->diyform['table'])->where('id', $diydata_id)->find();
  153. }
  154. $fields = Service::getCustomFields('diyform', $this->diyform['id'], $values);
  155. $this->view->assign('fields', $fields);
  156. $this->view->assign('values', $values);
  157. }
  158. }