Skripty : Kalendář

Popis

Tato třída vytiskne kalendář USA, třída je je snadno rozšířitelná na další země. Také je dobrým příkladem dědictví třídy v PHP.

Zdrojový kód



<?php
require_once("fpdf.php");

class PDF_USA_Calendar extends FPDF

{
var $date;
var $squareHeight;
var $squareWidth;
var $longestMonth;

var $tinySquareSize;

function PDF_USA_Calendar($orientation='L', $format="Letter")

{
    $this->FPDF($orientation, 'mm', $format);
    // vypočte název nejdelšího měsíce
    $this->longestMonth = "";

    for ($month = 1; $month <= 12; ++$month)
        {
        $monthYear = gmdate("F Y", jdtounix($this->MDYtoJD($month,1,2009)));

        if (strlen($monthYear) > strlen($this->longestMonth))
            {
            $this->longestMonth = $monthYear;

            }
        }
    // vypočte velikost písma
    $this->tinySquareSize = 4;
    $this->headerFontSize = 70;

    $this->SetFont("Times", "B", $this->headerFontSize);
    $width = $this->w - $this->lMargin - $this->rMargin;

    while ($this->GetStringWidth($this->longestMonth) > $width - $this->tinySquareSize * 22)

        {
        --$this->headerFontSize;
        $this->SetFont("Times", "B", $this->headerFontSize);

        }
}

// užitečná manipulační rutiny pro datum

function JDtoYMD($date, &$year, &$month, &$day)

{
    $string = JDToGregorian($date);
    $month = strtok($string, " -/");

    $day = strtok(" -/");
    $year = strtok(" -/");
}


function MDYtoJD($month, $day, $year)
{
    if (! $month || ! $day || ! $year)

        return 0;
    $a = floor((14-$month)/12);
    $y = floor($year+4800-$a);

    $m = floor($month+12*$a-3);
    $jd = $day+floor((153*$m+2)/5)+$y*365;

    $jd += floor($y/4)-floor($y/100)+floor($y/400)-32045;

    return $jd;
}

function lastMonth($date)
{
    $this->JDtoYMD($date, $year, $month, $day);

    if (--$month == 0)
        {
        $month = 12;
        $year--;
        }

    return GregorianToJD($month, $day, $year);
}

function nextMonth($date)

{
    $this->JDtoYMD($date, $year, $month, $day);
    if (++$month > 12)

        {
        $month = 1;
        ++$year;
        }
    return GregorianToJD($month, $day, $year);

}

function isHoliday($date)
{
    $this->JDtoYMD($date, $year, $month, $day);

    if ($month == 7 && $day == 4)
        return "Den nezávislosti";
    if ($month == 1 && $day == 1)

        return "Nový rok";
    if ($month == 12 && $day == 25)
        return "Vánoce";

    if ($month == 11)
        {
        $dow = gmdate("w", jdtounix($date));

        if ($day == 11 && $dow > 0 && $dow < 6) // does the eleventh fall on a weekday?
            return "Den veteránů";

        if ($dow == 1 && ($day == 12 || $day == 13))
            return "Den veteránů";

        }
    if ($this->isWeekHoliday($date, 4, 4, 11)) // Čtvrtek čtvrtý týden v listopadu

        return "Thanksgiving";
    if ($this->isWeekHoliday($date, 1, 3, 1))

        return "MLK, Jr. Day";
    if ($this->isWeekHoliday($date, 1, 3, 2))

        return "Prezidentský den ";
    if ($this->isWeekHoliday($date, 2, 1, 11))

        return "Den voleb";
    if ($this->isWeekHoliday($date, 1, 1, 9))

        return "Svátek práce";
    if ($this->isWeekHoliday($date, 1, 2, 10))

        return "Svátek";
    if ($this->isWeekHoliday($date, 1, 99, 5))

        return "Pamětní den";
    if ($this->isWeekHoliday($date, 0, 2, 5))

        return "Den matek";
    if ($this->isWeekHoliday($date, 0, 3, 6))

        return "Den otců";
    return "";
}

function isWeekHoliday($date, $dayOfWeek, $weekOfMonth, $monthOfDate)

{
    $this->JDtoYMD($date, $year, $month, $day);
    if ($monthOfDate != $month)

        return 0;
    $jd = jdtounix($date);
    $dow = gmdate("w", $jd);

    if ($dow != $dayOfWeek)
    return 0;
    $daysInMonth = gmdate("t", $jd);

    if ($weekOfMonth > 5 && $day + 6 > $daysInMonth)
        return 1;
     if ($day > ($weekOfMonth - 1) * 7 && $day <= ($weekOfMonth * 7))

        return 1;
    return 0;
}

function tinyCalendar($date, $square)

{
    $this->JDtoYMD($date, $year, $month, $day);
    // Tisk čísla do kolonek

    $wd=gmdate("w",jdtounix($date));
    $cur = $date - $wd;

    $this->SetFont("Helvetica", "B", 10);
    $monthStr = gmdate ("F", jdtounix($date))." $year";

    $this->JDtoYMD($date, $year, $month, $day);
    // Uložit místní kopii souřadnice pro budoucí použití
    $x = $this->x;

    $y = $this->y;
    $this->Cell(7*$square, $square, $monthStr, 0, 0, "C");

    $y+=$square;
    $this->SetXY($x, $y);
    $this->SetFontSize(8);

    for ($i = 1; $i <= 7; ++$i)
        {
        $day = strtoupper(gmdate("l", jdtounix($this->MDYtoJD(2,$i,2009))));

        $this->Cell($square, $square, $day[0], 0,0,"C");

        }
    $y+=$square;
    $this->SetXY($x, $y);
    for ($k=0;$k<6;$k++)

        {
        for ($i=0;$i<7;$i++ )
            {
            $this->JDtoYMD($cur++, $curYear, $curMonth, $curDay);

            if ($curMonth != $month)
                $curDay = " ";
            $this->Cell($square, $square, $curDay, 0, 0, "R");

            }
        $y+=$square;
        $this->SetXY($x, $y);
        }

}

function printDay($date)
{
// Nic, může být potlačen
}

function printHoliday($date)

{
    $x = $this->x;
    $y = $this->y;
    $height = 5.5;

    if ($this->squareHeight < 50)
        $height = 4;
    $widthPercent = .92;

    $fontSize = 11;
    $holiday = $this->isHoliday($date);
    if (strlen($holiday))

        {
        $wd = gmdate("w",jdtounix($date));
        if ($wd != 0 && $wd != 6)

            $this->Cell($this->squareWidth, $this->squareHeight, "", 0, 0, "", true);

        $this->SetXY($x + $this->squareWidth * (1-$widthPercent)/2,$y + $this->squareHeight * 0.83);

        $this->SetFont("Helvetica", "B", $fontSize);
        $this->Cell($this->squareWidth * $widthPercent,$height,$holiday, 0, 0, "C");

        }
}

function printMonth($date)
{
    $this->date = $date;

    $this->JDtoYMD($date, $year, $month, $day);
    $this->AddPage();

    // Vypočítat velikost základny na aktuální nastavení
    $width = $this->w - $this->lMargin - $this->rMargin;

    $height = $this->h - $this->tMargin - $this->bMargin;
    // Tisk předchozí a následující kalendáře
    $this->setXY($this->lMargin,$this->tMargin);
    $this->tinyCalendar($this->lastMonth($date), $this->tinySquareSize);

    $this->setXY($this->lMargin+$width - $this->tinySquareSize * 7,$this->tMargin);

    $this->tinyCalendar($this->nextMonth($date), $this->tinySquareSize);
    // Tisk záhlaví

    $firstLine = $this->tinySquareSize * 8 + $this->tMargin;
    $monthStr = strtoupper(gmdate ("F Y", jdtounix($date)));

    $this->SetXY($this->lMargin,$this->tMargin);
    $this->SetFont("Times", "B", $this->headerFontSize);

    $this->Cell($width, $firstLine, $monthStr, 0,0, "C");

    // Vypočítat počet týdnů v měsíci.
    $wd=gmdate("w",jdtounix($date));
    $start = $date - $wd;

    $numDays = $this->nextMonth($date) - $start;
    $numWeeks = 0;
    while ($numDays > 0)

        {
        $numDays -= 7;
        ++$numWeeks;
        }
    // Výpočet vodorovné čáry
    $this->squareHeight = ($height - 6 - $firstLine) / $numWeeks;

    $horizontalLines = array($firstLine,6);
    for ($i = 0; $i < $numWeeks; ++$i)

        {
        $horizontalLines[$i + 2] = $this->squareHeight;
        }
    // Výpočet svislé čáry

    $this->squareWidth = $width / 7;
    $verticalLines = array($this->lMargin, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth);

    
    // Tisk dnů v týdnu 
    $x = 0;
    $this->SetFont("Times", "B", 12);

    for ($i = 1; $i <= 7; ++$i)
        {
        $x += $verticalLines[$i-1];

        $this->SetXY($x,$firstLine);
        $day = gmdate("l", jdtounix($this->MDYtoJD(2,$i,2009)));

        $this->Cell($verticalLines[$i],6,$day,0,0,"C");

        }
    
    // Tisk čísla v kolonkách
    $wd=gmdate("w",jdtounix($date));
    $cur = $date - $wd;

    $this->SetFont("Times", "B", 20);
    $x = 0;
    $y = $horizontalLines[0];

    for ($k=0;$k<$numWeeks;$k++)
        {
        $y += $horizontalLines[$k+1];

        for ($i=0;$i<7;$i++ )
            {
            $this->JDtoYMD($cur, $curYear, $curMonth, $curDay);

            $x += $verticalLines[$i];
            $this->squareWidth = $verticalLines[$i+1];

            if ($curMonth == $month)
                {
                $this->setXY($x, $y);

                $this->printHoliday($cur);
                $this->setXY($x, $y);
                $this->printDay($cur);

                $this->SetFont("Times", "B", 20);
                $this->SetXY($x,$y+1);

                $this->Cell(5, 5, $curDay);
                }
            ++$cur;

            }
        $x = 0;
        }
    
    // Tisk vodorovné čáry
    $ly = 0;

    $ry = 0;
    foreach ($horizontalLines as $key => $value)
        {
        $ly += $value;

        $ry += $value;
        $this->Line($this->lMargin,$ly,$this->lMargin+$width,$ry);

        }
    // Tisk svislé čáry
    $lx = 0;
    $rx = 0;
    foreach ($verticalLines as $key => $value)

        {
        $lx += $value;
        $rx += $value;
        $this->Line($lx,$firstLine,$rx,$firstLine + 6 + $this->squareHeight * $numWeeks);

        }
}

} // Definice třídy PDF_USA_Calendar
?>



