<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * DateTimeFormatterBehavior
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
class DateTimeFormatterBehavior extends CActiveRecordBehavior
{
    // the table column for date added
    public $dateAddedAttribute = 'date_added';
    
    // the table column for last updated
    public $lastUpdatedAttribute = 'last_updated';
    
    // the timezone
    private $_timeZone = null;

    /**
     * DateTimeFormatterBehavior::getDateAdded()
     * 
     * This will format the date added attribute depending on the locale and timezone.
     * 
     * @return string
     */
    public function getDateAdded()
    {
        $dateAdded = $this->owner->hasAttribute($this->dateAddedAttribute) ? $this->owner->getAttribute($this->dateAddedAttribute) : null;
        
        return $this->formatDateTimeValue($dateAdded);
    }
    
    /**
     * DateTimeFormatterBehavior::getLastUpdated()
     * 
     * This will format the date added attribute depending on the locale and timezone.
     * 
     * @return string
     */
    public function getLastUpdated()
    {
        $lastUpdated = $this->owner->hasAttribute($this->lastUpdatedAttribute) ? $this->owner->getAttribute($this->lastUpdatedAttribute) : null;
        
        return $this->formatDateTimeValue($lastUpdated);
    }
    
    /**
     * DateTimeFormatterBehavior::formatDateTimeValue()
     * 
     * @param string $dateTimeValue
     * @param string $inFormat
     * @return mixed
     */
    public function formatDateTimeValue($dateTimeValue, $inFormat = 'yyyy-MM-dd HH:mm:ss')
    {
        if (empty($dateTimeValue)) {
            return;
        }
        
        if ($dateTimeValue == 'NOW()') {
            $dateTimeValue = date('Y-m-d H:i:s');
        }
        
        if (($this->getTimeZone())) {
            $dateTime = new DateTime($dateTimeValue);
            $dateTime->setTimezone(new DateTimeZone($this->timeZone));
            $dateTimeValue = $dateTime->format('Y-m-d H:i:s');
        }
        
        return Yii::app()->dateFormatter->formatDateTime(CDateTimeParser::parse($dateTimeValue, $inFormat), "short", "short");
    }
    
    /**
     * DateTimeFormatterBehavior::getTimeZone()
     * 
     * @return string
     */
    public function getTimeZone()
    {
        if ($this->_timeZone !== null) {
            return $this->_timeZone;
        }
        
        if (Yii::app()->hasComponent('user') && Yii::app()->user->getId() > 0 && !$this->owner->isNewRecord) {
            $this->_timeZone = Yii::app()->user->getModel()->timezone;
        }
        
        if (Yii::app()->hasComponent('customer') && Yii::app()->customer->getId() > 0 && !$this->owner->isNewRecord) {
            $this->_timeZone = Yii::app()->customer->getModel()->timezone;
        }
        
        return $this->_timeZone;
    }
    
    /**
     * DateTimeFormatterBehavior::setTimeZone()
     * 
     * @param mixed $value
     * @return DateTimeFormatterBehavior
     */
    public function setTimeZone($value)
    {
        $this->_timeZone = $value;
        return $this;
    }
    
}