home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / php.exe / Calendar.php < prev    next >
Encoding:
Text File  |  2001-07-02  |  9.8 KB  |  266 lines

  1. //**************************************
  2.     //     
  3.     // Name: Calendar Class
  4.     // Description:To create a navigable cal
  5.     //     endar which can be easily hooked into a 
  6.     //     database.
  7.     // By: Louie Simpson
  8.     //
  9.     //This code is copyrighted and has    // limited warranties.Please see http://
  10.     //     www.Planet-Source-Code.com/xq/ASP/txtCod
  11.     //     eId.279/lngWId.8/qx/vb/scripts/ShowCode.
  12.     //     htm    //for details.    //**************************************
  13.     //     
  14.     
  15.     <? 
  16.     class calendar { 
  17.     //Sets up some class vars 
  18.     var $month; 
  19.     var $year; 
  20.     var $firstdayofmonth; 
  21.     var $lastdayofmonth; 
  22.     var $numdays; 
  23.     var $days; 
  24.     var $monthname; 
  25.     //The constructor class gets pertinent d
  26.     //     ates and set them to the class vars 
  27.     function calendar() { 
  28.     global $month, $year; 
  29.     //If the month isn't set set it top the 
  30.     //     current month 
  31.     if ($month == "") $month = DATE("m"); 
  32.     $this->month = $month; 
  33.     //If the year isn't set set it top the c
  34.     //     urrent month 
  35.     if ($year == "") $year = DATE("Y"); 
  36.     $this->year = $year; 
  37.     //This gets the number of days in the cu
  38.     //     rrent month 
  39.     $this->numdays = date("t", mktime(0,0,0,$this->month, DATE("d"), $this->year)); 
  40.     //This gets the month name for current m
  41.     //     onth 
  42.     $this->monthname = date("F", mktime(0,0,0,$this->month, DATE("d"), $this->year)); 
  43.     //This gets the first day of the month 
  44.     $this->firstdayofmonth = DATE("D", mktime(0,0,0,$this->month, 1, $this->year)); 
  45.     //This gets the last day of the month 
  46.     $this->lastdayofmonth = DATE("D", mktime(0,0,0,$this->month, $this->numdays, $this->year)); 
  47.     //This assigns the daynames to an array 
  48.     //     for use later 
  49.     $this->days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); 
  50.     } 
  51.     //This sets some user defined cosmetic v
  52.     //     ars 
  53.     function set_attributes($colsize, $daycolor, $linkcolor, $bgcolor, $linksize, $daysize, 
  54.     $headersize, $headercolor, $headerweight, $dayheight, $fontface) { 
  55.     $this->daycolor = $daycolor; 
  56.     $this->linkcolor = $linkcolor; 
  57.     $this->bgcolor = $bgcolor; 
  58.     $this->linksize = $linksize; 
  59.     $this->daysize = $daysize; 
  60.     $this->headersize = $headersize; 
  61.     $this->headercolor = $headercolor; 
  62.     $this->headerweight = $headerweight; 
  63.     $this->colsize = $colsize / 7; 
  64.     $this->fontface = $fontface; 
  65.     $this->dayheight = $dayheight; 
  66.     } 
  67.     //Get the number of the firstday of the 
  68.     //     month like 1 for Sunday, 2 for Monday, e
  69.     //     tc.... 
  70.     function getfirst() { 
  71.     //Reset the $days array 
  72.     reset($this->days); 
  73.     //Loop through the $days array and look 
  74.     //     for a match for the 
  75.     //firstdayofmonth and then assign the ke
  76.     //     y to var $colspan 
  77.     while(list($key,$val) = each ($this->days)) { 
  78.     if ($this->firstdayofmonth == $val) { 
  79.     $colspan = $key; 
  80.     } 
  81.     } 
  82.     return $colspan; 
  83.     } 
  84.     function getlast() { 
  85.     //Reset the $days array 
  86.     reset($this->days); 
  87.     //Loop through the $days array and look 
  88.     //     for a match 
  89.     //for the lastdayofmonth and then assign
  90.     //     the key to var $remainder 
  91.     while(list($key,$val) = each ($this->days)) { 
  92.     if ($this->lastdayofmonth == $val) { 
  93.     $remainder = 6 - $key; 
  94.     } 
  95.     } 
  96.     return $remainder; 
  97.     } 
  98.     //This function creates the link for the
  99.     //     previous year 
  100.     function prev_year() { 
  101.     global $PHP_SELF; 
  102.     return sprintf('<a href="%s?month=%s&year=%s" 
  103.     style="font-family:%s;text-decoration:none; 
  104.     color:%s; font-size:%spx">%s</a>', $PHP_SELF, 
  105.     $this->month, $this->year - 1, $this->fontface, 
  106.     $this->linkcolor, $this->linksize, 
  107.     $this->year - 1); 
  108.     } 
  109.     //This function creates the link for the
  110.     //     next year 
  111.     function next_year() { 
  112.     global $PHP_SELF; 
  113.     return sprintf('<a href="%s?month=%s&year=%s" 
  114.     style="font-family:%s;text-decoration:none; 
  115.     color:%s; font-size:%spx">%s</a>', $PHP_SELF, $this->month, 
  116.     $this->year + 1, $this->fontface, $this->linkcolor, 
  117.     $this->linksize, $this->year + 1); 
  118.     } 
  119.     //This function creates the link for the
  120.     //     previous month 
  121.     function prev_month() { 
  122.     global $PHP_SELF; 
  123.     //Check to see if the month is not Janua
  124.     //     ry 
  125.     if ($this->month > 1) { 
  126.     $prevm = sprintf('<a href="%s?month=%s&year=%s" 
  127.     style="font-family:%s;text-decoration:none; 
  128.     color:%s; font-size:%spx"><<</a> ', 
  129.     $PHP_SELF, $this->month - 1, $this->year, $this->fontface, 
  130.     $this->linkcolor, $this->linksize); 
  131.     } 
  132.     //If it is January our prev link will be
  133.     //     December of the previous year 
  134.     else { 
  135.     $prevm = sprintf('<a href="%s?month=%s&year=%s" 
  136.     style="font-family:%s;text-decoration:none; 
  137.     color:%s; font-size:%spx"><<</a> ', 
  138.     $PHP_SELF, 12, $this->year - 1, $this->fontface, 
  139.     $this->linkcolor, $this->linksize); 
  140.     } 
  141.     return $prevm; 
  142.     } 
  143.     //This function creates the link for the
  144.     //     next month 
  145.     function next_month() { 
  146.     global $PHP_SELF; 
  147.     //Check to see if the month is not Decem
  148.     //     ber 
  149.     if ($this->month < 12) { 
  150.     $nextm = sprintf(' <a href="%s?month=%s&year=%s" 
  151.     style="font-family:%s;text-decoration:none; 
  152.     color:%s; font-size:%spx">>></a>', 
  153.     $PHP_SELF, $this->month + 1, $this->year, 
  154.     $this->fontface, $this->linkcolor, $this->linksize); 
  155.     } 
  156.     //If it is December our next link will b
  157.     //     e January of the next year 
  158.     else { 
  159.     $nextm = sprintf(' <a href="%s?month=%s&year=%s" 
  160.     style="font-family:%s;text-decoration:none; 
  161.     color:%s; font-size:%spx">>></a>', 
  162.     $PHP_SELF, 1, $this->year + 1, $this->fontface, 
  163.     $this->linkcolor, $this->linksize); 
  164.     } 
  165.     return $nextm; 
  166.     } 
  167.     //This is the main function and the one 
  168.     //     that generates the html for the calendar
  169.     //     
  170.     function make_calendar() { 
  171.     $result .= sprintf('<table bgcolor="%s" width=%s border=0 
  172.     cellspacing="0"cellpadding="4">', $this->bgcolor, 
  173.     $this->colsize * 7); 
  174.     $result .= sprintf('<tr><td width="%s">%s</td><td colspan=5 
  175.     align=center width="%s">%s<span style="font-family:%s; 
  176.     font-size:%s;color:%s;font-weight:%s">%s 
  177.     </span>%s</td><td width="%s">%s</td></tr>%s', 
  178.     $this->colsize, $this->prev_year(), $this->colsize * 5, 
  179.     $this->prev_month(), $this->fontface, $this->headersize, 
  180.     $this->headercolor, $this->headerweight, $this->monthname, 
  181.     $this->next_month(), $this->colsize, $this->next_year(), "\n"); 
  182.     $result .= '<tr align="center">'; 
  183.     //Reset the $days array 
  184.     reset($this->days); 
  185.     //Loop through the days array and create
  186.     //     a cell for each day 
  187.     for($i=0;$i<count($this->days);$i++) { 
  188.     $result .= sprintf('<td width="%s"><span style=" 
  189.     font-family:%s;font-size:%s;color:%s; 
  190.     font-weight:%s">%s</span></td>', $this->colsize, 
  191.     $this->fontface, $this->headersize, $this->headercolor, 
  192.     $this->headerweight, $this->days[$i]); 
  193.     } 
  194.     $result .= '</tr><tr align="center">'."\n"; 
  195.     //This sets the offset for the first day
  196.     //     of the onth in the calendar 
  197.     if ($this->getfirst() > 0) { 
  198.     $result .= sprintf('<td colspan=%s height="%s"> 
  199.     </td>', $this->getfirst(), 
  200.     $this->dayheight); 
  201.     } 
  202.     //This is the day counter 
  203.     $count = $this->getfirst() + 1; 
  204.     //This loops runs through the number fo 
  205.     //     days in the month 
  206.     for($j=1;$j<=$this->numdays;$j++) { 
  207.     //Create the cells for the indivdual day
  208.     //     s 
  209.     //Divide the current count by 7 if there
  210.     //     is no remainder we no we need 
  211.     //to end the row and start a new one 
  212.     if (is_int($count/7)) { 
  213.     $result .= sprintf('<td height="%s"><span style=" 
  214.     font-family:%s;font-size:%s;color:%s">%s</span> 
  215.     </td></tr><tr align="center">%s', $this->dayheight, 
  216.     $this->fontface, $this->daysize, $this->daycolor, 
  217.     $j, "\n"); 
  218.     } 
  219.     //Or elese we just print a row 
  220.     else { 
  221.     $result .= sprintf('<td height="%s"><span style=" 
  222.     font-family:%s;font-size:%s;color:%s">%s</span> 
  223.     </td>', $this->dayheight, $this->fontface, 
  224.     $this->daysize, $this->daycolor, $j); 
  225.     } 
  226.     //INcrement the day counter 
  227.     $count++; 
  228.     } 
  229.     //Check to see if there are left over sp
  230.     //     aces to fill up the week if so make 
  231.     //a cell with colspan set to that number
  232.     //     of spaces 
  233.     if ($this->getlast() > 0) { 
  234.     $result .= sprintf('<td colspan=%s height="%s"> </td>%s 
  235.     ', $this->getlast(), $this->dayheight, "\n"); 
  236.     } 
  237.     //End the cell 
  238.     $result .= sprintf('</tr></table>'); 
  239.     return $result; 
  240.     } 
  241.     } 
  242.     //Sample 
  243.     //Instaniate the calendar class 
  244.     $cal = new calendar; 
  245.     //This will set look and feel for the ca
  246.     //     lendar 
  247.     /* 
  248.     1st argument is the width of the calendar and should be divisble by 7 
  249.     2nd argument is the font color for the numbered days in the calendar 
  250.     3rd argument is the link color 
  251.     4th argument is the background color for the calendar 
  252.     5th argument is the font size for the link in pixels 
  253.     6th argument is the font size for the numbered days in pixels 
  254.     7th argument is the font size for the day header and month name in pixels 
  255.     8th argument is the font color for the day header and month name 
  256.     9th argument is the font weight for the day header and month name 
  257.     10th argument is the height of the cells that contain the days 
  258.     11th argument is the font face for the calendar 
  259.     */ 
  260.     $cal->set_attributes(210, "#000000", "#ff0000", "#cccccc", 
  261.     11, 11, 12, "navy", "bold", 
  262.     20, "Arial, Helvetica, Sans-Serif"); 
  263.     //Print the calendar 
  264.     print $cal->make_calendar(); 
  265.     ?>
  266.