CurdCategory.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. <?php
  2. namespace plugin\curd\library;
  3. use laytp\traits\Error;
  4. use plugin\curd\model\curd\Field;
  5. use plugin\curd\model\curd\Table;
  6. use plugin\curd\model\Migrations;
  7. use think\facade\Config;
  8. class CurdCategory
  9. {
  10. use Error;
  11. protected
  12. $tableId,//lt_plugin_autocreate_curd_table表主键ID
  13. $needCreateTable,//是否需要生成表结构
  14. $table,//lt_plugin_autocreate_curd_table表信息
  15. $fields,//lt_plugin_autocreate_curd_field表字段列表
  16. $options,//生成附加属性.只有分类CURD才会有值.存储一个json.格式:{"parent_field":"父级字段","name_field":"分类名字段","order_field":"排序字段","order_type":"排序方式,ASC或者DESC"}
  17. $createAddition,//生成附加属性.数组
  18. $controllerModelClassName,//控制器和模型的类名
  19. $midName,//中间名称,比如表名为lt_test_a_b那么这里的midName就是/test/a/B,拼接控制器和模型文件的路径和namespace都需要用到
  20. $migrationClassName,//数据迁移类名
  21. $migrationFileName, //要生成的数据迁移文件名
  22. $controllerFileName, //生成的控制器文件名
  23. $modelFileName, //要生成的模型文件名
  24. $jsFileName, //要生成的js文件名
  25. $recycleJsFileName, //要生成的回收站的js文件名
  26. $htmlIndexFileName, //要生成的首页html文件名
  27. $htmlAddFileName, //要生成的添加html文件名
  28. $htmlEditFileName, //要生成的编辑html文件名
  29. $htmlRecycleFileName, //要生成的回收站html文件名
  30. $migrationParam, //生成数据迁移文件,模板需要用到的参数数组
  31. $controllerParam, //生成controller文件,模板需要用到的参数数组
  32. $modelParam, //生成model文件,模板需要用到的参数数组
  33. $jsParam, //生成js文件,模板需要用到的参数数组
  34. $recycleJsParam, //生成回收站js文件,模板需要用到的参数数组
  35. $htmlIndexParam, //生成index.html,模板需要用到的参数数组
  36. $htmlAddParam, //生成add.html,模板需要用到的参数数组
  37. $htmlEditParam, //生成edit.html,模板需要用到的参数数组
  38. $htmlRecycleParam, //生成recycle.html,模板需要用到的参数数组
  39. $hasSoftDel //是否拥有软删除
  40. ;
  41. public function __construct($tableId, $needCreateTable, $options)
  42. {
  43. $this->tableId = $tableId;
  44. $this->table = Table::find($this->tableId);
  45. $this->needCreateTable = $needCreateTable;
  46. $this->fields = Field::where('table_id', '=', $this->tableId)->select();
  47. $this->hasSoftDel = false;
  48. //是否拥有软删除功能
  49. foreach ($this->fields as $k => $v) {
  50. if ($v->field == 'delete_time') {
  51. $this->hasSoftDel = true;
  52. }
  53. }
  54. $this->options = $options;
  55. $this->createAddition = json_decode($options['create_addition'], true);
  56. }
  57. public function execute()
  58. {
  59. $this->setParam();
  60. $this->create();
  61. return true;
  62. }
  63. /**
  64. * 设置参数,待生成
  65. * @throws Exception
  66. */
  67. public function setParam()
  68. {
  69. $this->setMidFileName();
  70. $this->setMigrationFileName();
  71. $this->setFileName();
  72. $this->setMigrationParam();
  73. $this->setControllerParam();
  74. $this->setModelParam();
  75. $this->setJsParam();
  76. $this->setHtmlParam();
  77. }
  78. /**
  79. * 设置当前生成的类路径名称
  80. */
  81. protected function setMidFileName()
  82. {
  83. $arrTable = explode('_', $this->table->table);
  84. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  85. $this->controllerModelClassName = $basename;
  86. array_shift($arrTable);
  87. array_pop($arrTable);
  88. if (count($arrTable)) {
  89. $strTable = implode('/', $arrTable) . '/' . $basename;
  90. } else {
  91. $strTable = $basename;
  92. }
  93. $this->midName = $strTable;
  94. }
  95. /**
  96. * 根据表名获取中间名
  97. * @param $tableName
  98. * @return string
  99. */
  100. protected function getMidName($tableName)
  101. {
  102. $arrTable = explode('_', $tableName);
  103. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  104. $this->controllerModelClassName = $basename;
  105. array_shift($arrTable);
  106. array_pop($arrTable);
  107. if (count($arrTable)) {
  108. $strTable = implode('/', $arrTable) . '/' . $basename;
  109. } else {
  110. $strTable = $basename;
  111. }
  112. return $strTable;
  113. }
  114. //设置需要生成的数据迁移文件名
  115. protected function setMigrationFileName()
  116. {
  117. if ($this->needCreateTable) {
  118. $arrTable = explode('_', $this->table->table);
  119. array_shift($arrTable);
  120. foreach ($arrTable as $k => $name) {
  121. $arrTable[$k] = ucfirst($name);
  122. }
  123. $this->migrationClassName = implode('', $arrTable);
  124. $migrations = new Migrations();
  125. $migration = $migrations->where('migration_name', '=', ucfirst($this->migrationClassName))->find();
  126. if ($migration) {
  127. $this->migrationFileName = app()->getRootPath() . 'database' . DS . 'migrations' . DS . $migration->version . '_' . lcfirst($migration->migration_name) . '.php';
  128. $migration->delete();
  129. } else {
  130. $this->migrationFileName = app()->getRootPath() . 'database' . DS . 'migrations' . DS . date('YmdHis') . '_' . lcfirst(implode('', $arrTable)) . '.php';
  131. }
  132. }
  133. }
  134. protected function filterMidName($midName)
  135. {
  136. if(substr($midName, 0, 6) === 'admin/'){
  137. return substr($midName, 6);
  138. }
  139. return $midName;
  140. }
  141. //设置所有需要生成的文件名
  142. protected function setFileName()
  143. {
  144. $this->controllerFileName = app()->getAppPath() . 'controller' . DS . 'admin' . DS . $this->filterMidName($this->midName) . '.php';
  145. $this->modelFileName = app()->getAppPath() . 'model' . DS . $this->midName . '.php';
  146. $this->jsFileName = app()->getRootPath() . 'public' . DS . 'static' . DS . 'admin' . DS . 'js' . DS . $this->filterMidName(strtolower($this->midName)) . '.js';
  147. $this->recycleJsFileName = app()->getRootPath() . 'public' . DS . 'static' . DS . 'admin' . DS . 'js' . DS . $this->filterMidName(strtolower($this->midName)) . 'Recycle.js';
  148. $this->htmlIndexFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'index.html';
  149. $this->htmlAddFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'add.html';
  150. $this->htmlEditFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'edit.html';
  151. $this->htmlRecycleFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'recycle.html';
  152. }
  153. //设置生成migration需要的参数
  154. protected function setMigrationParam()
  155. {
  156. $tplName = 'migration' . DS . 'base';
  157. $data['className'] = $this->migrationClassName;
  158. $data['tableName'] = str_replace(Config::get("database.connections." . Config::get("database.default") . ".prefix"), '', $this->table->table);
  159. $data['engine'] = $this->table->engine;
  160. $data['tableComment'] = $this->table->comment;
  161. $data['collation'] = $this->table->collation;
  162. $fields = '';
  163. foreach ($this->fields as $field) {
  164. $fieldData['field'] = $field->field;
  165. $fieldData['dataType'] = $field->data_type;
  166. $fieldData['null'] = ($field->is_empty == 2) ? 0 : 1;
  167. if (in_array($field->data_type, ['integer', 'biginteger', 'boolean', 'decimal', 'float'])) {
  168. $field->default = intval($field->default);
  169. }
  170. $fieldData['default'] = (!in_array($field->data_type, ['text', 'datetime', 'timestamp'])) ? ' \'default\' => \'' . $field->default . '\',' : ' ';
  171. if ($field->form_type == 'radio' || $field->form_type == 'select') {
  172. $comment = '';
  173. foreach ($field->addition['text'] as $k => $t) {
  174. $comment .= $field->addition['value'][$k] . '=' . $t . ',';
  175. }
  176. if (is_string($field->addition['default'])) {
  177. $comment .= '默认:' . $field->addition['default'];
  178. }
  179. $fieldData['comment'] = $field->comment . '.' . $comment;
  180. } elseif ($field->form_type == 'checkbox') {
  181. $comment = '';
  182. foreach ($field->addition['text'] as $k => $t) {
  183. $comment .= $field->addition['value'][$k] . '=' . $t . ',';
  184. }
  185. $fieldData['comment'] = $field->comment . '.' . $comment . '默认:' . implode(',', $field->addition['default']);
  186. } elseif ($field->form_type == 'switch') {
  187. $comment = $field->addition['close_value'] . '=' . $field->addition['close_text'] . ',' .
  188. $field->addition['open_value'] . '=' . $field->addition['open_text'] .
  189. ',默认:' . (($field->addition['default_status'] === 'close') ? $field->addition['close_value'] : $field->addition['open_value']);
  190. $fieldData['comment'] = $field->comment . '.' . $comment;
  191. } elseif ($field->form_type == 'xm_select' && $field->addition['data_from_type'] == 'data') {
  192. $comment = '';
  193. foreach ($field->addition['text'] as $k => $t) {
  194. $comment .= $field->addition['value'][$k] . '=' . $t . ',';
  195. }
  196. if ($field->addition['default']) {
  197. $comment .= '默认:' . implode(',', $field->addition['default']);
  198. }
  199. $fieldData['comment'] = $field->comment . '.' . $comment;
  200. } else {
  201. $fieldData['comment'] = $field->comment;
  202. }
  203. if (in_array($field->data_type, ["float", "decimal"])) {
  204. $fieldData['limitPrecisionScale'] = "'precision' => {$field->precision}, 'scale' => {$field->scale}, ";
  205. } elseif (in_array($field->data_type, ["text", "datetime", 'timestamp'])) {
  206. $fieldData['limitPrecisionScale'] = '';
  207. } else {
  208. $fieldData['limitPrecisionScale'] = "'limit' => {$field->limit}, ";
  209. }
  210. $fields .= $this->getReplacedTpl('migration' . DS . 'field', $fieldData) . "\n\t\t\t";
  211. }
  212. $data['fields'] = $fields;
  213. $this->migrationParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->migrationFileName];
  214. }
  215. /**
  216. * 设置生成controller需要的参数
  217. * [
  218. * 'tplName'=>模板名,
  219. * 'data' => '执行替换模板的key=>value数组',
  220. * 'fileName' => '要生成的文件名'
  221. * ]
  222. */
  223. protected function setControllerParam()
  224. {
  225. $tplName = 'controller' . DS . 'base';
  226. $data['controllerNamespace'] = str_replace('/', '\\', dirname('app/controller/admin/' . $this->filterMidName($this->midName)));
  227. $data['tableComment'] = $this->table->comment;
  228. $data['modelName'] = strtolower(str_replace('/', '_', $this->midName));
  229. $data['modelClassName'] = $this->controllerModelClassName;
  230. $data['modelNamespace'] = str_replace('/', '\\', dirname('app/model/' . $this->midName));
  231. $data['controllerClassName'] = $this->controllerModelClassName;
  232. $data['indexFunction'] = $this->setIndexFunctionController();
  233. $data['infoFunction'] = $this->setInfoFunctionController();
  234. $data['recycleFunction'] = $this->setRecycleFunctionController();
  235. $data['addFunction'] = $this->setAddFunctionController();
  236. $data['editFunction'] = $this->setEditFunctionController();
  237. $data['tableEditFunction'] = $this->setTableEditFunctionController();
  238. $data['useAdminUserServiceFacade'] = '';
  239. $data['uploadDomainPackage'] = '';
  240. foreach ($this->fields as $k => $v) {
  241. if($v['form_type'] === 'admin_user_id'){
  242. $data['useAdminUserServiceFacade'] = "\nuse app\service\admin\UserServiceFacade;";
  243. }
  244. if($v['form_type'] == 'upload' || $v['form_type'] == 'editor'){
  245. $data['uploadDomainPackage'] = "\nuse laytp\library\UploadDomain;";
  246. }
  247. }
  248. $this->controllerParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->controllerFileName];
  249. }
  250. //在index方法里,如果有关联模型,在查询时需要查询出关联模型数据
  251. protected function setIndexFunctionController()
  252. {
  253. $relationIndexFunctionLt = 'controller' . DS . 'index';
  254. $with = [];
  255. $multiUpload = [];
  256. $data['withRelation'] = "";
  257. $data['multiUpload'] = "";
  258. $data['pidName'] = ($this->createAddition['parent_field'] && $this->createAddition['parent_field'] != 'pid') ?
  259. "\n\t\t\$treeLib->pidName = '{$this->createAddition['parent_field']}';\n" : '';
  260. foreach ($this->fields as $k => $v) {
  261. if ($v['relation']['table']) {
  262. $with[] = $v['relation']['fun_name'];
  263. }
  264. if ($v['form_type'] === 'upload' && $v['addition']['multi'] === 'single') {
  265. $with[] = $this->underlineToCamel($v['field']) . 'File';
  266. }
  267. if ($v['form_type'] === 'upload' && $v['addition']['multi'] === 'multi') {
  268. $multiUpload[] = $v['field'];
  269. }
  270. }
  271. if ($with) {
  272. $data['withRelation'] = "->with(['" . implode("','", $with) . "'])";
  273. }
  274. if($multiUpload){
  275. $data['multiUpload'] .= "->each(function(\$item){\n";
  276. foreach($multiUpload as $field){
  277. $data['multiUpload'] .=
  278. "\t\t\t\t\${$field}FileInfo = UploadDomain::multiJoin(\$item['{$field}']);\n" .
  279. "\t\t\t\t\$item->{$field}_path = \${$field}FileInfo['path'];\n" .
  280. "\t\t\t\t\$item->{$field}_filename = \${$field}FileInfo['filename'];\n";
  281. }
  282. $data['multiUpload'] .="\t\t\t\treturn \$item;\n";
  283. $data['multiUpload'] .="\t\t\t})";
  284. }
  285. $indexFunctionController = $this->getReplacedTpl($relationIndexFunctionLt, $data);
  286. return $indexFunctionController;
  287. }
  288. //在info方法里,如果有上传组件,在查询的时候需要把文件相关的数据查询出来
  289. protected function setInfoFunctionController(){
  290. $infoFunctionLt = 'controller' . DS . 'info';
  291. $data = [];
  292. $data['withRelation'] = "";
  293. $data['multiUpload'] = "";
  294. $with = [];
  295. $multiUpload = [];
  296. foreach ($this->fields as $k => $v) {
  297. if ($v['form_type'] === 'upload' && $v['addition']['multi'] === 'single') {
  298. $with[] = $this->underlineToCamel($v['field']) . 'File';
  299. }
  300. if ($v['form_type'] === 'upload' && $v['addition']['multi'] === 'multi') {
  301. $multiUpload[] = $v['field'];
  302. }
  303. }
  304. if ($with) {
  305. $data['withRelation'] = "->with(['" . implode("','", $with) . "'])";
  306. }
  307. if($multiUpload){
  308. $data['multiUpload'] .= "\n";
  309. foreach($multiUpload as $field){
  310. $data['multiUpload'] .=
  311. "\t\t\${$field}FileInfo = UploadDomain::multiJoin(\$info['{$field}']);\n" .
  312. "\t\t\$info['{$field}_path'] = \${$field}FileInfo['path'];\n" .
  313. "\t\t\$info['{$field}_filename'] = \${$field}FileInfo['filename'];\n";
  314. }
  315. }
  316. $infoFunctionController = $this->getReplacedTpl($infoFunctionLt, $data);
  317. return $infoFunctionController;
  318. }
  319. //在index方法里,如果有关联模型,在查询时需要查询出关联模型数据
  320. protected function setRecycleFunctionController()
  321. {
  322. $recycleFunctionController = '';
  323. $with = [];
  324. foreach ($this->fields as $k => $v) {
  325. if ($v['relation']['table']) {
  326. $with[] = $v['relation']['fun_name'];
  327. }
  328. }
  329. if ($with) {
  330. $relationIndexFunctionLt = 'controller' . DS . 'recycle';
  331. $data['withRelation'] = "->with(['" . implode("','", $with) . "'])";
  332. $recycleFunctionController = $this->getReplacedTpl($relationIndexFunctionLt, $data);
  333. }
  334. return $recycleFunctionController;
  335. }
  336. //富文本编辑器字段,在添加和编辑的控制器方法里面,所有链接要去掉前缀
  337. protected function setAddFunctionController()
  338. {
  339. $arrUploadDomainFields = [];
  340. $adminUserId = '';
  341. foreach ($this->fields as $k => $v) {
  342. if ($v['form_type'] == 'editor') {
  343. $arrUploadDomainFields[] = "\$post['{$v['field']}'] = UploadDomain::delUploadDomain(\$post['{$v['field']}'], '{$v['addition']['upload_type']}');";
  344. }
  345. if($v['form_type'] == 'admin_user_id'){
  346. $adminUserId = "\$post['{$v['field']}'] = UserServiceFacade::getUser()->id;";
  347. }
  348. }
  349. $data['uploadDomainFields'] = '';
  350. if ($arrUploadDomainFields || $adminUserId) {
  351. $data['uploadDomainFields'] = implode("\n\t\t", $arrUploadDomainFields);
  352. $data['adminUserId'] = $adminUserId;
  353. $addFunctionLt = 'controller' . DS . 'add';
  354. $addFunctionController = $this->getReplacedTpl($addFunctionLt, $data);
  355. return $addFunctionController;
  356. } else {
  357. return '';
  358. }
  359. }
  360. //富文本编辑器字段,在添加和编辑的控制器方法里面,所有链接要去掉前缀
  361. protected function setEditFunctionController()
  362. {
  363. $arrUploadDomainFields = [];
  364. $adminUserId = '';
  365. foreach ($this->fields as $k => $v) {
  366. if ($v['form_type'] == 'editor') {
  367. $arrUploadDomainFields[] = "\$post['{$v['field']}'] = UploadDomain::delUploadDomain(\$post['{$v['field']}'], '{$v['addition']['upload_type']}');";
  368. }
  369. if($v['form_type'] == 'admin_user_id'){
  370. $adminUserId = "\$post['{$v['field']}'] = UserServiceFacade::getUser()->id;";
  371. }
  372. }
  373. $data['uploadDomainFields'] = '';
  374. if ($arrUploadDomainFields) {
  375. $data['uploadDomainFields'] = implode("\n\t\t", $arrUploadDomainFields);
  376. $data['adminUserId'] = $adminUserId;
  377. $editFunctionLt = 'controller' . DS . 'edit';
  378. $editFunctionController = $this->getReplacedTpl($editFunctionLt, $data);
  379. return $editFunctionController;
  380. } else {
  381. return '';
  382. }
  383. }
  384. //设置表格编辑方法
  385. protected function setTableEditFunctionController(){
  386. $functionContent = [];
  387. foreach ($this->fields as $k => $v) {
  388. if (($v['form_type'] == 'input' && isset($v['addition']['open_table_edit']) && $v['addition']['open_table_edit'] == 1) || $v['form_type'] == 'switch') {
  389. $data['field'] = $v['field'];
  390. $data['comment'] = $v['comment'];
  391. $data['funcName'] = ucfirst($this->underlineToCamel($v['field']));
  392. $tableEditFunctionLt = 'controller' . DS . 'tableEdit';
  393. $functionContent[] = $this->getReplacedTpl($tableEditFunctionLt, $data);
  394. }
  395. }
  396. $tableEditFunctionController = implode("\n\t\t", $functionContent);
  397. return $tableEditFunctionController;
  398. }
  399. /**
  400. * 设置生成model需要的参数
  401. * [
  402. * 'tplName'=>模板名,
  403. * 'data' => '执行替换模板的key=>value数组',
  404. * 'fileName' => '要生成的文件名'
  405. * ]
  406. */
  407. protected function setModelParam()
  408. {
  409. $tplName = 'model' . DS . 'base';
  410. $arrTableName = explode('_', $this->table->table);
  411. $tablePrefix = $arrTableName['0'] . '_';
  412. $data['tableName'] = '';
  413. if ($tablePrefix != Config::get('database.connections.' . Config::get('database.default') . '.prefix')) {
  414. $data['tableName'] = 'protected $table = \'' . $this->table->table . '\';';
  415. }
  416. $data['tableComment'] = $this->table->comment;
  417. $data['modelName'] = strtolower(str_replace('/', '_', $this->midName));
  418. $data['modelClassName'] = $this->controllerModelClassName;
  419. $data['modelNamespace'] = str_replace('/', '\\', dirname('app/model/' . $this->midName));
  420. $data['relationModel'] = $this->setRelationModel();
  421. $data['autoTimeFormat'] = $this->autoTimeFormat();
  422. $data['getAttrFun'] = $this->getAttrFun();
  423. $this->modelParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->modelFileName];
  424. }
  425. //时间选择器,int类型,自动类型转换
  426. protected function autoTimeFormat()
  427. {
  428. $timeSet = [];
  429. foreach ($this->fields as $k => $v) {
  430. if ($v['form_type'] == 'laydate' && $v['data_type'] == 'integer') {
  431. if ($v['addition']['date_type'] == 'datetime') {
  432. $timeSet[$v['field']] = "\n\t\t" . '\'' . $v['field'] . '\' => \'timestamp:Y-m-d H:i:s\',';
  433. } else if ($v['addition']['date_type'] == 'month') {
  434. $timeSet[$v['field']] = "\n\t\t" . '\'' . $v['field'] . '\' => \'timestamp:Y-m\',';
  435. } else if ($v['addition']['date_type'] == 'date') {
  436. $timeSet[$v['field']] = "\n\t\t" . '\'' . $v['field'] . '\' => \'timestamp:Y-m-d\',';
  437. }
  438. }
  439. }
  440. if ($timeSet) {
  441. return 'protected $type = [' . implode('', $timeSet) . "\n\t];";
  442. } else {
  443. return '';
  444. }
  445. }
  446. protected function getAttrFun()
  447. {
  448. $fun = [];
  449. foreach ($this->fields as $k => $v) {
  450. if ($v['form_type'] == 'laydate' && $v['data_type'] == 'integer') {
  451. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'IntAttr($value, $data)' . "\n\t" .
  452. '{' . "\n\t\t" .
  453. 'return isset($data[\'' . $v['field'] . '\']) ? $data[\'' . $v['field'] . '\'] : 0;' . "\n\t" .
  454. '}';
  455. }
  456. if ($v['form_type'] == 'laydate' && $v['data_type'] == 'datetime') {
  457. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'IntAttr($value, $data)' . "\n\t" .
  458. '{' . "\n\t\t" .
  459. 'return isset($data[\'' . $v['field'] . '\']) ? strtotime($data[\'' . $v['field'] . '\']) : 0;' . "\n\t" .
  460. '}';
  461. }
  462. if ($v['form_type'] == 'upload' && $v['addition']['multi'] == 'multi') {
  463. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'FileAttr($value, $data)' . "\n\t" .
  464. '{' . "\n\t\t" .
  465. 'return (isset($data[\'' . $v['field'] . '\']) && $data[\'' . $v['field'] . '\']) ? UploadDomain::multiJoin($data[\''.$v['field'].'\']) : \'\';' . "\n\t" .
  466. '}';
  467. }
  468. if ($v['form_type'] == 'editor') {
  469. if($v['addition']['upload_type'] != 'local'){
  470. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'Attr($value, $data)' . "\n\t" .
  471. '{' . "\n\t\t" .
  472. 'return $value ? UploadDomain::addUploadDomain($value, \''.$v['addition']['upload_type'].'\') : \'\';' . "\n\t" .
  473. '}';
  474. }else{
  475. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'Attr($value, $data)' . "\n\t" .
  476. '{' . "\n\t\t" .
  477. 'return $value ? UploadDomain::addUploadDomain($value) : \'\';' . "\n\t" .
  478. '}';
  479. }
  480. }
  481. }
  482. if ($fun) {
  483. return implode("\n\n\t", $fun);
  484. } else {
  485. return '';
  486. }
  487. }
  488. protected function setRelationModel()
  489. {
  490. $relationModelFunctionLt = 'model' . DS . 'relation_model_function';
  491. $arrayRelationModel = [];
  492. $relationModel = '';
  493. foreach ($this->fields as $k => $item) {
  494. if ($item['relation']['table']) {
  495. $data['relationFunctionName'] = $this->underlineToCamel($item['relation']['fun_name']);
  496. $data['relationType'] = $item['relation']['type'];
  497. $data['relationModelName'] = 'app\model\\' . $this->getNameByTable($item['relation']['table']);
  498. $data['foreignKey'] = $item['field'];
  499. $data['localKey'] = $item['relation']['field'];
  500. $arrayRelationModel[] = $this->getReplacedTpl($relationModelFunctionLt, $data);
  501. }
  502. if($item['form_type'] === 'upload' && $item['addition']['multi'] === 'single'){
  503. $data['relationFunctionName'] = $this->underlineToCamel($item['field']) . 'File';
  504. $data['relationType'] = 'belongsTo';
  505. $data['relationModelName'] = 'app\model\Files';
  506. $data['foreignKey'] = $item['field'];
  507. $data['localKey'] = 'id';
  508. $arrayRelationModel[] = $this->getReplacedTpl($relationModelFunctionLt, $data);
  509. }
  510. }
  511. if ($arrayRelationModel) {
  512. $relationModel = implode("\n\n\t", $arrayRelationModel);
  513. }
  514. return $relationModel;
  515. }
  516. /**
  517. * 根据表名获取类路径名称
  518. * @param $table
  519. * @return string
  520. */
  521. protected function getNameByTable($table)
  522. {
  523. $arrTable = explode('_', $table);
  524. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  525. array_shift($arrTable);//del_db_prefix
  526. array_pop($arrTable);
  527. if (count($arrTable)) {
  528. $strTable = implode('\\', $arrTable) . '\\' . $basename;
  529. } else {
  530. $strTable = $basename;
  531. }
  532. return $strTable;
  533. }
  534. protected function setJsParam()
  535. {
  536. $apiPrefix = $this->compatibleApiRoute();
  537. $tplName = 'js' . DS . 'index';
  538. $cols = "{type:'checkbox',fixed:'left'}\n\t\t\t\t";
  539. $recycleCols = "{type:'checkbox',fixed:'left'}\n\t\t\t\t";
  540. $hasFirstCols = true;//是否已经有了正常的第一行数据
  541. //是否隐藏主键列
  542. if ($this->table->is_hide_pk != 1) {
  543. $hasFirstCols = true;
  544. $temp = ",{field:'id',title:'ID',align:'center',width:80,fixed:'left'}";
  545. $cols .= $temp;
  546. $recycleCols .= $temp;
  547. } else {
  548. $temp = "//,{field:'id',title:'ID',align:'center',width:80}";
  549. $cols .= $temp;
  550. $recycleCols .= $temp;
  551. }
  552. //是否生成序号列
  553. if ($this->table->is_create_number == 1) {
  554. if ($hasFirstCols) {
  555. $temp = "\t\t\t\t,{field:'layui_number',title:'序号',align:'center',width:80,type:'numbers'}";
  556. $cols .= $temp;
  557. $recycleCols .= $temp;
  558. } else {
  559. $temp = "\t\t\t\t{field:'layui_number',title:'序号',align:'center',width:80,type:'numbers'}";
  560. $cols .= $temp;
  561. $recycleCols .= $temp;
  562. }
  563. if (!$hasFirstCols) $hasFirstCols = true;
  564. }
  565. $isTree = 0;
  566. foreach ($this->fields as $k => $v) {
  567. if ($v['table_show'] == 2) continue;
  568. $isTree = $isTree + 1;
  569. if (!$hasFirstCols) {
  570. $hasFirstCols = true;
  571. $temp = "\t\t\t\t{field:'{$v['field']}',title:'{$v['comment']}'";
  572. $recycleTemp = "\t\t\t\t{field:'{$v['field']}',title:'{$v['comment']}'";
  573. } else {
  574. $temp = "\n\t\t\t\t,{field:'{$v['field']}',title:'{$v['comment']}'";
  575. $recycleTemp = "\n\t\t\t\t,{field:'{$v['field']}',title:'{$v['comment']}'";
  576. }
  577. if ($v['cell_width']) {
  578. $temp .= ",width:{$v['cell_width']}";
  579. $recycleTemp .= ",width:{$v['cell_width']}";
  580. }
  581. if ($isTree > 1) {
  582. $temp .= ",align:'center'";
  583. $recycleTemp .= ",align:'center'";
  584. }
  585. //表头排序
  586. if ($v['is_thead_sort'] == 1) {
  587. $temp .= ",sort:true";
  588. $recycleTemp .= ",sort:true";
  589. }
  590. //关联模型js渲染
  591. if ($v['relation']['table'] && $v['relation']['show_field']) {
  592. if ($v['form_type'] === 'xm_select') {
  593. if (isset($v['addition']['single_multi_type']) && $v['addition']['single_multi_type'] === 'single') {
  594. $templet = "{{# if(d." . $v['relation']['fun_name'] . "){ }}{{d." . $v['relation']['fun_name'] . "." . $v['relation']['show_field'] . "}}{{# }else{ }}-{{# } }}";
  595. $temp .= ",templet:'<div>" . $templet . "</div>'";
  596. $recycleTemp .= ",templet:'<div>" . $templet . "</div>'";
  597. }
  598. } else {
  599. $templet = "{{# if(d." . $v['relation']['fun_name'] . "){ }}{{d." . $v['relation']['fun_name'] . "." . $v['relation']['show_field'] . "}}{{# }else{ }}-{{# } }}";
  600. $temp .= ",templet:'<div>" . $templet . "</div>'";
  601. $recycleTemp .= ",templet:'<div>" . $templet . "</div>'";
  602. }
  603. }
  604. //单行输入框,开启了表格编辑,需要渲染成editInput模板,没有开启的话要转义展示,避免XSS
  605. if ($v['form_type'] == 'input') {
  606. if(isset($v['addition']['open_table_edit']) && $v['addition']['open_table_edit'] == 1){
  607. $editInputUrl = $apiPrefix . '/set' . ucfirst($this->underlineToCamel($v['field']));
  608. $temp .= ",templet:function(d){
  609. return laytpForm.tableForm.editInput('{$v['field']}',d,'{$editInputUrl}');
  610. }";
  611. $recycleTemp .= ",templet:function(d){
  612. return laytpForm.tableForm.recycleEditInput('{$v['field']}',d,'{$editInputUrl}');
  613. }";
  614. }
  615. }
  616. //文本域要转义展示,避免XSS
  617. if ($v['form_type'] == 'textarea') {
  618. $temp .= ",templet:function(d){
  619. return layui.laytpl('{{=d.{$v['field']}}}').render({{$v['field']}:d.{$v['field']}});
  620. }";
  621. $recycleTemp .= ",templet:function(d){
  622. return layui.laytpl('{{=d.{$v['field']}}}').render({{$v['field']}:d.{$v['field']}});
  623. }";
  624. }
  625. //渲染单选按钮,单选按钮渲染成status模板
  626. if ($v['form_type'] == 'radio') {
  627. $items = $v['addition'];
  628. $jsonArr['value'] = $items['value'];
  629. $jsonArr['text'] = $items['text'];
  630. $jsonObj = json_encode($jsonArr, JSON_UNESCAPED_UNICODE);
  631. $temp .= ",templet:function(d){
  632. return laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});
  633. }";
  634. $recycleTemp .= ",templet:function(d){
  635. return laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});
  636. }";
  637. }
  638. //渲染开关按钮,开关按钮渲染成开关
  639. if ($v['form_type'] == 'switch') {
  640. $items = $v['addition'];
  641. $temp .= ",templet:function(d){
  642. return laytpForm.tableForm.switch(\"{$v['field']}\", d, {
  643. \"open\": {\"value\": {$items['open_value']}, \"text\": \"{$items['open_text']}\"},
  644. \"close\": {\"value\": {$items['close_value']}, \"text\": \"{$items['close_text']}\"}
  645. }, 'laytp-table-switch',true);
  646. }";
  647. $recycleTemp .= ",templet:function(d){
  648. return laytpForm.tableForm.recycleSwitch(\"{$v['field']}\", d, {
  649. \"open\": {\"value\": {$items['open_value']}, \"text\": \"{$items['open_text']}\"},
  650. \"close\": {\"value\": {$items['close_value']}, \"text\": \"{$items['close_text']}\"}
  651. }, 'laytp-recycle-table-switch', true);
  652. }";
  653. }
  654. //渲染下拉框,下拉框渲染成status模板
  655. if ($v['form_type'] == 'select') {
  656. $jsonObj = json_encode($v['addition'], JSON_UNESCAPED_UNICODE);
  657. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj}, true);\n\t\t\t\t}";
  658. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  659. }
  660. //复选框渲染成flag的模板
  661. if ($v['form_type'] == 'checkbox') {
  662. $jsonObj = json_encode($v['addition'], JSON_UNESCAPED_UNICODE);
  663. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  664. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  665. }
  666. //xmSelect,数据来源=data,单选,渲染成status
  667. if ($v['form_type'] == 'xm_select' && $v['addition']['data_from_type'] == 'data' && $v['addition']['single_multi_type'] == 'single') {
  668. $jsonArr['value'] = $v['addition']['value'];
  669. $jsonArr['text'] = $v['addition']['text'];
  670. $jsonArr['default'] = $v['addition']['default'];
  671. $jsonObj = json_encode($jsonArr, JSON_UNESCAPED_UNICODE);
  672. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  673. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  674. }
  675. //xmSelect,数据来源=data,多选,渲染成flag
  676. if ($v['form_type'] == 'xm_select' && $v['addition']['data_from_type'] == 'data' && $v['addition']['single_multi_type'] == 'multi') {
  677. $jsonArr['value'] = $v['addition']['value'];
  678. $jsonArr['text'] = $v['addition']['text'];
  679. $jsonArr['default'] = $v['addition']['default'];
  680. $jsonObj = json_encode($jsonArr, JSON_UNESCAPED_UNICODE);
  681. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  682. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  683. }
  684. //image模板
  685. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'image') {
  686. if($v['addition']['multi'] == 'single'){
  687. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.images(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  688. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.images(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  689. }else{
  690. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.images(d.{$v['field']}_path);\n\t\t\t\t}";
  691. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.images(d.{$v['field']}_path);\n\t\t\t\t}";
  692. }
  693. }
  694. //video模板
  695. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'video') {
  696. if($v['addition']['multi'] == 'single'){
  697. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.video(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  698. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.video(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  699. }else{
  700. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.video(d.{$v['field']}_path);\n\t\t\t\t}";
  701. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.video(d.{$v['field']}_path);\n\t\t\t\t}";
  702. }
  703. }
  704. //audio模板
  705. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'audio') {
  706. if($v['addition']['multi'] == 'single'){
  707. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.audio(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  708. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.audio(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  709. }else{
  710. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.audio(d.{$v['field']}_path);\n\t\t\t\t}";
  711. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.audio(d.{$v['field']}_path);\n\t\t\t\t}";
  712. }
  713. }
  714. //file模板
  715. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'file') {
  716. if($v['addition']['multi'] == 'single'){
  717. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.file(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  718. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}File ? laytp.tableFormatter.file(d.{$v['field']}File.path) : \"\";\n\t\t\t\t}";
  719. }else{
  720. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.file(d.{$v['field']}_path);\n\t\t\t\t}";
  721. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.file(d.{$v['field']}_path);\n\t\t\t\t}";
  722. }
  723. }
  724. //colorPicker模板
  725. if ($v['form_type'] == 'color_picker') {
  726. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.colorPicker(d.{$v['field']});\n\t\t\t\t}";
  727. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.colorPicker(d.{$v['field']});\n\t\t\t\t}";
  728. }
  729. $temp .= "}";
  730. $recycleTemp .= "}";
  731. if ($v['table_show'] == 1) $cols .= $temp;
  732. $recycleCols .= $recycleTemp;
  733. }
  734. $data['cols'] = $cols;
  735. $data['recycleCols'] = $recycleCols;
  736. $data['cellMinWidth'] = 100;
  737. $data['toolbarDiv'] = $this->hasSoftDel ? 'default_toolbar' : 'un_del_toolbar';
  738. $data['apiPrefix'] = $this->compatibleApiRoute();
  739. $data['htmlPrefix'] = $this->compatibleHtmlPath();
  740. $this->jsParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->jsFileName];
  741. $this->recycleJsParam = ['tplName' => 'js' . DS . 'recycle', 'data' => $data, 'fileName' => $this->recycleJsFileName];
  742. }
  743. public function compatibleApiRoute($midName = false){
  744. if(!$midName){
  745. $midName = strtolower($this->midName);
  746. }else{
  747. $midName = strtolower($midName);
  748. }
  749. if($midName === "user"){
  750. return "/admin.api.user";
  751. }
  752. if(substr($midName,0,5) . '.' != 'admin.'){
  753. return '/admin.' . str_replace('/', '.', $midName);
  754. }else{
  755. return '/' . str_replace('/', '.', $midName);
  756. }
  757. }
  758. public function compatibleHtmlPath(){
  759. $midName = strtolower($this->midName);
  760. if(substr($midName,0,5) != 'admin/'){
  761. return '/admin/' . strtolower($midName);
  762. }else{
  763. return '/' . strtolower($midName);
  764. }
  765. }
  766. protected function setHtmlParam()
  767. {
  768. $indexTplName = 'html' . DS . 'index';
  769. $indexData['jsFileName'] = $this->filterMidName(strtolower($this->midName)) . '.js';
  770. $indexSearchForm = [];
  771. $linkageSelectSearchHtml = [];
  772. $xmSelectRecycle = [];
  773. $unSearchType = ['password', 'upload', 'color_picker'];
  774. foreach ($this->fields as $k => $v) {
  775. if ($v['search_show'] == 2) {
  776. continue;
  777. }
  778. if (!in_array($v['form_type'], $unSearchType)) {
  779. if (in_array($v['form_type'], ['linkage_select'])) {
  780. $linkageSelectSearchHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getSearchFormContent($v);
  781. } else {
  782. $searchItemContent = $this->getSearchFormContent($v);
  783. $indexSearchForm[] = $this->getSearchFormItem($v, $searchItemContent);
  784. $xmSelectRecycle[] = ['key' => count($indexSearchForm) - 1, 'v' => $v];
  785. }
  786. }
  787. }
  788. if (count($linkageSelectSearchHtml)) {
  789. foreach ($linkageSelectSearchHtml as $formType => $groupList) {
  790. foreach ($groupList as $groupName => $item) {
  791. $indexSearchForm[] = $this->getLinkageSelectSearchFormGroup($groupName, join("\n\t\t\t\t", $item));
  792. }
  793. }
  794. }
  795. $indexData['searchForm'] = implode("\n\n\t\t", $indexSearchForm);
  796. $indexData['apiPrefix'] = $this->compatibleApiRoute();
  797. $indexData['htmlPrefix'] = $this->compatibleHtmlPath();
  798. $this->htmlIndexParam = ['tplName' => $indexTplName, 'data' => $indexData, 'fileName' => $this->htmlIndexFileName];
  799. $addData = [];
  800. $editData = [];
  801. $recycleData = [];
  802. $linkageSelectAddHtml = [];
  803. $linkageSelectEditHtml = [];
  804. foreach ($this->fields as $k => $v) {
  805. if (in_array($v['form_type'], ['linkage_select'])) {
  806. if ($v['add_show'] == 1) {
  807. $linkageSelectAddHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getFormItem($v, 'add');
  808. }
  809. if ($v['edit_show'] == 1) {
  810. $linkageSelectEditHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getFormItem($v, 'edit');
  811. }
  812. } else if (in_array($v['form_type'], ['plugin_core_user_id'])) {
  813. $addData[] = $this->getFormItem($v, 'add');
  814. $editData[] = $this->getFormItem($v, 'edit');
  815. } else {
  816. if ($v['add_show'] == 1) {
  817. $addItemContent = $this->getFormItem($v, 'add');
  818. $addData[] = $this->getFormGroup($v['comment'], $addItemContent, $v['is_empty']);
  819. }
  820. if ($v['edit_show'] == 1) {
  821. $editItemContent = $this->getFormItem($v, 'edit');
  822. $editData[] = $this->getFormGroup($v['comment'], $editItemContent, $v['is_empty']);
  823. }
  824. }
  825. }
  826. if (count($linkageSelectAddHtml)) {
  827. foreach ($linkageSelectAddHtml as $form_type => $group_list) {
  828. foreach ($group_list as $group_name => $item) {
  829. $addData[] = $this->getFormGroup($group_name, join("\n\t\t\t\t\t\t", $item), 0);
  830. }
  831. }
  832. }
  833. if (count($linkageSelectEditHtml)) {
  834. foreach ($linkageSelectEditHtml as $form_type => $group_list) {
  835. foreach ($group_list as $group_name => $item) {
  836. $editData[] = $this->getFormGroup($group_name, join("\n\t\t\t\t\t\t", $item), 0);
  837. }
  838. }
  839. }
  840. $addTplName = 'html' . DS . 'add';
  841. $addForm['formContent'] = implode("\n\n", $addData);
  842. $addForm['action'] = $this->compatibleApiRoute() . '/add';
  843. $this->htmlAddParam = ['tplName' => $addTplName, 'data' => $addForm, 'fileName' => $this->htmlAddFileName];
  844. $editTplName = 'html' . DS . 'edit';
  845. $editForm['formContent'] = implode("\n\n", $editData);
  846. $editForm['action'] = $this->compatibleApiRoute() . '/edit';
  847. $editForm['infoAction'] = $this->compatibleApiRoute() . '/info';
  848. $this->htmlEditParam = ['tplName' => $editTplName, 'data' => $editForm, 'fileName' => $this->htmlEditFileName];
  849. $recycleTplName = 'html' . DS . 'recycle';
  850. $recycleSearchForm = $indexSearchForm;
  851. foreach ($xmSelectRecycle as $k => $v) {
  852. $v['v']['isRecycle'] = 1;
  853. $recycleSearchForm[$v['key']] = $this->getSearchFormItem($v['v'], $this->getSearchFormContent($v['v']));
  854. }
  855. $recycleData['searchForm'] = implode("\n\n\t\t", $recycleSearchForm);
  856. $recycleData['jsFileName'] = strtolower($this->midName) . 'Recycle.js';
  857. $recycleData['apiPrefix'] = $this->compatibleApiRoute();
  858. $recycleData['htmlPrefix'] = $this->compatibleHtmlPath();
  859. $this->htmlRecycleParam = ['tplName' => $recycleTplName, 'data' => $recycleData, 'fileName' => $this->htmlRecycleFileName];
  860. }
  861. protected function getFormItem($info, $type = 'add')
  862. {
  863. $func = 'get' . ucfirst($this->underlineToCamel($info['form_type'])) . 'Html';
  864. return $this->$func($info, $type);
  865. }
  866. /**
  867. * 获取表单分组数据
  868. * @param string $field
  869. * @param string $content
  870. * @param integer $isEmpty
  871. * @return string
  872. */
  873. protected function getFormGroup($field, $content, $isEmpty)
  874. {
  875. if ($isEmpty != 1) {
  876. $requiredHtml = " <text title=\"必填项\" style=\"color:red;\">*</text>";
  877. } else {
  878. $requiredHtml = "";
  879. }
  880. return <<<EOD
  881. \t\t\t\t<div class="layui-form-item">
  882. \t\t\t\t<label class="layui-form-label" title="{$field}">{$field}{$requiredHtml}</label>
  883. \t\t\t\t<div class="layui-input-block">
  884. \t\t\t{$content}
  885. \t\t\t\t</div>
  886. \t\t\t\t</div>
  887. EOD;
  888. }
  889. protected function getSearchFormContent($info)
  890. {
  891. $func = 'getSearch' . ucfirst($this->underlineToCamel($info['form_type'])) . 'Html';
  892. return $this->$func($info);
  893. }
  894. protected function getSearchFormItem($info, $content)
  895. {
  896. $fieldComment = $info['comment'];
  897. return <<<EOD
  898. <div class="layui-form-item layui-inline">
  899. <label class="layui-form-label" title="{$fieldComment}">{$fieldComment}</label>
  900. <div class="layui-input-inline">
  901. {$content}
  902. </div>
  903. </div>
  904. EOD;
  905. }
  906. protected function getLinkageSelectSearchFormGroup($field, $content)
  907. {
  908. return <<<EOD
  909. <div class="layui-form-item layui-inline">
  910. <label class="layui-form-label" title="{$field}">{$field}</label>
  911. {$content}
  912. </div>
  913. EOD;
  914. }
  915. /**
  916. * 接下来是html的各种控件模板内容生成,比如input、select、upload等等
  917. */
  918. /**
  919. * 获取input需要生成的html,在生成add和edit表单的时候可以用到
  920. * @param $info
  921. * @param $type string 类型,add或者edit
  922. * @return string
  923. */
  924. protected function getInputHtml($info, $type)
  925. {
  926. $name = 'html' . DS . $type . DS . 'input';
  927. $data['field'] = $info['field'];
  928. $data['comment'] = $info['comment'];
  929. if ($info['is_empty'] == 2) {
  930. $data['verify'] = $info['addition']['verify'] ? 'required|' . $info['addition']['verify'] : 'required';
  931. } else {
  932. $data['verify'] = $info['addition']['verify'];
  933. }
  934. $data['defaultValue'] = ($info['default'] === "") ? "" : " value=\"" . $info['default'] ."\"";
  935. return $this->getReplacedTpl($name, $data);
  936. }
  937. /**
  938. * 获取input需要生成的html,在生成搜索表单时用到
  939. * @param $info
  940. * @return string
  941. */
  942. protected function getSearchInputHtml($info)
  943. {
  944. $name = 'html' . DS . 'search' . DS . 'input';
  945. $data['field'] = $info['field'];
  946. $data['comment'] = $info['comment'];
  947. return $this->getReplacedTpl($name, $data);
  948. }
  949. /**
  950. * admin_id类型模板
  951. * @param $info
  952. * @param $type
  953. * @return string
  954. */
  955. protected function getPluginCoreUserIdHtml($info, $type)
  956. {
  957. $name = 'html' . DS . $type . DS . 'admin_id';
  958. $data['field'] = $info['field'];
  959. $data['comment'] = $info['comment'];
  960. return $this->getReplacedTpl($name, $data);
  961. }
  962. /**
  963. * admin_id类型模板
  964. * @param $info
  965. * @return string
  966. */
  967. protected function getSearchPluginCoreUserIdHtml($info)
  968. {
  969. $name = 'html' . DS . 'search' . DS . 'admin_id';
  970. $data['field'] = $info['field'];
  971. $data['comment'] = $info['comment'];
  972. return $this->getReplacedTpl($name, $data);
  973. }
  974. /**
  975. * 获取input需要生成的html,在生成add和edit表单的时候可以用到
  976. * @param $info
  977. * @param $type string 类型,add或者edit
  978. * @return string
  979. */
  980. protected function getTextareaHtml($info, $type)
  981. {
  982. $name = 'html' . DS . $type . DS . 'textarea';
  983. $data['field'] = $info['field'];
  984. $data['comment'] = $info['comment'];
  985. $data['verify'] = $info['is_empty'] == 1 ? '' : 'required';
  986. return $this->getReplacedTpl($name, $data);
  987. }
  988. /**
  989. * 获取input需要生成的html,在生成搜索表单时用到
  990. * @param $info
  991. * @return string
  992. */
  993. protected function getSearchTextareaHtml($info)
  994. {
  995. $name = 'html' . DS . 'search' . DS . 'textarea';
  996. $data['field'] = $info['field'];
  997. $data['comment'] = $info['comment'];
  998. return $this->getReplacedTpl($name, $data);
  999. }
  1000. /**
  1001. * 获取input需要生成的html,在生成add和edit表单的时候可以用到
  1002. * @param $info
  1003. * @param $type string 类型,add或者edit
  1004. * @return string
  1005. */
  1006. protected function getRadioHtml($info, $type)
  1007. {
  1008. $items = $info['addition'];
  1009. $name = 'html' . DS . $type . DS . 'radio';
  1010. $radioHtml = '';
  1011. foreach ($items['value'] as $k => $v) {
  1012. $tempData['field'] = $info['field'];
  1013. $tempData['value'] = $items['value'][$k];
  1014. $tempData['title'] = $items['text'][$k];
  1015. $tempData['checkedStatus'] = ($items['value'][$k] == $items['default']) ? 'checked="checked"' : '';
  1016. $radioHtml .= $this->getReplacedTpl($name, $tempData) . "\n\t\t\t\t\t\t";
  1017. }
  1018. $radioHtml = rtrim($radioHtml, "\n\t\t\t");
  1019. return $radioHtml;
  1020. }
  1021. protected function getSearchRadioHtml($info)
  1022. {
  1023. $name = 'html' . DS . 'search' . DS . 'radio';
  1024. $data['field'] = $info['field'];
  1025. $items = $info['addition'];
  1026. $options = '';
  1027. foreach ($items['value'] as $k => $v) {
  1028. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['value'][$k] . '">' . $items['text'][$k] . '</option>' . "\n";
  1029. }
  1030. $options = "\t\t\t\t" . '<option value=""></option>' . "\n" . rtrim($options, "\n");
  1031. $data['options'] = $options;
  1032. $data['comment'] = $info['comment'];
  1033. return $this->getReplacedTpl($name, $data);
  1034. }
  1035. protected function getSwitchHtml($info, $type)
  1036. {
  1037. $items = $info['addition'];
  1038. $name = 'html' . DS . $type . DS . 'switch';
  1039. $data['field'] = $info['field'];
  1040. $data['close_value'] = $items['close_value'];
  1041. $data['open_value'] = $items['open_value'];
  1042. $data['checked_status'] = ($items['default_status'] == 'open') ? 'checked="checked"' : '';
  1043. $data['lay_text'] = $items['open_text'] . '|' . $items['close_text'];
  1044. return $this->getReplacedTpl($name, $data);
  1045. }
  1046. protected function getSearchSwitchHtml($info)
  1047. {
  1048. $name = 'html' . DS . 'search' . DS . 'radio';
  1049. $data['field'] = $info['field'];
  1050. $items = $info['addition'];
  1051. $options = '';
  1052. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['close_value'] . '">' . $items['close_text'] . '</option>' . "\n";
  1053. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['open_value'] . '">' . $items['open_text'] . '</option>' . "\n";
  1054. $data['options'] = $options;
  1055. $data['comment'] = $info['comment'];
  1056. return $this->getReplacedTpl($name, $data);
  1057. }
  1058. protected function getSearchCheckboxHtml($info)
  1059. {
  1060. $name = 'html' . DS . 'search' . DS . 'checkbox';
  1061. $data['field'] = $info['field'];
  1062. $items = $info['addition'];
  1063. $data['max'] = count($items);
  1064. $optionItems = [];
  1065. foreach ($items['value'] as $k => $v) {
  1066. $optionItems[] = ['value' => $items['value'][$k], 'name' => $items['text'][$k]];
  1067. }
  1068. $data['source'] = str_replace("\"", "'", json_encode($optionItems, JSON_UNESCAPED_UNICODE));
  1069. return $this->getReplacedTpl($name, $data);
  1070. }
  1071. protected function getCheckboxHtml($info, $type)
  1072. {
  1073. $name = 'html' . DS . $type . DS . 'checkbox';
  1074. $data['field'] = $info['field'];
  1075. $items = $info['addition'];
  1076. $optionItems = [];
  1077. $checkboxHtml = '';
  1078. foreach ($items['value'] as $k => $v) {
  1079. $optionItems[] = ['value' => $items['value'][$k], 'text' => $items['text'][$k]];
  1080. }
  1081. $defaultValueArr = isset($items['default']) ? $items['default'] : [];
  1082. foreach ($optionItems as $k => $v) {
  1083. if($k == 0 && $type == 'edit'){
  1084. $checkboxHtml .= "{{# var " . $info['field'] . "ValueArr = d." . $info['field'] . ".split(','); }}\n\t\t\t\t\t\t";
  1085. }
  1086. if ($type == 'add') {
  1087. $data['checked'] = in_array($v['value'], $defaultValueArr) ? 'checked="checked"' : '';
  1088. }
  1089. $data['value'] = $v['value'];
  1090. $data['text'] = $v['text'];
  1091. $checkboxHtml .= $this->getReplacedTpl($name, $data) . "\n\t\t\t";
  1092. }
  1093. $checkboxHtml = rtrim($checkboxHtml, "\n\t\t\t");
  1094. return $checkboxHtml;
  1095. }
  1096. protected function getSearchSelectHtml($info)
  1097. {
  1098. $name = 'html' . DS . 'search' . DS . 'radio';
  1099. $data['field'] = $info['field'];
  1100. $items = $info['addition'];
  1101. $options = '';
  1102. foreach ($items['value'] as $k => $v) {
  1103. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['value'][$k] . '">' . $items['text'][$k] . '</option>' . "\n";
  1104. }
  1105. $options = "\t\t\t\t" . '<option value=""></option>' . "\n" . rtrim($options, "\n");
  1106. $data['options'] = $options;
  1107. $data['comment'] = $info['comment'];
  1108. return $this->getReplacedTpl($name, $data);
  1109. }
  1110. /**
  1111. * 获取select需要生成的html,在生成add和edit表单的时候可以用到
  1112. * @param $info
  1113. * @param $type string 类型,add或者edit
  1114. * @return string
  1115. */
  1116. protected function getSelectHtml($info, $type)
  1117. {
  1118. $name = 'html' . DS . $type . DS . 'select';
  1119. $items = $info['addition'];
  1120. $options = "\t\t\t\t\t\t\t" . '<option value="">请选择' . $info['comment'] . '</option>' . "\n";;
  1121. foreach ($items['value'] as $k => $v) {
  1122. if ($type == 'add') {
  1123. if ($items['value'][$k] === $items['default']) {
  1124. $options .= "\t\t\t\t\t\t\t" . '<option value="' . $v . '" selected="selected">' . $items['text'][$k] . '</option>' . "\n";
  1125. } else {
  1126. $options .= "\t\t\t\t\t\t\t" . '<option value="' . $v . '">' . $items['text'][$k] . '</option>' . "\n";
  1127. }
  1128. } else {
  1129. $options .= "\t\t\t\t\t\t\t" . '<option value="' . $v . '" {{# if(d.' . $info['field'] . '.toString() == \'' . $v . '\'){ }}selected="selected"{{# } }}>' . $items['text'][$k] . '</option>' . "\n";
  1130. }
  1131. }
  1132. $data['options'] = $options;
  1133. $data['field'] = $info['field'];
  1134. $data['comment'] = $info['comment'];
  1135. $data['verify'] = $info['is_empty'] == 1 ? '' : 'required';
  1136. return $this->getReplacedTpl($name, $data);
  1137. }
  1138. protected function getSearchXmSelectHtml($info)
  1139. {
  1140. $name = 'html' . DS . 'search' . DS . 'xm_select';
  1141. $data['field'] = $info['field'];
  1142. if ($info['addition']['data_from_type'] === "data") {
  1143. $data['sourceType'] = "data";
  1144. $source = [];
  1145. foreach ($info['addition']['value'] as $k => $v) {
  1146. $source[] = ['name' => $info['addition']['text'][$k], 'value' => $info['addition']['value'][$k]];
  1147. }
  1148. $data['source'] = str_replace('"', '\'', json_encode($source, JSON_UNESCAPED_UNICODE));
  1149. $data['sourceTree'] = "false";
  1150. $data['paging'] = "false";
  1151. $data['textField'] = "";
  1152. $data['subTextField'] = "";
  1153. $data['iconField'] = "";
  1154. $data['valueField'] = "";
  1155. } else {
  1156. $data['sourceType'] = "route";
  1157. $tableName = $info['addition']['table_id'];
  1158. $midName = str_replace('/', '.', $this->getMidName($tableName));
  1159. $data['source'] = $this->compatibleApiRoute($midName) . '/index';
  1160. $createType = Table::where('table', '=', $tableName)->value('create_type');
  1161. $table_is_tree = ($createType == 1) ? 0 : 1;//取到数据表是否为无限极分类模型
  1162. if ($table_is_tree) {
  1163. if (isset($info['isRecycle']) && $info['isRecycle'] == 1) {
  1164. $data['sourceTree'] = "false";
  1165. $data['paging'] = "true";
  1166. } else {
  1167. $data['sourceTree'] = "true";
  1168. $data['paging'] = "false";
  1169. }
  1170. } else {
  1171. $data['sourceTree'] = "false";
  1172. $data['paging'] = "true";
  1173. }
  1174. $data['textField'] = $info['addition']['title_field'] ? "\n\t\t\t\t\t\t" . 'data-textField="' . $info['addition']['title_field'] . '"' : '';
  1175. $data['subTextField'] = $info['addition']['sub_title_field'] ? "\n\t\t\t\t\t\t" . 'data-subTextField="' . $info['addition']['sub_title_field'] . '"' : '';
  1176. $data['iconField'] = $info['addition']['icon_field'] ? "\n\t\t\t\t\t\t" . 'data-iconField="' . $info['addition']['icon_field'] . '"' : '';
  1177. $data['valueField'] = "id";
  1178. }
  1179. $data['comment'] = $info['comment'] ? "\n\t\t\t\t\t\t" . 'data-placeholder="请选择' . $info['comment'] . '"' : '';
  1180. $data['direction'] = $info['addition']['direction'] ? "\n\t\t\t\t\t\t" . 'data-direction="' . $info['addition']['direction'] . '"' : '';
  1181. return $this->getReplacedTpl($name, $data);
  1182. }
  1183. protected function getXmSelectHtml($info, $type)
  1184. {
  1185. $name = 'html' . DS . $type . DS . 'xm_select';
  1186. $data['field'] = $info['field'];
  1187. if ($info['addition']['data_from_type'] === "data") {
  1188. $data['sourceType'] = "data";
  1189. $source = [];
  1190. foreach ($info['addition']['value'] as $k => $v) {
  1191. $source[] = ['name' => $info['addition']['text'][$k], 'value' => $info['addition']['value'][$k]];
  1192. }
  1193. $data['source'] = str_replace('"', '\'', json_encode($source, JSON_UNESCAPED_UNICODE));
  1194. $data['sourceTree'] = "false";
  1195. $data['paging'] = "false";
  1196. $data['textField'] = "";
  1197. $data['subTextField'] = "";
  1198. $data['iconField'] = "";
  1199. $data['valueField'] = "";
  1200. } else {
  1201. $data['sourceType'] = "route";
  1202. $tableName = $info['addition']['table_id'];
  1203. $midName = str_replace('/', '.', $this->getMidName($tableName));
  1204. $data['source'] = $this->compatibleApiRoute($midName) . '/index';
  1205. if($tableName == $this->table->table){
  1206. $data['sourceTree'] = "true";
  1207. $data['paging'] = "false";
  1208. }else{
  1209. $createType = Table::where('table', '=', $tableName)->value('create_type');
  1210. $tableIsTree = ($createType == 1) ? 0 : 1;//取到数据表是否为无限极分类模型
  1211. if ($tableIsTree) {
  1212. $data['sourceTree'] = "true";
  1213. $data['paging'] = "false";
  1214. } else {
  1215. $data['sourceTree'] = "false";
  1216. $data['paging'] = "true";
  1217. }
  1218. }
  1219. $data['textField'] = $info['addition']['title_field'] ? "\n\t\t\t\t\t\t\t" . 'data-textField="' . $info['addition']['title_field'] . '"' : '';
  1220. $data['subTextField'] = $info['addition']['sub_title_field'] ? "\n\t\t\t\t\t\t\t" . 'data-subTextField="' . $info['addition']['sub_title_field'] . '"' : '';
  1221. $data['iconField'] = $info['addition']['icon_field'] ? "\n\t\t\t\t\t\t\t" . 'data-iconField="' . $info['addition']['icon_field'] . '"' : '';
  1222. $data['valueField'] = "\n\t\t\t\tdata-valueField=\"id\"";
  1223. }
  1224. $data['layVerify'] = ($info['is_empty'] == 1) ? "" : "\n\t\t\t\t\t\t\t" . 'data-layVerify="required"';
  1225. $data['comment'] = $info['comment'] ? "\n\t\t\t\t\t\t\t" . 'data-placeholder="请选择' . $info['comment'] . '"' : '';
  1226. $data['direction'] = $info['addition']['direction'] ? "\n\t\t\t\t\t\t\t" . 'data-direction="' . $info['addition']['direction'] . '"' : '';
  1227. $data['radio'] = ($info['addition']['single_multi_type'] === 'single') ? "true" : "false";
  1228. $data['max'] = $info['addition']['max'] ? 'data-max="' . $info['addition']['max'] . '"' : '';
  1229. return $this->getReplacedTpl($name, $data);
  1230. }
  1231. public function getSearchLinkageSelectHtml($info)
  1232. {
  1233. $name = 'html' . DS . 'search' . DS . 'linkage_select';
  1234. $data['field'] = $info['field'];
  1235. $data['comment'] = $info['comment'];
  1236. $midName = str_replace('/', '.', $this->getMidName($info['addition']['table_id']));
  1237. $data['url'] = 'admin/' . $midName . '/index';
  1238. $data['leftField'] = $info['addition']['left_field'];
  1239. $data['rightField'] = $info['addition']['right_field'];
  1240. $data['showField'] = $info['addition']['show_field'];
  1241. $data['searchField'] = $info['addition']['search_field'];
  1242. $data['searchCondition'] = $info['addition']['search_condition'];
  1243. $data['searchVal'] = $info['addition']['search_val'];
  1244. return $this->getReplacedTpl($name, $data);
  1245. }
  1246. public function getLinkageSelectHtml($info, $type)
  1247. {
  1248. $name = 'html' . DS . $type . DS . 'linkage_select';
  1249. $data['field'] = $info['field'];
  1250. $data['verify'] = ($info['is_empty'] == 1) ? "" : "required";
  1251. $data['comment'] = $info['comment'];
  1252. $midName = str_replace('/', '.', $this->getMidName($info['addition']['table_id']));
  1253. $data['url'] = 'admin/' . $midName . '/index';
  1254. $data['leftField'] = $info['addition']['left_field'];
  1255. $data['rightField'] = $info['addition']['right_field'];
  1256. $data['showField'] = $info['addition']['show_field'];
  1257. $data['searchField'] = $info['addition']['search_field'];
  1258. $data['searchCondition'] = $info['addition']['search_condition'];
  1259. $data['searchVal'] = $info['addition']['search_val'];
  1260. return $this->getReplacedTpl($name, $data);
  1261. }
  1262. public function getSearchLaydateHtml($info)
  1263. {
  1264. $name = 'html' . DS . 'search' . DS . 'laydate';
  1265. $data['field'] = $info['field'];
  1266. $data['comment'] = $info['comment'];
  1267. $data['laydateType'] = $info['addition']['date_type'];
  1268. if ($info['data_type'] == 'integer') {
  1269. if (in_array($info['addition']['date_type'], ['datetime', 'month', 'date'])) {
  1270. $data['searchType'] = 'BETWEEN_STRTOTIME';
  1271. } else {
  1272. $data['searchType'] = 'BETWEEN';
  1273. }
  1274. } else {
  1275. $data['searchType'] = 'BETWEEN';
  1276. }
  1277. return $this->getReplacedTpl($name, $data);
  1278. }
  1279. public function getLaydateHtml($info, $type)
  1280. {
  1281. $name = 'html' . DS . $type . DS . 'laydate';
  1282. $data['field'] = $info['field'];
  1283. $data['comment'] = $info['comment'];
  1284. $data['laydateType'] = $info['addition']['date_type'];
  1285. $data['verify'] = ($info['is_empty'] == 1) ? "" : "required";
  1286. return $this->getReplacedTpl($name, $data);
  1287. }
  1288. public function getSearchColorPickerHtml()
  1289. {
  1290. return "";
  1291. }
  1292. public function getColorPickerHtml($info, $type)
  1293. {
  1294. $name = 'html' . DS . $type . DS . 'color_picker';
  1295. $data['field'] = $info['field'];
  1296. $data['comment'] = $info['comment'];
  1297. $data['color'] = $info['addition']['color'];
  1298. $data['predefine'] = isset($info['addition']['predefine']) ? "true" : "false";
  1299. $data['colors'] = isset($info['addition']['colors']) ?
  1300. json_encode($info['addition']['colors'], JSON_UNESCAPED_UNICODE) : '';
  1301. $data['alpha'] = (isset($info['addition']['alpha']) && $info['addition']['alpha']) ? "true" : "false";
  1302. $data['format'] = $info['addition']['format'];
  1303. return $this->getReplacedTpl($name, $data);
  1304. }
  1305. public function getSearchUploadHtml()
  1306. {
  1307. return "";
  1308. }
  1309. public function getUploadHtml($info, $type)
  1310. {
  1311. $name = 'html' . DS . $type . DS . 'upload';
  1312. $data['field'] = $info['field'];
  1313. $data['comment'] = $info['comment'];
  1314. $data['accept'] = $info['addition']['accept'];
  1315. $data['width'] = ($info['addition']['accept'] == 'image' && $info['addition']['width']) ? "\n\t\t\t\t\t\t\t\tdata-width=\"" . $info['addition']['width'] . "\"" : "";
  1316. $data['height'] = ($info['addition']['accept'] == 'image' && $info['addition']['height']) ? "\n\t\t\t\t\t\t\t\tdata-height=\"" . $info['addition']['height'] . "\"" : "";
  1317. $data['multi'] = ($info['addition']['multi'] == 'single') ? "false" : "true";
  1318. $data['max'] = intval($info['addition']['max']) ? "\n\t\t\t\t\t\t\t\tdata-max=\"" . intval($info['addition']['max']) . "\"" : "";
  1319. $data['dir'] = $info['addition']['dir'] ? "\n\t\t\t\t\t\t\t\tdata-dir=\"" . $info['addition']['dir'] . "\"" : "";
  1320. $data['url'] = $info['addition']['url'] ? "\n\t\t\t\t\t\t\t\tdata-url=\"" . $info['addition']['url'] . "\"" : "";
  1321. $data['mime'] = $info['addition']['mime'] ? "\n\t\t\t\t\t\t\t\tdata-mime=\"" . $info['addition']['mime'] . "\"" : "";
  1322. $data['size'] = $info['addition']['size'] ? "\n\t\t\t\t\t\t\t\tdata-size=\"" . $info['addition']['size'] . "\"" : "";
  1323. $data['verify'] = ($info['is_empty'] == 1) ? "" : "\n\t\t\t\t\t\t\t\tdata-layVerify=\"required\"";
  1324. $data['type'] = ($info['addition']['upload_type'] !== "local") ? "\n\t\t\t\t\t\t\tdata-type=\"" . $info['addition']['upload_type'] . "\"" : "";
  1325. $data['viaServer'] = ($info['addition']['upload_type'] !== "local" && $info['addition']['via_server'] === "unVia") ? "\n\t\t\t\t\t\t\tdata-viaServer=\"unVia\"" : "";
  1326. if($info['addition']['multi'] == 'single'){
  1327. $data['uploadedInfo'] = "data-uploaded=\"{{# if(d.".$info['field']."File){ }}{{=d.".$info['field']."File.path}}{{# } }}\"
  1328. data-uploadedId=\"{{=d.".$info['field']."}}\"
  1329. data-uploadedFilename=\"{{# if(d.".$info['field']."File){ }}{{=d.".$info['field']."File.name}}{{# } }}\"";
  1330. }else{
  1331. $data['uploadedInfo'] = "data-uploaded=\"{{=d.".$info['field']."_path}}\"
  1332. data-uploadedId=\"{{=d.".$info['field']."}}\"
  1333. data-uploadedFilename=\"{{=d.".$info['field']."_filename}}\"";
  1334. }
  1335. return $this->getReplacedTpl($name, $data);
  1336. }
  1337. public function getSearchEditorHtml($info)
  1338. {
  1339. $name = 'html' . DS . 'search' . DS . 'editor' . DS . $info['addition']['type'];
  1340. $data['field'] = $info['field'];
  1341. $data['comment'] = $info['comment'];
  1342. return $this->getReplacedTpl($name, $data);
  1343. }
  1344. public function getEditorHtml($info, $type)
  1345. {
  1346. $name = 'html' . DS . $type . DS . 'editor' . DS . $info['addition']['type'];
  1347. $referer = request()->header('referer');
  1348. $refDomain = '';
  1349. if($referer){
  1350. $refPath = parse_url($referer);
  1351. $refDomain = $refPath['scheme'] . '://' . $refPath['host'];
  1352. }
  1353. $domain = request()->domain();
  1354. if($refDomain && $domain && $refDomain != $domain){
  1355. $data['iframeSrc'] = '/'.$info['addition']['type'].'.html';
  1356. }else{
  1357. $data['iframeSrc'] = '/admin/'.$info['addition']['type'].'.html';
  1358. }
  1359. if($info['addition']['upload_type'] != 'local'){
  1360. if($type == 'add'){
  1361. $data['iframeSrc'] .= '?upload_type='.$info['addition']['upload_type'];
  1362. }else{
  1363. $data['iframeSrc'] .= '?id='.$info['field'].'&upload_type='.$info['addition']['upload_type'];
  1364. }
  1365. }else{
  1366. if($type == 'edit'){
  1367. $data['iframeSrc'] .= '?id='.$info['field'];
  1368. }
  1369. }
  1370. $data['field'] = $info['field'];
  1371. $data['comment'] = $info['comment'];
  1372. return $this->getReplacedTpl($name, $data);
  1373. }
  1374. public function create()
  1375. {
  1376. $this->createMigration();
  1377. $this->createController();
  1378. $this->createModel();
  1379. $this->createJs();
  1380. $this->createHtml();
  1381. }
  1382. //生成数据迁移文件并执行数据库迁移命令,生成数据表
  1383. protected function createMigration()
  1384. {
  1385. if ($this->needCreateTable) {
  1386. $this->writeToFile($this->migrationParam['tplName'], $this->migrationParam['data'], $this->migrationParam['fileName']);
  1387. sleep(1);// 接下来的名称要用到刚生成的文件,直接执行,文件会找不到,休眠1秒后执行
  1388. // system('cd ' . app()->getRootPath() . '&& php think migrate:run > /dev/null', $return);
  1389. exec('php '.app()->getRootPath().'\think migrate:run');
  1390. }
  1391. }
  1392. //生成controller层
  1393. protected function createController()
  1394. {
  1395. if ($this->hasSoftDel) {
  1396. $this->controllerParam['data']['hasSoftDel'] = "\n\tprotected \$hasSoftDel=1;//是否拥有软删除功能";
  1397. } else {
  1398. $this->controllerParam['data']['hasSoftDel'] = "\n\tprotected \$hasSoftDel=0;//是否拥有软删除功能";
  1399. }
  1400. $this->controllerParam['data']['orderRule'] = "\n\tpublic \$orderRule=[";
  1401. if ($this->createAddition['order_field']) {
  1402. $this->controllerParam['data']['orderRule'] .= "'{$this->createAddition['order_field']}' => '{$this->createAddition['order_type']}',";
  1403. }
  1404. $this->controllerParam['data']['orderRule'] .= "'id'=>'{$this->createAddition['id_order_type']}'";
  1405. $this->controllerParam['data']['orderRule'] .= "];";
  1406. $this->writeToFile($this->controllerParam['tplName'], $this->controllerParam['data'], $this->controllerParam['fileName']);
  1407. }
  1408. protected function createModel()
  1409. {
  1410. //是否拥有软删除功能
  1411. $this->modelParam['data']['softDelPackage'] = "";
  1412. $this->modelParam['data']['useSoftDel'] = "";
  1413. $this->modelParam['data']['defaultSoftDelete'] = "";
  1414. $this->modelParam['data']['uploadDomainPackage'] = "";
  1415. $this->modelParam['data']['append'] = "";
  1416. $append = [];
  1417. foreach ($this->fields as $k => $v) {
  1418. if ($v['field'] == 'delete_time') {
  1419. $this->modelParam['data']['softDelPackage'] = "\nuse think\model\concern\SoftDelete;";
  1420. $this->modelParam['data']['useSoftDel'] = "\n\tuse SoftDelete;";
  1421. if ($v['data_type'] === 'datetime' || $v['data_type'] === 'timestamp') {
  1422. $this->modelParam['data']['defaultSoftDelete'] = "";
  1423. } else if ($v['data_type'] === 'integer') {
  1424. $this->modelParam['data']['defaultSoftDelete'] = "\n\tprotected \$defaultSoftDelete=0;";
  1425. }
  1426. }
  1427. if(($v['form_type'] == 'upload' && $v['addition']['multi'] == 'multi') || $v['form_type'] == 'editor'){
  1428. $this->modelParam['data']['uploadDomainPackage'] = "\nuse laytp\library\UploadDomain;";
  1429. }
  1430. if(($v['form_type'] == 'upload' && $v['addition']['multi'] == 'multi')){
  1431. $append[] = '\'' . $v['field'] . '_file\'';
  1432. }
  1433. }
  1434. if($append){
  1435. $this->modelParam['data']['append'] = implode(',', $append);
  1436. }
  1437. $this->writeToFile($this->modelParam['tplName'], $this->modelParam['data'], $this->modelParam['fileName']);
  1438. }
  1439. //生成js文件
  1440. protected function createJs()
  1441. {
  1442. $this->writeToFile($this->jsParam['tplName'], $this->jsParam['data'], $this->jsParam['fileName']);
  1443. if ($this->hasSoftDel) {
  1444. $this->writeToFile($this->recycleJsParam['tplName'], $this->recycleJsParam['data'], $this->recycleJsParam['fileName']);
  1445. }
  1446. }
  1447. //生成html模板文件
  1448. protected function createHtml()
  1449. {
  1450. $this->writeToFile($this->htmlIndexParam['tplName'], $this->htmlIndexParam['data'], $this->htmlIndexParam['fileName']);
  1451. $this->writeToFile($this->htmlAddParam['tplName'], $this->htmlAddParam['data'], $this->htmlAddParam['fileName']);
  1452. $this->writeToFile($this->htmlEditParam['tplName'], $this->htmlEditParam['data'], $this->htmlEditParam['fileName']);
  1453. //是否拥有软删除功能
  1454. foreach ($this->fields as $k => $v) {
  1455. if ($v->field == 'delete_time') {
  1456. $this->writeToFile($this->htmlRecycleParam['tplName'], $this->htmlRecycleParam['data'], $this->htmlRecycleParam['fileName']);
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. /**
  1462. * 写入到文件
  1463. * @param string $name 模板文件名
  1464. * @param array $data key=>value形式的替换数组
  1465. * @param string $pathname 生成的绝对文件名
  1466. * @return mixed
  1467. */
  1468. protected function writeToFile($name, $data, $pathname)
  1469. {
  1470. foreach ($data as $index => &$datum) {
  1471. $datum = is_array($datum) ? '' : $datum;
  1472. }
  1473. unset($datum);
  1474. $content = $this->getReplacedTpl($name, $data);
  1475. if (!is_dir(dirname($pathname))) {
  1476. mkdir(dirname($pathname), 0777, true);
  1477. }
  1478. return file_put_contents($pathname, $content);
  1479. }
  1480. /**
  1481. * 获取替换后的数据
  1482. * @param string $name 模板文件名
  1483. * @param array $data key=>value形式的替换数组
  1484. * @return string
  1485. */
  1486. protected function getReplacedTpl($name, $data = [])
  1487. {
  1488. foreach ($data as $index => &$datum) {
  1489. $datum = is_array($datum) ? '' : $datum;
  1490. }
  1491. unset($datum);
  1492. $search = $replace = [];
  1493. foreach ($data as $k => $v) {
  1494. $search[] = "{%{$k}%}";
  1495. $replace[] = $v;
  1496. }
  1497. $tplTrueName = $this->getTplTrueName($name);
  1498. $tpl = file_get_contents($tplTrueName);
  1499. $content = str_replace($search, $replace, $tpl);
  1500. return $content;
  1501. }
  1502. /**
  1503. * 获取模板文件真实路径
  1504. * @param string $name 举例: $name = 'controller' . DS . 'edit';
  1505. * @return string
  1506. */
  1507. protected function getTplTrueName($name)
  1508. {
  1509. $curdTemplateFile = app()->getRootPath() . DS . 'plugin' . DS . 'curd' . DS . 'library' . DS . 'curd_template' . DS . $name . '.lt';
  1510. $curdCategoryTemplateFile = app()->getRootPath() . DS . 'plugin' . DS . 'curd' . DS . 'library' . DS . 'curd_category_template' . DS . $name . '.lt';
  1511. if (file_exists($curdCategoryTemplateFile)) {
  1512. return $curdCategoryTemplateFile;
  1513. } else {
  1514. return $curdTemplateFile;
  1515. }
  1516. }
  1517. /*
  1518. * 下划线转驼峰
  1519. */
  1520. protected function underlineToCamel($str)
  1521. {
  1522. $str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
  1523. return strtoupper($matches[2]);
  1524. }, $str);
  1525. return $str;
  1526. }
  1527. }