From 61bb16ee2e5f0a66234e1575242169a10fde47b5 Mon Sep 17 00:00:00 2001
From: xue <>
Date: Fri, 7 Jul 2006 14:54:15 +0000
Subject: Merge from 3.0 branch till 1253.

---
 framework/Web/UI/WebControls/TClientScript.php     | 128 +++++----------------
 framework/Web/UI/WebControls/TDataList.php         |   4 +-
 framework/Web/UI/WebControls/TRadioButton.php      |  79 +++++++++++++
 .../Web/UI/WebControls/TRequiredFieldValidator.php |  49 ++++++--
 framework/Web/UI/WebControls/TStyleSheet.php       |   7 +-
 5 files changed, 152 insertions(+), 115 deletions(-)

(limited to 'framework/Web/UI')

diff --git a/framework/Web/UI/WebControls/TClientScript.php b/framework/Web/UI/WebControls/TClientScript.php
index 79279a93..88f7f7d4 100644
--- a/framework/Web/UI/WebControls/TClientScript.php
+++ b/framework/Web/UI/WebControls/TClientScript.php
@@ -14,19 +14,14 @@
  * TClientScript class
  *
  * Allows importing of Prado Client Scripts from template via the
- * {@link setUsingPradoScripts UsingPradoScripts} property. Multiple Prado
+ * {@link setPradoScripts PradoScripts} property. Multiple Prado
  * client-scripts can be specified using comma delimited string of the
  * javascript library to include on the page. For example,
  *
  * <code>
- * <com:TClientScript UsingPradoScripts="effects, rico" />
+ * <com:TClientScript PradoScripts="effects, rico" />
  * </code>
  *
- * The {@link setPreRenderControlTypes PreRenderControlTypes} property can
- * be used to specify that controls type/class names that should pre-render itself
- * even though they may not be rendered on the page. This is useful to publish
- * controls that require assets and is only visible after a callback response.
- *
  * Custom javascript files can be register using the {@link setScriptUrl ScriptUrl}
  * property.
  * <code>
@@ -34,17 +29,12 @@
  * </code>
  *
  * Contents within TClientScript will be treated as javascript code and will be
- * rendered.
- *
- * The {@link setScriptPosition ScriptPosition} property specifies where the script
- * will be rendered. The allows values of {@link setScriptPosition ScriptPosition} are
+ * rendered in place.
  *
- *  - <b>Head</b> -- renders the script within the &lt;head&gt;
- *  - <b>Begin</b> -- renders the script within and near the begining of TForm
- *  - <b>Here</b> -- renders the script inplace, this is the default
- *  - <b>End</b> -- renders the script within and near the end of TForm
- *
- * TODO: Allow binding expressions inside scripts
+ * The {@link setPreRenderControlTypes PreRenderControlTypes} property can
+ * be used to specify that controls type/class names that should pre-render itself
+ * even though they may not be rendered on the page. This is useful to publish
+ * controls that require assets and is only visible after a callback response.
  *
  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  * @version $Revision: $  $Date: $
@@ -53,16 +43,11 @@
  */
 class TClientScript extends TControl
 {
-	/**
-	 * @var string body contents
-	 */
-	private $_content = '';
-
 	/**
 	 * @return string comma delimited list of javascript libraries to included
 	 * on the page.
 	 */
-	public function getUsingPradoScripts()
+	public function getPradoScripts()
 	{
 		return $this->getViewState('PradoScripts', '');
 	}
@@ -75,7 +60,7 @@ class TClientScript extends TControl
 	 *
 	 * @param string comma delimited list of javascript libraries to include.
 	 */
-	public function setUsingPradoScripts($value)
+	public function setPradoScripts($value)
 	{
 		$this->setViewState('PradoScripts', $value, '');
 	}
@@ -113,25 +98,6 @@ class TClientScript extends TControl
 		return $this->getViewState('PreRenderControls', '');
 	}
 