Příklad

<?php

require_once("pdfUSACalendar.php");

class MyCalendar extends PDF_USA_Calendar
{

function printDay($date)

{
    // add logic here to customize a day
    $this->JDtoYMD($date,$year,$month,$day);

    if ($month == 1 && $day == 10)
        {
        $this->SetXY($this->x, $this->y + $this->squareHeight / 2);

        $this->SetFont("Arial", "B", 10);
        $this->Cell($this->squareWidth,5,"Všechno nejlepší k narozeninám!", 0,0, "C");

        }
}

function isHoliday($date)
{
    // Zde vložte vaše oblíbené svátky
    $this->JDtoYMD($date, $year, $month, $day);

    if ($date == easter_days($year) + $this->MDYtoJD(3,21,$year))

        {
        $noSchool = false;
        return "Velikonoce";
        }
    if ($date == easter_days($year) + $this->MDYtoJD(3,21,$year) - 2)

        {
        $noSchool = false;
        return "Velký pátek";
        }
    $jewishDate = explode("/", jdtojewish(gregoriantojd($month,$day,$year)));

    $month = $jewishDate[0];
    $day = $jewishDate[1];
    if ($month == 1 && $day == 1)

        return "Rosh Hashanah";
    if ($month == 1 && $day == 2)
        return "Rosh Hashanah";

    if ($month == 1 && $day == 10)
        return "Yom Kippur";
    if ($month == 3 && $day == 25)

        return "Chanuka";
    if ($month == 8 && $day == 15)
        return "Pesachu ";

    // call the base class for USA holidays
    return parent::isHoliday($date);
}

} // class MyCalendar extends PDF_USA_Calendar

// MyCalendar shows how to customize your calendar with Easter, some select Jewish holidays and a birthday

// Jsou podporovány všechny velikosti papíru, které umí FPDF
$pdf = new MyCalendar("L", "Letter");
// Zde můžete nastavit okraje a šířku linky. PDF_USA_Calendar použije aktuální nastavení..
$pdf->SetMargins(7,7);

$pdf->SetAutoPageBreak(false, 0);
// Nastavit barvu pro vyplnění nevíkendové svátky 
$greyValue = 190;
$pdf->SetFillColor($greyValue,$greyValue,$greyValue);

// Tisk kalendáře na celý rok
$year = gmdate("Y");
for ($month = 1; $month <= 12; ++$month)

    {
    $date = $pdf->MDYtoJD($month, 1, $year);
    $pdf->printMonth($date);

    }
$pdf->Output();
?>



Ukázka

fpdf-kalendar.pdf

Download

fpdf-kalendar.zip

Informace

Author: BKW
License: FPDF