Login   Register  
PHP Classes
elePHPant
Icontem

File: easyphpeventcalendar.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of JN  >  Easy PHP Event Calendar  >  easyphpeventcalendar.class.php  >  Download  
File: easyphpeventcalendar.class.php
Role: Class source
Content type: text/plain
Description: The source code
Class: Easy PHP Event Calendar
Display a calendar with events
Author: By
Last change:
Date: 5 years ago
Size: 18,063 bytes
 

Contents

Class file image Download
<?php

/**

EasyPhpEventCalendar Class version 1.0.0 

Copyright (c) 2009 JF Nutbroek <jfnutbroek@gmail.com>
For more information visit http://wwww.mywebmymail.com

Permission to use, copy, modify, and/or distribute this software for any
purpose without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

class easyphpeventcalendar {
    
	 /**
	 * URL to gfx directory with calendar backgrounds
	 *
	 * @var string
	 */		
	public $Gfxurl;
	 /**
	 * Days; array with 7 string values representing the days
	 * [0]: Sunday
	 * [1]: Monday
	 * [2]: Tuesday
	 * [3]: Wednesday
	 * [4]: Thursday
	 * [5]: Friday
	 * [6]: Saturday
	 *
	 * @var array
	 */		
	public $Days;
	 /**
	 * Months; array with 12 string values representing the months
	 * [0]: January
	 * [1]: February
	 * [2]: March
	 * [3]: April
	 * [4]: May
	 * [5]: June
	 * [6]: July
	 * [7]: August
	 * [8]: September
	 * [9]: October
	 * [10]: November
	 * [11]: December
	 *
	 * @var array
	 */		
	public $Months;        
	 /**
	 * Events; array with arrays representing the events
	 * [0]: array ('YYYYMMDD','Event 1 as text with html markup')
	 * [1]: array ('YYYYMMDD','Event 2 as text with html markup')
	 * [2]: etc.
	 *
	 * @var array
	 */		
	public $Events;
        
	 /**
	 * Class constructor
	 *
	 */	
	public function __construct() {
	
            $this->Gfxurl  = 'gfx';           
            $this->Days    = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
            $this->Months  = array('January','February','March','April','May','June','July','August','September','October','November','December');
            $this->Events  = array();
                
	}

	 /**
	 * Creates Event Calendar
	 *
	 * @param string/array $filename
	 * @param string $output
	 */	
	public function CreateEventCalendar() {

            $this->Gfxurl=rtrim($this->Gfxurl,'/');
            echo "<div id=\"eventcalendar\">\n";
            echo "<style type=\"text/css\">\n";							
            echo ".eventtable {\n";
            echo "    text-align      : center;\n";
            echo "    font-family     : Verdana, Helvetica, Arial, sans-serif;\n";
            echo "    font-size       : 11px;\n";
            echo "    color           : #000000;\n";
            echo "    border-collapse : separate;\n"; 
            echo "    border-spacing  : 2px;\n";
            echo "    width           : 410px;\n";						
            echo "}\n";									
            echo ".eventweekmonth {\n";
            echo "    background-image  : url('$this->Gfxurl/weekmonth.gif');\n";
            echo "    background-repeat : no-repeat;\n";					
            echo "    height            : 25px;\n";
            echo "    width             : 40px;\n";
            echo "}\n";
            echo ".eventweekmonthactive {\n";  
            echo "    background-image  : url('$this->Gfxurl/weekmonth_event.gif');\n";
            echo "    background-repeat : no-repeat;\n";						
            echo "    height            : 25px;\n";
            echo "    width             : 40px;\n";						
            echo "    font-weight       : bold;\n";
            echo "}\n";						
            echo ".eventweekend {\n";
            echo "    background-image  : url('$this->Gfxurl/weekend.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    height            : 19px;\n";
            echo "    width             : 40px;\n";						
            echo "}\n";
            echo ".eventweekendactive {\n";
            echo "    background-image  : url('$this->Gfxurl/weekend_event.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    height            : 19px;\n";
            echo "    width             : 40px;\n";							
            echo "    font-weight       : bold;\n";
            echo "}\n";
            echo ".eventday {\n";  
            echo "    background-image  : url('$this->Gfxurl/day.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    height            : 19px;\n";
            echo "    width             : 40px;\n";	
            echo "}\n";                    
            echo ".eventdayactive {\n";  
            echo "    background-image  : url('$this->Gfxurl/day_event.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    height            : 19px;\n";
            echo "    width             : 40px;\n";							
            echo "    font-weight       : bold;\n";
            echo "}\n";
            echo ".eventdaydisabled {\n";  
            echo "    background-image  : url('$this->Gfxurl/day.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    height            : 19px;\n";
            echo "    width             : 40px;\n";							
            echo "    color             : #969696;\n";
            echo "}\n";
            echo ".eventdetails {\n";  
            echo "    background-image  : url('$this->Gfxurl/eventdetails.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    text-align        : left;\n";
            echo "    vertical-align    : top;\n";
            echo "    height            : 160px;\n";
            echo "    width             : 304px;\n";							
            echo "    padding           : 5px;\n";            
            echo "}\n";  						
            echo ".eventcalendartitle {\n";
            echo "    background-image  : url('$this->Gfxurl/title.gif');\n";
            echo "    background-repeat : no-repeat;\n";
            echo "    height            : 25px;\n";
            echo "    width             : 304px;\n";							
            echo "    font-weight       : bold;\n";
            echo "}\n";
            echo "</style>\n";

            // Get current date
            $date=time();$month=date('m',$date);$year=date('Y',$date);

            // Left column with months
            echo "<table class=\"eventtable\"><tr><td style=\"vertical-align : top;width : 44px;\">\n";
            echo "<table style=\"vertical-align : top;border-collapse : separate;border-spacing : 2px;width : 44px;\">\n";
            echo "<tr><td style=\"height : 25px;\">&nbsp;</td></tr>\n";
            if ($this->eventcheck(1)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('1')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[0],0,3) . "</td></tr>\n";
            if ($this->eventcheck(2)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('2')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[1],0,3) . "</td></tr>\n";
            if ($this->eventcheck(3)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('3')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[2],0,3) . "</td></tr>\n";
            if ($this->eventcheck(4)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('4')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[3],0,3) . "</td></tr>\n";
            if ($this->eventcheck(5)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('5')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[4],0,3) . "</td></tr>\n";
            if ($this->eventcheck(6)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('6')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[5],0,3) . "</td></tr>\n";
            echo "</table>\n";
            echo "</td><td style=\"vertical-align : top;width : 308px;\">\n";

            // Add Events
            foreach ($this->Events as $key => $event) {
                $eventmonth=intval(substr($event[0],4,2));$eventyear=intval(substr($event[0],0,4));$eventday=intval(substr($event[0],6,2));
                if (checkdate($eventmonth,$eventday,$eventyear)) {
                    $day_no=date('N',mktime(0,0,0,$eventmonth,$eventday,$eventyear));$month_no=date('n',mktime(0,0,0,$eventmonth,$eventday,$eventyear))-1;
                    if ($day_no==7) {$day_no=0;}
                    $date=$this->Days[$day_no] . ' ' . $eventday . ' ' . $this->Months[$month_no] . ' ' . $eventyear;
                    echo "<div id=\"" . ($key + 13) ."\" style=\"display: none\">\n";
                    echo "<table style=\"width : 308px;vertical-align : top;border-collapse : separate;border-spacing : 2px;\"><tr><td class=\"eventcalendartitle\">$date</td></tr><tr><td class=\"eventdetails\" onClick=\"expandCollapse($eventmonth)\" onMouseOver=\"this.style.cursor='pointer';\">" . $event[1] . "</td></tr></table>\n";
                    echo "</div>\n";
                }
            }
	    
            // Add Months
            for ($m=1;$m<13;$m++) {
                if ($m==$month) {
                    echo "<div id=\"$m\">\n";
                    $this->eventcalendar(mktime(0,0,0,$m,1,$year));
                    echo "</div>\n";
                } else {
                    if ($m<$month) {$i=1;} else {$i=0;}
                    echo "<div id=\"$m\" style=\"display: none\">\n";
                    $this->eventcalendar(mktime(0,0,0,$m,1,$year+$i));
                    echo "</div>\n";        
                }
            }

            // Right column with months
            echo "</td><td style=\"vertical-align : top;width : 44px;\">\n";
            echo "<table style=\"width : 44px;vertical-align : top;border-collapse : separate;border-spacing : 2px;\">\n";
            echo "<tr><td style=\"height : 25px;\">&nbsp;</td></tr>\n";
            if ($this->eventcheck(7)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('7')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[6],0,3) . "</td></tr>\n";
            if ($this->eventcheck(8)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('8')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[7],0,3) . "</td></tr>\n";
            if ($this->eventcheck(9)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('9')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[8],0,3) . "</td></tr>\n";
            if ($this->eventcheck(10)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('10')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[9],0,3) . "</td></tr>\n";
            if ($this->eventcheck(11)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('11')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[10],0,3) . "</td></tr>\n";
            if ($this->eventcheck(12)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
            echo "<tr><td class=\"$class\" onClick=\"expandCollapse('12')\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\">". substr($this->Months[11],0,3) . "</td></tr>\n";
            echo "</table>\n";
            echo "</td></tr></table>\n";
						
            // Javascript to show and hide content
            echo "<script language=\"JavaScript\">\n";
            echo "<!-- EasyPhpEventCalendar Copyright (c) 2009 JF Nutbroek -->\n";
            echo "<!-- More information on http://www.mywebmymail.com -->\n";
            echo "function expandCollapse(divsection) {\n";
            echo " for(i=1;i<" . (13+count($this->Events)) . ";i++) {Collapse(i);}\n";	
            echo " var element = document.getElementById(divsection);\n";
            echo " element.style.display = \"block\";\n";
            echo "}\n";
            echo "function Collapse(divsection) {\n";
            echo " var element = document.getElementById(divsection);\n";
            echo " element.style.display = \"none\";\n";
            echo "}\n";
            echo "</script>\n";
            echo "</div>\n";   
						
	}
        
	 /**
	 * Check if month has an event
	 *
	 */
  	private function eventcheck($month) {
	
            foreach ($this->Events as $key => $event) {
                if (intval(substr($event[0],4,2))==$month) 
                    return true;
            }
            return false;
						
	}
				
	 /**
	 * Print the month
	 *
	 */
	private function eventcalendar($date) {
	
            $day=date('d',$date);$month=date('m',$date);$year=date('Y',$date);$first_day=mktime(0,0,0,$month,1,$year);$title=$this->Months[date('n',mktime(0,0,0,$month,$day,$year))-1]; 
            $days_in_month=cal_days_in_month(0,$month,$year);$day_of_week=date('N',$first_day); if ($day_of_week==7) {$blank=0;} else {$blank=$day_of_week;}
            echo "<table style=\"vertical-align : top;border-collapse : separate;border-spacing : 2px;width : 308px;\">";echo "<tr><td colspan=\"7\" class=\"eventcalendartitle\">$title $year</td></tr>";
            echo "<tr><td class=\"eventweekmonth\">". substr($this->Days[0],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[1],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[2],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[3],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[4],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[5],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[6],0,3) . "</td></tr>";
            if (($month-1)==0) {$prevmonth=1;$prevyear=($year-1);} else {$prevmonth=($month-1);$prevyear=$year;}
            $day_prevmonth=cal_days_in_month(0,$prevmonth,$prevyear)-($blank-1);echo "<tr>";
	    $day_num=1;$day_count=1;$datenow=time();$monthnow=date('n',$datenow);$yearnow=date('Y',$datenow);$daynow=date('j',$datenow);
            while ($blank>0) {echo "<td class=\"eventdaydisabled\">$day_prevmonth</td>";$blank=$blank-1;$day_count++;$day_prevmonth++;} 
            while ($day_num<=$days_in_month) {    
                $class="class=\"eventday\"";$id=0;
                if ($day_count==1 || $day_count==7) {$class="class=\"eventweekend\"";}
                foreach ($this->Events as $key => $event) {
                    $eventyear=intval(substr($event[0],0,4));$eventmonth=intval(substr($event[0],4,2));$eventday=intval(substr($event[0],6,2));
                    if ($eventyear==$year && $eventmonth==$month && $eventday==$day_num) {
                        $class="class=\"eventdayactive\"";
                        if ($day_count==1 || $day_count==7) {$class="class=\"eventweekendactive\"";}   
                        $id=($key +13);break;
                    }       
                }
	        if ($yearnow==$year && $monthnow==$month && $daynow==$day_num) {$textstyle='style="color : #FF0000;font-weight : bold;"';} else {$textstyle='';}
                if ($id>0) {
                    echo "<td $class $textstyle onClick=\"expandCollapse($id)\" onMouseOver=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onMouseOut=\"this.style.textDecoration='none';\"> $day_num </td>"; 
                } else {
                    echo "<td $class $textstyle> $day_num </td>"; 
                }
                $day_num++;$day_count++;
                if ($day_count>7) {echo "</tr><tr>";$day_count=1;}
            }
            $day_nextmonth=1;
            if ($day_count==1) {
                echo "<td colspan=\"7\">&nbsp;</td>";
            } else {
                while ($day_count>1 && $day_count<=7 ) {echo "<td class=\"eventdaydisabled\">$day_nextmonth</td>";$day_count++;$day_nextmonth++;}
            }
            echo "</tr></table>\n";
						
  }
        
}

?>