[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/framework/template/tags/data/ -> data_table.tag.php (source)

   1  <?php
   2  //--------------------------------------------------------------------------------
   3  // Copyright 2004 Procata, Inc.
   4  // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html)
   5  //--------------------------------------------------------------------------------
   6  /**
   7  * @package WACT_TAG
   8  * @author    Jason E. Sweat <jsweat_php AT yahoo DOT com>
   9  * @version $Id: data_table.tag.php,v 1.53 2004/11/18 04:22:48 jeffmoore Exp $
  10  */
  11  //--------------------------------------------------------------------------------
  12  if (!function_exists('clone_obj')) {
  13      require WACT_ROOT . 'util/phpcompat/clone.php';
  14  }
  15  
  16  /**
  17  * Register tags
  18  */
  19  TagDictionary::registerTag(new TagInfo('data:TABLE', 'DataTableTag'), __FILE__);
  20  TagDictionary::registerTag(new TagInfo('data:GROUP', 'DataGroupTag'), __FILE__);
  21  TagDictionary::registerTag(new TagInfo('data:COLUMN', 'DataColumnTag'), __FILE__);
  22  TagDictionary::registerTag(new TagInfo('data:CELL', 'DataCellTag'), __FILE__);
  23  TagDictionary::registerTag(new TagInfo('data:HEADER', 'DataHeaderTag'), __FILE__);
  24  TagDictionary::registerTag(new TagInfo('data:FOOTER', 'DataFooterTag'), __FILE__);
  25  TagDictionary::registerTag(new TagInfo('data:DEFAULT', 'DataDefaultTag'), __FILE__);
  26  
  27  /**
  28  * The compile time component to output a result set as an HTML table
  29  * @author    Jason E. Sweat <jsweat_php AT yahoo DOT com>
  30  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
  31  * @access protected
  32  * @package WACT_TAG
  33  */
  34  class DataTableTag extends ServerDataComponentTag {
  35      /**
  36      * File to include at runtime
  37      * @var string path to runtime component relative to WACT_ROOT
  38      * @access private
  39      */
  40      var $runtimeIncludeFile = 'template/components/data/data_table.inc.php';
  41      /**
  42      * Name of runtime component class
  43      * @var string
  44      * @access private
  45      */
  46      var $runtimeComponentName = 'DataTableComponent';
  47      /**
  48      * @param CodeWriter
  49      * @return void
  50      * @access protected
  51      */
  52  	function preGenerate(&$code) {
  53          parent::preGenerate($code);
  54          //$code->writePHP($this->getComponentRefCode() . '->prepare();');
  55  
  56          $att_list = '';
  57          foreach ($this->getTableAttributeMap() as $key => $value) {
  58              if ($att = $this->getAttribute($key)) {
  59                  $att_list .= ' '.$value.'="'.$att.'"';
  60              }
  61          }
  62          //row attributes
  63          $row_att_list = '';
  64          foreach ($this->getRowAttributeMap() as $key => $value) {
  65              if ($row_att = $this->getAttribute($key)) {
  66                  $row_att_list .= ' '.$value.'="'.$row_att.'"';
  67              }
  68          }
  69          if (trim($row_att_list)) {
  70              $code->writePHP($this->getComponentRefCode().'->setRowAttrib(');
  71              $code->writePHPLiteral($row_att_list);
  72              $code->writePHP(');');
  73          }
  74          
  75          //start output
  76          $code->writeHTML("<table $att_list >\n");
  77          $code->writePHP('if ('.$this->getDataSourceRefCode().'->next()) {'); 
  78              // write column setup
  79              $columns = $this->findChildrenByClass('DataColumnTag');
  80              foreach ($columns as $colkey => $colval) {
  81                  $columns[$colkey]->preGenerate($code);
  82                  // cell attributes
  83                  $col_att_list = '';
  84                  foreach ($this->getCellAttributeMap() as $key => $value) {
  85                      if ($col_att = $columns[$colkey]->getAttribute($key)) {
  86                          $col_att_list .= ' '.$value.'="'.$col_att.'"';
  87                      }
  88                  }
  89                  if (trim($col_att_list)) {
  90                      $code->writePHP($columns[$colkey]->getComponentRefCode().'->setAttrib(');
  91                      $code->writePHPLiteral($col_att_list);
  92                      $code->writePHP(');');
  93                  }            
  94              }
  95              $key = $code->getTempVarRef();
  96              $val = $code->getTempVarRef();
  97              // supress automatic column generation if autogen=FALSE
  98              if ($this->getBoolAttribute('autogen', TRUE)) {
  99                  $code->writePHP('foreach ('.$this->getDataSourceRefCode().'->export() as '.$key.' => '.$val.') {');
 100                      $code->writePHP($this->getComponentRefCode().'->addColumn("'.$key.'");');
 101                  $code->writePHP('}');
 102              }
 103              // group setup
 104              $groups = $this->findChildrenByClass('DataGroupTag');
 105              foreach ($groups as $grpkey => $obj) {
 106                  $groups[$grpkey]->preGenerate($code);
 107              }
 108              //table wide header
 109              if ($header =& $this->findImmediateChildByClass('DataHeaderTag')) {
 110                  $code->writeHTML('<tr><th colspan="');
 111                  $code->writePHP('echo '.$this->getComponentRefCode().'->colCount(),"\">";');
 112                  $header->generateNow($code);
 113                  $code->writeHTML('</th></tr>');
 114              }
 115              //column/group headers
 116              $code->writePHP($this->getComponentRefCode().'->genHeaders('.$this->getDataSourceRefCode().', $root);');
 117          $code->writePHP('}do{');
 118      }
 119      
 120      /**
 121      * @param CodeWriter
 122      * @return void
 123      * @access protected
 124      */
 125  	function generateContents(&$code) {
 126          //uncommenting this would cause comments inside the table to be output
 127          //as raw text in the HTML
 128          //parent::generateContents($code);
 129          
 130          //create cell content
 131          $code->writePHP($this->getComponentRefCode().'->renderRow($root);');
 132      }
 133  
 134      /**
 135      * @param CodeWriter
 136      * @return void
 137      * @access protected
 138      */
 139  	function postGenerate(&$code) {
 140          $code->writePHP('}while ('.$this->getDataSourceRefCode().'->next());'); 
 141          //column footers
 142          $code->writePHP($this->getComponentRefCode().'->genColFooters('.$this->getDataSourceRefCode().', $root);');
 143          $emptyChild =& $this->findChildByClass('DataDefaultTag');
 144          if ($emptyChild) {
 145              $code->writePHP('if (!'.$this->getComponentRefCode().'->RowCount) { ');
 146              $code->writeHTML('<tr><td>');
 147              $emptyChild->generateNow($code);
 148              $code->writeHTML('</td></tr>');
 149              $code->writePHP('}');
 150          }
 151          //table wide footer
 152          if ($footer =& $this->findImmediateChildByClass('DataFooterTag')) {
 153              $cell_type = ($footer->hasAttribute('heading')) ? 'th' : 'td';
 154              $code->writeHTML('<tr><'.$cell_type.' colspan="');
 155              $code->writePHP('echo '.$this->getComponentRefCode().'->colCount(),"\">";');
 156              $footer->generateNow($code);
 157              $code->writeHTML('</'.$cell_type.'></tr>');
 158          }
 159          
 160          $code->writeHTML("</table>\n");
 161          parent::postGenerate($code);
 162      }
 163  
 164      /**
 165      * return map of table attributes
 166      * @return array
 167      * @access private
 168      * @see http://www.w3.org/TR/html4/struct/tables.html#h-11.2.1
 169      */
 170  	function getTableAttributeMap() {
 171          //table attributes w3c 
 172          return array(
 173               'id'            => 'id'
 174              ,'tableclass'     => 'class'
 175              ,'border'        => 'border'
 176              ,'width'        => 'width'
 177              ,'summary'        => 'summary'
 178              ,'frame'        => 'frame'
 179              ,'rules'        => 'rules'
 180              ,'cellspacing'    => 'cellspacing'
 181              ,'cellpadding'    => 'cellpadding'
 182              ,'lang'            => 'lang'
 183              ,'dir'            => 'dir'
 184              ,'title'        => 'title'
 185              ,'style'        => 'style'
 186              ,'onclick'        => 'onclick'
 187              ,'ondblclick'    => 'ondblclick'
 188              ,'onmousedown'    => 'onmousedown'
 189              ,'onmouseup'    => 'onmouseup'
 190              ,'onmouseover'    => 'onmouseover'
 191              ,'onmousemove'    => 'onmousemove'
 192              ,'onmouseout'    => 'onmouseout'
 193              ,'onkeypress'    => 'onkeypress'
 194              ,'onkeydown'    => 'onkeydown'
 195              ,'onkeyup'        => 'onkeyup'
 196              ,'bgcolor'        => 'bgcolor'
 197              );
 198      }
 199      /**
 200      * return map of row attributes
 201      * @return array
 202      * @access private
 203      * @see http://www.w3.org/TR/html4/struct/tables.html#h-11.2.5
 204      */
 205  	function getRowAttributeMap() {
 206          //tr attributes per w3c 
 207          return array(
 208               //class and id skipped intentionally
 209               'rowstyle'            => 'style'
 210              ,'rowtitle'            => 'title'
 211              ,'rowlang'            => 'lang'
 212              ,'rowdir'             => 'dir'
 213              ,'rowonclick'        => 'onclick'    
 214              ,'rowondblclick'     => 'ondblclick' 
 215              ,'rowonmousedown'    => 'onmousedown'
 216              ,'rowonmouseup'      => 'onmouseup'  
 217              ,'rowonmouseover'    => 'onmouseover'
 218              ,'rowonmousemove'    => 'onmousemove'
 219              ,'rowonmouseout'     => 'onmouseout' 
 220              ,'rowonkeypress'     => 'onkeypress' 
 221              ,'rowonkeydown'      => 'onkeydown'  
 222              ,'rowonkeyup'        => 'onkeyup' 
 223              ,'align'             => 'align'  
 224              ,'char'              => 'char'   
 225              ,'charoff'           => 'charoff'
 226              ,'valign'            => 'valign'
 227              );
 228      }
 229      /**
 230      * return map of cell attributes
 231      * @return array
 232      * @access private
 233      * @see http://www.w3.org/TR/html4/struct/tables.html#h-11.2.6
 234      */
 235  	function getCellAttributeMap() {
 236          // td/th attributes per w3c 
 237          return array(
 238               //class, id, rowspan and colspan skipped intentionally
 239               'style'            => 'style'
 240              ,'title'            => 'title'
 241              ,'lang'                => 'lang'
 242              ,'dir'                 => 'dir'
 243              ,'onclick'            => 'onclick'    
 244              ,'ondblclick'         => 'ondblclick' 
 245              ,'onmousedown'        => 'onmousedown'
 246              ,'onmouseup'          => 'onmouseup'  
 247              ,'onmouseover'        => 'onmouseover'
 248              ,'onmousemove'        => 'onmousemove'
 249              ,'onmouseout'         => 'onmouseout' 
 250              ,'onkeypress'         => 'onkeypress' 
 251              ,'onkeydown'          => 'onkeydown'  
 252              ,'onkeyup'            => 'onkeyup' 
 253              ,'align'             => 'align'  
 254              ,'char'              => 'char'   
 255              ,'charoff'           => 'charoff'
 256              ,'valign'            => 'valign'
 257              ,'abbr'                => 'abbr'           
 258              ,'axis'                => 'axis'           
 259              ,'headers'            => 'headers'    
 260              ,'scope'            => 'scope'      
 261              );
 262      }
 263  }
 264  
 265  
 266  /**
 267  * The compile time component representing a group of columns
 268  * @author Jason E. Sweat
 269  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
 270  * @access protected
 271  * @package WACT_TAG
 272  */
 273  class DataGroupTag extends ServerComponentTag {
 274      /**
 275      * File to include at runtime
 276      * @var string path to runtime component relative to WACT_ROOT
 277      * @access private
 278      */
 279      var $runtimeIncludeFile = 'template/components/data/data_table.inc.php';
 280      /**
 281      * Name of runtime component class
 282      * @var string
 283      * @access private
 284      */
 285      var $runtimeComponentName = 'DataGroupComponent';
 286      /**
 287      * @return void
 288      * @access protected
 289      */
 290  	function CheckNestingLevel() {
 291          if ( !is_a($this->parent, 'DataTableTag')
 292              ) {
 293              RaiseError('compiler', 'MISSINGENCLOSURE', array(
 294                  'tag' => $this->tag,
 295                  'EnclosingTag' => 'data:TABLE',
 296                  'file' => $this->SourceFile,
 297                  'line' => $this->StartingLineNo));
 298          }
 299      }
 300      /**
 301      * @param CodeWriter
 302      * @return void
 303      * @access protected
 304      */
 305  	function preGenerate(&$code) {
 306          parent::preGenerate($code);
 307          //code to register columns here
 308          $columns = $this->findChildrenByClass('DataColumnTag');
 309          foreach ($columns as $key => $obj) {
 310              $code->writePHP($columns[$key]->getComponentRefCode().'->registerGroup('.$this->getComponentRefCode().');');
 311          }
 312          if (count($columns)) {
 313          $DataTable =& $this->findParentByClass('DataTableTag');
 314              if ($DataTable) {
 315                  $code->writePHP($DataTable->getComponentRefCode().'->registerGroup('.$this->getComponentRefCode().');');
 316                  $funct = $code->getTempVarRef();
 317                  if ($header =& $this->findChildByClass('DataHeaderTag')) {
 318                      $header_code = clone_obj($code);
 319                      $header_code->code = ''; 
 320                      $header->generateNow($header_code);
 321                      $header_code->writePHP('echo "";'); //make sure you end in php mode or the create_function will fail
 322                      $code->writePHP($funct.' = create_function(\'&'.$DataTable->getDataSourceRefCode().', &$root\', ');
 323                      $code->writePHPLiteral($header_code->code);
 324                      foreach($header_code->includeList as $file) { $code->registerInclude($file); }
 325                      unset($header_code);
 326                      $code->writePHP(');');
 327                      $code->writePHP($this->getComponentRefCode().'->setHeaderFunct('.$funct.');');
 328                  }
 329              }
 330          }
 331      }
 332  }
 333  
 334  /**
 335  * The compile time component representing common attributes for a column of table cells
 336  * @author Jason E. Sweat
 337  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
 338  * @access protected
 339  * @package WACT_TAG
 340  */
 341  class DataColumnTag extends ServerComponentTag {
 342      /**
 343      * File to include at runtime
 344      * @var string path to runtime component relative to WACT_ROOT
 345      * @access private
 346      */
 347      var $runtimeIncludeFile = 'template/components/data/data_table.inc.php';
 348      /**
 349      * Name of runtime component class
 350      * @var string
 351      * @access private
 352      */
 353      var $runtimeComponentName = 'DataColumnComponent';
 354      /**
 355      * @return void
 356      * @access protected
 357      */
 358  	function CheckNestingLevel() {
 359          if ( !(is_a($this->parent, 'DataTableTag')
 360              || is_a($this->parent, 'DataGroupTag'))) {
 361              RaiseError('compiler', 'MISSINGENCLOSURE', array(
 362                  'tag' => $this->tag,
 363                  'EnclosingTag' => 'data:TABLE',
 364                  'file' => $this->SourceFile,
 365                  'line' => $this->StartingLineNo));
 366          }
 367      }
 368      /**
 369      * @param CodeWriter
 370      * @return void
 371      * @access protected
 372      */
 373  	function preGenerate(&$code) {
 374          parent::preGenerate($code);
 375          if ( !$this->getAttribute('name')) {
 376              RaiseError('compiler', 'MISSINGREQUIREATTRIBUTE', array(
 377                  'tag' => $this->tag,
 378                  'attribute' => 'name',
 379                  'file' => $this->SourceFile,
 380                  'line' => $this->StartingLineNo));
 381          } else {
 382  
 383              parent::preGenerate($code);
 384              if ($this->hasAttribute('hide')) {
 385                  $code->writePHP($this->getComponentRefCode().'->hide();');
 386              }
 387              if ($this->hasAttribute('heading')) {
 388                  $code->writePHP($this->getComponentRefCode().'->outputTh();');
 389              }
 390              if ($this->hasAttribute('label')) {
 391                  $code->writePHP($this->getComponentRefCode().'->setLabel(');
 392                  $code->writePHPLiteral($this->getAttribute('label'));
 393                  $code->writePHP(');');
 394              }
 395              $DataTable =& $this->findParentByClass('DataTableTag');
 396              if ($DataTable) {
 397                  $code->writePHP($DataTable->getComponentRefCode().'->registerColumn(');
 398                  $code->writePHPLiteral($this->getAttribute('name'));
 399                  $code->writePHP(','.$this->getComponentRefCode().');');
 400                  $funct = $code->getTempVarRef();
 401                  if ($cell =& $this->findChildByClass('DataCellTag')) {
 402                      $cell_code = clone_obj($code);
 403                      $cell_code->code = ''; 
 404                      $cell->generateNow($cell_code);
 405                      $cell_code->writePHP('echo "";'); //make sure you end in php mode or the create_function will fail
 406                      $code->writePHP($funct.' = create_function(\''.$DataTable->getDataSourceRefCode().', $root\', ');
 407                      $code->writePHPLiteral($cell_code->code);
 408                      // this is a no no, accessing a private var, but we need any new includes :(
 409                      foreach($cell_code->includeList as $file) { $code->registerInclude($file); }
 410                      unset($cell_code);
 411                      $code->writePHP(');');
 412                      $code->writePHP($this->getComponentRefCode().'->setRenderFunct('.$funct.');');
 413                  }
 414                  if ($header =& $this->findChildByClass('DataHeaderTag')) {
 415                      $head_att_list = '';
 416                      foreach ($DataTable->getCellAttributeMap() as $key => $value) {
 417                          if ($head_att = $header->getAttribute($key)) {
 418                              $head_att_list .= ' '.$value.'="'.$head_att.'"';
 419                          }
 420                      }
 421                      if (trim($head_att_list)) {
 422                          $code->writePHP($this->getComponentRefCode().'->setHeaderAttrib(');
 423                          $code->writePHPLiteral($head_att_list);
 424                          $code->writePHP(');');
 425                      }
 426                      $header_code = clone_obj($code);
 427                      $header_code->code = ''; 
 428                      $header->generateNow($header_code);
 429                      $header_code->writePHP('echo "";'); //make sure you end in php mode or the create_function will fail
 430                      $code->writePHP($funct.' = create_function(\''.$DataTable->getDataSourceRefCode().', $root\', ');
 431                      $code->writePHPLiteral($header_code->code);
 432                      foreach($header_code->includeList as $file) { $code->registerInclude($file); }
 433                      unset($header_code);
 434                      $code->writePHP(');');
 435                      $code->writePHP($this->getComponentRefCode().'->setHeaderFunct('.$funct.');');
 436                  }
 437                  $footers = $this->findChildrenByClass('DataFooterTag');
 438                  foreach ($footers as $key => $obj) {
 439                      $cell_type = ($footers[$key]->hasAttribute('heading')) ? 'th' : 'td';
 440                      $wrap_start = $wrap_end = '';
 441                      if ($footers[$key]->hasAttribute('bold')
 442                                  ||$footers[$key]->hasAttribute('b')) {
 443                          $wrap_start = '<b>';
 444                          $wrap_end = '</b>';
 445                      }
 446                      $footer_code = clone_obj($code); 
 447                      $footer_code->code = '';
 448                      $footer_code->writePHP(' echo "<'.$cell_type.'$attribs>'.$wrap_start.'";');
 449                      $footers[$key]->generateNow($footer_code);
 450                      $footer_code->writePHP(' echo "'.$wrap_end.'</'.$cell_type.'>";');
 451                      $code->writePHP($funct.' = create_function(\''.$DataTable->getDataSourceRefCode().', $root, $attribs\', ');
 452                      $code->writePHPLiteral($footer_code->code);
 453                      foreach($footer_code->includeList as $file) { $code->registerInclude($file); }
 454                      unset($footer_code);
 455                      $code->writePHP(');');
 456                      $code->writePHP($this->getComponentRefCode().'->addFooterFunct('.$funct.');');
 457                  }
 458              }
 459          }
 460      }
 461  }
 462  
 463  /**
 464  * The compile time component to output when the DataSet is empty
 465  * @author    Jason E. Sweat < jsweat_php AT yahoo DOT com >
 466  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
 467  * @access protected
 468  * @package WACT_TAG
 469  */
 470  class DataDefaultTag extends SilentCompilerDirectiveTag {
 471      /**
 472      * @return void
 473      * @access protected
 474      */
 475  	function CheckNestingLevel() {
 476          if ( !is_a($this->parent, 'DataTableTag') ) {
 477              RaiseError('compiler', 'MISSINGENCLOSURE', array(
 478                  'tag' => $this->tag,
 479                  'EnclosingTag' => 'data:TABLE',
 480                  'file' => $this->SourceFile,
 481                  'line' => $this->StartingLineNo));
 482          }
 483      }
 484  }
 485  
 486  /**
 487  * The compile time component for a cell template
 488  * @author Jason E. Sweat
 489  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
 490  * @access protected
 491  * @package WACT_TAG
 492  */
 493  class DataCellTag extends SilentCompilerDirectiveTag {
 494      /**
 495      * @return void
 496      * @access protected
 497      */
 498  	function CheckNestingLevel() {
 499          if ( !is_a($this->parent, 'DataColumnTag') ) {
 500              RaiseError('compiler', 'MISSINGENCLOSURE', array(
 501                  'tag' => $this->tag,
 502                  'EnclosingTag' => 'data:TABLE',
 503                  'file' => $this->SourceFile,
 504                  'line' => $this->StartingLineNo));
 505          }
 506      }
 507      /**
 508      * @param CodeWriter
 509      * @return void
 510      * @access protected
 511      */
 512  	function preGenerate(&$code) {
 513          parent::preGenerate($code);
 514      }    
 515  }
 516  
 517  /**
 518  * The compile time component to output a heading for a column, group or table
 519  * @author Jason E. Sweat
 520  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
 521  * @access protected
 522  * @package WACT_TAG
 523  */
 524  class DataHeaderTag extends SilentCompilerDirectiveTag {
 525      /**
 526      * @return void
 527      * @access protected
 528      */
 529  	function CheckNestingLevel() {
 530          if ( !(is_a($this->parent, 'DataTableTag') 
 531              || is_a($this->parent, 'DataGroupTag')
 532              || is_a($this->parent, 'DataColumnTag')
 533              )) {
 534              RaiseError('compiler', 'MISSINGENCLOSURE', array(
 535                  'tag' => $this->tag,
 536                  'EnclosingTag' => 'data:TABLE',
 537                  'file' => $this->SourceFile,
 538                  'line' => $this->StartingLineNo));
 539          }
 540      }
 541  }
 542  
 543  /**
 544  * The compile time component to output a footer for a column or table
 545  * @author Jason E. Sweat
 546  * @see http://wact.sourceforge.net/index.php/ResultsetTableTag
 547  * @access protected
 548  * @package WACT_TAG
 549  */
 550  class DataFooterTag extends SilentCompilerDirectiveTag {
 551      /**
 552      * @return void
 553      * @access protected
 554      */
 555  	function CheckNestingLevel() {
 556          if ( !(is_a($this->parent, 'DataTableTag') 
 557              || is_a($this->parent, 'DataColumnTag')
 558              )) {
 559              RaiseError('compiler', 'MISSINGENCLOSURE', array(
 560                  'tag' => $this->tag,
 561                  'EnclosingTag' => 'data:TABLE',
 562                  'file' => $this->SourceFile,
 563                  'line' => $this->StartingLineNo));
 564          }
 565      }
 566  }
 567  
 568  
 569  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5