summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxue <>2006-04-07 18:44:07 +0000
committerxue <>2006-04-07 18:44:07 +0000
commit1d94593d09b68786f7035e7b06ec6d8324894d77 (patch)
tree8922c1a64f6f2d49875d31667fae542be9a41eea
parent095e118cd4469f1b77e920a0238798d23089ae8d (diff)
Format string in classes extending TListControl can now evaluate an expression
-rw-r--r--.gitattributes1
-rw-r--r--HISTORY4
-rw-r--r--framework/Util/TDataValueFormatter.php67
-rw-r--r--framework/Web/UI/WebControls/TDataGridColumn.php30
-rw-r--r--framework/Web/UI/WebControls/TListControl.php17
5 files changed, 83 insertions, 36 deletions
diff --git a/.gitattributes b/.gitattributes
index e3cb02e2..0846c876 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -668,6 +668,7 @@ framework/TComponent.php -text
framework/TModule.php -text
framework/TService.php -text
framework/Util/TDataFieldAccessor.php -text
+framework/Util/TDataValueFormatter.php -text
framework/Util/TLogRouter.php -text
framework/Util/TLogger.php -text
framework/Util/TParameterModule.php -text
diff --git a/HISTORY b/HISTORY
index e650cb4f..b3a122a1 100644
--- a/HISTORY
+++ b/HISTORY
@@ -1,6 +1,8 @@
Version 3.1.0 To be released
============================
-ENH: Format string in TDataGrid columns can now evaluate an expression (Qiang)
+ENH: Format string in classes extending TDataGridColumn can now evaluate an expression (Qiang)
+ENH: Format string in classes extending TListControl can now evaluate an expression (Qiang)
+NEW: TDataValueFormatter class (Qiang)
Version 3.0RC2 April 16, 2006
=============================
diff --git a/framework/Util/TDataValueFormatter.php b/framework/Util/TDataValueFormatter.php
new file mode 100644
index 00000000..797a88d1
--- /dev/null
+++ b/framework/Util/TDataValueFormatter.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * TDataValueFormatter class file
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Util
+ */
+
+/**
+ * TDataValueFormatter class
+ *
+ * TDataValueFormatter is a utility class that formats a data value
+ * according to a format string.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Util
+ * @since 3.0
+ */
+class TDataValueFormatter
+{
+ /**
+ * Formats the text value according to a format string.
+ * If the format string is empty, the original value is converted into
+ * a string and returned.
+ * If the format string starts with '#', the string is treated as a PHP expression
+ * within which the token '{0}' is translated with the data value to be formated.
+ * Otherwise, the format string and the data value are passed
+ * as the first and second parameters in {@link sprintf}.
+ * @param string format string
+ * @param mixed the data associated with the cell
+ * @param TComponent the context to evaluate the expression
+ * @return string the formatted result
+ */
+ public static function format($formatString,$value,$context=null)
+ {
+ if($formatString==='')
+ return TPropertyValue::ensureString($value);
+ else if($formatString[0]==='#')
+ {
+ $expression=strtr(substr($formatString,1),array('{0}'=>'$value'));
+ if($context instanceof TComponent)
+ return $context->evaluateExpression($expression);
+ else
+ {
+ try
+ {
+ if(eval("\$result=$expression;")===false)
+ throw new Exception('');
+ return $result;
+ }
+ catch(Exception $e)
+ {
+ throw new TInvalidOperationException('datavalueformatter_expression_invalid',$expression,$e->getMessage());
+ }
+ }
+ }
+ else
+ return sprintf($formatString,$value);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/framework/Web/UI/WebControls/TDataGridColumn.php b/framework/Web/UI/WebControls/TDataGridColumn.php
index a22fa240..ea825360 100644
--- a/framework/Web/UI/WebControls/TDataGridColumn.php
+++ b/framework/Web/UI/WebControls/TDataGridColumn.php
@@ -11,9 +11,10 @@
*/
/**
- * Includes TDataFieldAccessor class
+ * Includes TDataFieldAccessor and TDataValueFormatter classes
*/
Prado::using('System.Util.TDataFieldAccessor');
+Prado::using('System.Util.TDataValueFormatter');
/**
* TDataGridColumn class
@@ -332,36 +333,15 @@ abstract class TDataGridColumn extends TApplicationComponent
/**
* Formats the text value according to a format string.
- * If the format string is empty, the original value is converted into
- * a string and returned.
- * If the format string starts with '#', the string is treated as a PHP expression
- * within which the token '$$' is translated with the data value to be formated.
- * Otherwise, the format string and the data value are passed
- * as the first and second parameters in {@link sprintf}.
+ * It uses {@link TDataValueFormatter} to do the actual formatting.
* @param string format string
* @param mixed the data associated with the cell
* @return string the formatted result
+ * @see TDataValueFormatter::format()
*/
protected function formatDataValue($formatString,$value)
{
- if($formatString==='')
- return TPropertyValue::ensureString($value);
- else if($formatString[0]==='#')
- {
- $expression=strtr(substr($formatString,1),array('$$'=>'$value'));
- try
- {
- if(eval("\$result=$expression;")===false)
- throw new Exception('');
- return $result;
- }
- catch(Exception $e)
- {
- throw new TInvalidOperationException('datagridcolumn_expression_invalid',get_class($this),$expression,$e->getMessage());
- }
- }
- else
- return sprintf($formatString,$value);
+ return TDataValueFormatter::format($formatString,$value,$this);
}
}
diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php
index 37c232e6..5c1e9083 100644
--- a/framework/Web/UI/WebControls/TListControl.php
+++ b/framework/Web/UI/WebControls/TListControl.php
@@ -11,17 +11,13 @@
*/
/**
- * Includes TDataBoundControl class
+ * Includes the supporting classes
*/
Prado::using('System.Web.UI.WebControls.TDataBoundControl');
-/**
- * Includes TAttributeCollection class
- */
Prado::using('System.Collections.TAttributeCollection');
-/**
- * Includes TDataFieldAccessor class
- */
Prado::using('System.Util.TDataFieldAccessor');
+Prado::using('System.Util.TDataValueFormatter');
+
/**
* TListControl class
@@ -75,7 +71,7 @@ Prado::using('System.Util.TDataFieldAccessor');
* the second item's text be 'Cary', value be 28, and so on.
* The {@link setDataTextFormatString DataTextFormatString} property may be further
* used to format how the item should be displayed. The formatting function is
- * the sprintf() PHP function.
+ * {@link TDataValueFormatter::format()}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
@@ -192,7 +188,7 @@ abstract class TListControl extends TDataBoundControl
$text=$object;
$item->setValue("$key");
}
- $item->setText($textFormat===''?$text:sprintf($textFormat,$text));
+ $item->setText(TDataValueFormatter::format($textFormat,$text,$this));
$items->add($item);
}
// SelectedValue or SelectedIndex may be set before databinding
@@ -329,9 +325,10 @@ abstract class TListControl extends TDataBoundControl
/**
* Sets data text format string.
- * The format string is used in sprintf() to format the Text property value
+ * The format string is used in {@link TDataValueFormatter::format()} to format the Text property value
* of each item in the list control.
* @param string the formatting string used to control how data bound to the list control is displayed.
+ * @see TDataValueFormatter::format()
*/
public function setDataTextFormatString($value)
{