-	/**
-	 * @return string position the script should be rendered, default is 'Here'.
-	 */
-	public function getScriptPosition()
-	{
-		return $this->getViewState('ScriptPosition', 'Here');
-	}
-
-	/**
-	 * Sets the position where the script will be rendered.
-	 * The allow positions are 'Head', 'Begin', 'Here', and 'End', default is 'Here'.
-	 * @param string script position 'Head', 'Begin', 'Here' or 'End'.
-	 */
-	public function setScriptPosition($value)
-	{
-		$this->setViewState('ScriptPosition',
-			TPropertyValue::ensureEnum($value, 'Head', 'Begin', 'Here', 'End'), 'Here');
-	}
-
 	/**
 	 * Calls the client script manager to add each of the requested client
 	 * script libraries.
@@ -140,50 +106,16 @@ class TClientScript extends TControl
 	public function onPreRender($param)
 	{
 		parent::onPreRender($param);
-		$scripts = preg_split('/,|\s+/', $this->getUsingPradoScripts());
+		$scripts = preg_split('/,|\s+/', $this->getPradoScripts());
 		$cs = $this->getPage()->getClientScript();
 		foreach($scripts as $script)
 		{
-			$script = trim($script);
-			if(strlen($script) > 0)
+			if(($script = trim($script))!=='')
 				$cs->registerPradoScript($script);
 		}
-		if($this->getEnabled(true))
-		{
-			$this->registerCustomScriptFile();
-			$this->registerCustomScript();
-		}
 		$this->preRenderControls($param);
 	}
 
-	/**
-	 * Registers the custom script file.
-	 */
-	protected function registerCustomScriptFile()
-	{
-		$scriptUrl = $this->getScriptUrl();
-		if(strlen($scriptUrl))
-		{
-			$position = $this->getScriptPosition();
-			$cs = $this->getPage()->getClientScript();
-			switch($this->getScriptPosition())
-			{
-				case 'Head':
-					$cs->registerHeadScriptFile($scriptUrl, $scriptUrl);
-					break;
-				case 'Begin':
-					$cs->registerScriptFile($scriptUrl, $scriptUrl);
-					break;
-				case 'Here':
-					$this->_content .= TJavascript::renderScriptFile($scriptUrl);
-					break;
-				default :
-					throw new TConfigurationException('clientscript_invalid_file_position',
-								$this->getID(), $position);
-			}
-		}
-	}
-
 	/**
 	 * Renders the body content as javascript block.
 	 * Overrides parent implementation, parent renderChildren method is called during
@@ -192,31 +124,31 @@ class TClientScript extends TControl
 	 */
 	public function render($writer)
 	{
-		if($this->getEnabled(true) && strlen($this->_content) > 0)
-			$writer->write($this->_content);
+		$this->renderCustomScriptFile($writer);
+		$this->renderCustomScript($writer);
+	}
+
+	/**
+	 * Renders the custom script file.
+	 * @param THtmLWriter the renderer
+	 */
+	protected function renderCustomScriptFile($writer)
+	{
+		if(($scriptUrl = $this->getScriptUrl())!=='')
+			$writer->write("<script type=\"text/javascript\" src=\"$scriptUrl\"></script>\n");
 	}
 
 	/**
-	 * Registers the body content as scripts at specific locations. Calls
-	 * {@link parent::renderChildren} to capture the body contents.
+	 * Registers the body content as javascript.
+	 * @param THtmlWriter the renderer
 	 */
-	protected function registerCustomScript()
+	protected function renderCustomScript($writer)
 	{
-		$textWriter=new TTextWriter;
-		$this->renderChildren(new THtmlWriter($textWriter));
-		$script=$textWriter->flush();
-		if(strlen($script)>0)
+		if($this->getHasControls())
 		{
-			$cs = $this->getPage()->getClientScript();
-			$position = $this->getScriptPosition();
-			if($position == 'Here')
-				$this->_content .= TJavaScript::renderScriptBlock($script);
-			else
-			{
-				$key = sprintf('%08X', crc32($script));
-				$method = 'register'.$position.'Script';
-				$cs->{$method}($key, $script);
-			}
+			$writer->write("<script type=\"text/javascript\">\n/*<![CDATA[*/\n");
+			$this->renderChildren($writer);
+			$writer->write("\n/*]]>*/\n</script>\n");
 		}
 	}
 
