Form.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. <?php
  2. namespace fast;
  3. use ArrayAccess;
  4. /**
  5. * 表单元素生成
  6. * @class Form
  7. * @package fast
  8. * @method string token() static 生成Token
  9. * @method string label(string $name, string $value = null, array $options = []) static label标签
  10. * @method string input($type, $name, string $value = null, array $options = []) static 按类型生成文本框
  11. * @method string text(string $name, string $value = null, array $options = []) static 普通文本框
  12. * @method string password(string $name, array $options = []) static 密码文本框
  13. * @method string hidden(string $name, string $value = null, array $options = []) static 隐藏文本框
  14. * @method string email(string $name, string $value = null, array $options = []) static Email文本框
  15. * @method string url(string $name, string $value = null, array $options = []) static URL文本框
  16. * @method string file(string $name, array $options = []) static 文件上传组件
  17. * @method string textarea(string $name, string $value = null, array $options = []) static 多行文本框
  18. * @method string editor(string $name, string $value = null, array $options = []) static 富文本编辑器
  19. * @method string select(string $name, array $list = [], string $selected = null, array $options = []) static 下拉列表组件
  20. * @method string selects(string $name, array $list = [], string $selected = null, array $options = []) static 下拉列表组件(多选)
  21. * @method string selectpicker(string $name, array $list = [], string $selected = null, array $options = []) static 下拉列表组件(友好)
  22. * @method string selectpickers(string $name, array $list = [], string $selected = null, array $options = []) static 下拉列表组件(友好)(多选)
  23. * @method string selectpage(string $name, string $value, string $url, string $field = null, string $primaryKey = null, array $options = []) static 动态下拉列表组件
  24. * @method string selectpages(string $name, string $value, string $url, string $field = null, string $primaryKey = null, array $options = []) static 动态下拉列表组件(多选)
  25. * @method string citypicker(string $name, string $value, array $options = []) static 城市选择组件
  26. * @method string switcher(string $name, string $value, array $options = []) static 切换组件
  27. * @method string datepicker(string $name, string $value, array $options = []) static 日期选择组件
  28. * @method string timepicker(string $name, string $value, array $options = []) static 时间选择组件
  29. * @method string datetimepicker(string $name, string $value, array $options = []) static 日期时间选择组件
  30. * @method string daterange(string $name, string $value, array $options = []) static 日期区间组件
  31. * @method string timerange(string $name, string $value, array $options = []) static 时间区间组件
  32. * @method string datetimerange(string $name, string $value, array $options = []) static 日期时间区间组件
  33. * @method string fieldlist(string $name, string $value, string $title = null, string $template = null, array $options = []) static 字段列表组件
  34. * @method string cxselect(string $url, array $names = [], array $values = [], array $options = []) static 联动组件
  35. * @method string selectRange(string $name, string $begin, string $end, string $selected = null, array $options = []) static 选择数字区间
  36. * @method string selectYear(string $name, string $begin, string $end, string $selected = null, array $options = []) static 选择年
  37. * @method string selectMonth(string $name, string $selected = null, array $options = [], string $format = '%m') static 选择月
  38. * @method string checkbox(string $name, string $value = '1', string $checked = null, array $options = []) static 单个复选框
  39. * @method string checkboxs(string $name, array $list = [], string $checked = null, array $options = []) static 一组复选框
  40. * @method string radio(string $name, string $value = null, string $checked = null, array $options = [])) static 单个单选框
  41. * @method string radios(string $name, array $list = [], string $checked = null, array $options = [])) static 一组单选框
  42. * @method string image(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) static 上传图片组件
  43. * @method string images(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) static 上传图片组件(多图))
  44. * @method string upload(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) static 上传文件组件
  45. * @method string uploads(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) static 上传文件组件(多文件))
  46. * @method string button(string $value = null, array $options = []) static 表单button
  47. */
  48. class Form
  49. {
  50. public function __construct()
  51. {
  52. }
  53. /**
  54. * @param $name
  55. * @param $arguments
  56. * @return FormBuilder
  57. */
  58. public static function __callStatic($name, $arguments)
  59. {
  60. return call_user_func_array([FormBuilder::instance(), $name], $arguments);
  61. }
  62. }
  63. /**
  64. *
  65. * 表单元素生成
  66. * @from https://github.com/illuminate/html
  67. * @package fast
  68. */
  69. class FormBuilder
  70. {
  71. /**
  72. * Token
  73. *
  74. * @var string
  75. */
  76. protected $csrfToken = array('name' => '__token__');
  77. /**
  78. * 已创建的标签名称
  79. *
  80. * @var array
  81. */
  82. protected $labels = [];
  83. /**
  84. * 跳过的填充value值的类型
  85. *
  86. * @var array
  87. */
  88. protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio');
  89. /**
  90. * 转义HTML
  91. * @var boolean
  92. */
  93. protected $escapeHtml = true;
  94. protected static $instance;
  95. public function __construct()
  96. {
  97. }
  98. /**
  99. * 获取单例
  100. * @param array $options
  101. * @return static
  102. */
  103. public static function instance($options = [])
  104. {
  105. if (is_null(self::$instance)) {
  106. self::$instance = new static($options);
  107. }
  108. return self::$instance;
  109. }
  110. /**
  111. * 设置是否转义
  112. * @param boolean $escape
  113. */
  114. public function setEscapeHtml($escape)
  115. {
  116. $this->escapeHtml = $escape;
  117. }
  118. /**
  119. * 获取转义编码后的值
  120. * @param string $value
  121. * @return string
  122. */
  123. public function escape($value)
  124. {
  125. if (!$this->escapeHtml) {
  126. return $value;
  127. }
  128. if (is_array($value)) {
  129. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  130. }
  131. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
  132. }
  133. /**
  134. * 生成Token
  135. *
  136. * @param string $name
  137. * @param string $type
  138. * @return string
  139. */
  140. public function token($name = '__token__', $type = 'md5')
  141. {
  142. if (function_exists('token')) {
  143. return token($name, $type);
  144. }
  145. return '';
  146. }
  147. /**
  148. * 生成Label标签
  149. *
  150. * @param string $name
  151. * @param string $value
  152. * @param array $options
  153. * @return string
  154. */
  155. public function label($name, $value = null, $options = [])
  156. {
  157. $this->labels[] = $name;
  158. $options = $this->attributes($options);
  159. $value = $this->escape($this->formatLabel($name, $value));
  160. return '<label for="' . $name . '"' . $options . '>' . $value . '</label>';
  161. }
  162. /**
  163. * Format the label value.
  164. *
  165. * @param string $name
  166. * @param string|null $value
  167. * @return string
  168. */
  169. protected function formatLabel($name, $value)
  170. {
  171. return $value ?: ucwords(str_replace('_', ' ', $name));
  172. }
  173. /**
  174. * 生成文本框(按类型)
  175. *
  176. * @param string $type
  177. * @param string $name
  178. * @param string $value
  179. * @param array $options
  180. * @return string
  181. */
  182. public function input($type, $name, $value = null, $options = [])
  183. {
  184. if (!isset($options['name']))
  185. $options['name'] = $name;
  186. $id = $this->getIdAttribute($name, $options);
  187. if (!in_array($type, $this->skipValueTypes)) {
  188. $value = $this->getValueAttribute($name, $value);
  189. $options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
  190. }
  191. $merge = compact('type', 'value', 'id');
  192. $options = array_merge($options, $merge);
  193. return '<input' . $this->attributes($options) . '>';
  194. }
  195. /**
  196. * 生成普通文本框
  197. *
  198. * @param string $name
  199. * @param string $value
  200. * @param array $options
  201. * @return string
  202. */
  203. public function text($name, $value = null, $options = [])
  204. {
  205. return $this->input('text', $name, $value, $options);
  206. }
  207. /**
  208. * 生成密码文本框
  209. *
  210. * @param string $name
  211. * @param array $options
  212. * @return string
  213. */
  214. public function password($name, $options = [])
  215. {
  216. return $this->input('password', $name, '', $options);
  217. }
  218. /**
  219. * 生成隐藏文本框
  220. *
  221. * @param string $name
  222. * @param string $value
  223. * @param array $options
  224. * @return string
  225. */
  226. public function hidden($name, $value = null, $options = [])
  227. {
  228. return $this->input('hidden', $name, $value, $options);
  229. }
  230. /**
  231. * 生成Email文本框
  232. *
  233. * @param string $name
  234. * @param string $value
  235. * @param array $options
  236. * @return string
  237. */
  238. public function email($name, $value = null, $options = [])
  239. {
  240. return $this->input('email', $name, $value, $options);
  241. }
  242. /**
  243. * 生成URL文本框
  244. *
  245. * @param string $name
  246. * @param string $value
  247. * @param array $options
  248. * @return string
  249. */
  250. public function url($name, $value = null, $options = [])
  251. {
  252. return $this->input('url', $name, $value, $options);
  253. }
  254. /**
  255. * 生成上传文件组件
  256. *
  257. * @param string $name
  258. * @param array $options
  259. * @return string
  260. */
  261. public function file($name, $options = [])
  262. {
  263. return $this->input('file', $name, null, $options);
  264. }
  265. /**
  266. * 生成多行文本框
  267. *
  268. * @param string $name
  269. * @param string $value
  270. * @param array $options
  271. * @return string
  272. */
  273. public function textarea($name, $value = null, $options = [])
  274. {
  275. if (!isset($options['name']))
  276. $options['name'] = $name;
  277. $options = $this->setTextAreaSize($options);
  278. $options['id'] = $this->getIdAttribute($name, $options);
  279. $value = (string)$this->getValueAttribute($name, $value);
  280. unset($options['size']);
  281. $options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
  282. $options = $this->attributes($options);
  283. return '<textarea' . $options . '>' . $this->escape($value) . '</textarea>';
  284. }
  285. /**
  286. * 生成富文本编辑器
  287. *
  288. * @param string $name
  289. * @param string $value
  290. * @param array $options
  291. * @return string
  292. */
  293. public function editor($name, $value = null, $options = [])
  294. {
  295. $options['class'] = isset($options['class']) ? $options['class'] . ' editor' : 'editor';
  296. return $this->textarea($name, $value, $options);
  297. }
  298. /**
  299. * 设置默认的文本框行列数
  300. *
  301. * @param array $options
  302. * @return array
  303. */
  304. protected function setTextAreaSize($options)
  305. {
  306. if (isset($options['size'])) {
  307. return $this->setQuickTextAreaSize($options);
  308. }
  309. $cols = array_get($options, 'cols', 50);
  310. $rows = array_get($options, 'rows', 5);
  311. return array_merge($options, compact('cols', 'rows'));
  312. }
  313. /**
  314. * 根据size设置行数和列数
  315. *
  316. * @param array $options
  317. * @return array
  318. */
  319. protected function setQuickTextAreaSize($options)
  320. {
  321. $segments = explode('x', $options['size']);
  322. return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1]));
  323. }
  324. /**
  325. * 生成滑块
  326. *
  327. * @param string $name
  328. * @param string $min
  329. * @param string $max
  330. * @param string $step
  331. * @param string $value
  332. * @param array $options
  333. * @return string
  334. */
  335. public function slider($name, $min, $max, $step, $value = null, $options = [])
  336. {
  337. $options = array_merge($options, ['data-slider-min' => $min, 'data-slider-max' => $max, 'data-slider-step' => $step,'data-slider-value' => $value ? $value : '']);
  338. $options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' slider form-control') : 'slider form-control';
  339. return $this->input('text', $name, $value, $options);
  340. }
  341. /**
  342. * 生成下拉列表框
  343. *
  344. * @param string $name
  345. * @param array $list
  346. * @param mixed $selected
  347. * @param array $options
  348. * @return string
  349. */
  350. public function select($name, $list = [], $selected = null, $options = [])
  351. {
  352. $selected = $this->getValueAttribute($name, $selected);
  353. $options['id'] = $this->getIdAttribute($name, $options);
  354. if (!isset($options['name']))
  355. $options['name'] = $name;
  356. $html = [];
  357. foreach ($list as $value => $display) {
  358. $html[] = $this->getSelectOption($display, $value, $selected);
  359. }
  360. $options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
  361. $options = $this->attributes($options);
  362. $list = implode('', $html);
  363. return "<select{$options}>{$list}</select>";
  364. }
  365. /**
  366. * 下拉列表(多选)
  367. *
  368. * @param string $name
  369. * @param array $list
  370. * @param mixed $selected
  371. * @param array $options
  372. * @return string
  373. */
  374. public function selects($name, $list = [], $selected = null, $options = [])
  375. {
  376. $options[] = 'multiple';
  377. return $this->select($name, $list, $selected, $options);
  378. }
  379. /**
  380. * 下拉列表(友好)
  381. *
  382. * @param string $name
  383. * @param array $list
  384. * @param mixed $selected
  385. * @param array $options
  386. * @return string
  387. */
  388. public function selectpicker($name, $list = [], $selected = null, $options = [])
  389. {
  390. $options['class'] = isset($options['class']) ? $options['class'] . ' selectpicker' : 'selectpicker';
  391. return $this->select($name, $list, $selected, $options);
  392. }
  393. /**
  394. * 下拉列表(友好)(多选)
  395. *
  396. * @param string $name
  397. * @param array $list
  398. * @param mixed $selected
  399. * @param array $options
  400. * @return string
  401. */
  402. public function selectpickers($name, $list = [], $selected = null, $options = [])
  403. {
  404. $options[] = 'multiple';
  405. return $this->selectpicker($name, $list, $selected, $options);
  406. }
  407. /**
  408. * 生成动态下拉列表
  409. *
  410. * @param string $name 名称
  411. * @param mixed $value
  412. * @param string $url 数据源地址
  413. * @param string $field 显示的字段名称,默认为name
  414. * @param string $primaryKey 主键,数据库中保存的值,默认为id
  415. * @param array $options
  416. * @return string
  417. */
  418. public function selectpage($name, $value, $url, $field = null, $primaryKey = null, $options = [])
  419. {
  420. $options = array_merge($options, ['data-source' => $url, 'data-field' => $field ? $field : 'name', 'data-primary-key' => $primaryKey ? $primaryKey : 'id']);
  421. $options['class'] = isset($options['class']) ? $options['class'] . ' selectpage' : 'selectpage';
  422. return $this->text($name, $value, $options);
  423. }
  424. /**
  425. * 生成动态下拉列表(复选)
  426. *
  427. * @param string $name 名称
  428. * @param mixed $value
  429. * @param string $url 数据源地址
  430. * @param string $field 显示的字段名称,默认为name
  431. * @param string $primaryKey 主键,数据库中保存的值,默认为id
  432. * @param array $options
  433. * @return string
  434. */
  435. public function selectpages($name, $value, $url, $field = null, $primaryKey = null, $options = [])
  436. {
  437. $options['data-multiple'] = "true";
  438. return $this->selectpage($name, $value, $url, $field, $primaryKey, $options);
  439. }
  440. /**
  441. * 生成城市选择框
  442. *
  443. * @param string $name
  444. * @param mixed $value
  445. * @param array $options
  446. * @return string
  447. */
  448. public function citypicker($name, $value, $options = [])
  449. {
  450. $options['data-toggle'] = 'city-picker';
  451. return "<div class='control-relative'>" . $this->text($name, $value, $options) . "</div>";
  452. }
  453. /**
  454. * 生成switch组件
  455. *
  456. * @param string $name
  457. * @param mixed $value
  458. * @param array $options
  459. * @return string
  460. */
  461. public function switcher($name, $value, $options = [])
  462. {
  463. $domname = str_replace(['[', ']', '.'], '', $name);
  464. $btn = $this->hidden($name, $value, ['id' => "c-{$domname}"]);
  465. $yes = 1;
  466. $no = 0;
  467. if (isset($options['yes']) && isset($options['no'])) {
  468. $yes = $options['yes'];
  469. $no = $options['no'];
  470. }
  471. $selected = $no == $value ? "fa-flip-horizontal text-gray" : "";
  472. $disabled = (isset($options['disabled']) && $options['disabled']) || in_array('disabled', $options) ? "disabled" : '';
  473. $color = isset($options['color']) ? $options['color'] : 'success';
  474. unset($options['yes'], $options['no'], $options['color'], $options['disabled']);
  475. $attr = $this->attributes($options);
  476. $html = <<<EOD
  477. {$btn}
  478. <a href="javascript:;" data-toggle="switcher" class="btn-switcher {$disabled}" data-input-id="c-{$domname}" data-yes="{$yes}" data-no="{$no}" {$attr}><i class="fa fa-toggle-on text-{$color} {$selected} fa-2x"></i></a>
  479. EOD;
  480. return $html;
  481. }
  482. /**
  483. * 日期选择器
  484. * @param string $name
  485. * @param mixed $value
  486. * @param array $options
  487. * @return string
  488. */
  489. public function datepicker($name, $value, $options = [])
  490. {
  491. $defaults = [
  492. 'data-date-format' => "YYYY-MM-DD",
  493. ];
  494. $options = array_merge($defaults, $options);
  495. $value = is_numeric($value) ? date("Y-m-d", $value) : $value;
  496. return $this->datetimepicker($name, $value, $options);
  497. }
  498. /**
  499. * 时间选择器
  500. *
  501. * @param string $name
  502. * @param mixed $value
  503. * @param array $options
  504. * @return string
  505. */
  506. public function timepicker($name, $value, $options = [])
  507. {
  508. $defaults = [
  509. 'data-date-format' => "HH:mm:ss",
  510. ];
  511. $options = array_merge($defaults, $options);
  512. $value = is_numeric($value) ? date("H:i:s", $value) : $value;
  513. return $this->datetimepicker($name, $value, $options);
  514. }
  515. /**
  516. * 日期时间选择器
  517. *
  518. * @param string $name
  519. * @param mixed $value
  520. * @param array $options
  521. * @return string
  522. */
  523. public function datetimepicker($name, $value, $options = [])
  524. {
  525. $defaults = [
  526. 'data-date-format' => "YYYY-MM-DD HH:mm:ss",
  527. 'data-use-current' => "true",
  528. ];
  529. $value = is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  530. $options = array_merge($defaults, $options);
  531. $options['class'] = isset($options['class']) ? $options['class'] . ' datetimepicker' : 'datetimepicker';
  532. return $this->text($name, $value, $options);
  533. }
  534. /**
  535. * 日期区间
  536. *
  537. * @param string $name
  538. * @param string $value
  539. * @param array $options
  540. * @return string
  541. */
  542. public function daterange($name, $value, $options = [])
  543. {
  544. $defaults = [
  545. 'data-locale' => [
  546. 'format' => 'YYYY-MM-DD'
  547. ]
  548. ];
  549. $options = array_merge($defaults, $options);
  550. return $this->datetimerange($name, $value, $options);
  551. }
  552. /**
  553. * 时间区间
  554. *
  555. * @param string $name
  556. * @param string $value
  557. * @param array $options
  558. * @return string
  559. */
  560. public function timerange($name, $value, $options = [])
  561. {
  562. $defaults = [
  563. 'data-locale' => [
  564. 'format' => 'HH:mm:ss'
  565. ],
  566. 'data-ranges' => [],
  567. 'data-show-custom-range-label' => "false",
  568. 'data-time-picker' => "true",
  569. ];
  570. $options = array_merge($defaults, $options);
  571. return $this->datetimerange($name, $value, $options);
  572. }
  573. /**
  574. * 日期时间区间
  575. *
  576. * @param string $name
  577. * @param string $value
  578. * @param array $options
  579. * @return string
  580. */
  581. public function datetimerange($name, $value, $options = [])
  582. {
  583. $defaults = [
  584. 'data-locale' => [
  585. 'format' => 'YYYY-MM-DD HH:mm:ss'
  586. ]
  587. ];
  588. $options = array_merge($defaults, $options);
  589. $options['class'] = isset($options['class']) ? $options['class'] . ' datetimerange' : 'datetimerange';
  590. return $this->text($name, $value, $options);
  591. }
  592. /**
  593. * 生成字段列表组件
  594. *
  595. * @param string $name
  596. * @param mixed $value
  597. * @param array $title
  598. * @param string $template
  599. * @param array $options
  600. * @return string
  601. */
  602. public function fieldlist($name, $value, $title = null, $template = null, $options = [])
  603. {
  604. $append = __('Append');
  605. $template = $template ? 'data-template="' . $template . '"' : '';
  606. $attributes = $this->attributes($options);
  607. if (is_null($title)) {
  608. $title = [__('Key'), __('Value')];
  609. }
  610. $ins = implode("\n", array_map(function ($value) {
  611. return "<ins>{$value}</ins>";
  612. }, $title));
  613. $value = is_array($value) ? json_encode($value) : $value;
  614. $html = <<<EOD
  615. <dl class="fieldlist" data-name="{$name}" {$template} {$attributes}>
  616. <dd>
  617. {$ins}
  618. </dd>
  619. <dd><a href="javascript:;" class="btn btn-sm btn-success btn-append"><i class="fa fa-plus"></i> {$append}</a></dd>
  620. <textarea name="{$name}" class="form-control hide" cols="30" rows="5">{$value}</textarea>
  621. </dl>
  622. EOD;
  623. return $html;
  624. }
  625. /**
  626. * 生成联动下拉列表
  627. *
  628. * @param string $url 联动获取数据源的URL地址
  629. * @param array $names 联动字段名称
  630. * @param array $values 联动字段默认选中的值
  631. * @param array $options 扩展属性
  632. * @return string
  633. */
  634. public function cxselect($url, $names = [], $values = [], $options = [])
  635. {
  636. $classes = [];
  637. $cxselect = [];
  638. $attributes = $this->attributes($options);
  639. foreach ($names as $index => $value) {
  640. $level = $index + 1;
  641. $class = "cxselect-{$level}";
  642. $classes[] = $class;
  643. $selectValue = isset($values[$value]) ? $values[$value] : (isset($values[$index]) ? $values[$index] : '');
  644. $cxselect[] = <<<EOD
  645. <select class="{$class} form-control" name="{$value}" data-value="{$selectValue}" data-url="{$url}?level={$level}&name={$value}" {$attributes}></select>
  646. EOD;
  647. }
  648. $cxselect = implode("\n", $cxselect);
  649. $selects = implode(',', $classes);
  650. $html = <<<EOD
  651. <div class="form-inline" data-toggle="cxselect" data-selects="{$selects}">
  652. {$cxselect}
  653. </div>
  654. EOD;
  655. return $html;
  656. }
  657. /**
  658. * 创建一个下拉列表选择区间组件
  659. *
  660. * @param string $name
  661. * @param string $begin
  662. * @param string $end
  663. * @param string $selected
  664. * @param array $options
  665. * @return string
  666. */
  667. public function selectRange($name, $begin, $end, $selected = null, $options = [])
  668. {
  669. $range = array_combine($range = range($begin, $end), $range);
  670. return $this->select($name, $range, $selected, $options);
  671. }
  672. /**
  673. * 生成选择年组件
  674. *
  675. * @param string $name
  676. * @param string $begin
  677. * @param string $end
  678. * @param string $selected
  679. * @param array $options
  680. * @return string
  681. */
  682. public function selectYear($name, $begin, $end, $selected, $options)
  683. {
  684. return call_user_func_array(array($this, 'selectRange'), func_get_args());
  685. }
  686. /**
  687. * 生成选择月组件
  688. *
  689. * @param string $name
  690. * @param string $selected
  691. * @param array $options
  692. * @param string $format
  693. * @return string
  694. */
  695. public function selectMonth($name, $selected = null, $options = [], $format = '%m')
  696. {
  697. $months = [];
  698. foreach (range(1, 12) as $month) {
  699. $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
  700. }
  701. return $this->select($name, $months, $selected, $options);
  702. }
  703. /**
  704. * 根据传递的值生成option
  705. *
  706. * @param string $display
  707. * @param string $value
  708. * @param string $selected
  709. * @return string
  710. */
  711. public function getSelectOption($display, $value, $selected)
  712. {
  713. if (is_array($display)) {
  714. return $this->optionGroup($display, $value, $selected);
  715. }
  716. return $this->option($display, $value, $selected);
  717. }
  718. /**
  719. * 生成optionGroup
  720. *
  721. * @param array $list
  722. * @param string $label
  723. * @param string $selected
  724. * @return string
  725. */
  726. protected function optionGroup($list, $label, $selected)
  727. {
  728. $html = [];
  729. foreach ($list as $value => $display) {
  730. $html[] = $this->option($display, $value, $selected);
  731. }
  732. return '<optgroup label="' . $this->escape($label) . '">' . implode('', $html) . '</optgroup>';
  733. }
  734. /**
  735. * 生成option选项
  736. *
  737. * @param string $display
  738. * @param string $value
  739. * @param string $selected
  740. * @return string
  741. */
  742. protected function option($display, $value, $selected)
  743. {
  744. $selected = $this->getSelectedValue($value, $selected);
  745. $options = array('value' => $this->escape($value), 'selected' => $selected);
  746. return '<option' . $this->attributes($options) . '>' . $this->escape($display) . '</option>';
  747. }
  748. /**
  749. * 检测value是否选中
  750. *
  751. * @param string $value
  752. * @param string $selected
  753. * @return string
  754. */
  755. protected function getSelectedValue($value, $selected)
  756. {
  757. if (is_array($selected)) {
  758. return in_array($value, $selected) ? 'selected' : null;
  759. }
  760. return ((string)$value == (string)$selected) ? 'selected' : null;
  761. }
  762. /**
  763. * 生成复选按钮
  764. *
  765. * @param string $name
  766. * @param mixed $value
  767. * @param bool $checked
  768. * @param array $options
  769. * @return string
  770. */
  771. public function checkbox($name, $value = 1, $checked = null, $options = [])
  772. {
  773. if ($checked)
  774. $options['checked'] = 'checked';
  775. return $this->input('checkbox', $name, $value, $options);
  776. }
  777. /**
  778. * 生成一组筛选框
  779. *
  780. * @param string $name
  781. * @param array $list
  782. * @param mixed $checked
  783. * @param array $options
  784. * @return string
  785. */
  786. public function checkboxs($name, $list, $checked, $options = [])
  787. {
  788. $html = [];
  789. $checked = is_null($checked) ? [] : $checked;
  790. $checked = is_array($checked) ? $checked : explode(',', $checked);
  791. foreach ($list as $k => $v) {
  792. $options['id'] = "{$name}-{$k}";
  793. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox($name, $k, in_array($k, $checked), $options));
  794. }
  795. return '<div class="checkbox">' . implode(' ', $html) . '</div>';
  796. }
  797. /**
  798. * 生成单选按钮
  799. *
  800. * @param string $name
  801. * @param mixed $value
  802. * @param bool $checked
  803. * @param array $options
  804. * @return string
  805. */
  806. public function radio($name, $value = null, $checked = null, $options = [])
  807. {
  808. if (is_null($value))
  809. $value = $name;
  810. if ($checked)
  811. $options['checked'] = 'checked';
  812. return $this->input('radio', $name, $value, $options);
  813. }
  814. /**
  815. * 生成一组单选框
  816. *
  817. * @param string $name
  818. * @param array $list
  819. * @param mixed $checked
  820. * @param array $options
  821. * @return string
  822. */
  823. public function radios($name, $list, $checked = null, $options = [])
  824. {
  825. $html = [];
  826. $checked = is_null($checked) ? key($list) : $checked;
  827. $checked = is_array($checked) ? $checked : explode(',', $checked);
  828. foreach ($list as $k => $v) {
  829. $options['id'] = "{$name}-{$k}";
  830. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $checked), $options));
  831. }
  832. return '<div class="radio">' . implode(' ', $html) . '</div>';
  833. }
  834. /**
  835. * 生成上传图片组件(单图)
  836. *
  837. * @param string $name
  838. * @param string $value
  839. * @param array $inputAttr
  840. * @param array $uploadAttr
  841. * @param array $chooseAttr
  842. * @param array $previewAttr
  843. * @return string
  844. */
  845. public function image($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  846. {
  847. $default = [
  848. 'data-mimetype' => 'image/gif,image/jpeg,image/png,image/jpg,image/bmp'
  849. ];
  850. $uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
  851. $chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
  852. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  853. }
  854. /**
  855. * 生成上传图片组件(多图)
  856. *
  857. * @param string $name
  858. * @param string $value
  859. * @param array $inputAttr
  860. * @param array $uploadAttr
  861. * @param array $chooseAttr
  862. * @param array $previewAttr
  863. * @return string
  864. */
  865. public function images($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  866. {
  867. $default = [
  868. 'data-multiple' => 'true',
  869. 'data-mimetype' => 'image/gif,image/jpeg,image/png,image/jpg,image/bmp'
  870. ];
  871. $uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
  872. $chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
  873. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  874. }
  875. /**
  876. * 生成上传文件组件(单文件)
  877. *
  878. * @param string $name
  879. * @param string $value
  880. * @param array $inputAttr
  881. * @param array $uploadAttr
  882. * @param array $chooseAttr
  883. * @param array $previewAttr
  884. * @return string
  885. */
  886. public function upload($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  887. {
  888. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  889. }
  890. /**
  891. * 生成上传文件组件(多文件)
  892. *
  893. * @param string $name
  894. * @param string $value
  895. * @param array $inputAttr
  896. * @param array $uploadAttr
  897. * @param array $chooseAttr
  898. * @param array $previewAttr
  899. * @return string
  900. */
  901. public function uploads($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  902. {
  903. $default = [
  904. 'data-multiple' => 'true',
  905. ];
  906. $uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
  907. $chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
  908. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  909. }
  910. protected function uploader($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  911. {
  912. $domname = str_replace(['[', ']', '.'], '', $name);
  913. $options = [
  914. 'id' => "plupload-{$domname}",
  915. 'class' => "btn btn-danger plupload",
  916. 'data-input-id' => "c-{$domname}",
  917. ];
  918. $upload = $uploadAttr === false ? false : true;
  919. $choose = $chooseAttr === false ? false : true;
  920. $preview = $previewAttr === false ? false : true;
  921. if ($preview) {
  922. $options['data-preview-id'] = "p-{$domname}";
  923. }
  924. $uploadBtn = $upload ? $this->button('<i class="fa fa-upload"></i> ' . __('Upload'), array_merge($options, $uploadAttr)) : '';
  925. $options = [
  926. 'id' => "fachoose-{$domname}",
  927. 'class' => "btn btn-danger fachoose",
  928. 'data-input-id' => "c-{$domname}",
  929. ];
  930. if ($preview) {
  931. $options['data-preview-id'] = "p-{$domname}";
  932. }
  933. $chooseBtn = $choose ? $this->button('<i class="fa fa-list"></i> ' . __('Choose'), array_merge($options, $chooseAttr)) : '';
  934. $previewAttrHtml = $this->attributes($previewAttr);
  935. $previewArea = $preview ? '<ul class="row list-inline plupload-preview" id="p-' . $domname . '" ' . $previewAttrHtml . '></ul>' : '';
  936. $input = $this->text($name, $value, array_merge(['size' => 50, 'id' => "c-{$domname}"], $inputAttr));
  937. $html = <<<EOD
  938. <div class="input-group">
  939. {$input}
  940. <div class="input-group-addon no-border no-padding">
  941. <span>{$uploadBtn}</span>
  942. <span>{$chooseBtn}</span>
  943. </div>
  944. <span class="msg-box n-right" for="c-{$domname}"></span>
  945. </div>
  946. {$previewArea}
  947. EOD;
  948. return $html;
  949. }
  950. /**
  951. * 生成一个按钮
  952. *
  953. * @param string $value
  954. * @param array $options
  955. * @return string
  956. */
  957. public function button($value = null, $options = [])
  958. {
  959. if (!array_key_exists('type', $options)) {
  960. $options['type'] = 'button';
  961. }
  962. return '<button' . $this->attributes($options) . '>' . $value . '</button>';
  963. }
  964. /**
  965. * 获取ID属性值
  966. *
  967. * @param string $name
  968. * @param array $attributes
  969. * @return string
  970. */
  971. public function getIdAttribute($name, $attributes)
  972. {
  973. if (array_key_exists('id', $attributes)) {
  974. return $attributes['id'];
  975. }
  976. if (in_array($name, $this->labels)) {
  977. return $name;
  978. }
  979. }
  980. /**
  981. * 获取Value属性值
  982. *
  983. * @param string $name
  984. * @param string $value
  985. * @return string
  986. */
  987. public function getValueAttribute($name, $value = null)
  988. {
  989. if (is_null($name))
  990. return $value;
  991. if (!is_null($value))
  992. return $value;
  993. }
  994. /**
  995. * 数组转换成一个HTML属性字符串。
  996. *
  997. * @param array $attributes
  998. * @return string
  999. */
  1000. public function attributes($attributes)
  1001. {
  1002. $html = [];
  1003. // 假设我们的keys 和 value 是相同的,
  1004. // 拿HTML“required”属性来说,假设是['required']数组,
  1005. // 会已 required="required" 拼接起来,而不是用数字keys去拼接
  1006. foreach ((array)$attributes as $key => $value) {
  1007. $element = $this->attributeElement($key, $value);
  1008. if (!is_null($element))
  1009. $html[] = $element;
  1010. }
  1011. return count($html) > 0 ? ' ' . implode(' ', $html) : '';
  1012. }
  1013. /**
  1014. * 拼接成一个属性。
  1015. *
  1016. * @param string $key
  1017. * @param string $value
  1018. * @return string
  1019. */
  1020. protected function attributeElement($key, $value)
  1021. {
  1022. if (is_numeric($key))
  1023. $key = $value;
  1024. if (!is_null($value)) {
  1025. if (is_array($value) || stripos($value, '"') !== false) {
  1026. $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  1027. return $key . "='" . $value . "'";
  1028. } else {
  1029. return $key . '="' . $value . '"';
  1030. }
  1031. }
  1032. }
  1033. }
  1034. class Arr
  1035. {
  1036. /**
  1037. * Determine whether the given value is array accessible.
  1038. *
  1039. * @param mixed $value
  1040. * @return bool
  1041. */
  1042. public static function accessible($value)
  1043. {
  1044. return is_array($value) || $value instanceof ArrayAccess;
  1045. }
  1046. /**
  1047. * Determine if the given key exists in the provided array.
  1048. *
  1049. * @param \ArrayAccess|array $array
  1050. * @param string|int $key
  1051. * @return bool
  1052. */
  1053. public static function exists($array, $key)
  1054. {
  1055. if ($array instanceof ArrayAccess) {
  1056. return $array->offsetExists($key);
  1057. }
  1058. return array_key_exists($key, $array);
  1059. }
  1060. /**
  1061. * Get an item from an array using "dot" notation.
  1062. *
  1063. * @param \ArrayAccess|array $array
  1064. * @param string $key
  1065. * @param mixed $default
  1066. * @return mixed
  1067. */
  1068. public static function get($array, $key, $default = null)
  1069. {
  1070. if (!static::accessible($array)) {
  1071. return $default;
  1072. }
  1073. if (is_null($key)) {
  1074. return $array;
  1075. }
  1076. if (static::exists($array, $key)) {
  1077. return $array[$key];
  1078. }
  1079. foreach (explode('.', $key) as $segment) {
  1080. if (static::accessible($array) && static::exists($array, $segment)) {
  1081. $array = $array[$segment];
  1082. } else {
  1083. return $default;
  1084. }
  1085. }
  1086. return $array;
  1087. }
  1088. /**
  1089. * Get all of the given array except for a specified array of items.
  1090. *
  1091. * @param array $array
  1092. * @param array|string $keys
  1093. * @return array
  1094. */
  1095. public static function except($array, $keys)
  1096. {
  1097. static::forget($array, $keys);
  1098. return $array;
  1099. }
  1100. /**
  1101. * Remove one or many array items from a given array using "dot" notation.
  1102. *
  1103. * @param array $array
  1104. * @param array|string $keys
  1105. * @return void
  1106. */
  1107. public static function forget(&$array, $keys)
  1108. {
  1109. $original = &$array;
  1110. $keys = (array)$keys;
  1111. if (count($keys) === 0) {
  1112. return;
  1113. }
  1114. foreach ($keys as $key) {
  1115. // if the exact key exists in the top-level, remove it
  1116. if (static::exists($array, $key)) {
  1117. unset($array[$key]);
  1118. continue;
  1119. }
  1120. $parts = explode('.', $key);
  1121. // clean up before each pass
  1122. $array = &$original;
  1123. while (count($parts) > 1) {
  1124. $part = array_shift($parts);
  1125. if (isset($array[$part]) && is_array($array[$part])) {
  1126. $array = &$array[$part];
  1127. } else {
  1128. continue 2;
  1129. }
  1130. }
  1131. unset($array[array_shift($parts)]);
  1132. }
  1133. }
  1134. }
  1135. if (!function_exists('array_get')) {
  1136. /**
  1137. * Get an item from an array using "dot" notation.
  1138. *
  1139. * @param \ArrayAccess|array $array
  1140. * @param string $key
  1141. * @param mixed $default
  1142. * @return mixed
  1143. */
  1144. function array_get($array, $key, $default = null)
  1145. {
  1146. return Arr::get($array, $key, $default);
  1147. }
  1148. }
  1149. if (!function_exists('e')) {
  1150. /**
  1151. * Escape HTML special characters in a string.
  1152. *
  1153. *
  1154. * @return string
  1155. */
  1156. function e($value)
  1157. {
  1158. if (is_array($value)) {
  1159. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  1160. }
  1161. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
  1162. }
  1163. }
  1164. if (!function_exists('array_except')) {
  1165. /**
  1166. * Get all of the given array except for a specified array of items.
  1167. *
  1168. * @param array $array
  1169. * @param array|string $keys
  1170. * @return array
  1171. */
  1172. function array_except($array, $keys)
  1173. {
  1174. return Arr::except($array, $keys);
  1175. }
  1176. }