From 54572d1e4a5914e44bcd35fa9406ea72a8f2066f Mon Sep 17 00:00:00 2001
From: wei <>
Date: Sun, 19 Feb 2006 22:25:23 +0000
Subject: Update TRatingList and date picker.

---
 framework/Web/UI/WebControls/TDatePicker.php | 114 +++++++++++++++---
 framework/Web/UI/WebControls/TRatingList.php | 173 +++++++++++++++------------
 2 files changed, 191 insertions(+), 96 deletions(-)

(limited to 'framework/Web/UI/WebControls')

diff --git a/framework/Web/UI/WebControls/TDatePicker.php b/framework/Web/UI/WebControls/TDatePicker.php
index f563e36e..8d4de13e 100644
--- a/framework/Web/UI/WebControls/TDatePicker.php
+++ b/framework/Web/UI/WebControls/TDatePicker.php
@@ -274,10 +274,26 @@ class TDatePicker extends TTextBox
 	public function render($writer)
 	{
 		if($this->getInputMode() == 'TextBox')
+		{
 			parent::render($writer);
+			$this->renderDatePickerButtons($writer);
+		}
 		else
+		{
 			$this->renderDropDownListCalendar($writer);
-	
+			if($this->hasDayPattern())
+			{
+				$this->registerCalendarClientScript();
+				$this->renderDatePickerButtons($writer);
+			}
+		}
+	}
+
+	/**
+	 * Renders the date picker popup buttons.
+	 */
+	protected function renderDatePickerButtons($writer)
+	{
 		if($this->getShowCalendar())
 		{
 			switch ($this->getMode())
@@ -317,12 +333,26 @@ class TDatePicker extends TTextBox
 	 */
 	protected function getDateFromPostData($key, $values)
 	{
-		$day = $values[$key.'$day'];
-		$month = $values[$key.'$month'];
-		$year = $values[$key.'$year'];
-		$date = @mktime(0, 0, 0, $month+1, $day, $year);
-		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter', 
-						$this->getDateFormat());
+		$date = @getdate();
+		
+		if(isset($values[$key.'$day']))
+			$day = intval($values[$key.'$day']);
+		else
+			$day = 1;
+
+		if(isset($values[$key.'$month']))
+			$month = intval($values[$key.'$month']) + 1;
+		else
+			$month = $date['mon'];
+
+		if(isset($values[$key.'$year']))
+			$year = intval($values[$key.'$year']);
+		else
+			$year = $date['year'];
+		$date = @mktime(0, 0, 0, $month, $day, $year);
+		$pattern = $this->getDateFormat();
+		$pattern = str_replace(array('MMMM', 'MMM'), array('MM','MM'), $pattern);
+		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter', $pattern);
 		return $formatter->format($date);
 	}
 
@@ -369,7 +399,8 @@ class TDatePicker extends TTextBox
 	protected function getCurrentCulture()
 	{
 		$app = $this->getApplication()->getGlobalization();
-		return $this->getCulture() == '' ? $app->getCulture() : $this->getCulture();
+		return $this->getCulture() == '' ? 
+				($app ? $app->getCulture() : 'en') : $this->getCulture();
 	}
 
 	/**
@@ -401,21 +432,43 @@ class TDatePicker extends TTextBox
 		$writer->renderBeginTag('span');
 
 		$date = $this->getDateFromText();
-
-		//renders the 3 drop down lists
-		$this->renderCalendarDayOptions($writer,$date['mday']);
-		$this->renderCalendarMonthOptions($writer,$date['mon']-1);
-		$this->renderCalendarYearOptions($writer,$date['year']);
-
+		$this->renderCalendarSelections($writer, $date);
+	
 		//render a hidden input field
 		$writer->addAttribute('name', $this->getUniqueID());
 		$writer->addAttribute('type', 'hidden');
 		$writer->addAttribute('value', $this->getText());
 		$writer->renderBeginTag('input');
-	
-		$this->registerCalendarClientScript();
+		
 		$writer->renderEndTag();
 	}
+
+	protected function hasDayPattern()
+	{
+		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter', 
+						$this->getDateFormat());
+		return !is_null($formatter->getDayPattern());		
+	}
+
+	/**
+	 * Renders the calendar drop down list depending on the DateFormat pattern.
+	 * @param THtmlWriter the Html writer to render the drop down lists.
+	 * @param array the current selected date
+	 */
+	protected function renderCalendarSelections($writer, $date)
+	{
+		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter', 
+						$this->getDateFormat());
+		foreach($formatter->getDayMonthYearOrdering() as $type)
+		{
+			if($type == 'day')
+				$this->renderCalendarDayOptions($writer,$date['mday']);
+			elseif($type == 'month')
+				$this->renderCalendarMonthOptions($writer,$date['mon']-1);
+			elseif($type == 'year')
+				$this->renderCalendarYearOptions($writer,$date['year']);
+		}
+	}
 	
 	/**
 	 * Gets the date from the text input using TSimpleDateFormatter
@@ -423,8 +476,9 @@ class TDatePicker extends TTextBox
 	 */
 	protected function getDateFromText()
 	{
-		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter', 
-						$this->getDateFormat());
+		$pattern = $this->getDateFormat();
+		$pattern = str_replace(array('MMMM', 'MMM'), array('MM','MM'), $pattern);
+		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter',$pattern);
 		return $formatter->parse($this->getText());
 	}
 
