Form.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 array $list
  329. * @param mixed $selected
  330. * @param array $options
  331. * @return string
  332. */
  333. public function select($name, $list = [], $selected = null, $options = [])
  334. {
  335. $selected = $this->getValueAttribute($name, $selected);
  336. $options['id'] = $this->getIdAttribute($name, $options);
  337. if (!isset($options['name']))
  338. $options['name'] = $name;
  339. $html = [];
  340. foreach ($list as $value => $display) {
  341. $html[] = $this->getSelectOption($display, $value, $selected);
  342. }
  343. $options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
  344. $options = $this->attributes($options);
  345. $list = implode('', $html);
  346. return "<select{$options}>{$list}</select>";
  347. }
  348. /**
  349. * 下拉列表(多选)
  350. *
  351. * @param string $name
  352. * @param array $list
  353. * @param mixed $selected
  354. * @param array $options
  355. * @return string
  356. */
  357. public function selects($name, $list = [], $selected = null, $options = [])
  358. {
  359. $options[] = 'multiple';
  360. return $this->select($name, $list, $selected, $options);
  361. }
  362. /**
  363. * 下拉列表(友好)
  364. *
  365. * @param string $name
  366. * @param array $list
  367. * @param mixed $selected
  368. * @param array $options
  369. * @return string
  370. */
  371. public function selectpicker($name, $list = [], $selected = null, $options = [])
  372. {
  373. $options['class'] = isset($options['class']) ? $options['class'] . ' selectpicker' : 'selectpicker';
  374. return $this->select($name, $list, $selected, $options);
  375. }
  376. /**
  377. * 下拉列表(友好)(多选)
  378. *
  379. * @param string $name
  380. * @param array $list
  381. * @param mixed $selected
  382. * @param array $options
  383. * @return string
  384. */
  385. public function selectpickers($name, $list = [], $selected = null, $options = [])
  386. {
  387. $options[] = 'multiple';
  388. return $this->selectpicker($name, $list, $selected, $options);
  389. }
  390. /**
  391. * 生成动态下拉列表
  392. *
  393. * @param string $name 名称
  394. * @param mixed $value
  395. * @param string $url 数据源地址
  396. * @param string $field 显示的字段名称,默认为name
  397. * @param string $primaryKey 主键,数据库中保存的值,默认为id
  398. * @param array $options
  399. * @return string
  400. */
  401. public function selectpage($name, $value, $url, $field = null, $primaryKey = null, $options = [])
  402. {
  403. $options = array_merge($options, ['data-source' => $url, 'data-field' => $field ? $field : 'name', 'data-primary-key' => $primaryKey ? $primaryKey : 'id']);
  404. $options['class'] = isset($options['class']) ? $options['class'] . ' selectpage' : 'selectpage';
  405. return $this->text($name, $value, $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 selectpages($name, $value, $url, $field = null, $primaryKey = null, $options = [])
  419. {
  420. $options['data-multiple'] = "true";
  421. return $this->selectpage($name, $value, $url, $field, $primaryKey, $options);
  422. }
  423. /**
  424. * 生成城市选择框
  425. *
  426. * @param string $name
  427. * @param mixed $value
  428. * @param array $options
  429. * @return string
  430. */
  431. public function citypicker($name, $value, $options = [])
  432. {
  433. $options['data-toggle'] = 'city-picker';
  434. return "<div class='control-relative'>" . $this->text($name, $value, $options) . "</div>";
  435. }
  436. /**
  437. * 生成switch组件
  438. *
  439. * @param string $name
  440. * @param mixed $value
  441. * @param array $options
  442. * @return string
  443. */
  444. public function switcher($name, $value, $options = [])
  445. {
  446. $domname = str_replace(['[', ']', '.'], '', $name);
  447. $btn = $this->hidden($name, $value, ['id' => "c-{$domname}"]);
  448. $yes = 1;
  449. $no = 0;
  450. if (isset($options['yes']) && isset($options['no'])) {
  451. $yes = $options['yes'];
  452. $no = $options['no'];
  453. }
  454. $selected = $no == $value ? "fa-flip-horizontal text-gray" : "";
  455. $disabled = (isset($options['disabled']) && $options['disabled']) || in_array('disabled', $options) ? "disabled" : '';
  456. $color = isset($options['color']) ? $options['color'] : 'success';
  457. unset($options['yes'], $options['no'], $options['color'], $options['disabled']);
  458. $attr = $this->attributes($options);
  459. $html = <<<EOD
  460. {$btn}
  461. <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>
  462. EOD;
  463. return $html;
  464. }
  465. /**
  466. * 日期选择器
  467. * @param string $name
  468. * @param mixed $value
  469. * @param array $options
  470. * @return string
  471. */
  472. public function datepicker($name, $value, $options = [])
  473. {
  474. $defaults = [
  475. 'data-date-format' => "YYYY-MM-DD",
  476. ];
  477. $options = array_merge($defaults, $options);
  478. $value = is_numeric($value) ? date("Y-m-d", $value) : $value;
  479. return $this->datetimepicker($name, $value, $options);
  480. }
  481. /**
  482. * 时间选择器
  483. *
  484. * @param string $name
  485. * @param mixed $value
  486. * @param array $options
  487. * @return string
  488. */
  489. public function timepicker($name, $value, $options = [])
  490. {
  491. $defaults = [
  492. 'data-date-format' => "HH:mm:ss",
  493. ];
  494. $options = array_merge($defaults, $options);
  495. $value = is_numeric($value) ? date("H:i:s", $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 datetimepicker($name, $value, $options = [])
  507. {
  508. $defaults = [
  509. 'data-date-format' => "YYYY-MM-DD HH:mm:ss",
  510. 'data-use-current' => "true",
  511. ];
  512. $value = is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  513. $options = array_merge($defaults, $options);
  514. $options['class'] = isset($options['class']) ? $options['class'] . ' datetimepicker' : 'datetimepicker';
  515. return $this->text($name, $value, $options);
  516. }
  517. /**
  518. * 日期区间
  519. *
  520. * @param string $name
  521. * @param string $value
  522. * @param array $options
  523. * @return string
  524. */
  525. public function daterange($name, $value, $options = [])
  526. {
  527. $defaults = [
  528. 'data-locale' => [
  529. 'format' => 'YYYY-MM-DD'
  530. ]
  531. ];
  532. $options = array_merge($defaults, $options);
  533. return $this->datetimerange($name, $value, $options);
  534. }
  535. /**
  536. * 时间区间
  537. *
  538. * @param string $name
  539. * @param string $value
  540. * @param array $options
  541. * @return string
  542. */
  543. public function timerange($name, $value, $options = [])
  544. {
  545. $defaults = [
  546. 'data-locale' => [
  547. 'format' => 'HH:mm:ss'
  548. ],
  549. 'data-ranges' => [],
  550. 'data-show-custom-range-label' => "false",
  551. 'data-time-picker' => "true",
  552. ];
  553. $options = array_merge($defaults, $options);
  554. return $this->datetimerange($name, $value, $options);
  555. }
  556. /**
  557. * 日期时间区间
  558. *
  559. * @param string $name
  560. * @param string $value
  561. * @param array $options
  562. * @return string
  563. */
  564. public function datetimerange($name, $value, $options = [])
  565. {
  566. $defaults = [
  567. 'data-locale' => [
  568. 'format' => 'YYYY-MM-DD HH:mm:ss'
  569. ]
  570. ];
  571. $options = array_merge($defaults, $options);
  572. $options['class'] = isset($options['class']) ? $options['class'] . ' datetimerange' : 'datetimerange';
  573. return $this->text($name, $value, $options);
  574. }
  575. /**
  576. * 生成字段列表组件
  577. *
  578. * @param string $name
  579. * @param mixed $value
  580. * @param array $title
  581. * @param string $template
  582. * @param array $options
  583. * @return string
  584. */
  585. public function fieldlist($name, $value, $title = null, $template = null, $options = [])
  586. {
  587. $append = __('Append');
  588. $template = $template ? 'data-template="' . $template . '"' : '';
  589. $attributes = $this->attributes($options);
  590. if (is_null($title)) {
  591. $title = [__('Key'), __('Value')];
  592. }
  593. $ins = implode("\n", array_map(function ($value) {
  594. return "<ins>{$value}</ins>";
  595. }, $title));
  596. $value = is_array($value) ? json_encode($value) : $value;
  597. $html = <<<EOD
  598. <dl class="fieldlist" data-name="{$name}" {$template} {$attributes}>
  599. <dd>
  600. {$ins}
  601. </dd>
  602. <dd><a href="javascript:;" class="btn btn-sm btn-success btn-append"><i class="fa fa-plus"></i> {$append}</a></dd>
  603. <textarea name="{$name}" class="form-control hide" cols="30" rows="5">{$value}</textarea>
  604. </dl>
  605. EOD;
  606. return $html;
  607. }
  608. /**
  609. * 生成联动下拉列表
  610. *
  611. * @param string $url 联动获取数据源的URL地址
  612. * @param array $names 联动字段名称
  613. * @param array $values 联动字段默认选中的值
  614. * @param array $options 扩展属性
  615. * @return string
  616. */
  617. public function cxselect($url, $names = [], $values = [], $options = [])
  618. {
  619. $classes = [];
  620. $cxselect = [];
  621. $attributes = $this->attributes($options);
  622. foreach ($names as $index => $value) {
  623. $level = $index + 1;
  624. $class = "cxselect-{$level}";
  625. $classes[] = $class;
  626. $selectValue = isset($values[$value]) ? $values[$value] : (isset($values[$index]) ? $values[$index] : '');
  627. $cxselect[] = <<<EOD
  628. <select class="{$class} form-control" name="{$value}" data-value="{$selectValue}" data-url="{$url}?level={$level}&name={$value}" {$attributes}></select>
  629. EOD;
  630. }
  631. $cxselect = implode("\n", $cxselect);
  632. $selects = implode(',', $classes);
  633. $html = <<<EOD
  634. <div class="form-inline" data-toggle="cxselect" data-selects="{$selects}">
  635. {$cxselect}
  636. </div>
  637. EOD;
  638. return $html;
  639. }
  640. /**
  641. * 创建一个下拉列表选择区间组件
  642. *
  643. * @param string $name
  644. * @param string $begin
  645. * @param string $end
  646. * @param string $selected
  647. * @param array $options
  648. * @return string
  649. */
  650. public function selectRange($name, $begin, $end, $selected = null, $options = [])
  651. {
  652. $range = array_combine($range = range($begin, $end), $range);
  653. return $this->select($name, $range, $selected, $options);
  654. }
  655. /**
  656. * 生成选择年组件
  657. *
  658. * @param string $name
  659. * @param string $begin
  660. * @param string $end
  661. * @param string $selected
  662. * @param array $options
  663. * @return string
  664. */
  665. public function selectYear($name, $begin, $end, $selected, $options)
  666. {
  667. return call_user_func_array(array($this, 'selectRange'), func_get_args());
  668. }
  669. /**
  670. * 生成选择月组件
  671. *
  672. * @param string $name
  673. * @param string $selected
  674. * @param array $options
  675. * @param string $format
  676. * @return string
  677. */
  678. public function selectMonth($name, $selected = null, $options = [], $format = '%m')
  679. {
  680. $months = [];
  681. foreach (range(1, 12) as $month) {
  682. $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
  683. }
  684. return $this->select($name, $months, $selected, $options);
  685. }
  686. /**
  687. * 根据传递的值生成option
  688. *
  689. * @param string $display
  690. * @param string $value
  691. * @param string $selected
  692. * @return string
  693. */
  694. public function getSelectOption($display, $value, $selected)
  695. {
  696. if (is_array($display)) {
  697. return $this->optionGroup($display, $value, $selected);
  698. }
  699. return $this->option($display, $value, $selected);
  700. }
  701. /**
  702. * 生成optionGroup
  703. *
  704. * @param array $list
  705. * @param string $label
  706. * @param string $selected
  707. * @return string
  708. */
  709. protected function optionGroup($list, $label, $selected)
  710. {
  711. $html = [];
  712. foreach ($list as $value => $display) {
  713. $html[] = $this->option($display, $value, $selected);
  714. }
  715. return '<optgroup label="' . $this->escape($label) . '">' . implode('', $html) . '</optgroup>';
  716. }
  717. /**
  718. * 生成option选项
  719. *
  720. * @param string $display
  721. * @param string $value
  722. * @param string $selected
  723. * @return string
  724. */
  725. protected function option($display, $value, $selected)
  726. {
  727. $selected = $this->getSelectedValue($value, $selected);
  728. $options = array('value' => $this->escape($value), 'selected' => $selected);
  729. return '<option' . $this->attributes($options) . '>' . $this->escape($display) . '</option>';
  730. }
  731. /**
  732. * 检测value是否选中
  733. *
  734. * @param string $value
  735. * @param string $selected
  736. * @return string
  737. */
  738. protected function getSelectedValue($value, $selected)
  739. {
  740. if (is_array($selected)) {
  741. return in_array($value, $selected) ? 'selected' : null;
  742. }
  743. return ((string)$value == (string)$selected) ? 'selected' : null;
  744. }
  745. /**
  746. * 生成复选按钮
  747. *
  748. * @param string $name
  749. * @param mixed $value
  750. * @param bool $checked
  751. * @param array $options
  752. * @return string
  753. */
  754. public function checkbox($name, $value = 1, $checked = null, $options = [])
  755. {
  756. if ($checked)
  757. $options['checked'] = 'checked';
  758. return $this->input('checkbox', $name, $value, $options);
  759. }
  760. /**
  761. * 生成一组筛选框
  762. *
  763. * @param string $name
  764. * @param array $list
  765. * @param mixed $checked
  766. * @param array $options
  767. * @return string
  768. */
  769. public function checkboxs($name, $list, $checked, $options = [])
  770. {
  771. $html = [];
  772. $checked = is_null($checked) ? [] : $checked;
  773. $checked = is_array($checked) ? $checked : explode(',', $checked);
  774. foreach ($list as $k => $v) {
  775. $options['id'] = "{$name}-{$k}";
  776. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox($name, $k, in_array($k, $checked), $options));
  777. }
  778. return '<div class="checkbox">' . implode(' ', $html) . '</div>';
  779. }
  780. /**
  781. * 生成单选按钮
  782. *
  783. * @param string $name
  784. * @param mixed $value
  785. * @param bool $checked
  786. * @param array $options
  787. * @return string
  788. */
  789. public function radio($name, $value = null, $checked = null, $options = [])
  790. {
  791. if (is_null($value))
  792. $value = $name;
  793. if ($checked)
  794. $options['checked'] = 'checked';
  795. return $this->input('radio', $name, $value, $options);
  796. }
  797. /**
  798. * 生成一组单选框
  799. *
  800. * @param string $name
  801. * @param array $list
  802. * @param mixed $checked
  803. * @param array $options
  804. * @return string
  805. */
  806. public function radios($name, $list, $checked = null, $options = [])
  807. {
  808. $html = [];
  809. $checked = is_null($checked) ? key($list) : $checked;
  810. $checked = is_array($checked) ? $checked : explode(',', $checked);
  811. foreach ($list as $k => $v) {
  812. $options['id'] = "{$name}-{$k}";
  813. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $checked), $options));
  814. }
  815. return '<div class="radio">' . implode(' ', $html) . '</div>';
  816. }
  817. /**
  818. * 生成上传图片组件(单图)
  819. *
  820. * @param string $name
  821. * @param string $value
  822. * @param array $inputAttr
  823. * @param array $uploadAttr
  824. * @param array $chooseAttr
  825. * @param array $previewAttr
  826. * @return string
  827. */
  828. public function image($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  829. {
  830. $default = [
  831. 'data-mimetype' => 'image/gif,image/jpeg,image/png,image/jpg,image/bmp'
  832. ];
  833. $uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
  834. $chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
  835. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  836. }
  837. /**
  838. * 生成上传图片组件(多图)
  839. *
  840. * @param string $name
  841. * @param string $value
  842. * @param array $inputAttr
  843. * @param array $uploadAttr
  844. * @param array $chooseAttr
  845. * @param array $previewAttr
  846. * @return string
  847. */
  848. public function images($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  849. {
  850. $default = [
  851. 'data-multiple' => 'true',
  852. 'data-mimetype' => 'image/gif,image/jpeg,image/png,image/jpg,image/bmp'
  853. ];
  854. $uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
  855. $chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
  856. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  857. }
  858. /**
  859. * 生成上传文件组件(单文件)
  860. *
  861. * @param string $name
  862. * @param string $value
  863. * @param array $inputAttr
  864. * @param array $uploadAttr
  865. * @param array $chooseAttr
  866. * @param array $previewAttr
  867. * @return string
  868. */
  869. public function upload($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  870. {
  871. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  872. }
  873. /**
  874. * 生成上传文件组件(多文件)
  875. *
  876. * @param string $name
  877. * @param string $value
  878. * @param array $inputAttr
  879. * @param array $uploadAttr
  880. * @param array $chooseAttr
  881. * @param array $previewAttr
  882. * @return string
  883. */
  884. public function uploads($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  885. {
  886. $default = [
  887. 'data-multiple' => 'true',
  888. ];
  889. $uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
  890. $chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
  891. return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
  892. }
  893. protected function uploader($name = null, $value, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
  894. {
  895. $domname = str_replace(['[', ']', '.'], '', $name);
  896. $options = [
  897. 'id' => "plupload-{$domname}",
  898. 'class' => "btn btn-danger plupload",
  899. 'data-input-id' => "c-{$domname}",
  900. ];
  901. $upload = $uploadAttr === false ? false : true;
  902. $choose = $chooseAttr === false ? false : true;
  903. $preview = $previewAttr === false ? false : true;
  904. if ($preview) {
  905. $options['data-preview-id'] = "p-{$domname}";
  906. }
  907. $uploadBtn = $upload ? $this->button('<i class="fa fa-upload"></i> ' . __('Upload'), array_merge($options, $uploadAttr)) : '';
  908. $options = [
  909. 'id' => "fachoose-{$domname}",
  910. 'class' => "btn btn-danger fachoose",
  911. 'data-input-id' => "c-{$domname}",
  912. ];
  913. if ($preview) {
  914. $options['data-preview-id'] = "p-{$domname}";
  915. }
  916. $chooseBtn = $choose ? $this->button('<i class="fa fa-list"></i> ' . __('Choose'), array_merge($options, $chooseAttr)) : '';
  917. $previewAttrHtml = $this->attributes($previewAttr);
  918. $previewArea = $preview ? '<ul class="row list-inline plupload-preview" id="p-' . $domname . '" ' . $previewAttrHtml . '></ul>' : '';
  919. $input = $this->text($name, $value, array_merge(['size' => 50, 'id' => "c-{$domname}"], $inputAttr));
  920. $html = <<<EOD
  921. <div class="input-group">
  922. {$input}
  923. <div class="input-group-addon no-border no-padding">
  924. <span>{$uploadBtn}</span>
  925. <span>{$chooseBtn}</span>
  926. </div>
  927. <span class="msg-box n-right" for="c-{$domname}"></span>
  928. </div>
  929. {$previewArea}
  930. EOD;
  931. return $html;
  932. }
  933. /**
  934. * 生成一个按钮
  935. *
  936. * @param string $value
  937. * @param array $options
  938. * @return string
  939. */
  940. public function button($value = null, $options = [])
  941. {
  942. if (!array_key_exists('type', $options)) {
  943. $options['type'] = 'button';
  944. }
  945. return '<button' . $this->attributes($options) . '>' . $value . '</button>';
  946. }
  947. /**
  948. * 获取ID属性值
  949. *
  950. * @param string $name
  951. * @param array $attributes
  952. * @return string
  953. */
  954. public function getIdAttribute($name, $attributes)
  955. {
  956. if (array_key_exists('id', $attributes)) {
  957. return $attributes['id'];
  958. }
  959. if (in_array($name, $this->labels)) {
  960. return $name;
  961. }
  962. }
  963. /**
  964. * 获取Value属性值
  965. *
  966. * @param string $name
  967. * @param string $value
  968. * @return string
  969. */
  970. public function getValueAttribute($name, $value = null)
  971. {
  972. if (is_null($name))
  973. return $value;
  974. if (!is_null($value))
  975. return $value;
  976. }
  977. /**
  978. * 数组转换成一个HTML属性字符串。
  979. *
  980. * @param array $attributes
  981. * @return string
  982. */
  983. public function attributes($attributes)
  984. {
  985. $html = [];
  986. // 假设我们的keys 和 value 是相同的,
  987. // 拿HTML“required”属性来说,假设是['required']数组,
  988. // 会已 required="required" 拼接起来,而不是用数字keys去拼接
  989. foreach ((array)$attributes as $key => $value) {
  990. $element = $this->attributeElement($key, $value);
  991. if (!is_null($element))
  992. $html[] = $element;
  993. }
  994. return count($html) > 0 ? ' ' . implode(' ', $html) : '';
  995. }
  996. /**
  997. * 拼接成一个属性。
  998. *
  999. * @param string $key
  1000. * @param string $value
  1001. * @return string
  1002. */
  1003. protected function attributeElement($key, $value)
  1004. {
  1005. if (is_numeric($key))
  1006. $key = $value;
  1007. if (!is_null($value)) {
  1008. if (is_array($value) || stripos($value, '"') !== false) {
  1009. $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  1010. return $key . "='" . $value . "'";
  1011. } else {
  1012. return $key . '="' . $value . '"';
  1013. }
  1014. }
  1015. }
  1016. }
  1017. class Arr
  1018. {
  1019. /**
  1020. * Determine whether the given value is array accessible.
  1021. *
  1022. * @param mixed $value
  1023. * @return bool
  1024. */
  1025. public static function accessible($value)
  1026. {
  1027. return is_array($value) || $value instanceof ArrayAccess;
  1028. }
  1029. /**
  1030. * Determine if the given key exists in the provided array.
  1031. *
  1032. * @param \ArrayAccess|array $array
  1033. * @param string|int $key
  1034. * @return bool
  1035. */
  1036. public static function exists($array, $key)
  1037. {
  1038. if ($array instanceof ArrayAccess) {
  1039. return $array->offsetExists($key);
  1040. }
  1041. return array_key_exists($key, $array);
  1042. }
  1043. /**
  1044. * Get an item from an array using "dot" notation.
  1045. *
  1046. * @param \ArrayAccess|array $array
  1047. * @param string $key
  1048. * @param mixed $default
  1049. * @return mixed
  1050. */
  1051. public static function get($array, $key, $default = null)
  1052. {
  1053. if (!static::accessible($array)) {
  1054. return $default;
  1055. }
  1056. if (is_null($key)) {
  1057. return $array;
  1058. }
  1059. if (static::exists($array, $key)) {
  1060. return $array[$key];
  1061. }
  1062. foreach (explode('.', $key) as $segment) {
  1063. if (static::accessible($array) && static::exists($array, $segment)) {
  1064. $array = $array[$segment];
  1065. } else {
  1066. return $default;
  1067. }
  1068. }
  1069. return $array;
  1070. }
  1071. /**
  1072. * Get all of the given array except for a specified array of items.
  1073. *
  1074. * @param array $array
  1075. * @param array|string $keys
  1076. * @return array
  1077. */
  1078. public static function except($array, $keys)
  1079. {
  1080. static::forget($array, $keys);
  1081. return $array;
  1082. }
  1083. /**
  1084. * Remove one or many array items from a given array using "dot" notation.
  1085. *
  1086. * @param array $array
  1087. * @param array|string $keys
  1088. * @return void
  1089. */
  1090. public static function forget(&$array, $keys)
  1091. {
  1092. $original = &$array;
  1093. $keys = (array)$keys;
  1094. if (count($keys) === 0) {
  1095. return;
  1096. }
  1097. foreach ($keys as $key) {
  1098. // if the exact key exists in the top-level, remove it
  1099. if (static::exists($array, $key)) {
  1100. unset($array[$key]);
  1101. continue;
  1102. }
  1103. $parts = explode('.', $key);
  1104. // clean up before each pass
  1105. $array = &$original;
  1106. while (count($parts) > 1) {
  1107. $part = array_shift($parts);
  1108. if (isset($array[$part]) && is_array($array[$part])) {
  1109. $array = &$array[$part];
  1110. } else {
  1111. continue 2;
  1112. }
  1113. }
  1114. unset($array[array_shift($parts)]);
  1115. }
  1116. }
  1117. }
  1118. if (!function_exists('array_get')) {
  1119. /**
  1120. * Get an item from an array using "dot" notation.
  1121. *
  1122. * @param \ArrayAccess|array $array
  1123. * @param string $key
  1124. * @param mixed $default
  1125. * @return mixed
  1126. */
  1127. function array_get($array, $key, $default = null)
  1128. {
  1129. return Arr::get($array, $key, $default);
  1130. }
  1131. }
  1132. if (!function_exists('e')) {
  1133. /**
  1134. * Escape HTML special characters in a string.
  1135. *
  1136. *
  1137. * @return string
  1138. */
  1139. function e($value)
  1140. {
  1141. if (is_array($value)) {
  1142. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  1143. }
  1144. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
  1145. }
  1146. }
  1147. if (!function_exists('array_except')) {
  1148. /**
  1149. * Get all of the given array except for a specified array of items.
  1150. *
  1151. * @param array $array
  1152. * @param array|string $keys
  1153. * @return array
  1154. */
  1155. function array_except($array, $keys)
  1156. {
  1157. return Arr::except($array, $keys);
  1158. }
  1159. }