Builder.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?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();
}
}