FormHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Library for ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace think\admin\helper;
  16. use think\admin\Helper;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\db\Query;
  21. use think\Model;
  22. /**
  23. * 表单视图管理器
  24. * Class FormHelper
  25. * @package think\admin\helper
  26. */
  27. class FormHelper extends Helper
  28. {
  29. /**
  30. * 逻辑器初始化
  31. * @param Model|Query|string $dbQuery
  32. * @param string $template 模板名称
  33. * @param string $field 指定数据主键
  34. * @param array $where 额外更新条件
  35. * @param array $data 表单扩展数据
  36. * @return array|boolean|mixed|void
  37. * @throws DataNotFoundException
  38. * @throws DbException
  39. * @throws ModelNotFoundException
  40. */
  41. public function init($dbQuery, string $template = '', string $field = '', array $where = [], array $data = [])
  42. {
  43. $query = $this->buildQuery($dbQuery);
  44. $field = $field ?: ($query->getPk() ?: 'id');
  45. $value = $data[$field] ?? input($field);
  46. if ($this->app->request->isGet()) {
  47. if ($value !== null) {
  48. $find = $query->where([$field => $value])->where($where)->find();
  49. if (!empty($find) && is_array($find)) $data = array_merge($data, $find);
  50. }
  51. if (false !== $this->class->callback('_form_filter', $data)) {
  52. $this->class->fetch($template, ['vo' => $data]);
  53. } else {
  54. return $data;
  55. }
  56. } elseif ($this->app->request->isPost()) {
  57. $data = array_merge($this->app->request->post(), $data);
  58. if (false !== $this->class->callback('_form_filter', $data, $where)) {
  59. $result = data_save($query, $data, $field, $where) !== false;
  60. if (false !== $this->class->callback('_form_result', $result, $data)) {
  61. if ($result !== false) {
  62. $this->class->success(lang('think_library_form_success'));
  63. } else {
  64. $this->class->error(lang('think_library_form_error'));
  65. }
  66. }
  67. return $result;
  68. }
  69. }
  70. }
  71. }