StepForm.php 5.3 KB
<?php
/**
 * +-----------------------------------------------------------------------------------------------------------------------
 * 重写 laravel-admin Widgets\StepForm 类
 * +-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Rewrite\Widgets
 * @package   App\Admin\Rewrite\Widgets
 * @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\Widgets;

use Encore\Admin\Widgets\Box;

/**
 * Class StepForm
 *
 * @category  App\Admin\Rewrite\Widgets
 * @package   App\Admin\Rewrite\Widgets
 * @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 StepForm extends Form
{
    /**
     * @var int|string
     */
    protected $current;

    /**
     * @var array
     */
    protected $steps = [];

    /**
     * @var string
     */
    protected $url;

    /**
     * @var array
     */
    protected $buttons = [];

    /**
     * @param array $data
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    protected function next($data = [])
    {
        $this->remember($data);

        return $this->redirectToNextStep();
    }

    protected function prev()
    {
        return back()->withInput();
    }

    /**
     * @param array $data
     */
    protected function remember($data)
    {
        session()->put("steps.{$this->current}", $data);
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    protected function redirectToNextStep()
    {
        $index = array_search($this->current, $this->steps);

        $step = $this->steps[$index + 1];

        $nextUrl = $this->url.'?'.http_build_query(compact('step'));

        return redirect($nextUrl);
    }

    /**
     * Get all data from steps.
     *
     * @return array
     */
    protected function all()
    {
        $prev = session()->get('steps', []);

        return array_merge($prev, [$this->current => request()->all()]);
    }

    /**
     * Clear all data from steps.
     */
    protected function clear()
    {
        session()->remove('steps');
    }

    /**
     * @param array $steps
     *
     * @return $this
     */
    public function setSteps($steps)
    {
        $this->steps = $steps;

        return $this;
    }

    /**
     * @param string|int $current
     *
     * @return $this
     */
    public function setCurrent($current)
    {
        $this->current = $current;

        return $this;
    }

    /**
     * @param string $url
     *
     * @return $this
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    protected function prepareForm()
    {
        parent::prepareForm();

        $url = request()->url();

        $this->hidden('_url')->default($url);
        $this->hidden('_current')->default($this->current);
        $this->hidden('_steps')->default(implode(',', $this->steps));

        $this->divider();

        $this->addFooter();
    }

    protected function addFooter()
    {
        $footer = '';

        $index = array_search($this->current, $this->steps);

        $trans = [
            'prev'   => __('admin.prev'),
            'next'   => __('admin.next'),
            'submit' => __('admin.submit'),
        ];

        // update By Richer 于2019年8月26日11:29:27 增加特殊的class方便在前端进行操作
        if ($index !== 0) {
            $step = $this->steps[$index - 1];
            $prevUrl = request()->fullUrlWithQuery(compact('step'));
            $footer .= "<a href=\"{$prevUrl}\" class=\"btn btn-warning pull-left step-prev\">{$trans['prev']}</a>";
        }

        if ($index !== count($this->steps) - 1) {
            $footer .= "<button class=\"btn btn-success pull-right step-next\">{$trans['next']}</button>";
        }

        if ($index === count($this->steps) - 1) {
            $footer .= "<button class=\"btn btn-success pull-right step-submit\">{$trans['submit']}</button>";
        }

        $this->html($footer)->setWidth(10, 1)->setGroupClass('col-md-12');
    }

    /**
     * @return $this
     */
    public function sanitize()
    {
        $this->setUrl(request('_url'))
            ->setCurrent(request('_current'))
            ->setSteps(explode(',', request('_steps')));

        foreach (['_form_', '_token', '_url', '_current', '_steps'] as $key) {
            request()->request->remove($key);
        }

        return $this;
    }

    /**
     * @return mixed
     */
    public function data()
    {
        return session()->get('steps.'.$this->current, []);
    }

    /**
     * Render the form.
     *
     * @return string
     * @throws \Throwable
     */
    public function render1()
    {
        $this->prepareForm();

        $this->prepareHandle();

        $form = view('admin::widgets.form-wizard', $this->getVariables())->render();

        if (!($title = $this->title()) || !$this->inbox) {
            return $form;
        }

        return (new Box($title, $form))->render();
    }
}