From 0674678a708ea01c83509f677797286bcee878a2 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 19 Dec 2006 00:16:49 +0000 Subject: [PATCH] Some serious updates to the new Printer system. It's now almost usable. --- PrinterBaseElement.php | 8 +-- PrinterElement.php | 43 ++++++++++- PrinterLabelElement.php | 3 +- PrinterRootElementTable.php | 56 ++++++++++++++- PrinterTableElement.php | 139 +++++++++++++++++++++++++++++------- 5 files changed, 214 insertions(+), 35 deletions(-) diff --git a/PrinterBaseElement.php b/PrinterBaseElement.php index 16c2284..0934484 100644 --- a/PrinterBaseElement.php +++ b/PrinterBaseElement.php @@ -74,6 +74,7 @@ class BSPrinterBaseElement extends BSPrinterElement $this->type = $type; $this->name = $name; $this->value = $value; + $this->setCssClass('input'); } // ################################################################### @@ -101,7 +102,6 @@ class BSPrinterBaseElement extends BSPrinterElement public function paint() { $name = ' name="' . $this->name . '"'; - $style = ($this->_prepareStyle() ? ' style="' . $this->_prepareStyle() . '"' : ''); $value = ($this->value ? ' value="' . $this->value . '"' : ''); switch ($this->type) { @@ -110,16 +110,16 @@ class BSPrinterBaseElement extends BSPrinterElement case 'button': case 'submit': case 'reset': - return ''; + return 'type == 'text' OR $this->type == 'password') ? ' size="35" maxlength="255"' : ''). $this->_prepareStyle() . ' />'; break; case 'checkbox': case 'radio': - return 'active ? ' checked="checked"' : '') . $style . ' />'; + return 'active ? ' checked="checked"' : '') . $this->_prepareStyle() . ' />'; break; case 'option': - return 'active ? ' selected="selected"' : '') . $style . '>' . $this->name . ''; + return 'active ? ' selected="selected"' : '') . $this->_prepareStyle() . '>' . $this->name . ''; break; default: diff --git a/PrinterElement.php b/PrinterElement.php index 73df3d0..dc294a0 100644 --- a/PrinterElement.php +++ b/PrinterElement.php @@ -45,11 +45,28 @@ abstract class BSPrinterElement */ protected $style = array(); + /** + * The CSS class to use + * @var string + */ + protected $cssClass = ':swap:'; + /** * Tells the element to paint itself (and any children) */ public abstract function paint(); + // ################################################################### + /** + * Sets the CSS class to use. Use ":swap:" to alternate + * + * @param string CSS class + */ + public function setCssClass($class) + { + $this->cssClass = $class; + } + // ################################################################### /** * Sets the style information @@ -67,11 +84,21 @@ abstract class BSPrinterElement */ protected function _prepareStyle() { - if (empty($this->style)) + if (empty($this->style) AND empty($this->cssClass)) { return; } + if ($this->cssClass == ':swap:') + { + BSFunctions::SwapCssClasses(); + $class = BSFunctions::$cssClass; + } + else + { + $class = $this->cssClass; + } + $attrs = array(); foreach ($this->style AS $prop => $value) @@ -79,7 +106,19 @@ abstract class BSPrinterElement $attrs[] = $prop . ': ' . $value; } - return explode('; ', $attrs); + $style = implode('; ', $attrs); + + $string = ''; + if ($class) + { + $string .= ' class="' . $class . '"'; + } + if ($style) + { + $string .= ' style="' . $style . '"'; + } + + return $string; } } diff --git a/PrinterLabelElement.php b/PrinterLabelElement.php index ca0026c..5e106c0 100644 --- a/PrinterLabelElement.php +++ b/PrinterLabelElement.php @@ -56,6 +56,7 @@ class BSPrinterLabelElement extends BSPrinterElement function __construct($string) { $this->string = $string; + $this->setCssClass(null); } // ################################################################### @@ -68,7 +69,7 @@ class BSPrinterLabelElement extends BSPrinterElement { if ($this->_prepareStyle()) { - return '' . $this->string . ''; + return '_prepareStyle() . '>' . $this->string . ''; } return $this->string; } diff --git a/PrinterRootElementTable.php b/PrinterRootElementTable.php index 539288b..6fe2587 100644 --- a/PrinterRootElementTable.php +++ b/PrinterRootElementTable.php @@ -47,6 +47,27 @@ class BSPrinterRootElementTable extends BSPrinterRootElement */ private $colspan = 0; + /** + * Heading child elements + * @var array + */ + private $headers = array(); + + /** + * Width of the table + * @var string + */ + private $width = '90%'; + + // ################################################################### + /** + * Constructor + */ + public function __construct() + { + $this->setCssClass('tborder'); + } + // ################################################################### /** * Adds a table row into the child list @@ -55,9 +76,36 @@ class BSPrinterRootElementTable extends BSPrinterRootElement */ public function addChild(BSPrinterElement $tr) { + if (!$tr instanceof BSPrinterTableElement) + { + trigger_error('BSPrinterRootElementTable::addChild() only accepts BSPrinterTableElement objects as children'); + } $this->children[] = $tr; } + // ################################################################### + /** + * Adds a table element to be a heading of the table. This is still + * considered a child, but it goes before all other child elemends + * + * @param BSPrinterTableElement Child element + */ + public function addHeadingChild(BSPrinterTableElement $tr) + { + $this->headers[] = $tr; + } + + // ################################################################### + /** + * Sets the width of the table + * + * @param string Width value + */ + public function setWidth($width) + { + $this->width = $width; + } + // ################################################################### /** * Calculates the number of columns to display based on the colspan @@ -84,11 +132,15 @@ class BSPrinterRootElementTable extends BSPrinterRootElement { $builder = ''; + $this->children = array_merge($this->headers, $this->children); + $this->headers = array(); + $this->_calculateColumnCount(); foreach ($this->children AS $child) { - $builder .= "\n" . $child->paint() . "\n"; + $child->setColumnNumber($this->colspan); + $builder .= "\n" . $child->paint(); } return $builder; @@ -102,7 +154,7 @@ class BSPrinterRootElementTable extends BSPrinterRootElement */ public function paint() { - return "" . $this->_paintChildren() . "
"; + return "width . "\"" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n
"; } } diff --git a/PrinterTableElement.php b/PrinterTableElement.php index 18fc225..03b2384 100644 --- a/PrinterTableElement.php +++ b/PrinterTableElement.php @@ -40,26 +40,94 @@ require_once('ISSO/PrinterBaseElement.php'); * */ class BSPrinterTableElement extends BSPrinterElement -{ - /** - * CSS class to use - * @var string - */ - private $cssClass = ':swap:'; - +{ /** * Array of child nodes * @var array */ private $children = array(); + /** + * Number of columns to span + * @var integer + */ + private $colspan = 0; + + // ################################################################### + /** + * Prints a simple row of text and text + * + * @param string Label (left side) + * @param string Value (right side) + * + * @return BSPrinterTableElement Table row element + */ + public static function RowLabel($label, $value) + { + $tr = new BSPrinterTableElement(); + + $tr->addChild(new BSPrinterLabelElement($label)); + $tr->addChild(new BSPrinterLabelElement($value)); + + return $tr; + } + + // ################################################################### + /** + * Creates an input[text/password] field row + * + * @param string Label + * @param string Field name + * @param mixed Default value + * @param bool A password field? + * + * @return BPrinterTableElement Table row element + */ + public static function RowText($label, $name, $value = null, $password = false) + { + $tr = new BSPrinterTableElement(); + + $tr->addChild(new BSPrinterLabelElement($label)); + $tr->addChild(new BSPrinterBaseElement(($password ? 'password' : 'text'), $name, $value)); + + return $tr; + } + // ################################################################### /** - * Returns a new instance of this class + * Creates a submit row + * + * @param array Child elements to add before the buttons + * @param string Save button text + * @param string Reset button text + * + * @return BSPrinterTableElement Table row element */ - public static function Init() + public static function RowSubmit(Array $children = null, $save = ':submit:', $reset = ':reset:') { - return new BSPrinterTableElement(); + $build = ''; + if (sizeof($children) > 0) + { + foreach ($children AS $child) + { + $build .= $child->paint(); + } + } + + $save = ($save == ':submit:' ? _('Submit') : $save); + $reset = ($reset == ':reset:' ? _('Reset') : $reset); + + $elm = new BSPrinterBaseElement('submit', '__submit__', " $save "); + $build .= $elm->paint(); + + $elm = new BSPrinterBaseElement('reset', '__reset__', " $reset "); + $build .= $elm->paint(); + + $tr = new BSPrinterTableElement(); + $tr->addChild(new BSPrinterLabelElement($build)); + $tr->setCssClass('tfoot'); + + return $tr; } // ################################################################### @@ -87,17 +155,6 @@ class BSPrinterTableElement extends BSPrinterElement return $tr; } - // ################################################################### - /** - * Sets the CSS class to use. Use ":swap:" to alternate - * - * @param string CSS class - */ - public function setCssClass($class) - { - $this->cssClass = $class; - } - // ################################################################### /** * Returns the number of columns in this element @@ -114,13 +171,26 @@ class BSPrinterTableElement extends BSPrinterElement * Adds a child node to the element * * @param BSPrinterElement A child element - * - * @return BSPrinterTableElement Returns the current object */ public function addChild(BSPrinterElement $child) { $this->children[] = $child; - return $this; + } + + // ################################################################### + /** + * Sets the number of columns this row should have and pads the + * elements accordingly + * + * @param integer Column count + */ + public function setColumnNumber($cols) + { + if ($cols < $this->numberOfColumns()) + { + trigger_error('You need to have at least ' . $this->numberOfColumns() . ' columns'); + } + $this->colspan = $cols; } // ################################################################### @@ -133,9 +203,26 @@ class BSPrinterTableElement extends BSPrinterElement { $builder = ''; + $numCols = $this->numberOfColumns(); + $even = true; + + if ($this->colspan % $numCols == 0) + { + $cols = $this->colspan / $numCols; + $even = true; + } + else + { + $cols = intval($this->colspan / $numCols); + $even = false; + } + + $i = 0; foreach ($this->children AS $child) { - $builder .= "\n" . $child->paint() . "\n"; + $i++; + $colspan = (($i == $numCols AND !$even) ? $cols + 1 : $cols); + $builder .= "\n\t\t 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . ""; } return $builder; @@ -149,7 +236,7 @@ class BSPrinterTableElement extends BSPrinterElement */ public function paint() { - return "" . $this->_paintChildren() . ""; + return "\t_prepareStyle() . ">" . $this->_paintChildren() . "\n\t"; } } -- 2.22.5