PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/protected/extensions/behaviors/DateTimeI18NBehavior.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 107 lines | 86 code | 12 blank | 9 comment | 24 complexity | 3dc8f1f01c5a840066625561bf8424c4 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * DateTimeI18NBehavior
  4. * Automatically converts date and datetime fields to I18N format
  5. *
  6. * Author: Ricardo Grana <rickgrana@yahoo.com.br>, <ricardo.grana@pmm.am.gov.br>
  7. * Version: 1.1
  8. * Requires: Yii 1.0.9 version
  9. */
  10. class DateTimeI18NBehavior extends CActiveRecordBehavior
  11. {
  12. public $dateOutcomeFormat = 'Y-m-d';
  13. public $dateTimeOutcomeFormat = 'Y-m-d H:i:s';
  14. public $dateIncomeFormat = 'yyyy-MM-dd';
  15. public $dateTimeIncomeFormat = 'yyyy-MM-dd hh:mm:ss';
  16. public $inFormat = 'long|short|medium';
  17. public $outFormat = 'medium';
  18. public function beforeSave($event){
  19. $informat = explode('|', $this->inFormat);
  20. //search for date/datetime columns. Convert it to pure PHP date format
  21. foreach($event->sender->tableSchema->columns as $columnName => $column){
  22. if (($column->dbType != 'date') and ($column->dbType != 'datetime')) continue;
  23. if (!is_string($event->sender->$columnName)) continue;
  24. if (!strlen($event->sender->$columnName)){
  25. $event->sender->$columnName = null;
  26. continue;
  27. }
  28. if (($column->dbType == 'date')) {
  29. foreach ($informat as $dateWidth) {
  30. Yii::log("Convert ".$event->sender->$columnName . " from format " . $dateWidth . ":" . Yii::app()->locale->getDateFormat($dateWidth), 'warning', 'ext.behaviors.DateTimeI18N');
  31. $timestamp = CDateTimeParser::parse($event->sender->$columnName, Yii::app()->locale->getDateFormat($dateWidth));
  32. if ($timestamp != FALSE) {
  33. $event->sender->$columnName = date($this->dateOutcomeFormat, $timestamp);
  34. break;
  35. }
  36. }
  37. if ($timestamp == FALSE) {
  38. $msg = "";
  39. foreach ($informat as $dateWidth) {
  40. $msg .= Yii::app()->locale->getDateFormat($dateWidth)."\n";
  41. }
  42. throw new CException("Invalid date time specified: ".$event->sender->$columnName." Available formats are: ".$msg, 500);
  43. }
  44. }else{
  45. foreach ($informat as $dateWidth) {
  46. Yii::log("Convert ".$event->sender->$columnName . " from format " . $dateWidth . ":" . Yii::app()->locale->getDateFormat($dateWidth), 'warning', 'ext.behaviors.DateTimeI18N');
  47. $timestamp = CDateTimeParser::parse($event->sender->$columnName,
  48. strtr('{0} {1}',
  49. array("{0}" => Yii::app()->locale->getTimeFormat($dateWidth),
  50. "{1}" => Yii::app()->locale->getDateFormat($dateWidth))));
  51. if ($timestamp != FALSE) {
  52. $event->sender->$columnName = date($this->dateTimeOutcomeFormat, $timestamp);
  53. break;
  54. }
  55. $timestamp = CDateTimeParser::parse($event->sender->$columnName,
  56. strtr('{1} {0}',
  57. array("{0}" => Yii::app()->locale->getTimeFormat($dateWidth),
  58. "{1}" => Yii::app()->locale->getDateFormat($dateWidth))));
  59. if ($timestamp != FALSE) {
  60. $event->sender->$columnName = date($this->dateTimeOutcomeFormat, $timestamp);
  61. break;
  62. }
  63. }
  64. if ($timestamp == FALSE) {
  65. $msg = "";
  66. foreach ($informat as $dateWidth)
  67. foreach ($informat as $timeWidth)
  68. $msg .= strtr(Yii::app()->locale->dateTimeFormat, array("{0}" => Yii::app()->locale->getTimeFormat($timeWidth), "{1}" => Yii::app()->locale->getDateFormat($dateWidth)))."\n";
  69. throw new CException("Invalid date time specified: ".$event->sender->$columnName." Available formats are: ".$msg, 500);
  70. }
  71. }
  72. }
  73. return true;
  74. }
  75. public function afterFind($event){
  76. foreach($event->sender->tableSchema->columns as $columnName => $column){
  77. if (($column->dbType != 'date') and ($column->dbType != 'datetime')) continue;
  78. if (!is_string($event->sender->$columnName)) continue;
  79. if (!strlen($event->sender->$columnName)){
  80. $event->sender->$columnName = null;
  81. continue;
  82. }
  83. if ($column->dbType == 'date'){
  84. $event->sender->$columnName = Yii::app()->dateFormatter->formatDateTime(
  85. CDateTimeParser::parse($event->sender->$columnName, $this->dateIncomeFormat),$this->outFormat,null);
  86. }else{
  87. $event->sender->$columnName =
  88. Yii::app()->dateFormatter->formatDateTime(
  89. CDateTimeParser::parse($event->sender->$columnName, $this->dateTimeIncomeFormat),
  90. $this->outFormat, $this->outFormat);
  91. }
  92. }
  93. return true;
  94. }
  95. }