招标
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

241 lines
6.6 KiB

<?php
/**
*
* View.php
* User: ChenLong
* DateTime: 2020/3/11 15:05
*/
namespace sdModule\makeAdminBasics\makeItem;
use sdModule\common\Sc;
use sdModule\makeAdminBasics\ConfigHelper;
use sdModule\makeAdminBasics\htmlUnit\{FormCheckbox,
FormEditor,
FormImage,
FormImages,
FormRadio,
FormSelect,
FormSwitch,
FormUnitInterface,
JsFacade};
use sdModule\makeAdminBasics\Make;
class View implements MakeItemInterface
{
/**
* @var array
*/
private $fileName = [
'createPage' => 'add.html',
'updatePage' => 'edit.html',
'listPage' => 'lists.html'
];
/**
* @var Make
*/
private $make;
/**
* 主体框架
* @var string
*/
private $main = <<<MAIN
{extend name="frame"}
{block name="title"}%s{/block}
{block name="meta"}{:token_meta()}{/block}
{block name="body"}
%s
{/block}
{block name="js"}
%s
{/block}
MAIN;
/**
* View constructor.
* @param Make $make
*/
public function __construct(Make $make)
{
$this->make = $make;
}
/**
* @return bool
* @throws \ReflectionException
*/
public function make():bool
{
return $this->listPage() && $this->createPage() && $this->editPage();
}
/**
* @return string
* @throws \ReflectionException
*/
protected function createPage()
{
$title = $this->make->pageName . '{:lang("add")}';
$form = $this->formBody();
$js = JsFacade::JsCompose('create', $this->make);
$text = sprintf($this->main, $title, $form, $js);
return $this->make->makeFile(
ConfigHelper::get('file_url.html') . "{$this->make->table}/" . $this->fileName['createPage'], $text);
}
/**
* @return string
* @throws \ReflectionException
*/
protected function editPage()
{
$title = $this->make->pageName . '{:lang("edit")}';
$form = $this->formBody('edit');
$js = JsFacade::JsCompose('edit', $this->make);
$text = sprintf($this->main, $title, $form, $js);
return $this->make->makeFile(
ConfigHelper::get('file_url.html') . "{$this->make->table}/" . $this->fileName['updatePage'], $text);
}
/**
* @return bool|int
*/
protected function listPage()
{
$title = $this->make->pageName . '{:lang("List data")}';
$js = JsFacade::JsCompose('list', $this->make);
$text = sprintf($this->main, $title, $this->listHtml(), $js);
return $this->make->makeFile(
ConfigHelper::get('file_url.html') . "{$this->make->table}/" . $this->fileName['listPage'], $text);
}
/**
* @param string $page
* @return string
* @throws \ReflectionException
*/
private function formBody($page = 'create')
{
$hidden = $page == 'edit' ? "<input type=\"hidden\" name=\"{$this->make->primaryKey}\" value=''/>" : '';
$reset = $page == 'create' ? 'type="reset"' : 'onclick="return formVal();"';
return<<<BODY
<div class="layui-container">
<div class="layui-row">
<form class="layui-form" action="" lay-filter="sd">
{$hidden}
{$this->formHtml($page)}
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo">{:lang("submit")}</button>
<button {$reset} class="layui-btn layui-btn-primary">{:lang("reset")}</button>
<button id="close" class="layui-btn layui-btn-primary">{:lang("close")}</button>
</div>
</div>
</form>
</div>
</div>
BODY;
}
/**
* 表单html
* @param $page
* @return string
* @throws \ReflectionException
*/
private function formHtml($page)
{
$formModule = ConfigHelper::get('form_module');
$html = '';
foreach ($this->make->makeData as $field => $makeDatum) {
if (empty($makeDatum['type'])) continue;
if (empty($formModule[$makeDatum['type']]) || !class_exists($formModule[$makeDatum['type']][0])) {
throw new \Exception("表单模块{$formModule[$makeDatum['type']][0]}不存在, value:{$makeDatum['type']}");
}
/** @var FormUnitInterface $module */
$module = Sc::reflex()->getInstance($formModule[$makeDatum['type']][0], ['label' => $makeDatum['label'], 'name' => $field]);
$methodParam = new \ReflectionMethod($formModule[$makeDatum['type']][0], 'getHtml');
if ($methodParam->getParameters() &&
is_array($methodParam->getParameters()[0]->getDefaultValue())
){
$joinData = $makeDatum['join'];
if (is_string($makeDatum['join'])) {
$joinData = explode(':', strtr($makeDatum['join'], [':' => ':']))[0]
? $joinData
: $this->make->table . $makeDatum['join'];
}
$html .= $page === 'edit'
? ($module instanceof FormImages ? $module->setDefault(true)->getHtml() : $module->getHtml($joinData))
: $this->defaultValue($module, $field)->getHtml($joinData);
}else{
if ( $page === 'edit') {
$html .= $module instanceof FormImage || $module instanceof FormEditor || $module instanceof FormImages
? $module->setDefault("{\$data.{$field}}")->getHtml()
: $module->getHtml();
}else{
$html .= $this->defaultValue($module, $field)->getHtml();
}
}
}
return $html;
}
/**
* 设置对应模块默认值
* @param FormUnitInterface $module
* @param $field
* @return mixed|FormUnitInterface
*/
protected function defaultValue(FormUnitInterface $module, $field)
{
return ($module instanceof FormCheckbox
|| $module instanceof FormRadio
|| $module instanceof FormSelect
|| $module instanceof FormSwitch)
? $module->setDefault($this->make->tableInfo[$field]['column_default'])
: $module;
}
/**
* @return string
*/
public function listHtml()
{
return <<<BODY
<div class="layui-card">
<div class="layui-card-header">{\$page_name ?: lang("Lists")}</div>
<div class="layui-card-body">
{:html_entity_decode(\$search ?? '')}
<table class="layui-hide" id="test" lay-filter="test"></table>
</div>
</div>
BODY;
}
}