|
@@ -27,22 +27,32 @@ class Crud extends Command
|
|
|
/**
|
|
|
* Enum类型识别为单选框的结尾字符,默认会识别为单选下拉列表
|
|
|
*/
|
|
|
- protected $enumRadioSuffix = 'data';
|
|
|
+ protected $enumRadioSuffix = ['data', 'state', 'status'];
|
|
|
|
|
|
/**
|
|
|
* Set类型识别为复选框的结尾字符,默认会识别为多选下拉列表
|
|
|
*/
|
|
|
- protected $setCheckboxSuffix = 'data';
|
|
|
+ protected $setCheckboxSuffix = ['data', 'state', 'status'];
|
|
|
|
|
|
/**
|
|
|
- * Int类型识别为日期时间的结尾字符,默认会识别为数字文本框
|
|
|
+ * Int类型识别为日期时间的结尾字符,默认会识别为日期文本框
|
|
|
*/
|
|
|
- protected $intDateSuffix = 'time';
|
|
|
+ protected $intDateSuffix = ['time'];
|
|
|
|
|
|
/**
|
|
|
* 开关后缀
|
|
|
*/
|
|
|
- protected $switchSuffix = 'switch';
|
|
|
+ protected $switchSuffix = ['switch'];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Selectpage对应的后缀
|
|
|
+ */
|
|
|
+ protected $selectpageSuffix = ['_id', '_ids'];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Selectpage多选对应的后缀
|
|
|
+ */
|
|
|
+ protected $selectpagesSuffix = ['_ids'];
|
|
|
|
|
|
/**
|
|
|
* 以指定字符结尾的字段格式化函数
|
|
@@ -98,6 +108,16 @@ class Crud extends Command
|
|
|
->addOption('mode', 'o', Option::VALUE_OPTIONAL, 'relation table mode,hasone or belongsto', 'belongsto')
|
|
|
->addOption('delete', 'd', Option::VALUE_OPTIONAL, 'delete all files generated by CRUD', null)
|
|
|
->addOption('menu', 'u', Option::VALUE_OPTIONAL, 'create menu when CRUD completed', null)
|
|
|
+ ->addOption('setcheckboxsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate checkbox component with suffix', null)
|
|
|
+ ->addOption('enumradiosuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate radio component with suffix', null)
|
|
|
+ ->addOption('imagefield', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate image component with suffix', null)
|
|
|
+ ->addOption('filefield', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate file component with suffix', null)
|
|
|
+ ->addOption('intdatesuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate date component with suffix', null)
|
|
|
+ ->addOption('switchsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate switch component with suffix', null)
|
|
|
+ ->addOption('selectpagesuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate selectpage component with suffix', null)
|
|
|
+ ->addOption('selectpagessuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate multiple selectpage component with suffix', null)
|
|
|
+ ->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null)
|
|
|
+ ->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null)
|
|
|
->setDescription('Build CRUD controller and model from table');
|
|
|
}
|
|
|
|
|
@@ -130,6 +150,47 @@ class Crud extends Command
|
|
|
$relationForeignKey = $input->getOption('relationforeignkey');
|
|
|
//主键
|
|
|
$relationPrimaryKey = $input->getOption('relationprimarykey');
|
|
|
+ //复选框后缀
|
|
|
+ $setcheckboxsuffix = $input->getOption('setcheckboxsuffix');
|
|
|
+ //单选框后缀
|
|
|
+ $enumradiosuffix = $input->getOption('enumradiosuffix');
|
|
|
+ //图片后缀
|
|
|
+ $imagefield = $input->getOption('imagefield');
|
|
|
+ //文件后缀
|
|
|
+ $filefield = $input->getOption('filefield');
|
|
|
+ //日期后缀
|
|
|
+ $intdatesuffix = $input->getOption('intdatesuffix');
|
|
|
+ //开关后缀
|
|
|
+ $switchsuffix = $input->getOption('switchsuffix');
|
|
|
+ //selectpage后缀
|
|
|
+ $selectpagesuffix = $input->getOption('selectpagesuffix');
|
|
|
+ //selectpage多选后缀
|
|
|
+ $selectpagessuffix = $input->getOption('selectpagessuffix');
|
|
|
+ //排序字段
|
|
|
+ $sortfield = $input->getOption('sortfield');
|
|
|
+ //编辑器Class
|
|
|
+ $editorclass = $input->getOption('editorclass');
|
|
|
+ if ($setcheckboxsuffix)
|
|
|
+ $this->setCheckboxSuffix = $setcheckboxsuffix;
|
|
|
+ if ($enumradiosuffix)
|
|
|
+ $this->enumRadioSuffix = $enumradiosuffix;
|
|
|
+ if ($imagefield)
|
|
|
+ $this->imageField = $imagefield;
|
|
|
+ if ($filefield)
|
|
|
+ $this->fileField = $filefield;
|
|
|
+ if ($intdatesuffix)
|
|
|
+ $this->intDateSuffix = $intdatesuffix;
|
|
|
+ if ($switchsuffix)
|
|
|
+ $this->switchSuffix = $switchsuffix;
|
|
|
+ if ($selectpagesuffix)
|
|
|
+ $this->selectpageSuffix = $selectpagesuffix;
|
|
|
+ if ($selectpagessuffix)
|
|
|
+ $this->selectpagesSuffix = $selectpagessuffix;
|
|
|
+ if ($editorclass)
|
|
|
+ $this->editorClass = $editorclass;
|
|
|
+ if ($sortfield)
|
|
|
+ $this->sortField = $sortfield;
|
|
|
+
|
|
|
//如果有启用关联模式
|
|
|
if ($relation && !in_array($mode, ['hasone', 'belongsto']))
|
|
|
{
|
|
@@ -140,14 +201,22 @@ class Crud extends Command
|
|
|
$prefix = Config::get('database.prefix');
|
|
|
|
|
|
//检查主表
|
|
|
- $tableName = $prefix . $table;
|
|
|
+ $tableName = $table;
|
|
|
+ $modelTableType = 'table';
|
|
|
$tableInfo = Db::query("SHOW TABLE STATUS LIKE '{$tableName}'", [], TRUE);
|
|
|
if (!$tableInfo)
|
|
|
{
|
|
|
- throw new Exception("table not found");
|
|
|
+ $tableName = $prefix . $table;
|
|
|
+ $modelTableType = 'name';
|
|
|
+ $tableInfo = Db::query("SHOW TABLE STATUS LIKE '{$tableName}'", [], TRUE);
|
|
|
+ if (!$tableInfo)
|
|
|
+ {
|
|
|
+ throw new Exception("table not found");
|
|
|
+ }
|
|
|
}
|
|
|
$tableInfo = $tableInfo[0];
|
|
|
-
|
|
|
+
|
|
|
+ $relationModelTableType = 'table';
|
|
|
//检查关联表
|
|
|
if ($relation)
|
|
|
{
|
|
@@ -155,7 +224,13 @@ class Crud extends Command
|
|
|
$relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], TRUE);
|
|
|
if (!$relationTableInfo)
|
|
|
{
|
|
|
- throw new Exception("relation table not found");
|
|
|
+ $relationTableName = $relation;
|
|
|
+ $relationModelTableType = 'name';
|
|
|
+ $relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], TRUE);
|
|
|
+ if (!$relationTableInfo)
|
|
|
+ {
|
|
|
+ throw new Exception("relation table not found");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -438,8 +513,9 @@ class Crud extends Command
|
|
|
{
|
|
|
$fieldName = $inputType == 'checkbox' ? $fieldName .= "[]" : $fieldName;
|
|
|
$attrArr['name'] = "row[{$fieldName}]";
|
|
|
- $itemArr = $this->getLangArray($itemArr, FALSE);
|
|
|
+
|
|
|
$this->getEnum($getEnumArr, $controllerAssignList, $field, $itemArr, $inputType);
|
|
|
+ $itemArr = $this->getLangArray($itemArr, FALSE);
|
|
|
//添加一个获取器
|
|
|
$this->getAttr($getAttrArr, $field, $inputType);
|
|
|
$this->appendAttr($appendAttrList, $field);
|
|
@@ -478,20 +554,21 @@ class Crud extends Command
|
|
|
{
|
|
|
$search = $replace = '';
|
|
|
//特殊字段为关联搜索
|
|
|
- if (substr($field, -3) == '_id' || substr($field, -4) == '_ids')
|
|
|
+ if ($this->isMatchSuffix($field, $this->selectpageSuffix))
|
|
|
{
|
|
|
$inputType = 'text';
|
|
|
$defaultValue = '';
|
|
|
$attrArr['data-rule'] = 'required';
|
|
|
$cssClassArr[] = 'selectpage';
|
|
|
$attrArr['data-db-table'] = substr($field, 0, strripos($field, '_'));
|
|
|
+ //如果是类型表需要特殊处理下
|
|
|
if ($attrArr['data-db-table'] == 'category')
|
|
|
{
|
|
|
$attrArr['data-params'] = '##replacetext##';
|
|
|
$search = '"##replacetext##"';
|
|
|
$replace = '\'{"custom[type]":"' . $table . '"}\'';
|
|
|
}
|
|
|
- if (substr($field, -4) == '_ids')
|
|
|
+ if ($this->isMatchSuffix($field, $this->selectpagesSuffix))
|
|
|
{
|
|
|
$attrArr['data-multiple'] = 'true';
|
|
|
}
|
|
@@ -508,13 +585,9 @@ class Crud extends Command
|
|
|
$step = array_intersect($cssClassArr, ['selectpage']) ? 0 : $step;
|
|
|
$attrArr['class'] = implode(' ', $cssClassArr);
|
|
|
$isUpload = false;
|
|
|
- foreach (array_merge($this->imageField, $this->fileField) as $m => $n)
|
|
|
+ if ($this->isMatchSuffix($field, array_merge($this->imageField, $this->fileField)))
|
|
|
{
|
|
|
- if (preg_match("/{$n}$/i", $field))
|
|
|
- {
|
|
|
- $isUpload = true;
|
|
|
- break;
|
|
|
- }
|
|
|
+ $isUpload = true;
|
|
|
}
|
|
|
//如果是步长则加上步长
|
|
|
if ($step)
|
|
@@ -556,11 +629,8 @@ class Crud extends Command
|
|
|
$javascriptList[] = "{checkbox: true}";
|
|
|
}
|
|
|
//构造JS列信息
|
|
|
- $javascriptList[] = $this->getJsColumn($field, $v['DATA_TYPE']);
|
|
|
- if ($inputType && in_array($inputType, ['select', 'checkbox', 'radio']))
|
|
|
- {
|
|
|
- $javascriptList[] = $this->getJsColumn($field, $v['DATA_TYPE'], '_text');
|
|
|
- }
|
|
|
+ $javascriptList[] = $this->getJsColumn($field, $v['DATA_TYPE'], $inputType && in_array($inputType, ['select', 'checkbox', 'radio']) ? '_text' : '');
|
|
|
+
|
|
|
//排序方式,如果有指定排序字段,否则按主键排序
|
|
|
$order = $field == $this->sortField ? $this->sortField : $order;
|
|
|
}
|
|
@@ -631,7 +701,9 @@ class Crud extends Command
|
|
|
'createTime' => in_array('createtime', $fieldArr) ? "'createtime'" : 'false',
|
|
|
'updateTime' => in_array('updatetime', $fieldArr) ? "'updatetime'" : 'false',
|
|
|
'modelTableName' => $table,
|
|
|
+ 'modelTableType' => $modelTableType,
|
|
|
'relationModelTableName' => $relation,
|
|
|
+ 'relationModelTableType' => $relationModelTableType,
|
|
|
'relationModelName' => $relationModelName,
|
|
|
'relationWith' => '',
|
|
|
'relationMethod' => '',
|
|
@@ -842,8 +914,12 @@ EOD;
|
|
|
$itemArr = [$field => $fieldLang];
|
|
|
foreach (explode(',', $item) as $k => $v)
|
|
|
{
|
|
|
- list($key, $value) = explode('=', $v);
|
|
|
- $itemArr[$field . ' ' . $key] = $value;
|
|
|
+ $valArr = explode('=', $v);
|
|
|
+ if (count($valArr) == 2)
|
|
|
+ {
|
|
|
+ list($key, $value) = $valArr;
|
|
|
+ $itemArr[$field . ' ' . $key] = $value;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -910,8 +986,12 @@ EOD;
|
|
|
$itemArr = [];
|
|
|
foreach (explode(',', $item) as $k => $v)
|
|
|
{
|
|
|
- list($key, $value) = explode('=', $v);
|
|
|
- $itemArr[$key] = $field . ' ' . $key;
|
|
|
+ $valArr = explode('=', $v);
|
|
|
+ if (count($valArr) == 2)
|
|
|
+ {
|
|
|
+ list($key, $value) = $valArr;
|
|
|
+ $itemArr[$key] = $field . ' ' . $key;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -964,22 +1044,22 @@ EOD;
|
|
|
}
|
|
|
$fieldsName = $v['COLUMN_NAME'];
|
|
|
// 指定后缀说明也是个时间字段
|
|
|
- if (preg_match("/{$this->intDateSuffix}$/i", $fieldsName))
|
|
|
+ if ($this->isMatchSuffix($fieldsName, $this->intDateSuffix))
|
|
|
{
|
|
|
$inputType = 'datetime';
|
|
|
}
|
|
|
// 指定后缀结尾且类型为enum,说明是个单选框
|
|
|
- if (preg_match("/{$this->enumRadioSuffix}$/i", $fieldsName) && $v['DATA_TYPE'] == 'enum')
|
|
|
+ if ($this->isMatchSuffix($fieldsName, $this->enumRadioSuffix) && $v['DATA_TYPE'] == 'enum')
|
|
|
{
|
|
|
$inputType = "radio";
|
|
|
}
|
|
|
// 指定后缀结尾且类型为set,说明是个复选框
|
|
|
- if (preg_match("/{$this->setCheckboxSuffix}$/i", $fieldsName) && $v['DATA_TYPE'] == 'set')
|
|
|
+ if ($this->isMatchSuffix($fieldsName, $this->setCheckboxSuffix) && $v['DATA_TYPE'] == 'set')
|
|
|
{
|
|
|
$inputType = "checkbox";
|
|
|
}
|
|
|
// 指定后缀结尾且类型为char或tinyint且长度为1,说明是个Switch复选框
|
|
|
- if (preg_match("/{$this->switchSuffix}$/i", $fieldsName) && ($v['COLUMN_TYPE'] == 'tinyint(1)' || $v['COLUMN_TYPE'] == 'char(1)') && $v['COLUMN_DEFAULT'] !== '' && $v['COLUMN_DEFAULT'] !== null)
|
|
|
+ if ($this->isMatchSuffix($fieldsName, $this->switchSuffix) && ($v['COLUMN_TYPE'] == 'tinyint(1)' || $v['COLUMN_TYPE'] == 'char(1)') && $v['COLUMN_DEFAULT'] !== '' && $v['COLUMN_DEFAULT'] !== null)
|
|
|
{
|
|
|
$inputType = "switch";
|
|
|
}
|
|
@@ -987,6 +1067,25 @@ EOD;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 判断是否符合指定后缀
|
|
|
+ * @param string $field 字段名称
|
|
|
+ * @param mixed $suffixArr 后缀
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ protected function isMatchSuffix($field, $suffixArr)
|
|
|
+ {
|
|
|
+ $suffixArr = is_array($suffixArr) ? $suffixArr : explode(',', $suffixArr);
|
|
|
+ foreach ($suffixArr as $k => $v)
|
|
|
+ {
|
|
|
+ if (preg_match("/{$v}$/i", $field))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取表单分组数据
|
|
|
* @param string $field
|
|
|
* @param string $content
|
|
@@ -1014,13 +1113,9 @@ EOD;
|
|
|
protected function getImageUpload($field, $content)
|
|
|
{
|
|
|
$filter = '';
|
|
|
- foreach ($this->imageField as $k => $v)
|
|
|
+ if ($this->isMatchSuffix($field, $this->imageField))
|
|
|
{
|
|
|
- if (preg_match("/{$v}$/i", $field))
|
|
|
- {
|
|
|
- $filter = ' data-mimetype="image/*"';
|
|
|
- break;
|
|
|
- }
|
|
|
+ $filter = ' data-mimetype="image/*"';
|
|
|
}
|
|
|
$multiple = substr($field, -1) == 's' ? ' data-multiple="true"' : ' data-multiple="false"';
|
|
|
$preview = $filter ? ' data-preview-id="p-' . $field . '"' : '';
|
|
@@ -1064,9 +1159,16 @@ EOD;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ $formatter = $extend ? '' : $formatter;
|
|
|
if ($extend)
|
|
|
+ {
|
|
|
$html .= ", operate:false";
|
|
|
- if ($formatter && !$extend)
|
|
|
+ if ($datatype == 'set')
|
|
|
+ {
|
|
|
+ $formatter = 'label';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($formatter)
|
|
|
$html .= ", formatter: Table.api.formatter." . $formatter . "}";
|
|
|
else
|
|
|
$html .= "}";
|