FormHelper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace UCore\DcatAdmin;
  3. use UCore\DcatAdmin\Traits\Options;
  4. use UCore\DcatAdmin\Traits\UserID;
  5. use Dcat\Admin\Form;
  6. /**
  7. * 表单助手类 - 自动根据类属性生成表单字段
  8. *
  9. * 使用示例:
  10. * $formHelper = new FormHelper($form);
  11. * $formHelper->embedClassFields('user', '用户信息', [User::class]);
  12. *
  13. * 类属性注释示例:
  14. *
  15. * @label 用户名
  16. */
  17. class FormHelper
  18. {
  19. use Options, UserID;
  20. /**
  21. * @var Form
  22. */
  23. public $form;
  24. /**
  25. * @var AdminController
  26. */
  27. public $controller;
  28. public function __construct($form, $controller)
  29. {
  30. $this->form = $form;
  31. $this->controller = $controller;
  32. }
  33. public function enableBack()
  34. {
  35. $this->form->tools(function (Form\Tools $tools) {
  36. $tools->prepend();
  37. });
  38. }
  39. /**
  40. * @param $field
  41. * @return Form\Field|Form\Field[]|\Illuminate\Support\Collection|null
  42. */
  43. public function field($field)
  44. {
  45. return $this->form->field($field);
  46. }
  47. /**
  48. * 嵌套表单
  49. * json,cats model
  50. *
  51. * @param $field
  52. * @param $lable
  53. * @return Form\Field\Embeds
  54. * @throws \ReflectionException
  55. */
  56. public function embedsCats($field, $lable)
  57. {
  58. $modeClass = get_class($this->form->repository()->model());
  59. $cates = $this->form->repository()->model()->getCasts();
  60. // dump($cates);
  61. $class = $cates[$field] ?? "";
  62. $reflectionClass = new \ReflectionClass($class);
  63. return $this->form->embeds($field, $lable, function (\Dcat\Admin\Form\EmbeddedForm $form) use ($class, $reflectionClass) {
  64. // 从注释中提取字段标签
  65. $getFieldLabel = function ($name, $comment) {
  66. if ($comment) {
  67. // 优先提取@label标签
  68. if (preg_match('/@label\s+([^\r\n]+)/', $comment, $matches)) {
  69. return trim($matches[1]);
  70. }
  71. // 处理多行注释,提取第一行描述
  72. $lines = explode("\n", $comment);
  73. foreach ($lines as $line) {
  74. // 匹配描述行 (以 * 开头但不包含@标签的行)
  75. if (preg_match('/^\s*\*\s+([^@*]+)/', $line, $matches)) {
  76. $desc = trim($matches[1]);
  77. if (!empty($desc) && !preg_match('/@\w+/', $desc)) {
  78. return $desc;
  79. }
  80. }
  81. }
  82. // 最后提取@var类型后的描述
  83. if (preg_match('/@var\s+\S+\s+([^\r\n]+)/', $comment, $matches)) {
  84. $desc = trim($matches[1]);
  85. if (!empty($desc)) {
  86. return $desc;
  87. }
  88. }
  89. }
  90. // 默认使用字段名转换
  91. return ucwords(str_replace([ '_', '-' ], ' ', $name));
  92. };
  93. foreach ($reflectionClass->getProperties() as $property) {
  94. // 设置不同类型的表单
  95. $type = $property->getType() ? $property->getType()->getName() : null;
  96. $name = $property->getName();
  97. $comment = $property->getDocComment();
  98. // 根据类型设置不同表单字段
  99. switch ($type) {
  100. case 'string':
  101. $form->text($name, $getFieldLabel($name, $comment));
  102. break;
  103. case 'int':
  104. $form->number($name, $getFieldLabel($name, $comment));
  105. break;
  106. case 'float':
  107. $form->decimal($name, $getFieldLabel($name, $comment));
  108. break;
  109. case 'bool':
  110. $form->switch($name, $getFieldLabel($name, $comment));
  111. break;
  112. default:
  113. $form->text($name, $getFieldLabel($name, $comment));
  114. }
  115. }
  116. // 返回表单对象
  117. return $form;
  118. });
  119. }
  120. /**
  121. * 开关
  122. *
  123. * @param $field
  124. * @param $default
  125. * @return Form\Field\SwitchField
  126. */
  127. public function switch($field, $label = null)
  128. {
  129. return $this->form->switch($field, $label);
  130. }
  131. /**
  132. * number
  133. *
  134. * @param $field
  135. * @return Form\Field\Number
  136. */
  137. public function number($field, $label = null)
  138. {
  139. return $this->form->number($field, $label);
  140. }
  141. /**
  142. * 字符串
  143. *
  144. * @param $field
  145. * @return Form\Field\Text
  146. */
  147. public function text($field, $label = null)
  148. {
  149. return $this->form->text($field, $label);
  150. }
  151. public function hidden($field, $label = null)
  152. {
  153. return $this->form->hidden($field, $label);
  154. }
  155. /**
  156. * 单select
  157. *
  158. * @param $field
  159. * @return Form\Field\Select
  160. */
  161. public function select($field, $label = null)
  162. {
  163. return $this->form->select($field, $label);
  164. }
  165. /**
  166. * 单select 带枚举的
  167. *
  168. * @param string $field 字段
  169. * @param array $enmu 枚举
  170. * @return Form\Field\Select|mixed
  171. */
  172. public function selectOption($field, $enmu, $label = null)
  173. {
  174. return $this->form->select($field, $label)->options($this->useing($field, $enmu));
  175. }
  176. public function selectOptionCast($field, $label = null)
  177. {
  178. $modeClass = get_class($this->form->repository()->model());
  179. $cates = $this->form->repository()->model()->getCasts();
  180. // dump($cates);
  181. $enmu = $cates[$field] ?? "";
  182. if ($enmu === '') {
  183. throw new \Exception("$field is not a model $modeClass casts");
  184. }
  185. $values = $enmu::getValueDescription();
  186. return $this->form->select($field, $label)->options($values);
  187. }
  188. /**
  189. * 多 select
  190. *
  191. * @param $field
  192. * @return Form\Field\MultipleSelect
  193. */
  194. public function multipleSelect($field, $label = null)
  195. {
  196. return $this->form->multipleSelect($field, $label);
  197. }
  198. /**
  199. * 多 select表,选择器
  200. *
  201. * @param $field
  202. * @return Form\Field\MultipleSelectTable
  203. */
  204. public function multipleSelectTable($field, $label = null)
  205. {
  206. return $this->form->multipleSelectTable($field, $label);
  207. }
  208. /**
  209. * 多 select表,选择器
  210. *
  211. * @param $field
  212. * @return Form\Field\SelectTable
  213. */
  214. public function selectTable($field, $label = null)
  215. {
  216. return $this->form->selectTable($field, $label);
  217. }
  218. /**
  219. * 多选
  220. *
  221. * @param $field
  222. * @return Form\Field\Checkbox
  223. */
  224. public function checkbox($field, $label = null)
  225. {
  226. return $this->form->checkbox($field, $label);
  227. }
  228. /**
  229. * 单选
  230. *
  231. * @param $field
  232. * @param array $enmu
  233. * @return Form\Field\Radio
  234. */
  235. public function radio($field, array $enmu, $label = null)
  236. {
  237. $lable = $lable ?? $field;
  238. return $this->form->radio($field, $label)->options($enmu);
  239. }
  240. /**
  241. * 列表
  242. *
  243. * @param $field
  244. *
  245. * @return Form\Field\ListField
  246. */
  247. public function list($field, $label = null)
  248. {
  249. return $this->form->list($field, $label);
  250. }
  251. /**
  252. * 图片表单
  253. *
  254. * @param $field
  255. * @return $this
  256. */
  257. public function image($field, $label = null)
  258. {
  259. $this->form->image($field, $label);
  260. return $this;
  261. }
  262. /**
  263. * @param $field
  264. * @return $this
  265. */
  266. public function editor($field, $label = null)
  267. {
  268. $this->form->editor($field, $label);
  269. return $this;
  270. }
  271. public function multipleImage($field, $label = null)
  272. {
  273. $this->form->multipleImage($field, $label);
  274. return $this;
  275. }
  276. /**
  277. * 分割线
  278. *
  279. * @param $title
  280. * @return $this
  281. */
  282. public function divider($title = null)
  283. {
  284. $this->form->divider($this->controller->_translation('divider-' . $title));
  285. return $this;
  286. }
  287. /**
  288. * 展示字段
  289. *
  290. * @param $field
  291. * @return Form\Field\Display
  292. */
  293. public function display($field, $label = null)
  294. {
  295. return $this->form->display($field, $label);
  296. }
  297. /**
  298. * @param $field
  299. * @return Form\Field\Display
  300. */
  301. public function displayOptions($field, $label = null)
  302. {
  303. $c = $this->controller;
  304. return $this->form->display($field, $label)->with(function ($value) use ($field, $c) {
  305. return $c->_option($field . '-' . $value);
  306. });
  307. }
  308. /**
  309. * 文本
  310. *
  311. * @param $field
  312. * @return $this
  313. *
  314. */
  315. public function textarea($field, $label = null)
  316. {
  317. return $this->form->textarea($field, $label);
  318. }
  319. /**
  320. * 增加表格
  321. *
  322. * @param $field
  323. * @param $call
  324. * @return Form\Field\Table
  325. */
  326. public function table($field, $call, $label = null)
  327. {
  328. return $this->form->table($field, $label, $call);
  329. }
  330. /**
  331. * tags
  332. *
  333. * @param $field
  334. * @return Form\Field\Tags
  335. */
  336. public function tags($field, $label = null)
  337. {
  338. return $this->form->tags($field, $label);
  339. }
  340. /**
  341. * 关闭所有的 多余
  342. *
  343. * @return void
  344. */
  345. public function disableAll()
  346. {
  347. $this->form->disableDeleteButton();
  348. $this->form->disableCreatingCheck();
  349. $this->form->disableViewButton();
  350. // $this->form->disableListButton();
  351. $this->form->disableViewCheck();
  352. $this->form->disableEditingCheck();
  353. }
  354. public function disableButton4()
  355. {
  356. $this->form->disableDeleteButton();
  357. $this->form->disableViewButton();
  358. // $this->form->disableListButton();
  359. $this->form->disableViewCheck();
  360. $this->form->disableEditingCheck();
  361. }
  362. /**
  363. * select模型选项
  364. * @param $field
  365. * @param $label
  366. * @param $modelClass
  367. * @param $name
  368. * @param $id
  369. * @return Form\Field\Select|mixed
  370. */
  371. public function selectModelOption($field, $label, $modelClass,$name = 'name', $id = 'id')
  372. {
  373. $options = $modelClass::all()->pluck($name, $id);
  374. return $this->form->select($field, $label)->options($options);
  375. }
  376. }