You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
solarai/app/Providers/FormMacroServiceProvider.php

145 lines
6.4 KiB
PHTML

2 years ago
<?php namespace App\Providers;
use Collective\Html\FormFacade;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class FormMacroServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
FormFacade::macro('quickForm', function (Model $model, $params = []) {
$funcGeneaterRoute = function ($model, $data) use($params) {
if(is_array($data)) {
$args = [];
if(isset($data[1]) && count($data[1]) > 0) {
foreach($data[1] as $key=>$attr) {
// $params 에서 찾기
if(isset($params[$attr])) {
$args[$key] = $params[$attr];
}
// 모델에서 찾기
if(property_exists($model, $attr)) {
$args[$key] = $model->{$attr};
}
// request 에서 찾기
if(request()->has($attr)) {
$args[$key] = request()->get($attr, '');
}
}
}
return route($data[0], $args);
}
return route($data);
};
$htmls = [];
if(property_exists($model, 'form')) {
$form = $model->form;
$htmls[] = FormFacade::model($model, ['id'=>'form', 'route'=>$form['action'], 'files' => true]);
$inputs = $model->inputs;
$rows = [];
foreach($inputs as $name => $element) {
$rowHtml = [];
$rowHtml['label'] = '';
$rowHtml['html'] = '';
// hidden
if($name == '#') {
$rowHtml['hidden'] = $element;
$rows[] = $rowHtml;
continue;
}
// custom view
else if(Str::startsWith($name, '@')) {
$rowHtml['view'] = $element;
$rows[] = $rowHtml;
continue;
}
$type = 'text';
if(isset($element['type'])) $type = $element['type'];
$rowHtml['label'] = FormFacade::label($name, $element['label']);
if($type == 'text') $rowHtml['html'] = FormFacade::text($name, null, array_merge(['class'=>'form-control'], isset($element['attrs'])?$element['attrs']:[]));
else if($type == 'textarea') $rowHtml['html'] = FormFacade::textarea($name, null, array_merge(['class'=>'form-control'], isset($element['attrs'])?$element['attrs']:[]));
else if($type == 'select') $rowHtml['html'] = FormFacade::select($name, $element['data'], null, array_merge(['class'=>'form-control'], isset($element['attrs'])?$element['attrs']:[]));
else if($type == 'radio') {
$radioLables = [];
$radioHtmls = [];
if(isset($element['data'])) {
$no = 0;
foreach($element['data'] as $value => $text) {
$id = $name.'-'.($no++);
$radioLables[] = FormFacade::label($id, $text, ['class'=>'custom-control-label']);
$radioHtmls[] = FormFacade::radio($name, $value, null, array_merge(['id'=>$id, 'class'=>'custom-control-input'], isset($element['attrs'])?$element['attrs']:[]));
}
}
$radioHtml = [];
foreach($radioLables as $index=>$label) {
$radioHtml[] = sprintf('<div class="custom-control custom-radio custom-control-inline">%s%s</div>', $radioHtmls[$index], $label);
}
$rowHtml['html'] = sprintf('<div class="frame-wrap">%s</div>', implode("", $radioHtml));
}
else if($type == 'file') $rowHtml['html'] = sprintf('<div class="custom-file">%s<label class="custom-file-label" for="customFile">파일선택</label></div>', FormFacade::file($name, ['class'=>'custom-file-input']));
$rows[] = $rowHtml;
}
$inputHtml = [];
foreach($rows as $row) {
if(isset($row['hidden'])) {
foreach($row['hidden'] as $hidden) {
$inputHtml[] = FormFacade::hidden($hidden);
}
}
else if(isset($row['view'])) $inputHtml[] = view($row['view'], array_merge(['data' => $model], $params))->render();
else $inputHtml[] = sprintf('<div class="form-group">%s%s</div>', $row['label'], $row['html']);
}
$htmls[] = implode("\n", $inputHtml);
$buttonText = '저장';
if($model->id > 0) $buttonText = '수정';
$buttons = [];
if(isset($form['list'])) $buttons[] = sprintf('<div class="col-sm"><a href="%s" class="btn btn-info btn-list">목록</a></div>', $funcGeneaterRoute($model, $form['list']));
else $buttons[] = '<div class="col-sm"></div>';
$buttons[] = sprintf('<div class="col-sm text-center">%s</div>', FormFacade::button($buttonText, ['type'=>'submit', 'class'=>'btn btn-primary waves-effect waves-themed']));
if(isset($form['delete'])) $buttons[] = sprintf('<div class="col-sm text-right"><a href="%s" class="btn btn-danger btn-delete">삭제</a></div>', $funcGeneaterRoute($model, $form['delete']));
else $buttons[] = '<div class="col-sm text-right"></div>';
$htmls[] = sprintf('<div class="row">%s</div>', implode("\n", $buttons) );
$htmls[] = FormFacade::close();
}
return new HtmlString( implode("\n", $htmls) );
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}