Builder.php 4.6 KB
<?php
/**
 * +-----------------------------------------------------------------------------------------------------------------------
 * 重写laravel-admin Form\Builder 类
 * +-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Rewrite\Form
 * @package   App\Admin\Rewrite\Form
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月12日13:49:31
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Rewrite\Form;

use App\Admin\Rewrite\Facades\Admin;
use App\Admin\Rewrite\Form;
use Encore\Admin\Form\Builder as Base;
use Encore\Admin\Form\Field\Hidden;
use Illuminate\Support\Arr;

/**
 * Class Builder.
 *
 * @category  App\Admin\Rewrite\Form
 * @package   App\Admin\Rewrite\Form
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月12日13:49:31
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class Builder extends Base
{
    /**
     * Get Form action.
     *
     * @return string
     */
    public function getAction(): string
    {
        if ($this->action) {
            return $this->action;
        }

        // update By Richer 于 2020年1月1日13:02:46  增加后面的参数
        $params = explode('?', request()->getUri());

        if ($this->isMode(static::MODE_EDIT)) {
            // update By Richer 于 2020年1月1日13:02:46  增加后面的参数
            $url = $this->form->resource().'/'.$this->id;
            if( count($params) > 1 ){
                $url .= '?'.\Arr::last( $params );
            }
            return $url;
        }

        if ($this->isMode(static::MODE_CREATE)) {
            // update By Richer 于 2020年1月1日13:02:46  增加后面的参数
            $url = $this->form->resource(-1);
            if( count($params) > 1 ){
                $url .= '?'.\Arr::last( $params );
            }
            return $url;
        }

        return '';
    }

    /**
     * Open up a new HTML form.
     *
     * @param array $options
     *
     * @return string
     */
    public function open($options = []): string
    {
        $attributes = [];

        if ($this->isMode(self::MODE_EDIT)) {
            $this->addHiddenField((new Hidden('_method'))->value('PUT'));
        }

        $this->addRedirectUrlField();

        $attributes['action'] = $this->getAction();
        $attributes['method'] = Arr::get($options, 'method', 'post');
        $attributes['accept-charset'] = 'UTF-8';

        $attributes['class'] = Arr::get($options, 'class');

        if ($this->hasFile()) {
            $attributes['enctype'] = 'multipart/form-data';
            $attributes['onsubmit'] = 'return handleFiles()';
        }

        $html = [];
        foreach ($attributes as $name => $value) {
            $html[] = "$name=\"$value\"";
        }

        return '<form '.implode(' ', $html).' pjax-container>';
    }

    /**
     * Determine if form fields has files.
     *
     * @return bool
     */
    public function hasFile(): bool
    {
        foreach ($this->fields() as $field) {
            if ($field instanceof \Encore\Admin\Form\Field\File || $field instanceof \App\Admin\Rewrite\Form\Field\Image || $field instanceof \App\Admin\Rewrite\Form\Field\MultipleImage) {
                return true;
            }
        }

        return false;
    }

    /**
     * Render form.
     *
     * @return string
     */
    public function render(): string
    {
        $this->removeReservedFields();

        $tabObj = $this->form->getTab();

        if ($tabObj && !$tabObj->isEmpty()) {
            $script = <<<'SCRIPT'

var hash = document.location.hash;
if (hash) {
    $('.nav-tabs a[href="' + hash + '"]').tab('show');
}

// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
    history.pushState(null,null, e.target.hash);
});

if ($('.has-error').length) {
    $('.has-error').each(function () {
        var tabId = '#'+$(this).closest('.tab-pane').attr('id');
        $('li a[href="'+tabId+'"] i').removeClass('hide');
    });

    var first = $('.has-error:first').closest('.tab-pane').attr('id');
    $('li a[href="#'+first+'"]').tab('show');
}

SCRIPT;
            Admin::script($script);
        }

        $data = [
            'form'   => $this,
            'tabObj' => $tabObj,
            'width'  => $this->width,
            'layout' => $this->form->getLayout(),
        ];

        return view($this->view, $data)->render();
    }
}