<?php
/**
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

include_once('config.inc.php');

/**
 * Allows date operations and outputs.
 *
 * @access   private
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	Date.class, Sun Nov 28 13:23:03 CET 2004
 * @package	Types
 */
class Date {
	/**
	 * @access private
	 * @var    integer
	 */
	var $timeStamp;

	function Date($date, $format='Y-m-d')	{
		// setlocale (LC_TIME, "french");
    $this->setValue($date, $format);
  }

  function setValue($date, $format='Y-m-d') {
		if(is_array($date)) {
			$year   = $date['year'];
			$month	= $date['month'];
			$day		= $date['day'];
		}
		else {
			list($year, $month, $day) = sscanf($date, "%04d-%02d-%02d");
		}
		if($year == "0000"){
			$this->timeStamp = 0;
		}
		else {
			$this->timeStamp = mktime(0, 0, 0, $month, $day, $year);
    }
  }

  function getType() {
    return EASY_CMS_TYPE_DATE;
  }

	function compareTo(&$dateObj){
		return $this->equals($dateObj);
	}
	
  function getYear() {
		if($this->timeStamp > 0) {
			return date('Y', $this->timeStamp);
		}
		return '0000';
  }

  function getMonth() {
		if($this->timeStamp > 0) {
			return date("n", $this->timeStamp);
		}
		return "00";
  }

  function getDay() {
		if($this->timeStamp > 0) {
			return date("j", $this->timeStamp);
		}
		return "00";
  }

  function getDate() {
    return sprintf("%04d-%02d-%02d", $this->getYear(), $this->getMonth(), $this->getDay());
  }

  function checkDate() {
    if (!checkdate($this->getMonth(), $this->getDay(), $this->getYear())) {
      return -1;
    }
    return 1;
  }

  function getDayName($index = -1) {
    global $days_names;
    if ($index < 0) {
      $index = $this->getDayOfWeekIndex();
    }
    return $days_names[$index];
  }

  function getMonthName($index = -1) {
    global $months_names;
    $month_name = '';
    if ($index < 0) {
      $index = $this->getMonth();
    }
    if($index > 0) {
      $month_name = $months_names[$index];
    }
    return $month_name;
  }

  function getWeekOfYearIndex() {
    if ($this->checkDate() < 0) {
      return -1;
    }
    return Date::weekOfYear($this->getDay(), $this->getMonth(), $this->getYear());
  }

  function getWeekOfMonthIndex() {
    return $this->getWeekOfYearIndex() - Date::weekOfYear(1, $this->getMonth(), $this->getYear()) + 1;
  }

  function getDayOfWeekIndex() {
    $day = getdate($this->timeStamp);
    return $day["wday"];
  }

  function getDayOfYearIndex() {
    $cnt = 0;
    for($i = 1 ; $i < $this->getMonth(); $i++) {
      $cnt += date("t", mktime (0, 0, 0, $i, 1, $this->getYear()));
    }
    $cnt += $this->getDay();
    return $cnt;
  }

  function addYears($years) {
    $this->timeStamp = mktime(0, 0, 0, $this->getMonth(), $this->getDay(), $this->getYear() + $years);
  }

  function addMonths($months) {
    $this->timeStamp = mktime(0, 0, 0, $this->getMonth() + $months, $this->getDay(), $this->getYear());
  }

  function addDays($days) {
    $this->timeStamp = mktime(0, 0, 0, $this->getMonth(), $this->getDay() + $days, $this->getYear());
  }

  function equals(&$date) {
    if ($date->toInt() == $this->toInt()) {
      return 0;
    }
    if ($date->toInt() > $this->toInt()) {
      return -1;
    }
    return 1;
  }

  function getValue() {
		return $this->getDate();
	}
	
  function toInt() {
    return (int) sprintf("%04d%02d%02d", $this->getYear(), $this->getMonth(), $this->getDay());
  }

  function toString() {
    return $this->getDate();
  }

  function toXML() {
		$xml  = "";
    $xml .= "<year>".sprintf("%04d", $this->getYear())."</year>\n";
		$xml .= "<month>".sprintf("%02d", $this->getMonth())."</month>\n";
		$xml .= "<day>".sprintf("%02d", $this->getDay())."</day>\n";
		$xml .= "<month_name><![CDATA[".$this->getMonthName()."]]></month_name>\n";
		$xml .= "<day_name><![CDATA[".ucfirst($this->getDayName())."]]></day_name>\n";
		return $xml;
  }

  function toSQL() {
    return $this->getDate();
  }
	
	/**
	 * @return integer
	 */
  function getCurrentDay() {
    return (int)date('j');
  }
    
  /**
   * @return integer
   */
  function getCurrentMonth() {
    return (int)date('n');
  }
    
  /**
   * @return integer
   */
  function getCurrentYear() {
    return (int)date('Y');
  }

  function formatDate($day, $month, $year) {
    return sprintf("%04d-%02d-%02d", $year, $month, $day);
  }

  function weekOfYear($day, $month, $year) {
    // US : %U, EU : %W
    return strftime("%W", mktime(0, 0, 0, $month, $day, $year));
  }
} 
?>