formsgen.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. class InputForm {
  3. var $options = array();
  4. var $_rows;
  5. var $_hiddens;
  6. var $_elements = array(
  7. 'hidden' => '<input type="hidden" name="%1" value="%2" />',
  8. 'file' => '<input type="file" name="%1" value="" />',
  9. );
  10. public function __construct($action = '', $method = 'get', $submit = 'Submit', $reset = '', $target = '', $enctype = '', $name = '', $events = '') {
  11. $this->InputForm($action, $method, $submit, $reset, $target, $enctype, $name, $events);
  12. }
  13. public function InputForm($action = '', $method = 'get', $submit = 'Submit', $reset = '', $target = '', $enctype = '', $name = '', $events = '') {
  14. $this->options = array(
  15. 'action' => $action,
  16. 'method' => $method,
  17. 'target' => $target,
  18. 'enctype' => $enctype,
  19. 'events' => $events,
  20. 'submit' => $submit,
  21. 'name' => $name,
  22. 'reset' => $reset,
  23. );
  24. }
  25. function addrow($title, $contents = '', $valign = 'middle', $align = 'left') {
  26. @list( $talign, $calign ) = explode(",", $align);
  27. if (empty($calign))
  28. $calign = $talign;
  29. @list( $tvalign, $cvalign ) = explode(",", $valign);
  30. if (empty($cvalign))
  31. $cvalign = $tvalign;
  32. $this->_rows[] = array(
  33. 'title' => $title,
  34. 'contents' => $contents,
  35. 'title_valign' => $tvalign,
  36. 'content_valign' => $cvalign,
  37. 'title_align' => $talign,
  38. 'content_align' => $calign
  39. );
  40. end($this->_rows);
  41. return key($this->_rows);
  42. }
  43. function hidden($name, $value) {
  44. $this->_hiddens[$name] = $value;
  45. }
  46. function addbreak($break = "&nbsp;") {
  47. $this->_rows[] = array('break' => $break);
  48. end($this->_rows);
  49. return key($this->_rows);
  50. }
  51. function addmessage($message) {
  52. $this->_rows[] = array("message" => $message);
  53. }
  54. function show($return = false) {
  55. $result = '<form action="' . $this->options['action'] . '" method="' . $this->options['method'] . '" name="' . $this->options['name'] . '"';
  56. if (!empty($this->options['target'])) {
  57. $result .= ' target="' . $this->options['target'] . '"';
  58. }
  59. if (!empty($this->options['enctype'])) {
  60. $result .= ' enctype="' . $this->options['enctype'] . '"';
  61. }
  62. if (!empty($this->options['events'])) {
  63. $result .= ' ' . $this->options['events'];
  64. }
  65. $result .= '>' . "\n";
  66. if (is_array($this->_hiddens)) {
  67. foreach ($this->_hiddens as $name => $value) {
  68. $result .= str_replace(array('%1', '%2'), array($name, $value), $this->_elements['hidden']) . "\n";
  69. }
  70. }
  71. $result .= '<table border="0" cellspacing="2" cellpadding="2" width="100%">' . "\n";
  72. if (is_array($this->_rows)) {
  73. foreach ($this->_rows as $row) {
  74. if (!empty($row['break'])) {
  75. $result .= '<tr>' . "\n";
  76. $result .= ' <th colspan="2">' . $row['break'] . '</td>' . "\n";
  77. $result .= '</tr>' . "\n";
  78. } elseif (!empty($row['message'])) {
  79. $result .= '<tr>' . "\n";
  80. $result .= ' <td colspan="2" class="row1">' . $row['message'] . '</td>' . "\n";
  81. $result .= '</tr>' . "\n";
  82. } else {
  83. $result .= '<tr>' . "\n";
  84. $result .= ' <td valign="' . $row['title_valign'] . '" align="' . $row['title_align'] . '" class="row2" ' . ((empty($row['contents'])) ? ' colspan="2"' : '') . '>' . $row['title'] . '</td>' . "\n";
  85. if (!empty($row['contents'])) {
  86. $result .= ' <td valign="' . $row['title_valign'] . '" align="' . $row['title_align'] . '" class="row3">' . $row['contents'] . '</td>' . "\n";
  87. }
  88. $result .= '</tr>' . "\n";
  89. }
  90. }
  91. }
  92. $result .= '<tr>' . "\n";
  93. $result .= ' <td align="center" colspan="2"><input type="submit" value="' . $this->options['submit'] . '" class="ubButton">';
  94. if (!empty($this->options['reset'])) {
  95. $result .= '<input type="reset" value="' . $this->options['reset'] . '" class="ubButton">';
  96. }
  97. $result .= '</td>' . "\n";
  98. $result .= '</tr>' . "\n";
  99. $result .= '</table>' . "\n";
  100. $result .= '</form>' . "\n";
  101. if ($return) {
  102. return $result;
  103. } else {
  104. echo $result;
  105. return true;
  106. }
  107. }
  108. function text_box($name, $value, $size = 0, $maxlength = 0, $password = false, $extra = '') {
  109. return '<input type="' . (($password) ? 'password' : 'text') . '" class="text" name="' . $name . '"' . (($size > 0) ? ' size="' . $size . '"' : '') . (($maxlength > 0) ? ' maxlength="' . $maxlength . '"' : '') . ' value="' . htmlspecialchars($value) . '" ' . $extra . '>';
  110. }
  111. function textarea($name, $value, $cols = 30, $rows = 5, $extra = '') {
  112. return '<textarea name="' . $name . '" cols="' . $cols . '" rows="' . $rows . '" ' . $extra . '>' . htmlspecialchars($value) . '</textarea>';
  113. }
  114. function select_tag($name, $values, $selected = '', $extra = '') {
  115. $data = '<select name="' . $name . '" ' . $extra . '>' . "\n";
  116. foreach ($values as $value => $text) {
  117. $data .= '<option value="' . $value . '" ' . (($selected == $value) ? 'selected' : '') . '>' . __($text) . '</option>' . "\n";
  118. }
  119. $data .= '</select> ' . "\n";
  120. return $data;
  121. }
  122. function radio_button($name, $values, $selected = '', $separator = ' ', $extra = '') {
  123. $data = '';
  124. foreach ($values as $value => $text) {
  125. $id = rcms_random_string(5);
  126. $data .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="' . $id . '" ' . (($selected == $value) ? 'checked' : '') . ' ' . $extra . '><label for="' . $id . '">' . $text . '</label>' . $separator;
  127. }
  128. return $data;
  129. }
  130. function radio_button_single($name, $value, $selected = '', $caption = ' ', $extra = '') {
  131. $id = rcms_random_string(5);
  132. return '<input type="radio" name="' . $name . '" value="' . $value . '" id="' . $id . '" ' . (($selected) ? 'checked' : '') . ' ' . $extra . '><label for="' . $id . '">' . $caption . '</label>';
  133. }
  134. function checkbox($name, $value, $caption, $checked = 0, $extra = '') {
  135. $id = rcms_random_string(5);
  136. return '<input type="checkbox" name="' . $name . '" value="' . $value . '" id="' . $id . '" ' . ((!empty($checked)) ? 'checked' : '') . ' ' . $extra . ' /><label for="' . $id . '">' . $caption . '</label>';
  137. }
  138. function file($name) {
  139. return '<input type="file" name="' . $name . '" value="" />';
  140. }
  141. }