|
|
|
@ -37,6 +37,10 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public $datetimeFormat = 'Y/m/d h:i:s A'; |
|
|
|
|
/** |
|
|
|
|
* @var string the text to be displayed when formatting a null. Defaults to '(not set)'. |
|
|
|
|
*/ |
|
|
|
|
public $nullDisplay; |
|
|
|
|
/** |
|
|
|
|
* @var array the text to be displayed when formatting a boolean value. The first element corresponds |
|
|
|
|
* to the text display for false, the second element for true. Defaults to `array('No', 'Yes')`. |
|
|
|
|
*/ |
|
|
|
@ -61,6 +65,9 @@ class Formatter extends Component
|
|
|
|
|
if (empty($this->booleanFormat)) { |
|
|
|
|
$this->booleanFormat = array(Yii::t('yii', 'No'), Yii::t('yii', 'Yes')); |
|
|
|
|
} |
|
|
|
|
if ($this->nullDisplay === null) { |
|
|
|
|
$this->nullDisplay = Yii::t('yii', '(not set)'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -71,6 +78,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asRaw($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return $value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -81,6 +91,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asText($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return Html::encode($value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -91,6 +104,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asNtext($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return nl2br(Html::encode($value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -103,6 +119,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asParagraphs($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return str_replace('<p></p>', '', |
|
|
|
|
'<p>' . preg_replace('/[\r\n]{2,}/', "</p>\n<p>", Html::encode($value)) . '</p>' |
|
|
|
|
); |
|
|
|
@ -118,6 +137,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asHtml($value, $config = null) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return HtmlPurifier::process($value, $config); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -128,6 +150,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asEmail($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return Html::mailto($value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -138,6 +163,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asImage($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return Html::img($value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -148,6 +176,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asUrl($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
$url = $value; |
|
|
|
|
if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) { |
|
|
|
|
$url = 'http://' . $url; |
|
|
|
@ -163,6 +194,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asBoolean($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
return $value ? $this->booleanFormat[1] : $this->booleanFormat[0]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -183,6 +217,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asDate($value, $format = null) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
$value = $this->normalizeDatetimeValue($value); |
|
|
|
|
return date($format === null ? $this->dateFormat : $format, $value); |
|
|
|
|
} |
|
|
|
@ -204,6 +241,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asTime($value, $format = null) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
$value = $this->normalizeDatetimeValue($value); |
|
|
|
|
return date($format === null ? $this->timeFormat : $format, $value); |
|
|
|
|
} |
|
|
|
@ -225,6 +265,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asDatetime($value, $format = null) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
$value = $this->normalizeDatetimeValue($value); |
|
|
|
|
return date($format === null ? $this->datetimeFormat : $format, $value); |
|
|
|
|
} |
|
|
|
@ -256,6 +299,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asInteger($value) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
if (is_string($value) && preg_match('/^(-?\d+)/', $value, $matches)) { |
|
|
|
|
return $matches[1]; |
|
|
|
|
} else { |
|
|
|
@ -274,6 +320,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asDouble($value, $decimals = 2) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
if ($this->decimalSeparator === null) { |
|
|
|
|
return sprintf("%.{$decimals}f", $value); |
|
|
|
|
} else { |
|
|
|
@ -292,6 +341,9 @@ class Formatter extends Component
|
|
|
|
|
*/ |
|
|
|
|
public function asNumber($value, $decimals = 0) |
|
|
|
|
{ |
|
|
|
|
if ($value === null) { |
|
|
|
|
return $this->nullDisplay; |
|
|
|
|
} |
|
|
|
|
$ds = isset($this->decimalSeparator) ? $this->decimalSeparator: '.'; |
|
|
|
|
$ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ','; |
|
|
|
|
return number_format($value, $decimals, $ds, $ts); |
|
|
|
|