FormBuilder.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Common\Builder;
  3. use Common\Controller\ControllerController;
  4. /**
  5. * 表单页面自动生成器
  6. *
  7. */
  8. class FormBuilder extends ControllerController
  9. {
  10. private $_meta_title; // 页面标题
  11. private $_tab_nav = array(); // 页面Tab导航
  12. private $_post_url; // 表单提交地址
  13. private $_form_items = array(); // 表单项目
  14. private $_extra_items = array(); // 额外已经构造好的表单项目
  15. private $_form_data = array(); // 表单数据
  16. private $_extra_html; // 额外功能代码
  17. private $_ajax_submit = true; // 是否ajax提交
  18. private $_submit_title; // 确定按钮文本自定义
  19. private $_template; // 模版
  20. /**
  21. * 初始化方法
  22. * @return $this
  23. *
  24. */
  25. protected function _initialize()
  26. {
  27. $this->_template = APP_PATH . 'Common/Builder/Layout/' . MODULE_MARK . '/form.html';
  28. }
  29. /**
  30. * 设置页面标题
  31. * @param $title 标题文本
  32. * @return $this
  33. *
  34. */
  35. public function setMetaTitle($meta_title)
  36. {
  37. $this->_meta_title = $meta_title;
  38. return $this;
  39. }
  40. /**
  41. * 设置Tab按钮列表
  42. * @param $tab_list Tab列表 array('title' => '标题', 'href' => 'http://www.corethink.cn')
  43. * @param $current_tab 当前tab
  44. * @return $this
  45. *
  46. */
  47. public function setTabNav($tab_list, $current_tab)
  48. {
  49. $this->_tab_nav = array('tab_list' => $tab_list, 'current_tab' => $current_tab);
  50. return $this;
  51. }
  52. /**
  53. * 直接设置表单项数组
  54. * @param $form_items 表单项数组
  55. * @return $this
  56. *
  57. */
  58. public function setExtraItems($extra_items)
  59. {
  60. $this->_extra_items = $extra_items;
  61. return $this;
  62. }
  63. /**
  64. * 设置表单提交地址
  65. * @param $url 提交地址
  66. * @return $this
  67. *
  68. */
  69. public function setPostUrl($post_url)
  70. {
  71. $this->_post_url = $post_url;
  72. return $this;
  73. }
  74. /**
  75. * 加入一个表单项
  76. * @param $type 表单类型(取值参考系统配置FORM_ITEM_TYPE)
  77. * @param $title 表单标题
  78. * @param $tip 表单提示说明
  79. * @param $name 表单名
  80. * @param $options 表单options
  81. * @param $extra 额外自定义项目
  82. * @param $extra_attr 表单项额外属性
  83. * @return $this
  84. *
  85. */
  86. public function addFormItem($name, $type, $title, $tip, $options = array(), $extra = '', $extra_attr = '')
  87. {
  88. $item['name'] = $name;
  89. $item['type'] = $type;
  90. $item['title'] = $title;
  91. $item['tip'] = $tip;
  92. $item['options'] = $options;
  93. if (is_array($extra)) {
  94. $item['extra']['class'] = $extra['class'];
  95. $item['extra']['self'] = $extra['self'];
  96. } else {
  97. $item['extra']['class'] = $extra;
  98. }
  99. $item['extra']['attr'] = $extra_attr;
  100. $item['value'] = '';
  101. $this->_form_items[] = $item;
  102. return $this;
  103. }
  104. /**
  105. * 设置表单表单数据
  106. * @param $form_data 表单数据
  107. * @return $this
  108. *
  109. */
  110. public function setFormData($form_data)
  111. {
  112. $this->_form_data = $form_data;
  113. return $this;
  114. }
  115. /**
  116. * 设置额外功能代码
  117. * @param $extra_html 额外功能代码
  118. * @return $this
  119. *
  120. */
  121. public function setExtraHtml($extra_html)
  122. {
  123. $this->_extra_html = $extra_html;
  124. return $this;
  125. }
  126. /**
  127. * 设置提交方式
  128. * @param $title 标题文本
  129. * @return $this
  130. *
  131. */
  132. public function setAjaxSubmit($ajax_submit = true)
  133. {
  134. $this->_ajax_submit = $ajax_submit;
  135. return $this;
  136. }
  137. /**
  138. * 确定按钮文本自定义
  139. * @param $submit_title 确定按钮文本
  140. * @return $this
  141. *
  142. */
  143. public function setSubmitTitle($submit_title)
  144. {
  145. $this->_submit_title = $submit_title;
  146. return $this;
  147. }
  148. /**
  149. * 设置页面模版
  150. * @param $template 模版
  151. * @return $this
  152. *
  153. */
  154. public function setTemplate($template)
  155. {
  156. $this->_template = $template;
  157. return $this;
  158. }
  159. /**
  160. * 显示页面
  161. *
  162. */
  163. public function display($template = '', $charset = '', $contentType = '', $content = '', $prefix = '')
  164. {
  165. //额外已经构造好的表单项目与单个组装的的表单项目进行合并
  166. $this->_form_items = array_merge($this->_form_items, $this->_extra_items);
  167. //编译表单值
  168. if ($this->_form_data) {
  169. foreach ($this->_form_items as &$item) {
  170. if (isset($item['name']) && isset($this->_form_data[$item['name']])) {
  171. $item['value'] = $this->_form_data[$item['name']];
  172. }
  173. }
  174. }
  175. $this->assign('meta_title', $this->_meta_title); //页面标题
  176. $this->assign('tab_nav', $this->_tab_nav); //页面Tab导航
  177. $this->assign('post_url', $this->_post_url); //标题提交地址
  178. $this->assign('form_items', $this->_form_items); //表单项目
  179. $this->assign('form_data', $this->_form_data); //表单项目默认值
  180. $this->assign('ajax_submit', $this->_ajax_submit); //额外HTML代码
  181. $this->assign('submit_title', $this->_submit_title); //确定按钮文本自定义
  182. $this->assign('extra_html', $this->_extra_html); //是否ajax提交
  183. // 显示页面
  184. $template = CONTROLLER_NAME . '/' . ACTION_NAME;
  185. if (is_file($this->view->parseTemplate($template))) {
  186. parent::display();
  187. } else {
  188. $this->assign('is_builder', 'form'); // Builder标记
  189. parent::display($this->_template);
  190. }
  191. }
  192. }