Form.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder;
  8. use FormBuilder\components\Cascader;
  9. use FormBuilder\traits\form\FormCascaderTrait;
  10. use FormBuilder\traits\form\FormCheckBoxTrait;
  11. use FormBuilder\traits\form\FormColorPickerTrait;
  12. use FormBuilder\traits\form\FormDatePickerTrait;
  13. use FormBuilder\traits\form\FormFrameTrait;
  14. use FormBuilder\traits\form\FormHiddenTrait;
  15. use FormBuilder\traits\form\FormInputNumberTrait;
  16. use FormBuilder\traits\form\FormInputTrait;
  17. use FormBuilder\traits\form\FormRadioTrait;
  18. use FormBuilder\traits\form\FormRateTrait;
  19. use FormBuilder\traits\form\FormSelectTrait;
  20. use FormBuilder\traits\form\FormSliderTrait;
  21. use FormBuilder\traits\form\FormSwitchesTrait;
  22. use FormBuilder\traits\form\FormTimePickerTrait;
  23. use FormBuilder\traits\form\FormUploadTrait;
  24. class Form
  25. {
  26. use FormColorPickerTrait,
  27. FormFrameTrait,
  28. FormInputNumberTrait,
  29. FormRadioTrait,
  30. FormRateTrait,
  31. FormSelectTrait,
  32. FormSwitchesTrait,
  33. FormUploadTrait,
  34. FormCheckBoxTrait,
  35. FormDatePickerTrait,
  36. FormInputTrait,
  37. FormSliderTrait,
  38. FormCascaderTrait,
  39. FormHiddenTrait,
  40. FormTimePickerTrait;
  41. /**
  42. * 三级联动 加载省市数据
  43. * @var bool
  44. */
  45. protected $loadCityData = false;
  46. /**
  47. * 三级联动 加载省市区数据
  48. * @var bool
  49. */
  50. protected $loadCityAreaData = false;
  51. protected $components = [];
  52. protected $fields = [];
  53. protected $script = [];
  54. protected $successScript = '';
  55. /**
  56. * 网页标题
  57. * @var string
  58. */
  59. protected $title = 'formBuilder';
  60. /**
  61. * 提交地址
  62. * @var string
  63. */
  64. protected $action = '';
  65. /**
  66. * 提交方式
  67. * @var string
  68. */
  69. protected $method = 'post';
  70. /**
  71. * 表单配置
  72. * @var array|mixed
  73. */
  74. protected $config = [];
  75. /**
  76. * Form constructor.
  77. * @param string $action 提交地址
  78. * @param array $components 组件
  79. */
  80. public function __construct($action, array $components = [])
  81. {
  82. foreach ($components as $component){
  83. $this->append($component);
  84. }
  85. $this->action = $action;
  86. $config = require_once 'config' . DIRECTORY_SEPARATOR . 'config.php';
  87. $this->setSuccessScript($config['formSuccessScript']);
  88. $this->config = $config['form'];
  89. $this->script = $config['script'];
  90. }
  91. /**
  92. * 修改配置
  93. * @param array $config
  94. * @return $this
  95. */
  96. public function config(array $config)
  97. {
  98. $this->config = array_merge($this->config, $config);
  99. return $this;
  100. }
  101. /**
  102. * 获取配置参数
  103. * @param String $configName
  104. * @param $default
  105. * @return array|mixed|string
  106. */
  107. public function getConfig($configName = '', $default = '')
  108. {
  109. $config = $this->config;
  110. if (!$configName) return $config;
  111. $configNameList = explode('.', $configName);
  112. $count = count($configNameList);
  113. foreach ($configNameList as $k => $cn) {
  114. if (!isset($config[$cn]))
  115. return $default;
  116. else if (($k + 1) == $count)
  117. return $config[$cn];
  118. else
  119. $config = $config[$cn];
  120. }
  121. return $default;
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getSuccessScript()
  127. {
  128. return $this->successScript;
  129. }
  130. /**
  131. * 表单提交后成功执行的js地址
  132. * formCreate.formSuccess(formData,$f)
  133. * @param string $successScript
  134. * @return $this
  135. */
  136. public function setSuccessScript($successScript)
  137. {
  138. $this->successScript = $successScript;
  139. return $this;
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function getAction()
  145. {
  146. return $this->action;
  147. }
  148. /**
  149. * 提交地址
  150. * @param string $action
  151. * @return $this
  152. */
  153. public function setAction($action)
  154. {
  155. $this->action = $action;
  156. return $this;
  157. }
  158. /**
  159. * @return string
  160. */
  161. public function getMethod()
  162. {
  163. return $this->method;
  164. }
  165. /**
  166. * 提交方式
  167. * @param string $method
  168. * @return $this
  169. */
  170. public function setMethod($method)
  171. {
  172. $this->method = $method;
  173. return $this;
  174. }
  175. /**
  176. * 标题
  177. * @return string
  178. */
  179. public function getTitle()
  180. {
  181. return $this->title;
  182. }
  183. /**
  184. * @param string $title
  185. * @return $this
  186. */
  187. public function setTitle($title)
  188. {
  189. $this->title = $title;
  190. return $this;
  191. }
  192. /**
  193. * 追加组件
  194. * @param FormComponentDriver $component
  195. * @return $this
  196. */
  197. public function append(FormComponentDriver $component)
  198. {
  199. $field = $component->getField();
  200. if(!isset($this->components[$field]))
  201. $this->fields[] = $field;
  202. $this->components[$field] = $component;
  203. return $this;
  204. }
  205. /**
  206. * 开头插入组件
  207. * @param FormComponentDriver $component
  208. * @return $this
  209. */
  210. public function prepend(FormComponentDriver $component)
  211. {
  212. $field = $component->getField();
  213. if(!isset($this->components[$field]))
  214. array_unshift($this->fields, $field);
  215. $this->components[$field] = $component;
  216. return $this;
  217. }
  218. /**
  219. * 获得表单规则
  220. * @return array
  221. */
  222. public function getRules()
  223. {
  224. $rules = [];
  225. foreach ($this->fields as $field) {
  226. $component = $this->components[$field];
  227. if (!($component instanceof FormComponentDriver))
  228. continue;
  229. $loadData = $this->loadCityData == true && $this->loadCityAreaData == true;
  230. if ($loadData == false && $component instanceof Cascader) {
  231. $type = $component->getType();
  232. if ($type == Cascader::TYPE_CITY)
  233. $this->loadCityData = true;
  234. else if ($type == Cascader::TYPE_CITY_AREA)
  235. $this->loadCityAreaData = true;
  236. }
  237. $rules[] = $component->build();
  238. }
  239. return $rules;
  240. }
  241. /**
  242. * 获取表单视图
  243. * @return string
  244. */
  245. public function view()
  246. {
  247. ob_start();
  248. $form = $this;
  249. $rule = $this->getRules();
  250. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'form.php';
  251. $html = ob_get_clean();
  252. return $html;
  253. }
  254. /**
  255. * 获取表单生成器所需全部js
  256. * @return string
  257. */
  258. public static function script()
  259. {
  260. $config = require_once 'config' . DIRECTORY_SEPARATOR . 'config.php';
  261. return implode("\r\n", $config['script']);
  262. }
  263. /**
  264. * 获取表单生成器全部js
  265. * @return string
  266. */
  267. public function getScript()
  268. {
  269. $_script = $this->script;
  270. $script = [
  271. $_script['iview-css'],
  272. $_script['jq'],
  273. $_script['vue'],
  274. $_script['iview'],
  275. $_script['form-create']
  276. ];
  277. if ($this->loadCityAreaData == true)
  278. $script[] = $_script['city-area-data'];
  279. if ($this->loadCityData == true)
  280. $script[] = $_script['city-data'];
  281. return implode("\r\n", $script);
  282. }
  283. /**
  284. * 生成表单快捷方法
  285. * @param $action
  286. * @param array $components
  287. * @return Form
  288. */
  289. public static function create($action, array $components = [])
  290. {
  291. return new self($action, $components);
  292. }
  293. }