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

Language PHP Lines 107
MD5 Hash 3dc8f1f01c5a840066625561bf8424c4 Estimated Cost $2,005 (why?)
Repository https://bitbucket.org/dinhtrung/yiicorecms/ View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
<?php

/*
 * DateTimeI18NBehavior
 * Automatically converts date and datetime fields to I18N format
 *
 * Author: Ricardo Grana <rickgrana@yahoo.com.br>, <ricardo.grana@pmm.am.gov.br>
 * Version: 1.1
 * Requires: Yii 1.0.9 version
 */

class DateTimeI18NBehavior  extends CActiveRecordBehavior
{
	public $dateOutcomeFormat = 'Y-m-d';
	public $dateTimeOutcomeFormat = 'Y-m-d H:i:s';

	public $dateIncomeFormat = 'yyyy-MM-dd';
	public $dateTimeIncomeFormat = 'yyyy-MM-dd hh:mm:ss';

	public $inFormat = 'long|short|medium';
	public $outFormat = 'medium';
	public function beforeSave($event){
		$informat = explode('|', $this->inFormat);

		//search for date/datetime columns. Convert it to pure PHP date format
		foreach($event->sender->tableSchema->columns as $columnName => $column){
			if (($column->dbType != 'date') and ($column->dbType != 'datetime')) continue;
			if (!is_string($event->sender->$columnName)) continue;
			if (!strlen($event->sender->$columnName)){
				$event->sender->$columnName = null;
				continue;
			}

			if (($column->dbType == 'date')) {
				foreach ($informat as $dateWidth) {
					Yii::log("Convert ".$event->sender->$columnName . " from format " . $dateWidth . ":" . Yii::app()->locale->getDateFormat($dateWidth), 'warning', 'ext.behaviors.DateTimeI18N');
					$timestamp = CDateTimeParser::parse($event->sender->$columnName, Yii::app()->locale->getDateFormat($dateWidth));
					if ($timestamp != FALSE) {
						$event->sender->$columnName = date($this->dateOutcomeFormat, $timestamp);
						break;
					}
				}
				if ($timestamp == FALSE) {
					$msg = "";
					foreach ($informat as $dateWidth) {
						$msg .= Yii::app()->locale->getDateFormat($dateWidth)."\n";
					}
					throw new CException("Invalid date time specified: ".$event->sender->$columnName." Available formats are: ".$msg, 500);
				}

			}else{
				foreach ($informat as $dateWidth) {
					Yii::log("Convert ".$event->sender->$columnName . " from format " . $dateWidth . ":" . Yii::app()->locale->getDateFormat($dateWidth), 'warning', 'ext.behaviors.DateTimeI18N');
					$timestamp = CDateTimeParser::parse($event->sender->$columnName,
						strtr('{0} {1}',
							array("{0}" => Yii::app()->locale->getTimeFormat($dateWidth),
								  "{1}" => Yii::app()->locale->getDateFormat($dateWidth))));
					if ($timestamp != FALSE) {
						$event->sender->$columnName = date($this->dateTimeOutcomeFormat, $timestamp);
						break;
					}
					$timestamp = CDateTimeParser::parse($event->sender->$columnName,
						strtr('{1} {0}',
							array("{0}" => Yii::app()->locale->getTimeFormat($dateWidth),
								  "{1}" => Yii::app()->locale->getDateFormat($dateWidth))));
					if ($timestamp != FALSE) {
						$event->sender->$columnName = date($this->dateTimeOutcomeFormat, $timestamp);
						break;
					}
				}
				if ($timestamp == FALSE) {
					$msg = "";
					foreach ($informat as $dateWidth)
						foreach ($informat as $timeWidth)
							$msg .= strtr(Yii::app()->locale->dateTimeFormat, array("{0}" => Yii::app()->locale->getTimeFormat($timeWidth), "{1}" => Yii::app()->locale->getDateFormat($dateWidth)))."\n";
					throw new CException("Invalid date time specified: ".$event->sender->$columnName." Available formats are: ".$msg, 500);
				}
			}

		}

		return true;
	}
	public function afterFind($event){

		foreach($event->sender->tableSchema->columns as $columnName => $column){

			if (($column->dbType != 'date') and ($column->dbType != 'datetime')) continue;
			if (!is_string($event->sender->$columnName)) continue;
			if (!strlen($event->sender->$columnName)){
				$event->sender->$columnName = null;
				continue;
			}

			if ($column->dbType == 'date'){
				$event->sender->$columnName = Yii::app()->dateFormatter->formatDateTime(
								CDateTimeParser::parse($event->sender->$columnName, $this->dateIncomeFormat),$this->outFormat,null);
			}else{
				$event->sender->$columnName =
					Yii::app()->dateFormatter->formatDateTime(
							CDateTimeParser::parse($event->sender->$columnName,	$this->dateTimeIncomeFormat),
							$this->outFormat, $this->outFormat);
			}
		}
		return true;
	}
}
Back to Top