Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

base-calendar.inc.php

Go to the documentation of this file.
00001 <?
00012 class BaseCalendar extends MyManager
00013 {
00018     public $startDay = 0;
00019 
00024     public $startMonth = 1;
00025 
00030     public $dayNames = array("S", "M", "T", "W", "T", "F", "S");
00031     
00036     public $monthNames = array("January", "February", "March", "April", "May", "June",
00037                             "July", "August", "September", "October", "November", "December");
00038                             
00043     public $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
00044 
00050     public $day = 0;
00051     
00055     public $dayCount = array();
00056 
00060     public $size;
00061 
00065     public $events = null;
00066 
00072     public function __construct($class = 'Event')
00073     {
00074         parent::__construct($class);
00075         
00076         if (!$this->page)
00077             $this->page = 'calendar';
00078     }
00079     
00089     public function getPagesXml()
00090     {
00091         $xml = parent::getPagesXml();
00092         $xml .= "<page name=\"upcoming\"/>\n";
00093         $xml .= "<page name=\"past\"/>\n";
00094         $xml .= "<page name=\"popular\"/>\n";
00095         $xml .= "<page name=\"calendar\">\n";
00096         $xml .= "\t<param name=\"month\" regex=\"/^[0-9]{1,2}$/\"/>\n";
00097         $xml .= "\t<param name=\"year\" regex=\"/^[0-9]{4}$/\"/>\n";
00098         $xml .= "</page>\n";
00099         $xml .= "<page name=\"date\">\n";
00100         $xml .= "\t<param name=\"year\" regex=\"/^[0-9]{4}$/\"/>\n";
00101         $xml .= "\t<param name=\"month\" regex=\"/^[0-9]{1,2}$/\"/>\n";
00102         $xml .= "\t<param name=\"day\" regex=\"/^[0-9]{1,2}$/\"/>\n";
00103         $xml .= "</page>\n";
00104         return $xml;
00105     }
00106 
00112     public function getSearchParamXml()
00113     {
00114         $xml = parent::getSearchParamXml();
00115         $xml .= <<<XML
00116             <param name="month" type="int"/>
00117             <param name="day" type="int"/>
00118             <param name="year" type="int"/>
00119             <param name="event_date_start" desc="start date for your query, eg 10-22-1983. it will get all the events happening on or after it" regex="/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?$/"/>
00120             <param name="event_date_stop" desc="end date for your query. it will get all the events happening on or before it.  used with start date you can specify a range."  regex="/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?$/"/>
00121 XML;
00122 
00123         return $xml;
00124     }
00125 
00130     public function getSearchSelect($params)
00131     { 
00132         $sql = parent::getSearchSelect($params);
00133 
00134         $sql .= ", count(eg.user_id) AS guests ";
00135 
00136         return $sql;
00137     }
00138 
00144     public function getSearchFrom($params)
00145     {
00146         $from = parent::getSearchFrom($params);
00147 
00148         $from .= " LEFT OUTER JOIN {$this->object->guestTable} eg ON o.id = eg.event_id ";
00149 
00150         return $from;
00151     }
00152     
00158     public function getSearchWhere($params)
00159     {
00160         global $me;
00161 
00162         $where = parent::getSearchWhere($params);
00163 
00164         //get our params..
00165         $year = $params['year'];
00166         $month = str_pad($params['month'], 2, "0", STR_PAD_LEFT);
00167         $day = str_pad($params['day'], 2, "0", STR_PAD_LEFT);
00168 
00169         //check and make sql.
00170         if ($year && (int)$month && (int)$day)
00171             $where .= " AND o.start_date <= '$year-$month-$day' AND o.end_date >= '$year-$month-$day'";
00172         else if ($year && (int)$month)
00173             $where .= " AND (
00174                 (o.start_date >= '$year-$month-01' && o.start_date <= '$year-$month-31') OR
00175                 (o.end_date >= '$year-$month-01' && o.end_date <= '$year-$month-31')
00176             )";
00177         else if ($year)
00178             $where .= " AND (
00179                 (o.start_date >= '$year-01-01' && o.start_date <= '$year-12-31') OR
00180                 (o.end_date >= '$year-01-01' && o.end_date <= '$year-12-31')
00181             )";
00182 
00183         //when is our event start date??
00184         $start = $params['event_date_start'];
00185         if ($start)
00186             $where .= " AND (o.start_date >= '$start' OR o.end_date >= '$start')";
00187         
00188         //what about event stop date?
00189         $stop = $params['event_date_stop'];
00190         if ($stop)
00191             $where .= " AND o.start_date <= '$stop'";
00192 
00193         //what about invite only stuff?
00194         if ($this->object->hasField('invite_only'))
00195             $where .= " AND (o.invite_only = 0 OR (o.invite_only = 1 AND eg.user_id = '$me->id')) ";
00196         
00197         return $where;
00198     }
00199 
00204     public function getSearchOrder($params)
00205     {
00206         //get our sort...
00207         $sort = $params['sort'];
00208         $order = $params['order'];
00209 
00210         //if we dont have order and sort... add date desc!
00211         if (!$sort)
00212             $params['sort'] = 'start_date';
00213         if (!$order)
00214             $params['order'] = 'ASC';
00215 
00216         return parent::getSearchOrder($params);
00217     }
00218     
00223     protected function getSortFieldsArray()
00224     {
00225         $sort = parent::getSortFieldsArray();
00226 
00227         $sort['name'] = 'sort by number of name';
00228         $sort['guests'] = 'sort by number of guests';
00229         $sort['start_date'] = 'sort by number of start date';
00230         $sort['end_date'] = 'sort by number of end date';
00231 
00232         return $sort;
00233     }
00234 
00238     public function getSearchGroupBy($params)
00239     {
00240         return ' GROUP BY o.id ';
00241     }
00242 
00246     public function search($params, $limit = null)
00247     {
00248         if (!$params['month'] && !$params['year'] && !$params['event_date_start'] && !$params['event_date_stop'])
00249             $params['event_date_start'] = date('Y-m-d');
00250         return parent::search($params, $limit);
00251     }
00252 
00256     public function findDate()
00257     {
00258         if ($this->params('month'))
00259             $month = (int)$this->params('month');
00260         if ($this->params('year'))
00261             $year = (int)$this->params('year');
00262         if ($this->params('day'))
00263             $day = (int)$this->params('day');
00264     
00265         if (!$month)
00266             $month = date('m');
00267         if (!$year)
00268             $year = date('Y');
00269 
00270         $this->setParam('month', $month);
00271         $this->setParam('year', $year);
00272     }
00273 
00277     public function initMainPage()
00278     {
00279         Util::redirect(".calendar");
00280     }
00281 
00285     public function initSearchPage()
00286     {
00287         parent::initSearchPage();
00288         
00289         $this->pageTitle = 'Calendar of Events';
00290     } 
00291 
00297     public function initCalendarPage()
00298     {
00299         //what date?
00300         $this->findDate();
00301         
00302         //box and title...
00303         $this->pageTitle = 'Calendar of Events - ' . $this->monthNames[(int)$this->params('month')-1];
00304     }
00305 
00309     public function initDatePage()
00310     {
00311         global $me;
00312 
00313         $this->pageTitle = "Events for " . $me->formatDate($this->params('year') . "-" . $this->params('month') . "-" . $this->params('day'));
00314     }
00315 
00319     public function initUpcomingPage()
00320     {
00321         $this->setParam('event_date_start', date('Y-m-d'));
00322 
00323         $this->initSearchPage();
00324         
00325         $this->pageTitle = 'Upcoming Events';
00326     }
00327 
00331     public function initPopularPage()
00332     {
00333         $this->setParam('event_date_start', date('Y-m-d'));
00334         $this->setParam('sort', 'guests');
00335         $this->setParam('order', 'desc');
00336 
00337         $this->initSearchPage();
00338         
00339         $this->pageTitle = 'Popular Events';
00340     }
00341     
00345     public function initPastPage()
00346     {
00347         $this->setParam('event_date_stop', date('Y-m-d'));
00348         $this->setParam('order', 'desc');
00349         $this->initSearchPage();
00350         
00351         $this->pageTitle = 'Past Events';
00352     }
00353 
00357     public function drawDatePage()
00358     {
00359         $year = $this->params('year');
00360         $month = $this->params('month');
00361         $day = $this->params('day');
00362     
00363         echo "<p>" . $this->object->getLink(".edit?date=$year-$month-$day", "Post a new event on this day.");
00364         echo " | " . $this->getLink(".calendar?month=$month&year=$year", "Back to " . date("M Y", strtotime("$year-$month-$day")));
00365         echo "</p>";
00366         
00367         $rs = $this->search($this->params());
00368         if (count($rs))
00369             $this->drawResults($rs);
00370         else
00371             echo "<p>Sorry, there aren't any events on this day yet.</p>";
00372 
00373     }
00374     
00378     public function drawCalendarPage()
00379     {
00380         $this->drawSearchForm();
00381 
00382         echo "<div id=\"bigCalendar\">";
00383         $this->drawCalendar($this->params('month'), $this->params('year'));
00384         echo "</div>";
00385 
00386         //draw our events
00387         $events = $this->getMonthEvents($month, $year);
00388         $this->drawResults($events);
00389     } 
00390 
00394     public function drawUpcomingPage()
00395     {
00396         $this->drawSearchPage();
00397     }
00398     
00402     public function drawPopularPage()
00403     {
00404         $this->drawSearchPage();
00405     }
00406     
00410     public function drawPastPage()
00411     {
00412         $this->drawSearchPage();
00413     }
00414 
00422     public function drawCalendar($month, $year, $size = 'big')
00423     {
00424         //remember our size.
00425         $this->size = $size;
00426         
00427         //get our month events...
00428         $this->setMonthEvents($month, $year);
00429 
00430         //draw our calendar.
00431         echo $this->getMonthView($month, $year);
00432     }
00433 
00441     protected function getMonthEvents($month, $year)
00442     {
00443         if ($this->events === null)
00444         {
00445             //check to see what events are on this calendar month.
00446             return $this->search(array(
00447                 'month' => $month,
00448                 'year' => $year,
00449             ));
00450         }
00451         else
00452             return $this->events;
00453     } 
00454     
00461     protected function setMonthEvents($month, $year)
00462     { 
00463         $this->events = $this->getMonthEvents($month, $year);
00464         
00465         //did we get any?
00466         if (count($this->events))
00467         {
00468             //loop thru and put them in our array.
00469             foreach ($this->events AS $ob)
00470             {
00471                 if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $ob->start_date, $m))
00472                     $this->dayCount[(int)$m[3]]++;          
00473             }
00474         }
00475     }
00476 
00484     function getDayNames()
00485     {
00486         return $this->dayNames;
00487     }
00488     
00489 
00497     function setDayNames($names)
00498     {
00499         $this->dayNames = $names;
00500     }
00501     
00509     function getMonthNames()
00510     {
00511         return $this->monthNames;
00512     }
00513     
00521     function setMonthNames($names)
00522     {
00523         $this->monthNames = $names;
00524     }
00525     
00532     function getStartDay()
00533     {
00534         return $this->startDay;
00535     }
00536     
00543     function setStartDay($day)
00544     {
00545         $this->startDay = $day;
00546     }
00547     
00548     
00555     function getStartMonth()
00556     {
00557         return $this->startMonth;
00558     }
00559     
00566     function setStartMonth($month)
00567     {
00568         $this->startMonth = $month;
00569     }
00570     
00586     function getCalendarLink($month, $year)
00587     {
00588         return $this->getUrl(".calendar?month=$month&year=$year");
00589     } 
00590     
00603     function getDateLink($day, $month, $year)
00604     {
00605         return $this->getUrl(".date?year=$year&month=$month&day=$day");
00606     }
00607 
00618     function getDateContent($day, $month, $year)
00619     {
00620         $s = $day;
00621     
00622         //how many events?
00623         if ($this->dayHasEvent($day) && $this->size == 'big')
00624             $s .= "<br/>" . Util::pluralize($this->object->english, $this->dayCount[$day], true);
00625     
00626         return $s;
00627     }
00628     
00638     function getMonthView($month, $year, $showYear = true)
00639     {
00640         //start our table.
00641         $s = "<table class=\"calendar\">\n";
00642         
00643         //adjust our date so its correct.
00644         $a = $this->adjustDate($month, $year);
00645         $month = $a[0];
00646         $year = $a[1];        
00647 
00648         //get our header.
00649         $s .= $this->getMonthHeader($month, $year, $showYear);
00650         $s .= $this->getMonthBody($month, $year);
00651         
00652         $s .= "</table>\n";
00653         
00654         return $s;      
00655     }
00656     
00666     function getMonthHeader($month, $year, $showYear = true)
00667     {
00668         //our monthname
00669         $monthName = $this->monthNames[$month - 1];
00670         
00671         //get our previous/next months.
00672         $prev = $this->adjustDate($month - 1, $year);
00673         $next = $this->adjustDate($month + 1, $year);
00674         
00675         //do we want links to prev/next?
00676         if ($showYear)
00677         {
00678             $prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
00679             $nextMonth = $this->getCalendarLink($next[0], $next[1]);
00680         }
00681         else
00682         {
00683             $prevMonth = "";
00684             $nextMonth = "";
00685         }
00686         
00687         //set our header.
00688         $header = $monthName . (($showYear) ? " " . $year : "");
00689         
00690         $s  = "<tr>\n";
00691         $s .= "<th valign=\"top\">" . (($prevMonth == "") ? "&nbsp;" : "<a href=\"$prevMonth\">&lt;&lt;</a>")  . "</th>\n";
00692         $s .= "<th valign=\"top\" colspan=\"5\">$header</th>\n"; 
00693         $s .= "<th valign=\"top\">" . (($nextMonth == "") ? "&nbsp;" : "<a href=\"$nextMonth\">&gt;&gt;</a>")  . "</th>\n";
00694         $s .= "</tr>\n";
00695         
00696         $s .= "<tr>\n";
00697         $s .= "<th>" . $this->dayNames[($this->startDay)%7] . "</th>\n";
00698         $s .= "<th>" . $this->dayNames[($this->startDay+1)%7] . "</th>\n";
00699         $s .= "<th>" . $this->dayNames[($this->startDay+2)%7] . "</th>\n";
00700         $s .= "<th>" . $this->dayNames[($this->startDay+3)%7] . "</th>\n";
00701         $s .= "<th>" . $this->dayNames[($this->startDay+4)%7] . "</th>\n";
00702         $s .= "<th>" . $this->dayNames[($this->startDay+5)%7] . "</th>\n";
00703         $s .= "<th>" . $this->dayNames[($this->startDay+6)%7] . "</th>\n";
00704         $s .= "</tr>\n";
00705 
00706         return $s;
00707     }
00708         
00715     protected function getMonthBody($month, $year)
00716     {
00717         //init return string.
00718         $s = '';
00719         
00720         //find out our day count and general date info.
00721         $daysInMonth = $this->getDaysInMonth($month, $year);
00722         $date = getdate(mktime(12, 0, 0, $month, 1, $year));
00723         
00724         //get our first date
00725         $first = $date["wday"];
00726         
00727         // We need to work out what date to start at so that the first appears in the correct column
00728         $d = $this->startDay + 1 - $first;
00729         while ($d > 1)
00730             $d -= 7;
00731 
00732         // Make sure we know when today is, so that we can use a different CSS style
00733         $today = getdate(time());
00734         
00735         //loop until we run out of days.
00736         while ($d <= $daysInMonth)
00737         {
00738             $s .= "<tr>\n";       
00739             
00740             //each week day.
00741             for ($i = 0; $i < 7; $i++)
00742             {
00743                 //if its today, we want a special class.
00744                 if ($this->dayHasEvent($d))
00745                     $s .= '<td class="hasEvent">';
00746                 else if ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"])
00747                     $s .= '<td class="today">';
00748                 else
00749                     $s .= '<td>';
00750 
00751                 //if its in the month... draw the day.  
00752                 if ($d > 0 && $d <= $daysInMonth)
00753                 {
00754                     $link = $this->getDateLink($d, $month, $year);
00755                     $content = $this->getDateContent($d, $month, $year);
00756                     if ($link)
00757                         $s .= "<a href=\"$link\">$content</a>";
00758                     else
00759                         $s .= $content;
00760                 }
00761                 //otherwise leave it blank.
00762                 else
00763                 {
00764                     $s .= "&nbsp;";
00765                 }
00766 
00767                 //end it
00768                 $s .= "</td>\n";
00769 
00770                 //next day.
00771                 $d++;
00772             }
00773 
00774             //next row
00775             $s .= "</tr>\n";    
00776         }
00777 
00778         return $s;
00779     }
00780 
00787     public function dayHasEvent($d)
00788     {
00789         return ($this->dayCount[$d]);
00790     }
00791     
00798     function getYearView($year)
00799     {
00800         $s = "";
00801         $prev = $this->getCalendarLink(0, $year - 1);
00802         $next = $this->getCalendarLink(0, $year + 1);
00803         
00804         $s .= "<table class=\"calendar\" border=\"0\">\n";
00805         $s .= "<tr>";
00806         $s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? "&nbsp;" : "<a href=\"$prev\">&lt;&lt;</a>")  . "</td>\n";
00807         $s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
00808         $s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? "&nbsp;" : "<a href=\"$next\">&gt;&gt;</a>")  . "</td>\n";
00809         $s .= "</tr>\n";
00810         $s .= "<tr>";
00811         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
00812         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
00813         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
00814         $s .= "</tr>\n";
00815         $s .= "<tr>\n";
00816         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
00817         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
00818         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
00819         $s .= "</tr>\n";
00820         $s .= "<tr>\n";
00821         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
00822         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
00823         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
00824         $s .= "</tr>\n";
00825         $s .= "<tr>\n";
00826         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
00827         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
00828         $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
00829         $s .= "</tr>\n";
00830         $s .= "</table>\n";
00831         
00832         return $s;
00833     }
00834     
00835     /********************************************************************************
00836         The rest are private methods. No user-servicable parts inside.
00837         You shouldn't need to call any of these functions directly.
00838     *********************************************************************************/
00839 
00840 
00848     protected function getDaysInMonth($month, $year)
00849     {
00850         if ($month < 1 || $month > 12)
00851         {
00852             return 0;
00853         }
00854    
00855         $d = $this->daysInMonth[$month - 1];
00856    
00857         if ($month == 2)
00858         {
00859             // Check for leap year
00860             // Forget the 4000 rule, I doubt I'll be around then...
00861         
00862             if ($year%4 == 0)
00863             {
00864                 if ($year%100 == 0)
00865                 {
00866                     if ($year%400 == 0)
00867                     {
00868                         $d = 29;
00869                     }
00870                 }
00871                 else
00872                 {
00873                     $d = 29;
00874                 }
00875             }
00876         }
00877     
00878         return $d;
00879     }
00880 
00888     protected function getYearHTML($year)
00889     {
00890     }
00891 
00901     protected function adjustDate($month, $year)
00902     {
00903         $a = array();  
00904         $a[0] = $month;
00905         $a[1] = $year;
00906         
00907         while ($a[0] > 12)
00908         {
00909             $a[0] -= 12;
00910             $a[1]++;
00911         }
00912         
00913         while ($a[0] <= 0)
00914         {
00915             $a[0] += 12;
00916             $a[1]--;
00917         }
00918         
00919         return $a;
00920     }
00921 }
00922 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1