<?php
/**
 * weekyear
 *
 * dentalair voorraadbeheer weekyear-class
 *
 * @version    1.0
 * @author     Erik Timmers, amgate <erik.timmers@amgate.com>
 * @copyright  Amgate, 2010
 * @since      14-01-2010 - initial creation
 * @package    CDVB0100
 */

require_once 'class.stdModule.php';


class weekyear
{
    
    var $_week;
    var $_year;

    /**
     * revenue
     *
     * PHP4 constructor
     *
     * @access public
     * @return void
     */
    function weekyear($nWeek = null, $nYear = null)
    {
        //$this->stdModule('cdvb0100');
        //$this->setVariable('modcode', 'cdvb0100');
        
        $this->_week = ($nWeek ? $nWeek : date('W'));
        $this->_year = ($nYear ? $nYear : date('Y'));
    }
    
    /**
     * year
     *
     * get year
     *
     * @access public
     * @return integer
     */
    function year() 
    {
        return $this->_year;
    }

    /**
     * week
     *
     * get week
     *
     * @access public
     * @return integer
     */
    function week() 
    {
        return $this->_week;
    }
    
    /**
     * subtractWeek
     *
     * subtract number of weeks of current object and return an weekyear object 
     *
     * @access public
     * @param  int    $nWeek number of weeks to subtract
     * @return object
     */    
    function subtractWeek($nWeek) {
        $tsMonday = $this->getFirstDay();
        
        $tsMondayNew = strtotime('-' . ($nWeek). ' weeks', $tsMonday);
        
        return new weekyear(date('W', $tsMondayNew), date('Y', $tsMondayNew));
    }

    /**
     * addWeek
     *
     * add number of weeks to current object and return an weekyear object 
     *
     * @access public
     * @param  int    $nWeek number of weeks to add
     * @return object
     */     
    function addWeek($nWeek) {
        $tsMonday = $this->getFirstDay();
        
        $tsMondayNew = strtotime('+' . ($nWeek). ' weeks', $tsMonday);
        
        return new weekyear(date('W', $tsMondayNew), date('Y', $tsMondayNew));
    }

 
    /**
     * getFirstDay
     *
     * returns timestamp of first day of week/year
     *
     * @access public
     * @return integer
     */
    function getFirstDay()
    {
        $offset = date('w', mktime(0, 0, 0, 1, 1, $this->_year));
        $offset = ($offset < 5) ? 1 - $offset : 8 - $offset;
        $monday = mktime(0, 0, 0, 1, 1 + $offset, $this->_year);
                
        return strtotime('+' . ($this->_week - 1) . ' weeks', $monday);
        
    }
    
    /**
     * greaterThen
     *
     * compare two weekyears and return true if one is greater than two
     *
     * @access public
     * @param  object   $owyOne week/year object 1
     * @param  object   $owyOne week/year object 2
     * @return boolean
     */    
    function greaterThen($owyOne, $owyTwo)
    {
        if ($owyOne->year() > $owyTwo->year()) {
            return true;
        } elseif ($owyOne->year() < $owyTwo->year()) {
            return false;
        } elseif ($owyOne->week() > $owyTwo->week()) {
            return true;
        } elseif ($owyOne->week() <= $owyTwo->week()) {
            return false;
        }
            
    } 
    
    /**
     * enumerateGap
     *
     * return an array of all weeks between object and another week/years-object
     * format array( 'YYYYmm-string' => array ( 'year', 'week') )
     *
     * @access public
     * @param  int    $nName beschrijving
     * @param  string $sName beschrijving
     * @param  array $aName beschrijving
     * @return void
     */
    function enumerateGap($owyTwo)
    {
        if ($this->greaterThen($this, $owyTwo)) {
            // to do
        } else {
            for($y = $this->year(); $y <= $owyTwo->year(); $y++){
                $firstweek = ($y == $this->year() ? $this->week() : 1);
                // 28 dec is always in last week, (jan 4 always in the first)
                $lastweek  = ($y == $owyTwo->year() ? $owyTwo->week() : date('W', mktime(0, 0, 0, 12, 28, $y)));
                
                for($w = $firstweek; $w <= $lastweek; $w++) {
                    $aReturn[$y . str_pad($w, 2, '0', STR_PAD_LEFT)] = array( 'year' => $y,
                                                                              'week' => str_pad($w, 2, '0', STR_PAD_LEFT)
                                                                             );
                }
            }           
        }    
        
        return $aReturn;    
    } 
    
    /**
     * toArray
     *
     * return an array ( 'year', 'week').
     * e.g. for use in smarty
     *
     * @access public
     * @return array
     */    
    function toArray() {
        return array('week' => $this->_week,
                     'year' => $this->_year
                     );
    }
 
    
}