Source

Apmgr / application / modules / calendar / models / Calendar.php

Full commit
<?php
/**
 * Calendar model to handle the calendar events.
 * @author Jorge Omar Vazquez <jvazquez@debserverp4.com.ar>
 */

abstract class Calendar_Model_Calendar extends ZFModel_ParentModel {

	/**
	 * Valid constants for the recursive period
	 */
	CONST DAY = 'dd';

	CONST WEEK = 'ww';

	CONST MONTH = 'MM';

	CONST YEAR = 'y';

	CONST FORMATSQL = 'sql';

	CONST FORMATVIEW = 'regular';

	CONST REGULAR = 'array';

	/**
	 * Read from the application.ini , the format of the date.
	 * @var string
	 */
	protected $dateFormat;

	/**
	 * The Sql Format for this date
	 * @var string
	 */
	protected $dateSqlFormat;

	/**
	 * The Sql Format for time
	 * @var string
	 */
	protected $timeSqlFormat;

	/**
	 * Read from application.ini , the format of the time
	 * @var string
	 */
	protected $timeFormat;

	/**
	 * @var Zend_Config_Ini
	 */
	protected $zendConfigIni;

	/**
	 * Sets the date Format fo this application
	 * @return Calendar_Model_Calendar
	 */
	public function initDateFormat() {
		$this->dateFormat = $this->zendConfigIni->appsetings->date->format;
		$this->dateSqlFormat = $this->zendConfigIni->appsetings->date->sqlformat;
		return $this;
	}

	/**
	 *
	 * @return Calendar_Model_Calendar
	 */
	public function initTimeFormat() {
		$this->timeFormat = $this->zendConfigIni->appsetings->time->format;
		$this->timeSqlFormat = $this->zendConfigIni->appsetings->time->sqlformat;
		return $this;
	}

	/**
	 * Sets the configuration file to use
	 * Initialize the format of the dates, refer to http://ar2.php.net/manual/en/function.date.php and
	 * http://framework.zend.com/manual/en/zend.date.constants.html#zend.date.constants.phpformats
	 * @param Zend_Config_Ini $zendConfigIni
	 * @return Calendar_Model_Calendar
	 */
	public function setConfigFile() {
		$this->zendConfigIni = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini',APPLICATION_ENV);
		return $this;
	}

	/**
	 * Retrieve two zend date objects and compare them, and retrieve the difference
	 * @param Zend_Date $dateA
	 * @param Zend_Date $dateB
	 * @param String $format The kind of comparison you are doing
	 * @throws Zend_Date_Exception
	 * @return integer
	 */
	public function compareDates(Zend_Date $dateA,Zend_Date $dateB,$format=self::DAY) {
		$result = 0;//They are equal
		$result = $dateA->compare($dateB,$format);
		return $result;
	}

	/**
	 * Convert a string to the desired array format
	 * @param string $date
	 * @return array.
	 */
	static public function properDateFormat($date) {
		$buffer = explode('-',$date);
		return array('year'=>$buffer[0],'month'=>$buffer[1],'day'=>$buffer[2]);
	}

        /**
         * Convert a time array to the proper time format
         * @param <type> $time
         * @return <type>
         */
	static public function properTimeFormat($time) {
		$buffer = explode(':',$time);
		return array('hour'=>$buffer[0],'minute'=>$buffer[1],'seconds'=>$buffer[2]);
	}
        
        /**
         * Convert a timestamp to the proper format
         * @param string $timestamp
         * @param string $format
         */
        public function timeStampToDateFormat($timestamp,$format) {
            Zend_Date::setOptions(array('format_type' => 'php'));
            $zendDate =  new Zend_Date($timestamp,null,Zend_Registry::get('Zend_Locale'));
            return $zendDate->toString($format);
        }

}