Form.php 8.1 KB

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