FormHelper.php 9.9 KB

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