I am attempting to create a calendar interface using specially formated
daytime
events as 5 digit strings. For example '11630' would mean Monday's 4:30pm.

In order to parse out the logic I created this php based template in a
separate
file. I am wondering how I would go about implementing this in PHPTAL.
Begining
with some best practices for "for loops" with iteration. Again, I really
just want to
replace this code with phptal code. This code originally was just a test.
=============================================================================
<?php
date_default_timezone_set('America/New_York');
$range = array(
    array(
        'start' => '21050',
        'end' => '21600'
    ) ,
    array(
        'start' => '01500',
        'end' => '01630'
    ) ,
    array(
        'start' => '31800',
        'end' => '31830'
    )
);
/**
 * timeWithin
 *
 * @param mixed $rangeEvent
 * @param Array $availbility
 * @param mixed $incrementfifteen
 * @access public
 * @return boolean
 */
 function timeWithin($rangeEvent, Array $availbility, &$incrementfifteen =
null) {
     if (preg_match('/^\d{3}(00|15|30|45)$/', $rangeEvent) == 1) {
        $dayHour = substr($rangeEvent, 0, 3);
        $minuteofthehour = substr($rangeEvent, 3, 2);
        $plusfifteen = (int)($minuteofthehour) + 15;
         foreach ($availbility as $rang) {
            $sdh = substr($rang['start'], 0, 3); // Start Day Hour
            $sh = substr($rang['start'], 1, 2); // Start Hour
            $sdm = substr($rang['start'], 3, 2); // Start Minutes
            $edh = substr($rang['end'], 0, 3); // End Day Hour
            $eh = substr($rang['end'], 1, 2); // End Hour
            $edm = substr($rang['end'], 3, 2); // End Minutes
             if ((((int)($rangeEvent)) >= ((int)($rang['start'])))  &&
(((int)($rangeEvent)) < ((int)($rang['end'])))) {
                 return true;
            }
        }
    }
     else {
         throw  new Exception("String input $rangeEvent should be a 5 digit
string only. With the last two character representing 15 minute increments
of an hour. 00,15,30,45");
    }
     return false;
}
/**
 * timeStart
 *
 * @param mixed $rangeEvent
 * @param Array $availbility
 * @param mixed $incrementfifteen
 * @access public
 * @return void
 */
 function timeStart($rangeEvent, Array $availbility, &$incrementfifteen =
null) {
     if (preg_match('/^\d{3}(00|15|30|45)$/', $rangeEvent) == 1) {
        $dayHour = substr($rangeEvent, 0, 3);
        $minuteofthehour = substr($rangeEvent, 3, 2);
        $plusfifteen = (int)($minuteofthehour) + 15;
         foreach ($availbility as $rang) {
            $sdh = substr($rang['start'], 0, 3); // Start Day Hour
            $sh = substr($rang['start'], 1, 2); // Start Hour
            $sdm = substr($rang['start'], 3, 2); // Start Minutes
            $edh = substr($rang['end'], 0, 3); // End Day Hour
            $eh = substr($rang['end'], 1, 2); // End Hour
            $edm = substr($rang['end'], 3, 2); // End Minutes
            // Check start time of each to see if it is within fifteen of
start or end
             if ($dayHour == $sdh) {
                 if ((((int)($sdm)) >= ((int)($minuteofthehour)))  &&
(((int)($sdm)) < $plusfifteen)) {
                    $incrementfifteen = ceil(((mktime((int)$eh, (int)$edm) -
mktime((int)$sh, (int)$sdm)) / 60) / 15);
                     return true;
                }
            }
        }
    }
     else {
         throw  new Exception("String input $rangeEvent should be a 5 digit
string only. With the last two character representing 15 minute increments
of an hour. 00,15,30,45");
    }
     return false;
}
/**
 * extremeHours
 *
 * @param Array $rng
 * @access public
 * @return void
 */
 function extremeHours(Array $rng) {
    $top = null;
    $bottom = null;
     foreach ($rng as $hour) {
        $top = (!isset($top)) ? (int)(substr(($hour['start']) , 1, 2)) :
$top;
        $bottom = (!isset($bottom)) ? (int)(substr(($hour['end']) , 1, 2)) :
$bottom;
        $top = ((substr($hour['start'], 1, 2)) < $top) ?
(int)substr($hour['start'], 1, 2) : $top;
        $bottom = ((int)(substr($hour['end'], 1, 2)) > $bottom) ?
(int)substr($hour['end'], 1, 2) : $bottom;
    }
    $extremes = array(
        'top' => $top,
        'bottom' => $bottom
    );
     return $extremes;
}
$tremes = extremeHours($range);
$earliest = $tremes['top'];
$latest = $tremes['bottom'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
<html metal:use-macro="wrapper.tal/wrapper"
    xmlns="http://www.w3.org/1999/xhtml";
    xmlns:xlink="http://www.w3.org/1999/xlink";
    xmlns:tal="http://xml.zope.org/namespaces/tal";
    xmlns:metal="http://xml.zope.org/namespaces/metal";
    xml:lang="en" lang="en">
    <head>
        <title>Availability Form</title>
        <style type="text/css">
            /*
                Calendar CSS - by Nathan Smith - www.sonspring.com
            */

            /* =Basic HTML

----------------------------------------------------------------------*/

            a
            {
                cursor: pointer;
                text-decoration: none;
            }

            body
            {
                background: #ddc url(../img/body.gif) repeat-y top center;
                color: #693;
                cursor: default;
                margin: 0;
                padding: 0;
            }

            caption
            {
                background: url(../img/logo.gif) no-repeat;
                border-top: 5px solid #c96;
                margin: 0 auto;
                text-align: right;
                font: 20px/30px Georgia, serif;
            }

            p
            {
                margin: 0;
            }

            strong
            {
                text-transform: uppercase;
            }

            table
            {
                background: #fff;
                border-bottom: 3px solid #9c6;
                border-right: 1px solid #ccb;
                margin: 0 auto;
                height:3em;
                overflow:scroll;
            }

            th,
            td
            {
                border-left: 1px solid #ccb;
                border-top: 1px solid #ccb;
                font-family: Verdana, sans-serif;
                padding: 0 2px;
                vertical-align: top;
                width: 100px;
                height: inherit;
            }

            td a
            {
                background: url(../img/arrow.gif) no-repeat center left;
                color: #33f;
                padding: 0 0 0 10px;
            }

            td a:hover
            {
                text-decoration: underline;
            }

            /* =Classes

----------------------------------------------------------------------*/

            col.Sat,
            col.Sun
            {
                background: #efe;
            }

            div.buttons
            {
                font: 10px Verdana, sans-serif;
                padding: 10px 0 20px;
                text-align: center;
            }

            div.buttons a
            {
                background: #ddc;
                border: 1px solid #bba;
                color: #000;
                padding: 3px 5px;
            }

            div.buttons a:hover
            {
                background-color: #efe;
                color: #693;
            }

            tr.day
            {
                color: #665;
                font-size: 9px;
                height: 90px;
            }

            tr.number
            {
                background: #eee;
                color: #998;
                font-size: 10px;
                height: 15px;
                text-align: right;
                vertical-align: middle;
            }

            th
            {
                background: #998;
                color: #fff;
                font-size: 11px;
                font-variant: small-caps;
                font-weight: normal;
                height: 15px;
                text-transform: capitalize;
                text-align: center;
                vertical-align: middle;
            }
            .week {border-collapse:collapse}
            .block {display:block;background-color:#333;margin-top:1px;}
            .available {background-color:#333;}
        </style>
    </head>
    <body>
        <table class="week" summary="Week Display">
            <caption>Weekly Availability Display</caption>
            <colgroup>
                <col class="hour" />
                <col class="Sun" />
                <col class="Mon" />
                <col class="Tue" />
                <col class="Wed" />
                <col class="Thu" />
                <col class="Fri" />
                <col class="Sat" />
            </colgroup>
            <thead>
                <tr>
                    <th>Time</th>
                    <th>Sunday</th>
                    <th>Monday</th>
                    <th>Tuesday</th>
                    <th>Wednesday</th>
                    <th>Thursday</th>
                    <th>Friday</th>
                    <th>Saturday</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Time</th>
                    <th>Sunday</th>
                    <th>Monday</th>
                    <th>Tuesday</th>
                    <th>Wednesday</th>
                    <th>Thursday</th>
                    <th>Friday</th>
                    <th>Saturday</th>
                </tr>
            </tfoot>
            <tbody>
                <tr>
                    <th rowspan="1">All Day</th>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>

<?php  for ($i = 0;$i < 24;$i++): ?>
<?php  if (($latest >= $i)  && ($i >= $earliest)): ?>
<?php  for ($j = 0;$j < 4;$j++): ?>
                <tr>
<?php  if ($j == 0): ?>
                    <th rowspan="4"><?php  echo date('g:i a', mktime($i,
0)); ?></th>
<?php  endif; ?>
<?php  for ($k = 0;$k < 7;$k++):
                    $timeid = $k . sprintf('%02d', $i) . sprintf('%02d', ($j
* 15));
                    $timetitle = date('l', strtotime("+$k day",
strtotime("last Sunday"))) . " " . date('g:i a', mktime($i, ($j * 15)));
                    $rowspan;
?>
<?php  if (timeStart($timeid, $range, &$rowspan)): ?>
                    <td class="available" rowspan="<?php  echo $rowspan; ?>"
id="<?php  echo $timeid; ?>" title="<?php  echo $timetitle; ?>">&nbsp;</td>
<?php  elseif (!timeWithin($timeid, $range)): ?>
                    <td id="<?php  echo $timeid; ?>" title="<?php  echo
$timetitle; ?>">&nbsp;</td>
<?php  endif; ?>
<?php  endfor; ?>
                </tr>
<?php  endfor; ?>
<?php  endif; ?>
<?php  endfor; ?>

            </tbody>
        </table>
    </body>
</html>
=============================================================================
_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to