Sådan benyttes komponenten ViewSimple klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/ViewSimple.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? ViewSimple::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new ViewSimple($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten ViewSimple klassen
Den fulde PHP kildekode for ViewSimple klassen
<?php/** * @package mvc * @see HTML_MVC_VIEW_PATH.'/ViewSimple.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_BASE_UTIL_PATH.'/Htmlspecialchars.php');require_once(HTML_MVC_VIEW_PATH.'/ViewCommon.php');require_once(HTML_BASE_UTIL_PATH.'/Hr.php');require_once(HTML_BASE_UTIL_PATH.'/EmailLink.php');require_once(HTML_BASE_UTIL_PATH.'/Lookup.php');if (defined('HTML_LANGUAGE_UTIL_PATH')) { require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');}/** * Generates the html for a View Simple like a business card look-a-like * <code> * Usage: * $view = new ViewSimple($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $layout); * print $view->getHtml(); * Or * ViewSimple::display($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $layout); * * Generates a complete View Simple interface * +--------+ * | Header | * +--------+ * | data1 | * +--------+ * | data2 | * +--------+ * </code> * @package mvc */class ViewSimple extends ViewCommon { /** * 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='') { $theText = $text != '' ? $text : ''; // Global table characteristics $theWidth = $width != '' ? $width : SIMPLE_VIEW_WIDTH; $theClass = $class != '' ? $class : SIMPLE_VIEW_CLASS; $theBorder = $border != '' ? $border : SIMPLE_VIEW_BORDER; $theCellPadding = $cellpadding != '' ? $cellpadding : SIMPLE_VIEW_CELLPADDING; $theCellSpacing = $cellspacing != '' ? $cellspacing : SIMPLE_VIEW_CELLSPACING; parent::__construct($datareader, $theText, $theWidth, $theClass, $theBorder, $theCellPadding, $theCellSpacing, $summary, $caption, $layout); } /** * Return the column header data as an object * @param String $key The key to use * @return Object The html as an object */ function newHeader($key) { $text = $key; if (defined('HTML_LANGUAGE_UTIL_PATH')) { $text = Translate::sql($key,TRANSLATE_QUERY); } return new Raw($text." "); } /** * Return the column data as an object * @param String $key The key to use * @param String $value The value to use * @param String $cssData The CSS Data to use * @param boolean $isSingleColumn The column data is a single column. Default more columns * @return Object The html as an object */ function newColumnSimple($key, $value, $cssData, $isSingleColumn=false) { $object = new Raw(); $text = ""; $isHeader = false; // not used $theValue = $this->getFormattedText($key, $value, $isHeader); $theValue = $this->strip($theValue, __FILE__, __LINE__); switch ($key) {// case @SELECT_MAIL: // deprecated case @SELECT_CUSTOMER_EMAIL: $text = $key; if (defined('HTML_LANGUAGE_UTIL_PATH')) { $text = Translate::sql($key, TRANSLATE_QUERY); } $object->add(new EmailLink($isSingleColumn ? "$text: $theValue" : '', $theValue, $cssData)); break; case @SELECT_PRODUCT_START_DATE: case @SELECT_PRODUCT_END_DATE: $text = $this->format($value, FORMAT_STYLE_DATE_LONG); $object->add($text); break; case @SELECT_CUSTOMER_PRIVATE_PHONE: case @SELECT_CUSTOMER_BUSINESS_PHONE: case @SELECT_CUSTOMER_MOBILE_PHONE: $text = $key; if (defined('HTML_LANGUAGE_UTIL_PATH')) { $text = Translate::sql($key, TRANSLATE_QUERY); } $object = new Raw(($isSingleColumn ? "$text: " : '').$theValue); //$object->add(new Lookup($value)); break; default: // Use as is $object = new Raw("$theValue "); break; } return $object; } /** * Return the row data as html * <tr> * <td>header 1</td> * <td>data 1</td> * </tr> ... * @param array $row The rows to loop * @param String $cssHeader The CSS Header to use * @param String $cssData The CSS Data to use * @param boolean $isSingleColumn This is a single column if true. Default is false * @return Object The html as an object */ function newRow($row, $cssHeader='', $cssData='', $isSingleColumn=false) { $object = new Raw(); if (count($row) > 0) { $zip = ''; $count = 0; foreach($row as $key=>$rawvalue) { $rowcolor = Rowcolor::get($count++); $value = $this->strip($rawvalue, __FILE__, __LINE__); switch ($key) { case @SELECT_CUSTOMER_ZIP: $zip = $value; break; case @SELECT_CUSTOMER_CITY: $value = $zip.' '.$value; // Intentionally fall through default: if ($value != '') { $tr = new Tr(); if ($isSingleColumn) { // Ignore key column } else { $td = new Th($cssHeader != '' ? $cssHeader : $rowcolor); $td->add($this->newHeader($key)); $tr->add($td); } $isHeader = false; $theCss = $this->getCssIsOnline($key, $rowcolor, $isHeader, $value); $td = new Td($theCss); $td->add($this->newColumnSimple($key, $value, $cssData, $isSingleColumn)); $tr->add($td); $object->add($tr); } break; } } } return $object; } /** * Return the content as an object * @return Object The content as an object */ function newContent() { $object = new Raw(); $numrows = $this->datareader->getNumRows(); if ($numrows > 0) { if ($numrows > 1) { $this->text .= $this->text != '' ? " ($numrows)" : ''; $td = new Td(); $td->add(new Raw("TODO More than one ($numrows) ...")); $tr = new Tr(); $tr->add($td); $object->add($tr); } $isSingleColumn = true; // Single Column, like a business card $count = 0; foreach($this->datareader->getRows() as $no=>$row) { $rowcolor = Rowcolor::get($count++); $object->add($this->newRow($row, CSS_COLOR_HEADER, $rowcolor, $isSingleColumn)); } } else { $object = new Tr(); $object->add($this->newTextRow(TEXT_NO_DATA, LINK_BACK)); } return $object; } /** * Return the html * @return String The html */ function getHtml() { $html = $this->html; $this->add($this->newContent()); // Render it if ($this->text != '') { $html .= $this->getTableHeader(); } $html .= $this->getStart(); $html .= $this->getEnd(); return $html; } /** * Display html * <code> * Usage: * ViewSimple::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 ViewSimple($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $layout); $html->addHtml(); }}?>
Den fulde HTML kildekode for ViewSimple klassen
<? <!-- DEBUG: ViewSimple --> <table id="ViewSimpleId" width="400" class="simpleView baseBorder" border="0" cellpadding="2" cellspacing="0"> <tr> <td class="baseColorDark" valign="top">Privat telefon: 48246030 </td> </tr> <tr> <td class="baseColorLight" valign="top">Finn Rasmussen </td> </tr> <tr> <td class="baseColorDark" valign="top">HvepseEksperten.dk ApS </td> </tr> <tr> <td class="baseColorLight" valign="top">Kongens Vænge 79 </td> </tr> <tr> <td class="baseColorLight" valign="top">3400 Hillerød </td> </tr> <tr> <td class="baseColorDark" valign="top">Denmark </td> </tr> </table> ?>
Her er 'klasse metoderne' for ViewSimple klassen:
Her er 'objekt variable' for ViewSimple klassen: