Calendar
[ class tree: Calendar ] [ index: Calendar ] [ all elements ]

Source for file 20.php

Documentation is available at 20.php

  1. <?php
  2. /**
  3. * Description: demonstrates a decorator used to "attach a payload" to a selection
  4. * to make it available when iterating over calendar children
  5. */
  6.  
  7. //if you use ISO-8601 dates, switch to PearDate engine
  8. define('CALENDAR_ENGINE''PearDate');
  9.  
  10. if !@include 'Calendar/Calendar.php' {
  11.     define('CALENDAR_ROOT','../../');
  12. }
  13.  
  14. require_once CALENDAR_ROOT . 'Month/Weekdays.php';
  15. require_once CALENDAR_ROOT . 'Day.php';
  16. require_once CALENDAR_ROOT . 'Decorator.php';
  17.  
  18. // accepts multiple entries
  19. {
  20.     var $entries = array();
  21.  
  22.     function DiaryEvent($calendar{
  23.         Calendar_Decorator::Calendar_Decorator($calendar);
  24.     }
  25.  
  26.     function addEntry($entry{
  27.         $this->entries[$entry;
  28.     }
  29.  
  30.     function getEntry({
  31.         $entry each($this->entries);
  32.         if ($entry{
  33.             return $entry['value'];
  34.         else {
  35.             reset($this->entries);
  36.             return false;
  37.         }
  38.     }
  39. }
  40.  
  41. {
  42.     //Calendar engine
  43.     var $cE;
  44.     var $tableHelper;
  45.  
  46.     var $year;
  47.     var $month;
  48.     var $firstDay = false;
  49.  
  50.     function build($events=array())
  51.     {
  52.         require_once CALENDAR_ROOT . 'Day.php';
  53.         require_once CALENDAR_ROOT .  'Table/Helper.php';
  54.  
  55.         $this->tableHelper = new Calendar_Table_Helper($this$this->firstDay);
  56.         $this->cE = $this->getEngine();
  57.         $this->year  = $this->thisYear();
  58.         $this->month = $this->thisMonth();
  59.  
  60.         $daysInMonth $this->cE->getDaysInMonth($this->year$this->month);
  61.         for ($i=1; $i<=$daysInMonth$i++{
  62.             $Day = new Calendar_Day(2000,1,1)// Create Day with dummy values
  63.             $Day->setTimeStamp($this->cE->dateToStamp($this->year$this->month$i));
  64.             $this->children[$i= new DiaryEvent($Day);
  65.         }
  66.         if (count($events> 0{
  67.             $this->setSelection($events);
  68.         }
  69.         Calendar_Month_Weekdays::buildEmptyDaysBefore();
  70.         Calendar_Month_Weekdays::shiftDays();
  71.         Calendar_Month_Weekdays::buildEmptyDaysAfter();
  72.         Calendar_Month_Weekdays::setWeekMarkers();
  73.         return true;
  74.     }
  75.  
  76.     function setSelection($events)
  77.     {
  78.         $daysInMonth $this->cE->getDaysInMonth($this->year$this->month);
  79.         for ($i=1; $i<=$daysInMonth$i++{
  80.             $stamp1 $this->cE->dateToStamp($this->year$this->month$i);
  81.             $stamp2 $this->cE->dateToStamp($this->year$this->month$i+1);
  82.             foreach ($events as $event{
  83.                 if (($stamp1 >= $event['start'&& $stamp1 $event['end']||
  84.                     ($stamp2 >= $event['start'&& $stamp2 $event['end']||
  85.                     ($stamp1 <= $event['start'&& $stamp2 $event['end'])
  86.                 {
  87.                     $this->children[$i]->addEntry($event);
  88.                     $this->children[$i]->setSelected();
  89.                 }
  90.             }
  91.         }
  92.     }
  93.  
  94.     function fetch()
  95.     {
  96.         $child each($this->children);
  97.         if ($child{
  98.             return $child['value'];
  99.         else {
  100.             reset($this->children);
  101.             return false;
  102.         }
  103.     }
  104. }
  105.  
  106. // Calendar instance used to get the dates in the preferred format:
  107. // you can switch Calendar Engine and the example still works
  108. $cal = new Calendar;
  109.  
  110. $events = array();
  111. //add some events
  112. $events[= array(
  113.     'start' => $cal->cE->dateToStamp(20046110),
  114.     'end'   => $cal->cE->dateToStamp(20046112),
  115.     'desc'  => 'Important meeting'
  116. );
  117. $events[= array(
  118.     'start' => $cal->cE->dateToStamp(20046121),
  119.     'end'   => $cal->cE->dateToStamp(2004612359),
  120.     'desc'  => 'Dinner with the boss'
  121. );
  122. $events[= array(
  123.     'start' => $cal->cE->dateToStamp(200465),
  124.     'end'   => $cal->cE->dateToStamp(20046102359),
  125.     'desc'  => 'Holidays!'
  126. );
  127.  
  128.  
  129.  
  130. $Month new Calendar_Month_Weekdays(20046);
  131. $MonthDecorator = new MonthPayload_Decorator($Month);
  132. $MonthDecorator->build($events);
  133.  
  134. ?>
  135. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
  136. <html>
  137. <head>
  138. <title> Calendar </title>
  139. <style text="text/css">
  140. table {
  141.     border-collapse: collapse;
  142. }
  143. caption {
  144.     font-family: verdana;
  145.     font-size: 14pt;
  146.     padding-bottom: 4pt;
  147. }
  148. th {
  149.     font-family: verdana;
  150.     font-size: 11px;
  151.     text-align: center;
  152.     background-color: #e7e3e7;
  153.     padding: 5pt;
  154.     line-height: 150%;
  155.     border: 1px solid #ccc;
  156. }
  157. td {
  158.     font-family: verdana;
  159.     font-size: 11px;
  160.     text-align: left;
  161.     vertical-align: top;
  162. }
  163. td.calCell {
  164.     border: 1px solid #b5bece;
  165.     padding: 3px;
  166. }
  167. td.calCellEmpty {
  168.     background-color: #f3f3f7;
  169. }
  170. td.calCellBusy {
  171.     background-color: #efeffa;
  172. }
  173. div.dayNumber {
  174.     text-align: right;
  175.     background-color: #f8f8f8;
  176.     border-bottom: 1px solid #ccc;
  177. }
  178. ul {
  179.     margin-left: 0;
  180.     margin-top: 5pt;
  181.     padding: 0 10pt 0 12pt;
  182.     list-style-type: square;
  183. }
  184. </style>
  185. </head>
  186.  
  187. <body>
  188.  
  189. <h2>Sample Calendar Payload Decorator (using <?php echo CALENDAR_ENGINE?> engine)</h2>
  190. <table class="calendar" width="98%" cellspacing="0" cellpadding="0">
  191. <caption>
  192.     <?php echo $MonthDecorator->thisMonth().' / '.$MonthDecorator->thisYear()?>
  193. </caption>
  194. <tr>
  195.     <th>Monday</th>
  196.     <th>Tuesday</th>
  197.     <th>Wednesday</th>
  198.     <th>Thursday</th>
  199.     <th>Friday</th>
  200.     <th>Saturday</th>
  201.     <th>Sunday</th>
  202. </tr>
  203. <?php
  204. while ($Day $MonthDecorator->fetch()) {
  205.  
  206.     if ($Day->isFirst()) {
  207.         echo "<tr>\n";
  208.     }
  209.  
  210.     echo '<td class="calCell';
  211.     if ($Day->isSelected()) {
  212.         echo ' calCellBusy';
  213.     elseif ($Day->isEmpty()) {
  214.         echo ' calCellEmpty';
  215.     }
  216.     echo '">';
  217.     echo '<div class="dayNumber">'.$Day->thisDay().'</div>';
  218.  
  219.     if ($Day->isEmpty()) {
  220.         echo '&nbsp;';
  221.     else {
  222.         echo '<div class="dayContents"><ul>';
  223.         while ($entry $Day->getEntry()) {
  224.             echo  '<li>'.$entry['desc'].'</li>';
  225.             //you can print the time range as well
  226.         }
  227.         echo '</ul></div>';
  228.     }
  229.     echo '</td>';
  230.  
  231.     if ($Day->isLast()) {
  232.         echo "</tr>\n";
  233.     }
  234. }
  235. ?>
  236. </table>
  237. </body>
  238. </html>

Documentation generated on Mon, 11 Mar 2019 15:37:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.