Heim  >  Artikel  >  Backend-Entwicklung  >  DBO Form Widget

DBO Form Widget

WBOY
WBOYOriginal
2016-07-25 09:09:461215Durchsuche
这是一个十分有用的组件,可用于生成一个数据的编辑form,他是MST Library 3.1一个十分重要的组件,可以实现dbo form和dbo form的循环嵌套,而且控制在一个form中,同时支持dbo form中再次嵌套自定义的widget组件。
很多PHP框架,将form的生成写在函数,这是无可厚非的,但是你无法接收在生成一个form的时候,循环调用用户函数所付出的性能成本,这时候,构建一个php和html混编的代码,远比循环执行函数的性能要高。
而且,多数时候,我们知道一个数据对象的结构,也已经定义了数据对象的每个字段的值类型,我们真的没有必要再去手动写什么form,或者还要用代码去判断,这个用select,那个用textarea什么的。
  1. // 要操作的對象的數據
  2. // $target必須為基於MST_DBO的實例
  3. if (!isset($data) || !is_object($data) || !$data instanceof MST_DBO) {
  4. echo '$data not a MST_DBO instance!';
  5. }
  6. else {
  7. // 獲取關聯的模塊
  8. $model = get_class($data);
  9. // 定義$columns
  10. // 如果未定義,默認根據MST_DBO的接口來取
  11. if (!isset($columns))
  12. $columns = $data->getFormColumns($data);
  13. if (empty($columns) || !is_array($columns)) {
  14. echo 'undefine form columns!';
  15. }
  16. else {
  17. // 生成該模塊的前綴
  18. if (!isset($prefix))
  19. $prefix = strtolower($model);
  20. else
  21. $prefix = MST_String::tableize($prefix);
  22. if (!isset($id))
  23. $id = $prefix . '_form';
  24. if (!isset($class))
  25. $class = $prefix . '-form';
  26. $errors = $data->getErrors();
  27. // 初始化Form配置
  28. // 定制提交的action
  29. if (!isset($action))
  30. $action = $this->params->uri;
  31. // method
  32. if (!isset($method))
  33. $method = 'post';
  34. else {
  35. $method = strtolower((string)$method);
  36. if ($method != 'get' && $method != 'post')
  37. $method = 'post';
  38. }
  39. // 是否需要上傳
  40. if (!isset($isUpload)) $isUpload = true;
  41. // 定制提交按鈕的文字
  42. if (!isset($submitText)) $submitText = 'Submit';
  43. // 定制label部分的寬度
  44. if (!isset($headWidth)) $headWidth = 130;
  45. $headWidth = is_numeric($headWidth) && $headWidth > 0 ? $headWidth : 120;
  46. if (!isset($continueForm)) $continueForm = false;
  47. // 重載
  48. if (!isset($lineStart)) $lineStart = 1;
  49. ?>
  50. >
  51. $lineNum = $lineStart;
  52. $tinymceTimes = 0;
  53. foreach ($columns as $key => $column) {
  54. // 跳過$column[0]為空的部分
  55. if (!isset($column[0])) continue;
  56. // 定制一行的$column常用變量
  57. $key = strtolower(trim($key));
  58. $columnId = $prefix . '_' . $key;
  59. $columnName = $prefix . '[' . $key . ']';
  60. $columnClass = $prefix . '-' . $key;
  61. $columnText = empty($column['title']) ? ucfirst($key) : $column['title'];
  62. $columnValue = isset($data[$key]) ? (string)$data[$key] : (isset($column['default']) ? $column['default'] : null);
  63. if (isset($column['forceValue'])) $columnValue = (string)$column['forceValue'];
  64. $isDisabled = isset($column['disabled']) ? $column['disabled'] : false;
  65. $isReadonly = isset($column['readonly']) ? $column['readonly'] : false;
  66. if (is_object($column[0]) && $column[0] instanceof Closure) {
  67. $editType = 'closure';
  68. }
  69. else {
  70. $editType = strtolower($column[0]);
  71. }
  72. ?>
  73. if ($editType == 'dbo_form') {
  74. if (empty($column[1])) {
  75. // 缺少dbo_form重用說明
  76. ?>
  77. } else {
  78. $column[1]['continueForm'] = true;
  79. $this->widget('base/dbo_form', $column[1]);
  80. }
  81. ?>
  82. $lineClass = $lineNum % 2 == 0 ? 'f-line f-line-odd' : 'f-line f-line-even';
  83. ?>
  84. }
  85. ?>
  86. Can't reuse dbo_form, please set the dbo_form options in $column[1].
  87. widget($column[1], $column[2]); ?>
  88. // 開始生成form行
  89. switch ($editType) {
  90. case 'text' :
  91. ?>
  92. if (isset($column['max']))
  93. echo ' maxlength="',$column['max'],'"';
  94. if ($isDisabled)
  95. echo ' disabled="disabled"';
  96. if ($isReadonly)
  97. echo ' readonly="readonly"';
  98. ?>/>
  99. break;
  100. case 'smalltextarea' :
  101. ?>
  102. break;
  103. case 'longtext' :
  104. ?>
  105. if (isset($column['max']))
  106. echo ' maxlength="',$column['max'],'"';
  107. if ($isDisabled)
  108. echo ' disabled="disabled"';
  109. if ($isReadonly)
  110. echo ' readonly="readonly"';
  111. ?>/>
  112. break;
  113. case 'upload' :
  114. ?>
  115. break;
  116. case 'password' :
  117. ?>
  118. if (isset($column['max']))
  119. echo ' maxlength="'.$column['max'].'"';
  120. if ($isDisabled)
  121. echo ' disabled="disabled"';
  122. if ($isReadonly)
  123. echo ' readonly="readonly"';
  124. ?>/>
  125. break;
  126. case 'textarea' :
  127. ?>
  128. break;
  129. case 'select' :
  130. $options = isset($column['options']) ? $column['options'] : array();
  131. $optionsType = isset($column['optionsType']) ? $column['optionsType'] : 'map';
  132. // 向前兼容:0 -> map, 1 -> list
  133. if (is_numeric($optionsType)) $optionsType = $optionsType > 0 ? 'list' : 'map';
  134. ?>
  135. break;
  136. case 'radiogroup' :
  137. $maps = isset($column['maps']) ? $column['maps'] : array();
  138. $mapsType = isset($column['mapsType']) ? $column['mapsType'] : 'map';
  139. // 向前兼容:0 -> map, 1 -> list
  140. if (is_numeric($mapsType)) $mapsType = $mapsType > 0 ? 'list' : 'map';
  141. ?>
  142. $index = 0;
  143. foreach ($maps as $key => $map) {
  144. $index++;
  145. ?>
  146. " class="f-radio" value="" />
  147. " class="f-radio" value="" />
  148. }
  149. ?>
  150. break;
  151. case 'datetime':
  152. $format = empty($column['dateFormat']) ? 'Y-m-d H:i:s' : $column['dateFormat'];
  153. $jsFormat = empty($column['jsFormat']) ? '%Y-%m-%d %H:00' : $column['jsFormat'];
  154. $pickSize = isset($column['pickSize']) ? intval($column['pickSize']) : 2;
  155. if ($columnValue > 0) {
  156. if (is_numeric($columnValue)) {
  157. $dtStr = date($format, $columnValue);
  158. $dtVal = $columnValue;
  159. }
  160. else {
  161. $dtStr = $columnValue;
  162. $dtVal = MST_String::date2num($columnValue);
  163. }
  164. }
  165. else {
  166. $dtStr = 0;
  167. $dtVal = 0;
  168. }
  169. if (is_numeric($dtVal) && $dtVal > 0)
  170. $dtStr = date($format, $dtVal);
  171. ?>
  172. type="text"
  173. id="_str"
  174. class="f-text f-dt "
  175. value=""
  176. />
  177. type="hidden"
  178. name=""
  179. id=""
  180. value=""
  181. />
  182. break;
  183. case 'tinymce' :
  184. $tinymceTimes++;
  185. ?>
  186. script('tiny_mce/tiny_mce.js'); ?>
  187. break;
  188. case 'widget' :
  189. $this->widget($column[1], $column[2]);
  190. break;
  191. }
  192. $lineNum++;
  193. ?>
  194. $index = 0;
  195. foreach ($submitText as $btnId => $btnText) {
  196. $index++;
  197. ?>
  198. validCode($prefix, 'input'); ?>
  199. }
  200. }
  201. ?>
复制代码
  1. $this->widget('base/dbo_form', array(
  2. 'data' => $this->list,
  3. ));
复制代码
  1. class Testimonial extends MST_DBO {
  2. protected static
  3. $columns = array(
  4. 'firstname' => array('text','title' => 'First Name', 'require' => 1, 'min' => 1, 'max' => 32),
  5. 'lastname' => array('text','title' => 'Last Name', 'require' => 1, 'min' => 1, 'max' => 32),
  6. 'avator' => array('title' => 'Avator', 'max' => 256),
  7. 'age_group' => array('title' => 'Age Group', 'require' => 1),
  8. 'secret' => array('textarea','title' => 'Secret', 'require' => 1, 'min' => 10, 'max' => 600),
  9. );
  10. public function getFormColumns() {
  11. if (GB_PERSSIONS == Region::ROOT) {
  12. $columns['region_id'] = array(
  13. 'select',
  14. 'title' => ' region ',
  15. 'optionsType' => 'list',
  16. 'options' => Region::find('all', array('select' => 'id, name')),
  17. );
  18. }
  19. else {
  20. $columns['region_id'] = array(
  21. 'hidden',
  22. 'default' => GB_PERSSIONS,
  23. );
  24. }
  25. $columns = array_merge($columns,self::$columns);
  26. $columns['age_group'] = array('widget', 'base/age_group', array(
  27. 'prefix' => 'testimonial',
  28. ), 'title' => 'Age Group');
  29. $columns['avator'] = array('widget', 'base/testmonial_upload', array(
  30. 'prefix' => 'testimonial',
  31. ), 'title' => 'Avator');
  32. return $columns;
  33. }
  34. public function beforeCreate(& $data) {
  35. $data['created_at'] = time();
  36. }
  37. public function getAge() {
  38. $ageGroup = array(
  39. 0 => '--',
  40. 1 => 'Under 18',
  41. 2 => '19 ? 25',
  42. 3 => '26 ? 35',
  43. 4 => '36 ? 45',
  44. 5 => '46 ? 55',
  45. 6 => '56 or above',
  46. );
  47. return isset($ageGroup[$this['age_group']]) ? $ageGroup[$this['age_group']] : $ageGroup[0];
  48. }
  49. public function getAvator() {
  50. return empty($this['avator']) ? httpUri('images/avator.png') : httpUri($this['avator']);
  51. }
  52. // 这是对MST_DBO的find的方法的重载
  53. static public function find($args = array(), $params = null, $isArray = false) {
  54. if (defined('GB_PERSSIONS') && GB_PERSSIONS == Region::ROOT) {
  55. self::initFind($args, $params, $isArray);
  56. return parent::find($args, $params, $isArray);
  57. }
  58. else {
  59. self::initFind($args, $params, $isArray);
  60. if (isset($args['where'])) {
  61. $args['where'][0] .= ' AND region_id = ?';
  62. $args['where'][] = GB_PERSSIONS;
  63. }
  64. else {
  65. $args['where'] = array('region_id = ?', GB_PERSSIONS);
  66. }
  67. return parent::find($args, $params, $isArray);
  68. }
  69. }
  70. }
复制代码
DBO Form Widget


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn