сегодня я переделал код и отделил отображение от логики
interface Show{
function show();
}
class Calendar{
private $date; //сдесь храним дату на которую делаем календарь
private $arr_calendar; //массив из всех дат(DateTime) месяца
function __construct(DateTime $date) { //принимаем только формат DateTime
$this->date=$date;
}
/**
* функция возвращает массив обектов состоящих из дат текущего месяца
* @return array
*/
function get_calendar(){
// идем от 1 числа по всем числам месяца
for ($i=1;$this->date->format('t')>=$i;$i++){
$tmp_date = new DateTime($i.".".$this->date->format('m').".".$this->date->format('Y'));
$this->arr_calendar[] = array(
"oject_data" => $tmp_date, //дата объектом
"active_data" => $this->test_curret_date($tmp_date), //проверку на текущию дату
);
}
return $this->arr_calendar;
}
/**
* если переданная дата будет равна дате $this->date то вернем true
* @param DateTime $current_date проверяемая дата
* @return boolean
*/
private function test_curret_date(DateTime $current_date){
if ($current_date->format('d.m.Y')==$this->date->format('d.m.Y')){
return true;
}else{
return false;
}
}
}
class ShowCalendar implements Show{
private $calendar;
private $calendar_format;//формат в днях недели 1=>"Понедельник", ... 7=>"Воскресение",
private $list_property=array("show_dat", "dat_format", "show_mount");
private $show_dat=true;
private $dat_format=self::DAY_SHORT_FORMAT;
private $show_mount=true;
private $mount_format=self::MOUNT_FULL_FORMAT;
const DAY_SHORT_FORMAT = "d.m";
const MOUNT_FULL_FORMAT = "m.Y";
function set_params($key, $value){
if (in_array($key, $this->list_property)){
$this->$key=$value;
}
}
function __construct(Calendar $calendar,array $calendar_format = array(
1=>"Понедельник",
2=>"Вторник",
3=>"Среда",
4=>"Четверг",
5=>"Пятница",
6=>"Суббота",
7=>"Воскресение",
) ) {
$this->calendar=$calendar->get_calendar();
$this->calendar_format=$calendar_format;
}
/**
* функция получения отображения
* @return string
*/
function show() {
//отдельно получаем и собираем вместе элементы (имитация темплейта)
$template = $this->show_get_style().
$this->show_get_header().
$this->show_get_body().
$this->show_get_footer();
return $template;
}
/**
* функция получения header
* @return string
*/
private function show_get_header(){
$template="<table>".
"<tr>";
if ($this->show_mount){
$mount_tmp=current($this->calendar);
$template .="<tr>".
'<td colspan=7>'.
$mount_tmp["oject_data"]->format($this->mount_format).
"</td>".
"</tr>";
}
foreach ($this->calendar_format as $day_name){
$template .="<td>".
$day_name.
"</td>";
}
$template .="</tr>";
return $template;
}
/**
* функция возвращает footer таблицы
* @return string
*/
private function show_get_footer(){
$template ="</table>";
return $template;
}
/**
* функция возращает основное тело таблицы
* @return string
*/
private function show_get_body(){
//получаем календарь в виде массива
$arr_calendar=array();
foreach ($this->calendar as $day_element){
if (!isset($arr_calendar[$day_element["oject_data"]->format("W")])){
$arr_calendar[$day_element["oject_data"]->format("W")]=array();
}
$arr_calendar[$day_element["oject_data"]->format("W")][$day_element["oject_data"]->format("N")]=array(
"data" =>$day_element["oject_data"]->format($this->dat_format),
"active"=>$day_element["active_data"],
);
}
//собираем календарь в таблицу
$template="";
foreach($arr_calendar as $row){
$template .="<tr>";
for($i=1;7>=$i;$i++){
if (isset($row[$i])){
//ячейка определена
$template .="<td ".($row[$i]["active"]&&$this->show_dat?'class="active"':'').">";
$template .=$row[$i]["data"];
$template .="</td>";
}else{
//ячейка пустая
$template .="<td></td>";
}
}
$template .="</tr>";
}
return $template;
}
/**
* функция возвращает стили для календаря
* @return string
*/
private function show_get_style(){
$template='<style type="text/css">'
.'.active{'
." color: red;"
." background: #ccc;"
.'}'
.'table tr td{'
." text-align: center;"
." border: 1px solid black; "
." padding: 5px; "
.'}'
.'</style>';
return $template;
}
}
$n=new DateTime("now");
$n1=$n->sub(new DateInterval('P1M'));
$calendar = new ShowCalendar(new Calendar($n1));
$calendar->set_params("show_dat", false);
echo $calendar->show();
$n2=$n->add(new DateInterval('P1M'));
$calendar = new ShowCalendar(new Calendar($n2));
echo $calendar->show();
$n3=$n->add(new DateInterval('P1M'));
$calendar = new ShowCalendar(new Calendar($n3));
$calendar->set_params("show_dat", false);
echo $calendar->show();
Комментариев нет:
Отправить комментарий