/htdocs/class/xoopsform/-gridform.php

https://github.com/Doap/xoops · PHP · 172 lines · 120 code · 18 blank · 34 comment · 15 complexity · ba63db1fe253df28874f937700ec2356 MD5 · raw file

  1. <?php
  2. /**
  3. * XOOPS Framework
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  13. * @license BSD liscense
  14. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  15. * @since 3.0
  16. * @package xoops
  17. * @version $Id$
  18. */
  19. global $xoops;
  20. include_once $xoops->path("www") . "/class/xoopsform/form.php";
  21. class XoopsGridForm extends XoopsForm
  22. {
  23. private $cols;
  24. private $rows;
  25. private $widths;
  26. private $head = array();
  27. public function setCols($cols)
  28. {
  29. $this->cols = $cols;
  30. }
  31. public function getCols()
  32. {
  33. return isset($this->cols) ? $this->cols : count($this->getHead());
  34. }
  35. public function setRows($rows)
  36. {
  37. $this->rows = $rows;
  38. }
  39. public function getRows()
  40. {
  41. return $this->rows;
  42. }
  43. public function addHead($head)
  44. {
  45. $this->head[] = $head;
  46. }
  47. public function setHead($head = array())
  48. {
  49. $this->head = $head;
  50. }
  51. public function getHead()
  52. {
  53. return $this->head;
  54. }
  55. public function setWidths($widths = array())
  56. {
  57. $this->widths = $widths;
  58. }
  59. public function getWidths()
  60. {
  61. if (isset($this->widths)) {
  62. return $this->widths;
  63. }
  64. $cols = $this->getCols();
  65. return array_fill(0, $cols, 100 / $cols);
  66. }
  67. /**
  68. * Insert an empty row in the table to serve as a seperator.
  69. *
  70. * @param string $extra HTML to be displayed in the empty row.
  71. * @param string $class CSS class name for <td> tag
  72. */
  73. public function insertBreak($extra = '', $class= '')
  74. {
  75. $class = ($class != '') ? " class='" . htmlspecialchars($class, ENT_QUOTES) . "'" : '';
  76. $extra = "<tr><td colspan='" . $this->getCols() . "' {$class}>" . (empty($extra) ? " " : $extra) . "</td></tr>";
  77. $this->addElement($extra);
  78. }
  79. /**
  80. * create HTML to output the form as a theme-enabled table with validation.
  81. *
  82. * @return string
  83. */
  84. public function render()
  85. {
  86. $ele_name = $this->getName();
  87. $ret = "<form name='{$ele_name}' id='{$ele_name}' action='" . $this->getAction() . "' method='" . $this->getMethod() . "' onsubmit='return xoopsFormValidate_{$ele_name}();'" . $this->getExtra() . ">";
  88. $ret .="<table width='100%' class='outer' cellspacing='1'>";
  89. $ret .="<caption>" . $this->getTitle() . "</caption>";
  90. $widths = $this->getWidths();
  91. if ($head = $this->getHead()) {
  92. $ret .= "<thead><tr class='head'>";
  93. $cols = $this->getCols();
  94. for ($i = 0; $i < $cols; $i++) {
  95. $ret .= "<th width='" . $widths[$i]. "%'>" . (isset($head[$i]) ? $head[$i] : " ") . "</th>";
  96. }
  97. $ret .= "</tr></thead>";
  98. $widths = null;
  99. }
  100. if ($description = $this->getDescription()) {
  101. $ret .= "<tfoot><tr class='foot'><td colspan='" . $this->getCols() . "'>{$description}</td></tr></tfoot>";
  102. }
  103. $hidden = '';
  104. $class ='even';
  105. foreach ($this->getElements() as $ele) {
  106. if (!is_object($ele)) {
  107. $ret .= $ele;
  108. } elseif (!$ele->isHidden()) {
  109. if (!is_null($widths)) {
  110. $left = array_shift($widths);
  111. } else {
  112. $left = null;
  113. }
  114. $ret .= "<tr valign='top' align='left'>";
  115. $ret .= "<td class='head'";
  116. $ret .= (empty($left) ? "" : " {$left}%");
  117. $ret .= ">";
  118. if (($caption = $ele->getCaption()) != '') {
  119. $ret .=
  120. "<div class='xoops-form-element-caption" . ($ele->isRequired() ? "-required" : "") . "'>".
  121. "<span class='caption-text'>{$caption}</span>".
  122. "<span class='caption-marker'>*</span>".
  123. "</div>";
  124. }
  125. if (($desc = $ele->getDescription()) != '') {
  126. $ret .= "<div class='xoops-form-element-help'>{$desc}</div>";
  127. }
  128. $ret .= "</td>";
  129. if ($ele instanceof XoopsFormElementRow) {
  130. $ret .= $ele->render($this->getCols() - 1, $widths);
  131. } else {
  132. $ret .= "<td class='{$class}' colspan='" . ($this->getCols() - 1). "'>" . $ele->render() . "</td>";
  133. }
  134. $ret .= "</tr>\n";
  135. $widths = null;
  136. } else {
  137. $hidden .= $ele->render();
  138. }
  139. }
  140. $ret .= "</table>\n{$hidden}\n</form>\n";
  141. $ret .= $this->renderValidationJS(true);
  142. return $ret;
  143. }
  144. /**
  145. * assign to smarty form template instead of displaying directly
  146. *
  147. * @param object &$tpl reference to a {@link Smarty} object
  148. * @see Smarty
  149. */
  150. public function assign(&$tpl)
  151. {
  152. $tpl->assign($this->getName(), $this->render());
  153. }
  154. }