From e95c5657ff14a9d795a3a796e87b0598565462ba Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 16 Oct 2008 13:26:25 +0000 Subject: [PATCH] API CHANGE Changed constructor parameters for LabelField and Header: First parameter is now $name, not $title. Necessary change to consistently support labelled fields which can be identified and modified within a FieldSet, and in the DOM structure. Both HeaderField and LabelField still acceppt old notation through checking the parameter signature with func_get_args(). API CHANGE Moved setAllowHTML() to DataLessField ENHANCEMENT Using createTag() to create HeaderField and LabelField, which adds support for HTML id attributes and extra css classes through addExtraClass() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64421 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/DatalessField.php | 35 +++++++++++++++++++++++++++++++---- forms/HeaderField.php | 40 ++++++++++++++++++++++++++++++---------- forms/LabelField.php | 41 ++++++++++++++++++++++++++++++----------- forms/LiteralField.php | 33 +++++++++++++++++++++++---------- 4 files changed, 114 insertions(+), 35 deletions(-) diff --git a/forms/DatalessField.php b/forms/DatalessField.php index 80afa5cb6..1c24beaf1 100755 --- a/forms/DatalessField.php +++ b/forms/DatalessField.php @@ -2,10 +2,31 @@ /** * Abstract class for all fields without data. * Labels, headings and the like should extend from this. + * * @package forms * @subpackage fields-dataless */ class DatalessField extends FormField { + + /** + * @var bool $allowHTML + */ + protected $allowHTML; + + /** + * @param string $name + * @param string $title The label itslef + * @param string $class An HTML class to apply to the label (Deprecated: use addExtraClass()) + * @param boolean $allowHTML Determine if the tag content needs to be escaped (Deprecated: use setAllowHTML()) + * @param Form $form + */ + function __construct($name, $title = null, $className = "", $allowHTML = false, $form = null) { + if($className) $this->extraClasses = array($className); + $this->allowHTML = $allowHTML; + + parent::__construct($name, $title, null, $form); + } + /** * Function that returns whether this field contains data. * Always returns false. @@ -36,11 +57,17 @@ class DatalessField extends FormField { } /** - * As DatalessFields are not initialized with an ID/name, - * we keep the title for a unique reference. + * @param bool $bool */ - function Name() { - return $this->title; + function setAllowHTML($bool) { + $this->allowHTML = $bool; + } + + /** + * @return bool + */ + function getAllowHTML() { + return $this->allowHTML; } } ?> \ No newline at end of file diff --git a/forms/HeaderField.php b/forms/HeaderField.php index 509df0000..c21272d01 100755 --- a/forms/HeaderField.php +++ b/forms/HeaderField.php @@ -6,21 +6,41 @@ * @subpackage fields-dataless */ class HeaderField extends DatalessField { - protected $headingLevel, $allowHTML; - function __construct($title, $headingLevel = 2, $allowHTML = false, $form = null) { + /** + * @var int $headingLevel The level of the

to

HTML tag. Default: 2 + */ + protected $headingLevel = 2; + + function __construct($name, $title, $headingLevel = 2, $allowHTML = false, $form = null) { + // legacy handling for old parameters: $title, $heading, ... + // instead of new handling: $name, $title, $heading, ... + $args = func_get_args(); + if(!isset($args[1]) || is_numeric($args[1])) { + $title = (isset($args[0])) ? $args[0] : null; + $name = $title; // this means i18nized fields won't be easily accessible through fieldByName() + $headingLevel = (isset($args[1])) ? $args[1] : null; + $allowHTML = (isset($args[2])) ? $args[2] : null; + $form = (isset($args[3])) ? $args[3] : null; + } + $this->headingLevel = $headingLevel; $this->allowHTML = $allowHTML; - - parent::__construct(null, $title, null, $form); + + parent::__construct($name, $title, null, $form); } + function Field() { - $XML_title = ($this->allowHTML) ? $this->title : Convert::raw2xml($this->title); - - // extraclass - $XML_class = ($this->extraClass()) ? " class=\"{$this->extraClass()}\"" : ''; - - return "headingLevel}{$XML_class}>$XML_titleheadingLevel>"; + $attributes = array( + 'class' => $this->extraClass(), + 'id' => $this->id(), + 'name' => $this->Name(), + ); + return $this->createTag( + "h{$this->headingLevel}", + $attributes, + ($this->getAllowHTML() ? $this->title : Convert::raw2xml($this->title)) + ); } } ?> \ No newline at end of file diff --git a/forms/LabelField.php b/forms/LabelField.php index bfca0abb9..b6dcd0dbb 100755 --- a/forms/LabelField.php +++ b/forms/LabelField.php @@ -6,27 +6,46 @@ * @subpackage fields-dataless */ class LabelField extends DatalessField { - protected $className; - protected $allowHTML; /** - * Create a new label. - * @param title The label itslef - * @param class An HTML class to apply to the label. + * @param string $name + * @param string $title + * @param string $className (Deprecated: use addExtraClass()) + * @param bool $allowHTML (Deprecated: use setAllowHTML()) + * @param Form $form */ - function __construct($title, $className = "", $allowHTML = false, $form = null) { - $this->className = $className; + function __construct($name, $title, $className = null, $allowHTML = false, $form = null) { + // legacy handling for old parameters: $title, $heading, ... + // instead of new handling: $name, $title, $heading, ... + $args = func_get_args(); + if(!isset($args[1])) { + $title = (isset($args[0])) ? $args[0] : null; + $name = $title; + $classname = (isset($args[1])) ? $args[1] : null; + $allowHTML = (isset($args[2])) ? $args[2] : null; + $form = (isset($args[3])) ? $args[3] : null; + } + + $this->headingLevel = $headingLevel; $this->allowHTML = $allowHTML; - - parent::__construct(null, $title, null, $form); + + parent::__construct($name, $title, null, $form); } /** * Returns a label containing the title, and an HTML class if given. */ function Field() { - $classClause = $this->className ? " class=\"$this->className\"" : ''; - return "" . ($this->allowHTML ? $this->title : htmlentities($this->title)) . ""; + $attributes = array( + 'class' => $this->extraClass(), + 'id' => $this->id(), + 'name' => $this->Name(), + ); + return $this->createTag( + 'label', + $attributes, + ($this->getAllowHTML() ? $this->title : htmlentities($this->title)) + ); } } ?> \ No newline at end of file diff --git a/forms/LiteralField.php b/forms/LiteralField.php index 51330bb08..408e2390c 100755 --- a/forms/LiteralField.php +++ b/forms/LiteralField.php @@ -1,32 +1,45 @@ content = $content; + + parent::__construct($name); } + function FieldHolder() { return is_object($this->content) ? $this->content->forTemplate() : $this->content; } + function Field() { return $this->FieldHolder(); } - /** - * @desc Sets the content of this field to a new value - */ - function setContent($content) { - $this->content = $content; - } + /** + * Sets the content of this field to a new value + * @param string $content + */ + function setContent($content) { + $this->content = $content; + } + + /** + * @return string + */ + function getContent() { + return $this->content; + } function performReadonlyTransformation() { $this->setReadonly(true);