Files
mythoughts/inc/calendar.php
2016-12-16 10:42:04 +13:00

142 lines
3.8 KiB
PHP
Executable File

<?php
class Calendar extends PhpObject
{
const CAL_YEAR = 'cy';
const CAL_MONTH = 'cm';
private $oMySql;
private $oSession;
private $oMask;
private $iUserId;
private $iYear;
private $iMonth;
function __construct($oMySql, $oSession)
{
parent::__construct();
$this->oMySql = $oMySql;
$this->oSession = $oSession;
$this->oMask = new Mask('calendar');
$this->iYear = 0;
$this->iMonth = 0;
}
public function setDate($iYear=0, $iMonth=0)
{
if($iYear==0)
{
$iYear = date('Y');
}
if($iMonth==0)
{
$iMonth = date('m');
}
$this->iYear = $iYear;
$this->iMonth = $iMonth;
}
private function getThoughts()
{
//TODO essayer avec selectRows
$sQuery = "SELECT DATE_FORMAT(led, '%d') AS day
FROM ".Db::THOUGHTS_TABLE."
WHERE ".Db::getId(Db::USERS_TABLE)." = ".$this->oSession->getUserId()."
AND YEAR(led) = ".$this->iYear."
AND MONTH(led) = ".$this->iMonth."
GROUP BY day
ORDER BY day";
return $this->oMySql->getArrayQuery($sQuery, true);
}
private function getUpdatedLink($asParams)
{
$sCurrentVariables = $_SERVER['QUERY_STRING'];
$asCurrentVariables = explode('&', $sCurrentVariables);
foreach($asCurrentVariables as $sParam)
{
$sKey = strstr($sParam, '=', true);
$sValue = substr(strstr($sParam, '='), 1);
$asVariables[$sKey] = $sValue;
}
return '?'.implodeAll(array_merge($asVariables, $asParams), '=', '&');
}
private function getLink($iOffset)
{
$iTimeStamp = mktime(0, 0, 0, $this->iMonth + $iOffset, 1, $this->iYear);
return $this->getUpdatedLink(array(self::CAL_MONTH=>date('n', $iTimeStamp), self::CAL_YEAR=>date('Y', $iTimeStamp)));
}
private function setMaskItems()
{
//week starting on the sunday : offset = 0, monday : offset = 1
$iOffset = 1;
//days in the month
$iMonthLastDay = date('d', mktime(0, 0, 0, $this->iMonth+1, 0, $this->iYear));
$asDays = range(1, $iMonthLastDay);
$iDayNb = 1 - date($iOffset?'N':'w', mktime(0, 0, 0, $this->iMonth, 1, $this->iYear)) + $iOffset;
$iCalendarLastDay = $iMonthLastDay + (7 - date($iOffset?'N':'w', mktime(0, 0, 0, $this->iMonth+1, 0, $this->iYear))) + $iOffset;
//days with thoughts
$asThoughts = $this->getThoughts();
while($iDayNb < $iCalendarLastDay)
{
$iCurrentDayTimeStamp = mktime(0, 0, 0, $this->iMonth, $iDayNb, $this->iYear);
$sItemDate = date('d', $iCurrentDayTimeStamp);
//new week
if(date('w', $iCurrentDayTimeStamp) == $iOffset)
{
$this->oMask->newInstance('WEEK');
}
//day within month
if(date('n', $iCurrentDayTimeStamp)==$this->iMonth)
{
$bThoughts = in_array($iDayNb, $asThoughts);
$sItemClass = $bThoughts?'full':'empty';
$sItemLink = $bThoughts?$this->getUpdatedLink(array('d'=>date(MyThoughts::URL_DATE_FORMAT, $iCurrentDayTimeStamp), 'p'=>'r')):'#';
$sItemLinkTitle = $bThoughts?'See my thoughts on '.date(MyThoughts::LAYOUT_DATE_FORMAT, $iCurrentDayTimeStamp):'';
}
else
{
$sItemClass = 'disabled';
$sItemLink = '#';
$sItemLinkTitle = '';
}
$this->oMask->addInstance('DAY', array('item_day'=>$sItemDate, 'item_class'=>$sItemClass, 'item_link'=>$sItemLink, 'item_link_title'=>$sItemLinkTitle));
$iDayNb++;
}
//column titles
$asDayNames = array('1'=>'Mon', '2'=>'Tue', '3'=>'Wed', '4'=>'Thu', '5'=>'Fri', '6'=>'Sat', $iOffset?'7':'0'=>'Sun');
ksort($asDayNames);
foreach($asDayNames as $sDayName)
{
$this->oMask->addInstance('TITLE', array('day_name'=>$sDayName));
}
}
public function getCalendar()
{
$sResult = '';
if($this->iYear!=0 && $this->iMonth!=0)
{
$this->oMask->setTag('link_prev', $this->getLink(-1));
$this->oMask->setTag('current_month', date('F', mktime(0, 0, 0, $this->iMonth, 1, $this->iYear)));
$this->oMask->setTag('link_next', $this->getLink(1));
$this->setMaskItems();
$sResult = $this->oMask->getMask();
}
return $sResult;
}
}
?>