2024-08-02 08:59:54 +08:00

79 lines
3.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | Library for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | 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\admin\service\SystemService;
use think\db\BaseQuery;
use think\Model;
/**
* 表单视图管理器
* @class FormHelper
* @package think\admin\helper
*/
class FormHelper extends Helper
{
/**
* 逻辑器初始化
* @param BaseQuery|Model|string $dbQuery
* @param string $template 视图模板名称
* @param string $field 指定数据主键
* @param mixed $where 限定更新条件
* @param array $edata 表单扩展数据
* @return void|array|boolean
* @throws \think\admin\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function init($dbQuery, string $template = '', string $field = '', $where = [], array $edata = [])
{
$query = static::buildQuery($dbQuery);
$field = $field ?: ($query->getPk() ?: 'id');
$value = $edata[$field] ?? input($field);
if ($this->app->request->isGet()) {
if ($value !== null) {
$exist = $query->where([$field => $value])->where($where)->find();
if ($exist instanceof Model) $exist = $exist->toArray();
$edata = array_merge($edata, $exist ?: []);
}
if (false !== $this->class->callback('_form_filter', $edata)) {
$this->class->fetch($template, ['vo' => $edata]);
} else {
return $edata;
}
}
if ($this->app->request->isPost()) {
$edata = array_merge($this->app->request->post(), $edata);
if (false !== $this->class->callback('_form_filter', $edata, $where)) {
$result = SystemService::save($query, $edata, $field, $where) !== false;
if (false !== $this->class->callback('_form_result', $result, $edata)) {
if ($result !== false) {
$this->class->success('数据保存成功!');
} else {
$this->class->error('数据保存失败!');
}
}
return $result;
}
}
}
}