00001 <?php
00002
00059 class TKTable extends TKContainer {
00060 protected $numRows = 0;
00061
00067 public function __construct($autoFirstRow = true) {
00068 parent::__construct();
00069
00070 $this->addProperty('cell_class', self::PROP_CLASS);
00071 $this->addProperty('cell_style', self::PROP_STYLE);
00072 $this->addProperty('header_class', self::PROP_CLASS);
00073 $this->addProperty('header_style', self::PROP_STYLE);
00074 $this->addProperty('footer_class', self::PROP_CLASS);
00075 $this->addProperty('footer_style', self::PROP_STYLE);
00076 $this->addProperty('row_ids', self::PROP_ARRAY);
00077 $this->addProperty('row_classes', self::PROP_CLASS);
00078 $this->addProperty('tbody_id', self::PROP_STRING);
00079
00080 $this->addSlot('header');
00081 $this->addSlot('footer');
00082 if($autoFirstRow)
00083 $this->addRow();
00084 }
00085
00094 public function addRow($id = "", $class = "") {
00095 $this->addSlot('row' . $this->numRows, array('type' => ""));
00096 $this->setDefaultSlot('row' . $this->numRows);
00097 if($id != "")
00098 $this->row_ids[$this->numRows] = $id;
00099 else
00100 $this->row_ids[$this->numRows] = $this->getId() . '_row_' . $this->numRows;
00101 if($class != '')
00102 $this->row_classes[$this->numRows] = $class;
00103 else
00104 $this->row_classes[$this->numRows] = '';
00105 $this->numRows++;
00106 }
00107
00114 public function getRowId($rowIdx) {
00115 if($rowIdx < 0 || $rowIdx >= count($this->row_ids))
00116 return false;
00117 return $this->row_ids[$rowIdx];
00118 }
00119
00129 public function addData($data) {
00130 foreach($data as $name => $value) {
00131 if(is_string($name))
00132 $this->addText($name);
00133 if(is_array($value)) {
00134 foreach($value as $v)
00135 $this->addText($v);
00136 }
00137 else
00138 $this->addText($value);
00139 $this->addRow();
00140 }
00141 }
00142
00144 protected function renderContainer($class, $style, $events, $id) {
00145 $collect = "<table $id$class[0]$style[0]$events>\n";
00146 $collect .= $this->renderHeader($class, $style);
00147 $collect .= $this->renderBody($class, $style);
00148 $collect .= $this->renderFooter($class, $style);
00149 $collect .= "</table>\n";
00150
00151 return $collect;
00152 }
00153
00155 protected function renderHeader($class, $style) {
00156 $collect = "";
00157 $header = $this->getChildrenBySlot('header');
00158 if(count($header) > 0) {
00159 $collect .= "<thead>\n<tr>";
00160 foreach($header as $id => $c) {
00161 $params = $this->getChildParams('header', $id);
00162 if(isset($params['colspan']))
00163 $colspan = " colspan=\"$params[colspan]\"";
00164 else
00165 $colspan = "";
00166 $html = is_object($c) ? $c->render() : $c;
00167 $ts = isset($params['style']) ?
00168 Toolkit::styleToString($params['style']) : "";
00169 $tc = isset($params['class']) ?
00170 Toolkit::classToString($params['class']) : "";
00171 $collect .= "<th$class[header]$style[header]$tc$ts$colspan>$html</th>";
00172 }
00173 $collect .= "</tr>\n</thead>\n";
00174 }
00175 return $collect;
00176 }
00177
00179 protected function renderBody($class, $style) {
00180 if($this->numRows == 0)
00181 return '';
00182 $tbodyId = $this->get('tbody_id');
00183 if($tbodyId != '')
00184 $collect = '<tbody id="' . $tbodyId . '">\n';
00185 else
00186 $collect = "<tbody>\n";
00187 $rowIds = $this->get('row_ids');
00188 $rowClasses = $this->get('row_classes');
00189 for($i = 0; $i < $this->numRows; $i++)
00190 $collect .= $this->renderRow($class, $style, $rowIds, $rowClasses, $i);
00191 $collect .= "</tbody>\n";
00192 return $collect;
00193 }
00194
00195 protected function renderRow($class, $style, $rowIds, $rowClasses, $rowIdx) {
00196 if(isset($rowIds[$rowIdx]))
00197 $rowId = " id=\"$rowIds[$rowIdx]\"";
00198 else
00199 $rowId = "";
00200 if(isset($rowClasses[$rowIdx]))
00201 $rowClass = " class=\"$rowClasses[$rowIdx]\"";
00202 else
00203 $rowClass = '';
00204
00205 $collect = "<tr$rowId$rowClass>";
00206 $comps = $this->getChildrenBySlot("row$rowIdx");
00207 $pos = 0;
00208 foreach($comps as $id => $comp) {
00209 $collect .= $this->renderCell($class, $style, $rowIdx, $comp, $id, $pos);
00210 $pos++;
00211 }
00212 $collect .= "</tr>\n";
00213
00214 return $collect;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 protected function renderCell($class, $style, $rowIdx, $comp, $id, $pos) {
00232 $html = is_object($comp) ? $comp->render() : $comp;
00233 $params = $this->getChildParams("row$rowIdx", $id);
00234 if(isset($params['colspan']))
00235 $cs = " colspan=\"$params[colspan]\"";
00236 else
00237 $cs = "";
00238 if (isset($params['rowspan']))
00239 $rs = " rowspan=\"$params[rowspan]\"";
00240 else
00241 $rs = "";
00242 if (isset($params['valign']))
00243 $va = " valign=\"$params[valign]\"";
00244 else
00245 $va = "";
00246 $ts = isset($params['style']) ? Toolkit::styleToString($params['style']) : "";
00247 $tc = isset($params['class']) ? Toolkit::classToString($params['class']) : "";
00248 $collect = "<td$class[cell]$style[cell]$ts$tc$cs$rs$va>$html</td>";
00249 return $collect;
00250 }
00251
00253 protected function renderFooter($class, $style) {
00254 $collect = "";
00255 $footer = $this->getChildrenBySlot('footer');
00256 if(count($footer) > 0) {
00257 $collect .= "<tfoot>\n<tr>";
00258 foreach($footer as $id => $c) {
00259 $html = is_object($c) ? $c->render() : $c;
00260 $ts = isset($params['style']) ?
00261 Toolkit::styleToString($params['style']) : "";
00262 $tc = isset($params['class']) ?
00263 Toolkit::classToString($params['class']) : "";
00264 $collect .= " <td$class[footer]$style[footer]$tc$ts>$html</td>";
00265 }
00266 $collect .= "</tr>\n</tfoot>\n";
00267 }
00268 return $collect;
00269 }
00270
00272 public static function createFromXML($attrs, $contents, $node) {
00273 $obj = new TKTable();
00274 $children = $node['children'];
00275 self::convertStdAttributes($obj, $attrs);
00276 self::convertStdEvents($obj, $node);
00277 $addingTo = 'main';
00278 foreach($children as $child) {
00279 if($child['tag'] == 'row') {
00280 $obj->addRow();
00281 $addingTo = 'main';
00282 }
00283 else if($child['tag'] == 'header') {
00284 $addingTo = 'header';
00285 }
00286 else if($child['tag'] == 'footer') {
00287 $addingTo = 'footer';
00288 }
00289 else {
00290 $nobj = self::convertChild($child);
00291 if($nobj !== null)
00292 $obj->add($nobj, $addingTo);
00293 }
00294 }
00295
00296 return $obj;
00297 }
00298 }
00299
00300 ?>