<?php
class Calendar {
protected $year;
protected $month;
protected $day;
protected $shortWeekdayNames = true;
protected $dateFormat ='d-m-Y';
protected static $weekdayNames = array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
);
protected $onTitleRenderFunc = null;
protected $onDayRenderFunc = null;
protected $onCurrentDayRenderFunc = null;
protected $id;
protected $cssClass;
public function __construct(){
$this->year = date('Y');
$this->month = date('m');
$this->day = date('d');
}
public function setId($id){
$this->id = $id;
}
public function setCssClassName($cssClass){
$this->cssClass = $cssClass;
}
public function setDate($day, $month, $year){
$this->day = $day;
$this->month = $month;
$this->year = $year;
return $this;
}
public function setDateFormat($format){
$this->dateFormat = $format;
return $this;
}
public function setShortWeekDayNames($bool){
$this->shortWeekdayNames = $bool;
return $this;
}
public function onTitleRender($onTitleRenderFunc){
if(is_callable($onTitleRenderFunc)){
$this->onTitleRenderFunc = $onTitleRenderFunc;
}
return $this;
}
public function onDayRender($onDayRenderFunc){
if(is_callable($onDayRenderFunc)){
$this->onDayRenderFunc = $onDayRenderFunc;
}
return $this;
}
public function onCurrentDayRender($onCurrentDayRenderFunc){
if(is_callable($onCurrentDayRenderFunc)){
$this->onCurrentDayRenderFunc = $onCurrentDayRenderFunc;
}
return $this;
}
public function render(){
$timestamp = mktime(0, 0, 0, $this->month, 1, $this->year);
$control = '<table border= "1" id= "' . $this->id . '" class= "' . $this->cssClass . '">';
if($this->onTitleRenderFunc){
$prevMonth = mktime(0, 0, 0, $this->month-1, $this->day, $this->year);
$currentMonth = mktime(0, 0, 0, $this->month, $this->day, $this->year);
$nextMonth = mktime(0, 0, 0, $this->month+1, $this->day, $this->year);
$titleLabel = call_user_func_array($this->onTitleRenderFunc, array($currentMonth, $prevMonth, $nextMonth));
}else{
$titleLabel = date('F - Y', $timestamp);
}
$control .= '<thead><tr><td colspan= "7">' . $titleLabel . '</td></tr><tr>';
foreach(static::$weekdayNames as $name){
$name = ($this->shortWeekdayNames) ? substr($name, 0,3) : $name;
$control .='<th>'.$name.'</th>';
}
$control .='</tr></thead><tbody>';
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year);
$monthStart = getdate($timestamp);
$weekDay = $monthStart['wday'];
$totalDays = $daysInMonth + $weekDay;
$weeks = ceil($totalDays / 7);
$day = 0;
$bDays = 0;
for($i=0; $i < $weeks; $i++){
$control .= '<tr>' . PHP_EOL;
for($wd=0; $wd < 7; $wd++){
if($bDays > ($weekDay-1) && $day < $daysInMonth){
++$day;
$cellDateTimestamp = $timestamp = mktime(0, 0, 0, $this->month, $day, $this->year);
if($this->onDayRenderFunc){
$dayLabel = call_user_func_array($this->onDayRenderFunc, array($day, $this->month, $this->year));
}else{
$dayLabel = $day;
}
if($this->onCurrentDayRenderFunc && $day == $this->day){
$dayLabel = call_user_func_array($this->onCurrentDayRenderFunc, array($day, $this->month, $this->year));
}
$control .= '<td data-date= "' . date($this->dateFormat, $cellDateTimestamp) . '">'.$dayLabel.'</td>';
}
elseif($day > $daysInMonth){
$control .= '<td> </td>';
}else{
++$bDays;
$control .= '<td> </td>';
}
}
$control .= '</tr>' . PHP_EOL;
}
return $control . '</tbody></table>';
}
}
?>
No comments:
Post a Comment