FormHelper.php 10 KB

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