diff --git a/framework/Web/UI/WebControls/TDataList.php b/framework/Web/UI/WebControls/TDataList.php
index 48392b1d..2c562d6d 100644
--- a/framework/Web/UI/WebControls/TDataList.php
+++ b/framework/Web/UI/WebControls/TDataList.php
@@ -960,6 +960,8 @@ class TDataList extends TBaseDataList implements INamingContainer, IRepeatInfoUs
 
 		foreach($this->getControls() as $index=>$item)
 		{
+			if(!($item instanceof TDataListItem))
+				continue;
 			switch($item->getItemType())
 			{
 				case 'Header':
@@ -1112,9 +1114,9 @@ class TDataList extends TBaseDataList implements INamingContainer, IRepeatInfoUs
 			$items=$this->getItems();
 			$selectedIndex=$this->getSelectedItemIndex();
 			$editIndex=$this->getEditItemIndex();
+			$hasSeparator=$this->_separatorTemplate!==null;
 			if($this->_headerTemplate!==null)
 				$this->_header=$this->createItemInternal(-1,'Header',false,null);
-			$hasSeparator=$this->_separatorTemplate!==null;
 			for($i=0;$i<$itemCount;++$i)
 			{
 				if($hasSeparator && $i>0)
diff --git a/framework/Web/UI/WebControls/TRadioButton.php b/framework/Web/UI/WebControls/TRadioButton.php
index 2bbdf22e..974a431c 100644
--- a/framework/Web/UI/WebControls/TRadioButton.php
+++ b/framework/Web/UI/WebControls/TRadioButton.php
@@ -37,6 +37,8 @@ Prado::using('System.Web.UI.WebControls.TRadioButtonList');
  * between posts to the server.
  *
  * TRadioButton uses {@link setGroupName GroupName} to group together a set of radio buttons.
+ * Once the {@link setGroupName GroupName} is set, you can use the {@link getRadioButtonsInGroup}
+ * method to get an array of TRadioButtons having the same group name.
  *
  * If {@link setAutoPostBack AutoPostBack} is set true, changing the radio button state
  * will cause postback action. And if {@link setCausesValidation CausesValidation}
@@ -53,11 +55,51 @@ Prado::using('System.Web.UI.WebControls.TRadioButtonList');
  */
 class TRadioButton extends TCheckBox
 {
+	/**
+	 * @param array list of radio buttons registered.
+	 */
+	private static $_radiobuttons=array();	
 	/**
 	 * @var string the name used to fetch radiobutton post data
 	 */
 	private $_uniqueGroupName=null;
+	/**
+	 * @var int number radio buttons created
+	 */
+	private static $_counter=0;
+	/**
+	 * @var int unique unmutable radio button id
+	 */
+	private $_radioUid;
 
+	public function __construct()
+	{
+		parent::__construct();
+		$this->_radioUid = self::$_counter++;
+	}
+	
+	/**
+	 * Registers the radio button groupings. If overriding onInit method, 
+	 * ensure to call parent implemenation.
+	 * @param TEventParameter event parameter to be passed to the event handlers
+	 */
+	public function onInit($param)
+	{
+		parent::onInit($param);
+		$this->registerRadioButton($this);
+	}
+	
+	/**
+	 * Unregisters the radio button groupings. If overriding onInit method,
+	 * ensure to call parent implemenation.
+	 * @param TEventParameter event parameter to be passed to the event handlers
+	 */
+	public function onUnLoad($param)
+	{
+		parent::onUnLoad($param);
+		$this->unregisterRadioButton($this);
+	}
+	
 	/**
 	 * Loads user input data.
 	 * This method is primarly used by framework developers.
@@ -100,6 +142,43 @@ class TRadioButton extends TCheckBox
 	{
 		$this->setViewState('GroupName',$value,'');
 	}
+	
+	/**
+	 * Register radio button control grouping.
+	 * @param TRadioButton control to add
+	 */
+	protected function registerRadioButton(TRadioButton $control)
+	{
+		if(!isset(self::$_radiobuttons[$control->_radioUid]))
+			self::$_radiobuttons[$control->_radioUid] = $control;
+	}
+	
+	/**
+	 * Unregister radio button control for grouping
+	 * @param TRadioButton control to unregister.
+	 */
+	protected function unregisterRadioButton(TRadioButton $control)
+	{
+		if(isset(self::$_radiobuttons[$control->_radioUid]))
+			unset(self::$_radiobuttons[$control->_radioUid]);
+	}
+	
+	/**  
+	 * Gets an array of RadioButtons with same group name. This method will
+	 * always return at least the current radio button in the array.
+	 * @return array list of TRadioButton with the same group
+	 */
+	public function getRadioButtonsInGroup()
+	{
+		$group = $this->getUniqueGroupName();
+		$buttons = array();
+		foreach(self::$_radiobuttons as $control)
+		{
+			if($control->getUniqueGroupName() === $group)
+				$buttons[] = $control;
+		}
+		return count($buttons) > 0 ? $buttons : array($this);
+	}
 
 	/**
 	 * @return string the value attribute to be rendered
diff --git a/framework/Web/UI/WebControls/TRequiredFieldValidator.php b/framework/Web/UI/WebControls/TRequiredFieldValidator.php
index b8a939ad..c99d9c19 100644
--- a/framework/Web/UI/WebControls/TRequiredFieldValidator.php
+++ b/framework/Web/UI/WebControls/TRequiredFieldValidator.php
@@ -76,22 +76,47 @@ class TRequiredFieldValidator extends TBaseValidator
 	protected function evaluateIsValid()
 	{
 		$control = $this->getValidationTarget();
-		$initial = trim($this->getInitialValue());
 		if($control instanceof TListControl)
+			return $this->validateListControl($control);
+		else if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
+			return $this->validateRadioButtonGroup($control);
+		else
+			return $this->validateStandardControl($control);
+	}
+	
+	private function validateListControl($control)
+	{
+		$initial = trim($this->getInitialValue());
+		$count = 0;
+		foreach($control->getItems() as $item)
 		{
-			$count = 0;
-			foreach($control->getItems() as $item)
-			{
-				if($item->getSelected() && $item->getValue() != $initial)
-					$count++;
-			}
-			return $count > 0;
+			if($item->getSelected() && $item->getValue() != $initial)
+				$count++;
 		}
-		else
+		return $count > 0;
+	}
+	
+	private function validateRadioButtonGroup($control)
+	{
+		$initial = trim($this->getInitialValue());
+		foreach($control->getRadioButtonsInGroup() as $radio)
 		{
-			$value=$this->getValidationValue($control);
-			return trim($value)!==$initial || (is_bool($value) && $value);
+			if($radio->getChecked())
+			{
+				if(strlen($value = $radio->getValue()) > 0)
+					return $value !== $initial;
+				else
+					return true;
+			}
 		}
+		return false;
+	}
+	
+	private function validateStandardControl($control)
+	{
+		$initial = trim($this->getInitialValue());
+		$value=$this->getValidationValue($control);
+		return trim($value)!==$initial || (is_bool($value) && $value);
 	}
 
 	/**
@@ -105,6 +130,8 @@ class TRequiredFieldValidator extends TBaseValidator
 		$control = $this->getValidationTarget();
 		if($control instanceof TListControl)
 			$options['TotalItems'] = $control->getItemCount();
+		if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
+			$options['GroupName'] = $control->getGroupName();
 		return $options;
 	}
 }
diff --git a/framework/Web/UI/WebControls/TStyleSheet.php b/framework/Web/UI/WebControls/TStyleSheet.php
index 02ac40cd..5925645c 100644
--- a/framework/Web/UI/WebControls/TStyleSheet.php
+++ b/framework/Web/UI/WebControls/TStyleSheet.php
@@ -52,11 +52,8 @@ class TStyleSheet extends TControl
 	 */
 	public function onPreRender($param)
 	{
-		if($this->getEnabled(true))
-		{
-			if(($url=$this->getStyleSheetUrl())!=='')
-				$this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
-		}
+		if(($url=$this->getStyleSheetUrl())!=='')
+			$this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
 	}
 
 	/**
-- 
cgit v1.2.3