mambax7/extcal

View on GitHub
class/pear/Calendar/docs/examples/10.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/**
 * Description: demonstrates a decorator to provide simple output formatting
 * on the month while still allowing the days to be accessed via the decorator
 * In practice you _wouldn't_ do this - each decorator comes with a performance
 * hit for extra method calls. For this example some simple functions could help
 * format the month while the days are accessed via the normal Month object.
 */
if (!@require_once __DIR__ . '/Calendar/Calendar.php') {
    define('CALENDAR_ROOT', '../../');
}
require_once CALENDAR_ROOT . 'Month/Weekdays.php';
require_once CALENDAR_ROOT . 'Decorator.php';

// Decorate a Month with methods to improve formatting

/**
 * Class MonthDecorator.
 */
class MonthDecorator extends Calendar_Decorator
{
    /**
     * @param object $Month
     *
     * @internal param $Calendar_Month
     */
    public function __construct(&$Month)
    {
        parent::__construct($Month);
    }

    /**
     * Override the prevMonth method to format the output.
     */
    public function prevMonth()
    {
        $prevStamp = parent::prevMonth(true);

        // Build the URL for the previous month
        return $_SERVER['SCRIPT_NAME'] . '?y=' . date('Y', $prevStamp) . '&m=' . date('n', $prevStamp) . '&d=' . date('j', $prevStamp);
    }

    /**
     * Override the thisMonth method to format the output.
     */
    public function thisMonth()
    {
        $thisStamp = parent::thisMonth(true);

        // A human readable string from this month
        return date('F Y', $thisStamp);
    }

    /**
     * Override the nextMonth method to format the output.
     */
    public function nextMonth()
    {
        $nextStamp = parent::nextMonth(true);

        // Build the URL for next month
        return $_SERVER['SCRIPT_NAME'] . '?y=' . date('Y', $nextStamp) . '&m=' . date('n', $nextStamp) . '&d=' . date('j', $nextStamp);
    }
}

if (!isset($_GET['y'])) {
    $_GET['y'] = date('Y');
}
if (!isset($_GET['m'])) {
    $_GET['m'] = date('n');
}

// Creata a month as usual
$Month = new Calendar_Month_Weekdays($_GET['y'], $_GET['m']);

// Pass it to the decorator and use the decorator from now on...
$MonthDecorator = new MonthDecorator($Month);
$MonthDecorator->build();
?>

<!DOCTYPE html>
<html>
<head>
    <title> A Simple Decorator </title>
</head>
<body>
<h1>A Simple Decorator</h1>
<table>
    <caption><?php echo $MonthDecorator->thisMonth(); ?></caption>
    <?php
    while (false !== ($Day = $MonthDecorator->fetch())) {
        if ($Day->isFirst()) {
            echo "\n<tr>\n";
        }
        if ($Day->isEmpty()) {
            echo '<td>&nbsp;</td>';
        } else {
            echo '<td>' . $Day->thisDay() . '</td>';
        }
        if ($Day->isLast()) {
            echo "\n</tr>\n";
        }
    }
    ?>
    <tr>
        <td><a href="<?php echo $MonthDecorator->prevMonth(); ?>">Prev</a></td>
        <td colspan="5">&nbsp;</td>
        <td><a href="<?php echo $MonthDecorator->nextMonth(); ?>">Next</a></td>
    </tr>
</table>
</body>
</html>