Sådan benyttes komponenten TableDataReader klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/TableDataReader.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? TableDataReader::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new TableDataReader($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten TableDataReader klassen
Den fulde PHP kildekode for TableDataReader klassen
<?php/** * @package table * @filesource * @see HTML_TABLE_COMPONENT_PATH.'/TableDataReader.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_TABLE_COMPONENT_PATH.'/TableHeader.php');require_once(HTML_TABLE_COMPONENT_PATH.'/Table.php');if (defined('HTML_DTO_UTIL_PATH')) { require_once(HTML_DTO_UTIL_PATH.'/DataReaderFactory.php');}require_once(HTML_BASIC_UTIL_PATH.'/Message.php');require_once(HTML_BASE_UTIL_PATH.'/Raw.php');require_once(HTML_BASE_UTIL_PATH.'/Ul.php');require_once(HTML_BASE_UTIL_PATH.'/Li.php');require_once(HTML_BASE_UTIL_PATH.'/Links.php');require_once(HTML_BASE_UTIL_PATH.'/Link.php');require_once(HTML_BASE_UTIL_PATH.'/Images.php');require_once(HTML_BASE_UTIL_PATH.'/Image.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Request.php');if (defined('HTML_COMPONENT_PAGE_PATH')) { require_once(HTML_COMPONENT_PAGE_PATH.'/Googlebox.php');}if (defined('HTML_LANGUAGE_UTIL_PATH')) { require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');}if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}/** * Generates the html for a table * <code> * Usage: * $rows = array( * array('head_1'=>'dat_0_1', 'head_2'=>'dat_0_2', 'head_3'=>'dat_0_3',), * array('head_1'=>'dat_1_1', 'head_2'=>'dat_1_2', 'head_3'=>'dat_1_3',), * ); * $datareader = DataReaderFactory::newDataReader($rows, $header, $default, $limit, $sort); * * $text = "Text header"; * $width = "100%"; * $class = "theTable"; * $border = "1"; * $cellpadding = "5" * $cellspacing = "0"; * $summary = ""; * $caption = ""; * $layout = ""; // The layout to use * * $table = new TableDataReader($datareader, $text, $width, $class, $border, * $cellpadding, $cellspacing, $summary, $caption, $layout); * print $table->getHtml(); * Or * TableDataReader::display($datareader, $text, $width, $class, $border, $cellpadding, * $cellspacing, $summary, $caption, $layout); * * Generates a complete table interface with data supplied by the DataReader object * ------------------------------+ * | Text header | * ------------------------------+ * | head_1 | head_2 | head_3 | * ------------------------------+ * | dat_0_1 | dat_0_2 | dat_0_3 | * | dat_1_1 | dat_1_2 | dat_1_3 | * ------------------------------+ * </code> * @package table */class TableDataReader extends Table { /** * @private $layout The different layouts to use * image link | image link | etc. on the same line * image link | image link | etc. on the same line and google search * image and link on each line * <li> image and link on each line * image <br> link etc. on the same line */ protected $layout = ''; /** * Constructor * @param DataReader / array $datareader The Data Reader object OR an array * @param String $text The text header for the table * @param String $width The Width for the table * @param String $class The Class * @param String $border The Border * @param String $cellpadding The CellSpacing * @param String $cellspacing The CellPadding * @param String $summary The Summary * @param String $caption The Caption * @param String $layout The layout to use */ function __construct($datareader=null, $text='', $width='', $class='', $border='', $cellpadding='', $cellspacing='', $summary='', $caption='', $layout='') { $theId = $this->getClassName()."Id"; parent::__construct($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $theId); $this->layout = $layout; $this->adjustColumns(); } /** * Return the column data for the view * @param String $value The value to use * @param String $css The CSS to use * @param String $valign The valign to use * @return Object The html as an object */ function newData($value='', $css='', $valign='') { $text = $value !== ''?$value:''; $object = new Td($css, $valign,'', $text); if ($this->layout & LINK_LAYOUT_LI) { $object = new Raw($text); } return $object; } /** * Get the CSS class Name for this component * You may override this function if you need another CSS class name * @abstract * @return String The CSS class name */ public function getCssClass() { return CSS_BODY; } /** * Return the column data as html * The column data may be * - LINK_NAME * - IMAGE_NAME * - link * - image * - link + image * <code> * </code> * @param String $page The page key * @param array $column The column to loop for key/value pairs * @param boolean $isActivetab The Tab is active, if TRUE * @param boolean $isActiveindex The Index is active, if TRUE * @return Object The html as an object */ function newColumn($page, $column, $isActiveTab=false, $isActiveIndex=false) { $object = new Raw(); $theKey = ""; if (is_array($column) && count($column) > 0) { // Link $text = ''; $href = ''; $title = ''; $aux = ''; $target = ''; // Image $src = ''; $width = ''; $height = ''; $alt = ''; $border = ''; $class = $this->getCssClass(); $theValue = ''; $valign = 'top'; foreach($column as $key=>$value) { switch ($key) { case KEY_TRIANGLE: $aux = ''; if ($this->layout & LINK_LAYOUT_LI) { $aux = LINK_LAYOUT_LI; } $theValue = $this->newTriangle($value, $class, $aux); $class .= ' '.CSS_ONEPC; break; case KEY_LINK: $class .= ' '.CSS_ALIGN_LEFT; // Intentionally fall through case KEY_POWERED: if ($class == $this->getCssClass()) { $class .= ' '.CSS_ALIGN_RIGHT; } $theValue = new Links($value, $text, $href, $class, $title, $aux); break; case KEY_VERSION: $theValue = TEXT_VERSION. " (" . phpversion() .") "; // Intentionally fall through case KEY_GOOGLE: $theKey = $key; // Intentionally fall through case KEY_PLAIN: $theValue .= $value; break; default: $$key = $value; break; } } if ($theValue === '') { $theValue = new Raw(); if ($href != '' || $text != '' || $src != '') { if (defined('HTML_LANGUAGE_UTIL_PATH')) { $text = ucfirst(Translate::get($text)); } $aux = ''; if ($this->layout & LINK_LAYOUT_LI) { $aux = LINK_LAYOUT_LI; } else if ($this->layout & LINK_LAYOUT_BR) { $aux = LINK_LAYOUT_BR; } if ($isActiveTab || $isActiveIndex ) { // The selected Tab if ($this->layout & LINK_LAYOUT_TAB) { $class = CSS_ATAB_ACTIVE; } $boldText = ' <b class="'.($isActiveIndex ? CSS_SELECTED : $class).'">'.$text."</b> "; $raw = new Raw($boldText); if ($this->layout & LINK_LAYOUT_LI) { $li = new Li(); $li->add($raw); $raw = $li; } else if ($this->layout & LINK_LAYOUT_BR) { // } $theValue = $raw; if ($src != '') { $theValue->add(new Image($src, $width, $height, $alt, $class, $border)); } if ($this->layout & LINK_LAYOUT_TAB) { $class = CSS_TAB_ACTIVE; $valign = "middle"; } } else { if ($href != '') { // A link $theValue = new Link($text, $href, $class, $title, $aux, $target); if ($src != '') { $theValue->add(new Image($src, $width, $height, $alt, $class, $border)); } } else { // Plain Image, no link if ($src != '') { $theValue->add(new Image($src, $width, $height, $alt, $class, $border)); } else { $theValue->add(new Raw("$text")); } } if ($this->layout & LINK_LAYOUT_TAB) { $class = CSS_TAB; $valign = "middle"; } } } } if ($theKey == KEY_GOOGLE) { $object->add(new Raw($theValue)); } else { if ($theValue !== '') { $object->add($this->newData($theValue, $class, $valign)); } else { // TODO what } } } else { // Ignore } return $object; } /** * Return true if this is an active item * @param String $page The current item to test * @param String $request The name of the key request * @param String $default The default request parameter to use, of no request parameter * @return boolean True if this is an active Tab, else return false */ function isActive($page, $request='', $default='') { $isActive = false; $item = ''; if (defined('REQUEST_TAB')) { if (TAB_SHOW & TAB_SHOW_URL) { $item = Request::get($request, $default); } else { $item = $default; } } if ($item !== '' && strtolower($page) == strtolower($item) ) { $isActive = true; } return $isActive; } /** * Return true if this is an active Tab * @param String $page The current item to test * @return boolean True if this is an active Tab, else return false */ function isActiveTab($page) { return $this->isActive($page, @REQUEST_TAB, @TAB_INDEX); } /** * Return true if this is an active Index * @param String $page The current item to test * @param array $columns The array of columns key/value pairs * @return boolean True if this is an active Index, else return false */ function isActiveIndex($page, $columns=array()) { $cwd = $page; if (is_numeric($cwd) || $cwd == "") { $cwd = getcwd(); } $pageUrl = substr($cwd, strlen(PROJECT_DOCUMENT_ROOT)); $href = ""; if (is_array($columns) && array_key_exists('href', $columns)) { $href = $columns['href']; } $isActive = $this->isActive($pageUrl, @REQUEST_INDEX, $href); return $isActive; } /** * Return the content as a row object * @return Object The content as an object */ function newColumns() { $object = new Tr(); // Menu Corner Left if ($this->layout & LINK_LAYOUT_CORNER) { if (!($this->layout & LINK_LAYOUT_TAB)) { $object->add($this->newCorner(IMAGE_CORNER_CONTENT_LEFT)); // L Border } } $numrows = 0; if ( $this->datareader instanceof DataReader ) { $numrows = $this->datareader->getNumRows(); } if ($numrows > 0) { if ($this->layout & LINK_LAYOUT_LI) { $object = new Ul($this->getCssClass()); } foreach($this->datareader->getRows() as $page=>$column) { $isActiveTab = $this->isActiveTab($page); $isActiveIndex = $this->isActiveIndex($page, $column); // Left corner if ($this->layout & LINK_LAYOUT_CORNER) { if ($this->layout & LINK_LAYOUT_TAB) { $object->add($this->newCorner($isActiveTab ? IMAGE_TAB_LEFT_SELECTED : IMAGE_TAB_LEFT)); // L Tab } else { $object->add($this->newCorner(IMAGE_CORNER_CONTENT_LEFT)); // Center corner } } /** * This is where the real data are generated */ $object->add($this->newColumn($page, $column, $isActiveTab, $isActiveIndex)); // Right corner if ($this->layout & LINK_LAYOUT_CORNER) { if ($this->layout & LINK_LAYOUT_TAB) { $object->add($this->newCorner($isActiveTab ? IMAGE_TAB_RIGHT_SELECTED : IMAGE_TAB_RIGHT)); // R Tab } else { $object->add($this->newCorner(IMAGE_CORNER_CONTENT_RIGHT)); // Center corner } } } // Menu Corner No Data if ($this->layout & LINK_LAYOUT_LI) { $tr = new Tr(); if ($this->layout & LINK_LAYOUT_CORNER) { if (!($this->layout & LINK_LAYOUT_TAB)) { $tr->add($this->newCorner(IMAGE_CORNER_CONTENT_CENTER)); // L Border } } $td = new Td($this->getCssClass(),'','','<!-- LINK_LAYOUT_XX='.$this->layout.' -->'); $td->add($object); $tr->add($td); $object = $tr; // Return the final object } } else { $object->add($this->newTextRow(TEXT_NO_DATA,'', $this->getCssClass())); } // Menu Corner Right if ($this->layout & LINK_LAYOUT_CORNER) { if (!($this->layout & LINK_LAYOUT_TAB)) { $object->add($this->newCorner(IMAGE_CORNER_CONTENT_RIGHT)); // R Border } } return $object; } /** * Adjust the columns for the horizontal view, and add the triangle gif */ function adjustColumns() { if ($this->datareader !== '' && is_array($this->datareader)) { $columns = array(); // DOES not work together with ViewLimit, so skip LINK_LAYOUT_TRIANGLE if ($this->layout & LINK_LAYOUT_TRIANGLE) { if ($this->layout & LINK_LAYOUT_TAB) { $columns[-2] = array(KEY_TRIANGLE=>IMAGE_TRIANGLE); } else { $columns[] = array(KEY_TRIANGLE=>IMAGE_TRIANGLE); } } $index = false; foreach($this->datareader as $key=>$column) { if ($this->layout & LINK_LAYOUT_TAB) { $columns[$key] = $column; // Speciel case for TAB } else { $columns[] = $column; } if ($this->layout & LINK_LAYOUT_HORIZONTAL) { $columns[] = array(KEY_PLAIN=>' ¦ ',); } } if ($this->layout & LINK_LAYOUT_FILL_OUT) { if ($this->layout & LINK_LAYOUT_TAB) { $columns[-1] = array(KEY_PLAIN=>' ','class'=>$this->getCssClass().' '.CSS_FILL_OUT,); } else { $columns[] = array(KEY_PLAIN=>' ','class'=>$this->getCssClass().' '.CSS_FILL_OUT,); } } if ($this->layout & LINK_LAYOUT_GOOGLE && defined('HTML_COMPONENT_PAGE_PATH')) { if (LINK_SHOW & LINK_SHOW_GOOGLE_TOP) { $googlebox = new Raw("<!-- Googlebox is disabled -->"); if (defined('HTML_COMPONENT_PAGE_PATH')) { $googlebox = new Googlebox('', $this->getCssClass()); } // Form start/end are defined in pagestart.php $columns[] = array(KEY_PLAIN=>' ','class'=>$this->getCssClass().' '.CSS_FILL_OUT,); $columns[] = array(KEY_GOOGLE=>$googlebox->getHtml(),); $columns[] = array(KEY_PLAIN=>' ',); } } if (defined('HTML_DTO_UTIL_PATH')) { $header = ''; // The header meta data $default = ''; // The default meta data array $limit = ''; // The limit to use for the array $sort = false; // DO NOT SORT $this->datareader = DataReaderFactory::newDataReader($columns, $header, $default, $limit, $sort); } else { $msg = $this->getClassName().'(), '.DATA_READER_CLASS_NAME." HTML_DTO_UTIL_PATH is not defined"; if (defined('HTML_LOG_UTIL_PATH')) { Log::error($msg, __FILE__, __LINE__); } else { // TODO what? } Message::add($msg, __FILE__, __LINE__); } } if ($this->datareader !== null && ! $this->datareader instanceof DataReader ) { // DATA_READER_CLASS_NAME $msg = $this->getClassName().'(), '.DATA_READER_CLASS_NAME." object expected, unknown datareader type=".gettype($this->datareader); if (defined('HTML_LOG_UTIL_PATH')) { Log::error($msg, __FILE__, __LINE__); } else { // TODO what? } Message::add($msg, __FILE__, __LINE__); } } /** * Return a new Corner or Border object * Corner: only the gif file should be added with no class name * Border: only the class name should be added * @param String $imageName The name of the image to get * @return Td The new Td object */ function newCorner($imageName='') { $class = $imageName !== '' ? "" : $this->getCssClass(); if (defined('CSS_CORNER_VIEW')) { /** * Workaround for Firefox is unable to set height=100% */ if ($imageName == IMAGE_CORNER_CONTENT_LEFT || $imageName == IMAGE_CORNER_CONTENT_CENTER || $imageName == IMAGE_CORNER_CONTENT_RIGHT) { $class .= " ".CSS_CORNER_VIEW; } } $valign = "top"; $colspan = ""; $text = ""; $width = "1px"; // NOT XHTML 1.0 strict compliant $title = $imageName; $object = new Td($class, $valign, $colspan, $text, $width, $title); if ($imageName !== '') { /** * CSS_FLOAT_LEFT is a Bugfix for Firefox strict compliant * @see http://www.webmasterworld.com/css/3458552.htm */ $width = ""; $height = ""; $alt = ""; $border = "0"; $class = CSS_FLOAT_LEFT; $object->add(new Images($imageName, $width, $height, $alt, $class, $border)); } else { $object->add(new Raw(' ')); } return $object; } /** * Get the content for the columns * Each row is definition of a column * @return String The html */ function getColumns() { $html = ''; // Menu Corner Top if ($this->layout & LINK_LAYOUT_CORNER) { if (!($this->layout & LINK_LAYOUT_TAB)) { $tr = new Tr(); $tr->add($this->newCorner(IMAGE_CORNER_TOP_LEFT)); $tr->add($this->newCorner(IMAGE_CORNER_TOP_CENTER)); // MT border $tr->add($this->newCorner(IMAGE_CORNER_TOP_RIGHT)); $this->add($tr); } } /** * This is the content, which is generated here */ $this->add($this->newColumns()); // Menu Corner Bottom if ($this->layout & LINK_LAYOUT_CORNER) { if (!($this->layout & LINK_LAYOUT_TAB)) { $tr = new Tr(); $tr->add($this->newCorner(IMAGE_CORNER_BOTTOM_LEFT)); $tr->add($this->newCorner(IMAGE_CORNER_BOTTOM_CENTER)); // MB border $tr->add($this->newCorner(IMAGE_CORNER_BOTTOM_RIGHT)); $this->add($tr); } } // Render it if ($this->text != '') { $html .= $this->getTableHeader(); } $html .= $this->getStart(); $html .= $this->getEnd(); return $html; } /** * Builds the html, and return it for a TableDataReader * @return String The html */ function getHtml() { $html = $this->html; $html .= $this->getColumns(); return $html; } /** * Display html * <code> * Usage: * TableDataReader::display($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $layout); * </code> * @static * @param DataReader / array $datareader The Data Reader object OR an array * @param String $text The text header for the table * @param String $width The width of the table * @param String $class The class of the table * @param String $border The border of the table * @param String $cellpadding The CellSpacing * @param String $cellspacing The CellPadding * @param String $summary The Summary * @param String $caption The Caption * @param String $layout The layout to use */ public static function display($datareader=null, $text='', $width='', $class='', $border='', $cellpadding='', $cellspacing='', $summary='', $caption='', $layout='') { $html = new TableDataReader($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $layout); $html->addHtml(); }}?>
Den fulde HTML kildekode for TableDataReader klassen
<? <!-- DEBUG: TableDataReader --> <table id="TableDataReaderId" width="100%" class="theTable" border="0" cellpadding="2" cellspacing="0"> <tr> <td class="baseBody" valign="top">Der er ikke fundet noget </td> </tr> </table> ?>
Her er 'klasse metoderne' for TableDataReader klassen:
Her er 'objekt variable' for TableDataReader klassen: