DBO Form Widget

WBOY
WBOYOriginal
2016-07-25 09:09:461234browse
This is a very useful component that can be used to generate a data editing form. It is a very important component of MST Library 3.1. It can realize the loop nesting of dbo form and dbo form, and control it in one form. It also supports dbo The custom widget component is nested again in the form.
Many PHP frameworks write form generation in functions. This is understandable, but you cannot accept the performance cost of cyclically calling user functions when generating a form. At this time, build a mixed PHP and HTML Code, the performance is much higher than the function of loop execution.
Moreover, most of the time, we know the structure of a data object and have defined the value type of each field of the data object. We really don’t need to manually write any form, or use code to judge. This uses select , that uses textarea or something.
  1. //Data of the object to be operated
  2. // $target must be an instance based on 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. // Get the associated module
  8. $model = get_class($data);
  9. // Define $columns
  10. // If not Definition, taken by default according to the MST_DBO interface
  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. // Generate the prefix of this module
  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. // Initialize Form configuration
  28. // Customize submitted 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. // Do you need to upload? if (!isset($isUpload)) $isUpload = true;
  40. // Customize the text of the submit button
  41. if (!isset($submitText)) $submitText = 'Submit';
  42. // Customize the width of the label part
  43. if (!isset($headWidth)) $headWidth = 130;
  44. $ headWidth = is_numeric($headWidth) && $headWidth > 0 ? $headWidth : 120;
  45. if (!isset($continueForm)) $continueForm = false;
  46. // Overloading
  47. if (!isset($lineStart )) $lineStart = 1;
  48. ?>
  49. >
  50. < ?php } ?>
  51. $lineNum = $lineStart;
  52. $tinymceTimes = 0;
  53. foreach ($columns as $key => $column) {
  54. // Skip $column[0] as Empty part
  55. if (!isset($column[0])) continue;
  56. // Customize a row of common $column variables
  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. // Missing dbo_form reuse instructions
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn