123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- // +----------------------------------------------------------------------
- // | Library for ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
- // | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace think\admin\helper;
- use think\admin\Helper;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\db\Query;
- use think\Model;
- /**
- * 表单视图管理器
- * Class FormHelper
- * @package think\admin\helper
- */
- class FormHelper extends Helper
- {
- /**
- * 逻辑器初始化
- * @param Model|Query|string $dbQuery
- * @param string $template 模板名称
- * @param string $field 指定数据主键
- * @param array $where 额外更新条件
- * @param array $data 表单扩展数据
- * @return array|boolean|mixed|void
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function init($dbQuery, string $template = '', string $field = '', array $where = [], array $data = [])
- {
- $query = $this->buildQuery($dbQuery);
- $field = $field ?: ($query->getPk() ?: 'id');
- $value = $data[$field] ?? input($field);
- if ($this->app->request->isGet()) {
- if ($value !== null) {
- $find = $query->where([$field => $value])->where($where)->find();
- if (!empty($find) && is_array($find)) $data = array_merge($data, $find);
- }
- if (false !== $this->class->callback('_form_filter', $data)) {
- $this->class->fetch($template, ['vo' => $data]);
- } else {
- return $data;
- }
- } elseif ($this->app->request->isPost()) {
- $data = array_merge($this->app->request->post(), $data);
- if (false !== $this->class->callback('_form_filter', $data, $where)) {
- $result = data_save($query, $data, $field, $where) !== false;
- if (false !== $this->class->callback('_form_result', $result, $data)) {
- if ($result !== false) {
- $this->class->success(lang('think_library_form_success'));
- } else {
- $this->class->error(lang('think_library_form_error'));
- }
- }
- return $result;
- }
- }
- }
- }
|