@@ -476,10 +530,32 @@ class TDatePicker extends TTextBox
 		$writer->addAttribute('class', 'datepicker_month_options');
 		$writer->renderBeginTag('select');
 		$this->renderDropDownListOptions($writer, 
-					$info->getMonthNames(), $selected);
+					$this->getLocalizedMonthNames($info), $selected);
 		$writer->renderEndTag();
 	}
 
+	/**
+	 * Returns the localized month names that depends on the month format pattern.
+	 * "MMMM" will return the month names, "MM" or "MMM" return abbr. month names
+	 * and "M" return month digits.
+	 * @param DateTimeFormatInfo localized date format information.
+	 * @return array localized month names.
+	 */
+	protected function getLocalizedMonthNames($info)
+	{
+		$formatter = Prado::createComponent('System.Data.TSimpleDateFormatter', 
+						$this->getDateFormat());
+		switch($formatter->getMonthPattern())
+		{
+			case 'MMM':
+			case 'MM': return $info->getAbbreviatedMonthNames();
+			case 'M': 
+				$array = array(); for($i=1;$i<=12;$i++) $array[$i] = $i;
+				return $array;
+			default :	return $info->getMonthNames();
+		}
+	}
+
 	/**
 	 * Renders the year drop down list options.
 	 * @param THtmlWriter the writer used for the rendering purpose
diff --git a/framework/Web/UI/WebControls/TRatingList.php b/framework/Web/UI/WebControls/TRatingList.php
index 006391f7..b2861f2b 100644
--- a/framework/Web/UI/WebControls/TRatingList.php
+++ b/framework/Web/UI/WebControls/TRatingList.php
@@ -12,6 +12,7 @@ Prado::using('System.Web.UI.WebControls.TRadioButtonList');
  */
 class TRatingList extends TRadioButtonList
 {
+	private $_ratingImages = array();
 
 	public function __construct()
 	{
@@ -19,12 +20,30 @@ class TRatingList extends TRadioButtonList
 		$this->getRepeatInfo()->setRepeatDirection('Horizontal');
 	}
 
-	/**
-	 * @param string the direction (Vertical, Horizontal) of traversing the list
-	 */
-	public function setRepeatDirection($value)
+	public function getAllowInput()
+	{
+		return $this->getViewState('AllowInput', true);
+	}
+
+	public function setAllowInput($value)
+	{
+		$this->setViewState('AllowInput', TPropertyValue::ensureBoolean($value), true);
+	}
+
+	public function getRating()
+	{
+		if($this->getAllowInput())
+			return $this->getSelectedIndex();
+		else
+			return $this->getViewState('Rating',0);
+	}
+
+	public function setRating($value)
 	{
-		throw new TNotSupportedException('ratinglits_repeatdirection_unsupported');
+		if($this->getAllowInput())
+			$this->setSelectedIndex($value);
+		else
+			return $this->setViewState('Rating', TPropertyValue::ensureFloat($value),0);
 	}
 
 	/**
@@ -40,19 +59,13 @@ class TRatingList extends TRadioButtonList
 	 */
 	public function getRatingStyle()
 	{
-	   $style = $this->getViewState('RatingStyle', 'default');
-	   return is_string($style) ? $this->createRatingStyle($style) : $style;
-	}
-
-	protected function createRatingStyle($type)
-	{
-		return new TRatingListDefaultStyle;
+	   return $this->getViewState('RatingStyle', 'default');
 	}
 
 	/**
 	 * @return string caption text. Default is "Rate It:".
 	 */
-	public function getCaptionText()
+	public function getCaption()
 	{
 		return $this->getViewState('Caption', 'Rate It:');
 	}
@@ -60,40 +73,60 @@ class TRatingList extends TRadioButtonList
 	/**
 	 * @param string caption text
 	 */
-	public function setCaptionText($value)
+	public function setCaption($value)
 	{
 		$this->setViewState('Caption', $value, 'Rate It:');
 	}
 
+
+	public function setHalfRatingLimit($value)
+	{
+		$this->setViewState('HalfRating', 
+				TPropertyValue::ensureArray($value), array(0.3, 0.7));
+	}
+
+	public function getHalfRatingLimit()
+	{
+		return $this->getViewState('HalfRating', array(0.3, 0.7));
+	}
+
 	public function getRatingClientOptions()
 	{
-		$options = $this->getRatingStyle()->getOptions();
+		$options['cssClass'] = 'TRatingList_'.$this->getRatingStyle();
 		$options['ID'] = $this->getClientID();
-		$options['caption'] = $this->getCaptionText();
+		$options['caption'] = $this->getCaption();
 		$options['field'] = $this->getUniqueID();
-		$options['total'] = $this->getItems()->getCount();
-		$options['pos'] = $this->getSelectedIndex();
+		$options['selectedIndex'] = $this->getSelectedIndex();
 		return $options;
 	}
 
-	protected function publishRatingListStyle()
+	protected function publishRatingListStyle($style)
 	{
 		$cs = $this->getPage()->getClientScript();
-		$style = $this->getRatingStyle()->getStyleSheet();
-		$url = $this->publishFilePath($style);
+		$stylesheet = 'System.Web.Javascripts.ratings.'.$style;
+		if(($cssFile=Prado::getPathOfNamespace($stylesheet,'.css'))===null)
+			throw new TConfigurationException('ratinglist_stylesheet_not_found',$style);
+		$url = $this->publishFilePath($cssFile);
 		if(!$cs->isStyleSheetFileRegistered($style))
 			$cs->registerStyleSheetFile($style, $url);
 		return $url;
 	}
 
-	protected function publishRatingListAssets()
+	protected function publishRatingListImages($style, $fileExt='.gif')
 	{
 		$cs = $this->getPage()->getClientScript();
-		$assets = $this->getRatingStyle()->getAssets();
-		$list = array();
-		foreach($assets as $file)
-			$list[] = $this->publishFilePath($file);
-		return $list;
+		$images['blank'] = "System.Web.Javascripts.ratings.{$style}_blank";
+		$images['hover'] = "System.Web.Javascripts.ratings.{$style}_hover";
+		$images['selected'] = "System.Web.Javascripts.ratings.{$style}_selected";
+		$images['half'] = "System.Web.Javascripts.ratings.{$style}_half";
+		$files = array();
+		foreach($images as $type => $image)
+		{
+			if(($file=Prado::getPathOfNamespace($image, $fileExt))===null)
+				throw TConfigurationException('ratinglist_image_not_found',$image);
+			$files[$type] = $this->publishFilePath($file);
+		}
+		return $files;
 	}
 
 	/**
@@ -102,8 +135,21 @@ class TRatingList extends TRadioButtonList
 	public function onPreRender($param)
 	{
 		parent::onPreRender($param);
-		$this->publishRatingListStyle();
-		$this->publishRatingListAssets();
+
+		$this->publishRatingListStyle($this->getRatingStyle());
+		$this->_ratingImages = $this->publishRatingListImages($this->getRatingStyle());
+
+		if($this->getAllowInput())
+			$this->registerRatingListClientScript();
+		else
+		{
+			$this->getRepeatInfo()->setCaption($this->getCaption());
+			$this->setAttribute('title', $this->getRating());
+		}
+	}
+
+	protected function registerRatingListClientScript()
+	{
 		$id = $this->getClientID();
 		$scripts = $this->getPage()->getClientScript();
 		$scripts->registerPradoScript('prado');
@@ -111,65 +157,38 @@ class TRatingList extends TRadioButtonList
 		$code = "new Prado.WebUI.TRatingList($options);";
 		$scripts->registerEndScript("prado:$id", $code);
 	}
-}
 
-abstract class TRatingListStyle
-{
-	private $_options = array();
-
-	public function __construct()
+	public function renderItem($writer,$repeatInfo,$itemType,$index)
 	{
-		$options['pos'] = -1;
-		$options['dx'] = 22;
-		$options['dy'] = 30;
-		$options['ix'] = 4;
-		$options['iy'] = 4;
-		$options['hx'] = 240;
-		$options['total'] = -1;
-		$this->_options = $options;
+		if($this->getAllowInput())
+			parent::renderItem($writer, $repeatInfo, $itemType, $index);
+		else
+			$this->renderRatingListItem($writer, $repeatInfo, $itemType, $index);
 	}
 
-	public function getOptions()
+	protected function renderRatingListItem($writer, $repeatInfo, $itemType, $index)
 	{
-		return $this->_options;
-	}
-
-	public function setOptions($options)
-	{
-		$this->_options = $options;
-	}
-
-	abstract function getStyleSheet();
-
-	abstract function getAssets();
-}
-
-class TRatingListDefaultStyle extends TRatingListStyle
-{
-	public function __construct()
-	{
-		parent::__construct();
-		$options = $this->getOptions();
-		$options['cssClass'] = 'TRatingList_default';
-		$this->setOptions($options);
+		$image = new TImage;		
+		$image->setImageUrl($this->_ratingImages[$this->getRatingImageType($index)]);
+		$image->setAlternateText($this->getRating());
+		$image->render($writer);
 	}
 
-	public function getStyleSheet()
+	protected function getRatingImageType($index)
 	{
-		$style = 'System.Web.Javascripts.ratings.default';
-		if(($cssFile=Prado::getPathOfNamespace($style,'.css'))===null)
-			throw new TConfigurationException('ratinglist_stylesheet_invalid',$style);
-		return $cssFile;
+		$rating = floatval($this->getRating());
+		$int = intval($rating);
+		$limit = $this->getHalfRatingLimit();
+		if($index < $int || ($rating < $index + 1 && $rating > $index +$limit[1]))
+			return 'selected';
+		if($rating >= $index+$limit[0] && $rating <= $index+$limit[1])
+			return 'half';
+		return 'blank';
 	}
 
-	public function getAssets()
+	public function generateItemStyle($itemType,$index)
 	{
-		$assets = array();
-		$image = 'System.Web.Javascripts.ratings.10star_white';
-		if(($file=Prado::getPathOfNamespace($image, '.gif'))===null)
-			throw TConfigurationException('ratinglist_asset_invalid',$image);
-		$assets[] =  $file;
-		return $assets;
+		return new TStyle;
 	}
 }
 
-- 
cgit v1.2.3