summaryrefslogtreecommitdiff
path: root/framework/Web
diff options
context:
space:
mode:
authorxue <>2006-04-07 19:07:14 +0000
committerxue <>2006-04-07 19:07:14 +0000
commit325b2ab61e3cf03e9ae7fb03ee1030cae6b08b3e (patch)
treedcd1adebecc1f0270872f0f8e78c6d4601c6499a /framework/Web
parent1d94593d09b68786f7035e7b06ec6d8324894d77 (diff)
Removed TDataValueFormatter.
Diffstat (limited to 'framework/Web')
-rw-r--r--framework/Web/UI/WebControls/TDataGridColumn.php30
-rw-r--r--framework/Web/UI/WebControls/TListControl.php41
2 files changed, 62 insertions, 9 deletions
diff --git a/framework/Web/UI/WebControls/TDataGridColumn.php b/framework/Web/UI/WebControls/TDataGridColumn.php
index ea825360..e43b4895 100644
--- a/framework/Web/UI/WebControls/TDataGridColumn.php
+++ b/framework/Web/UI/WebControls/TDataGridColumn.php
@@ -14,7 +14,6 @@
* Includes TDataFieldAccessor and TDataValueFormatter classes
*/
Prado::using('System.Util.TDataFieldAccessor');
-Prado::using('System.Util.TDataValueFormatter');
/**
* TDataGridColumn class
@@ -333,15 +332,36 @@ abstract class TDataGridColumn extends TApplicationComponent
/**
* Formats the text value according to a format string.
- * It uses {@link TDataValueFormatter} to do the actual formatting.
+ * 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 mixed the data to be formatted
* @return string the formatted result
- * @see TDataValueFormatter::format()
*/
protected function formatDataValue($formatString,$value)
{
- return TDataValueFormatter::format($formatString,$value,$this);
+ if($formatString==='')
+ return TPropertyValue::ensureString($value);
+ else if($formatString[0]==='#')
+ {
+ $expression=strtr(substr($formatString,1),array('{0}'=>'$value'));
+ try
+ {
+ if(eval("\$result=$expression;")===false)
+ throw new Exception('');
+ return $result;
+ }
+ catch(Exception $e)
+ {
+ throw new TInvalidDataValueException('datagridcolumn_expression_invalid',get_class($this),$expression,$e->getMessage());
+ }
+ }
+ else
+ return sprintf($formatString,$value);
}
}
diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php
index 5c1e9083..1c615edc 100644
--- a/framework/Web/UI/WebControls/TListControl.php
+++ b/framework/Web/UI/WebControls/TListControl.php
@@ -16,7 +16,6 @@
Prado::using('System.Web.UI.WebControls.TDataBoundControl');
Prado::using('System.Collections.TAttributeCollection');
Prado::using('System.Util.TDataFieldAccessor');
-Prado::using('System.Util.TDataValueFormatter');
/**
@@ -70,8 +69,8 @@ Prado::using('System.Util.TDataValueFormatter');
* to 'name' and 'age' will make the first item's text be 'John', value be 31,
* 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
- * {@link TDataValueFormatter::format()}.
+ * used to format how the item should be displayed. See {@link formatDataValue()}
+ * for an explanation of the format string.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
@@ -188,7 +187,7 @@ abstract class TListControl extends TDataBoundControl
$text=$object;
$item->setValue("$key");
}
- $item->setText(TDataValueFormatter::format($textFormat,$text,$this));
+ $item->setText($this->formatDataValue($textFormat,$text));
$items->add($item);
}
// SelectedValue or SelectedIndex may be set before databinding
@@ -582,6 +581,40 @@ abstract class TListControl extends TDataBoundControl
}
}
}
+
+ /**
+ * 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 to be formatted
+ * @return string the formatted result
+ */
+ protected function formatDataValue($formatString,$value)
+ {
+ if($formatString==='')
+ return TPropertyValue::ensureString($value);
+ else if($formatString[0]==='#')
+ {
+ $expression=strtr(substr($formatString,1),array('{0}'=>'$value'));
+ try
+ {
+ if(eval("\$result=$expression;")===false)
+ throw new Exception('');
+ return $result;
+ }
+ catch(Exception $e)
+ {
+ throw new TInvalidDataValueException('listcontrol_expression_invalid',get_class($this),$expression,$e->getMessage());
+ }
+ }
+ else
+ return sprintf($formatString,$value);
+ }
}
/**