- <?php
- /**
- * <b>Database Class : Studies</b><br />
- * Provides all database access methods for the studies/events section
- * @author Kristen O'Brien <kristen_paristemi-com>
- * @copyright Copyright 2004, Kristen O'Brien
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- * @link http://www.paristemi.com Paristemi Main Site
- * @package paristemi
- * @subpackage paristemi_database_layer
- * @since Build 0.5
- * @version Build 0.7
- * @filesource
- */
- /**
- * <b>Database Class : Studies</b><br />
- * Provides all database access methods for the studies/events section
- * @author Kristen O'Brien <kristen_paristemi-com>
- * @copyright Copyright 2004, Kristen O'Brien
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- * @link http://www.paristemi.com Paristemi Main Site
- * @package paristemi
- * @subpackage paristemi_database_layer
- * @since Build 0.5
- * @version Build 0.7
- * @filesource
- */
- class DB_Studies {
-
- function DB_Studies() { }
-
- function &getWeekdayNumber($weekday)
- {
- switch($weekday) {
- case 'Sunday' : return 0;
- case 'Monday' : return 1;
- case 'Tuesday' : return 2;
- case 'Wednesday' : return 3;
- case 'Thursday' : return 4;
- case 'Friday' : return 5;
- case 'Saturday' : return 6;
- default : return null;
- }
- }
-
- // functions taken from calendar include so this can stand alone
-
- function &nextDay ($day_marker,$begin_week)
- {
- $total_days = getdate(mktime(0,0,0,$begin_week['mon']+1,1,$begin_week['year'])-1);
- if($total_days['mday'] < ($begin_week['mday']+$day_marker)) {
- if($today['mon']+1 > 12) {
- $next_day = getdate(mktime(0,0,0,1,($begin_week['mday']+day_marker) - $total_days['mday'], $begin_week['year']+1));
- }
- else {
- $next_day = getdate(mktime(0, 0, 0, ($begin_week['mon']+1), (($begin_week['mday']+$day_marker) - $total_days['mday']), $begin_week['year']));
- }
- }
- else {
- $next_day = getdate(mktime(0,0,0,$begin_week['mon'],($begin_week['mday']+($day_marker)),$begin_week['year']));
- }
- return $next_day;
- }
- function &beginWeek ($today)
- {
- $diff_begin = ($today['mday'] - $today['wday']);
- if($diff_begin < 1) {
- if($today['mon'] == 1) {
- $end_day = getdate(mktime(0,0,0,1,1,$today['year'])-1);
- $begin_week = getdate(mktime(0,0,0,12,$end_day['mday']+$diff_begin,$today['year']-1));
- }
- else {
- $end_day = getdate(mktime(0,0,0,$today['mon'],1,$today['year'])-1);
- $begin_week = getdate(mktime(0,0,0,$today['mon']-1,$end_day['mday']+$diff_begin,$today['year']));
- }
- }
- else {
- $begin_week = getdate(mktime(0, 0, 0, $today['mon'], $diff_begin, $today['year']));
- }
- return $begin_week;
- }
-
- // Given an ID return the study information
- // ID, Title, Timestamp, First, Last, Description, LocationName, LocationID
-
- function &GetStudyInfo(&$id) {
-
- $result = mysql_query("select i.Title as InstanceTitle, i.ID as iID,
- i.Recursive, i.RecDay, S.MeetByMonthDays,
- unix_timestamp(i.Start) as iStart,
- unix_timestamp(i.RecEnd) as RecursiveEnd
- from Instance i, Series S
- where (i.SeriesID = $id OR i.SubSeriesID = $id)
- AND S.ID = $id
- group by i.Start;");
- $temp[] = array(); $meetdays="";
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- $temp[] = new BibleStudyDate($id,$row['InstanceTitle'],$row['iID'],
- $row['iStart'],$row['Recursive'],$row['RecDay'],$row['RecursiveEnd']);
- $meetdays = $row['MeetByMonthDays'];
- }
- $today = time();
- $bestdate = null;
- $diffval = 999999999; //impossibly large value
- $stamp = "";
- foreach($temp as $value) {
- if($value->Recursive && $value->RecEnd >= $today) {
- $todaydate = getdate($today);
- $day= $todaydate['mday'];
- $mkr = $day; $addflg=false;$useday = 0;
- if($meetdays==0) { $addflg = true; }
- else {
- $laststamp=mktime(23,59,59,$todaydate['mon'],$todaydate['mday'],$todaydate['year']);
- $last = getdate($laststamp);
- $nextmonth = $last['mon']+1;
- $nextyear = $last['year'];
- if($nextmonth > 12) { $nextmonth = 1; $nextyear++; }
- $endstamp = (mktime(0,0,0,$nextmonth,1,$nextyear)-1);
- $endmonth = getdate($endstamp);
- $endrec = getdate($value->RecEnd);
- $meetdays = explode(',',$meetdays);
- while($mkr > 0) { $mkr = $mkr-7; }
- $continue=true;
- while($continue) {
- if($endrec['mon']==$endmonth['mon'] && $endrec['year']==$endrec['year']) {
- $endmday = $endrec['mday']; }
- else { $endmday = $endmonth['mday']; }
- $mkr = $endmday; $dayarr=array();
- while($mkr > 0) { $mkr = $mkr-7; }
- $i=$mkr; while($i<=$endmday) { $dayarr[]=$i;$i=$i+7; }
- foreach($meetdays as $meetingdays) {
- if($dayarr[$meetingdays] >= $day) {
- $addflg=true; $useday=$dayarr[$meetingdays];
- $usemon=$endmonth['mon']; $useyear=$endmonth['year']; break; } }
- $nextmonth = $nextmonth+2;
- $nextyear = $nextyear;
- if($nextmonth > 12) { $nextmonth = 1; $nextyear++; }
- $endstamp = (mktime(0,0,0,$nextmonth,1,$nextyear)-1);
- $endmonth = getdate($endstamp);
- if($value->RecEnd >= $endstamp){$continue=true;}
- elseif($endrec['mon'] >= $endmonth['mon']){
- if($endrec['year']==$endmonth['year']) {$continue=true;}
- else {$continue=false;}}
- elseif($endrec['year'] > $endmonth['year']){$continue=true;}
- else{$continue=false;}
- $day =1;
- }
- }
- if($addflg) {
- $start = getdate($value->iStart);
- $recday = $this->getWeekdayNumber($value->RecDay);
- $recdate = $this->nextDay($recday,$this->beginWeek($todaydate));
- $recstamp = mktime($start['hours'],$start['minutes'],$start['seconds'],
- $recdate['mon'],$recdate['mday'],$recdate['year']);
- if($useday==0) {
- if($recstamp < $today) {
- $todaydate = getdate($today+604800);
- $recdate = $this->nextDay($recday,$this->beginWeek($todaydate));
- $recstamp = mktime($start['hours'],$start['minutes'],$start['seconds'],
- $recdate['mon'],$recdate['mday'],$recdate['year']);
- }
- }
- else {
- $recstamp = mktime($start['hours'],$start['minutes'],$start['seconds'],
- $usemon,$useday,$useyear);
- }
- $diffval = ($recstamp-$today);
- $bestdate = $value;
- $stamp = $recstamp;
- }
- }
- else if($value->iStart > $today) {
- if((($value->iStart)-$today) < $diffval) {
- $diffval = (($value->iStart)-$today);
- $bestdate = $value;
- $stamp = $value->iStart;
- }
- }
-
- }
-
- $result2 = mysql_query("select s.ID, s.Title,
- time_format(s.Start, '%l:%i %p') as Start,
- time_format(s.Stop, '%l:%i %p') as Stop,
- s.Day, s.MeetByMonthDays, s.Description, s.Type,
- l.Name as LocationName, l.ID as LocationID
- from Series s, Location l
- where s.ID = $id and s.LocationID = l.ID
- group by s.Day, s.Start, s.Title
- limit 1;");
-
- $row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
-
- //Process the row.
- $data = new BibleStudyDetailed($row2['ID'], $row2['Title'],
- $bestdate->iTitle, $bestdate->iID, $stamp,
- $row2['Start'], $row2['Stop'], $row2['Day'], $row2['MeetByMonthDays'],
- $row2['Description'], $row2['Type'], $row2['LocationName'],
- $row2['LocationID']);
-
- return $data;
- }
-
-
- // Return a list of current studies
- // ID, Timestamp, Title
-
- function &GetAllStudies() {
- $data = Array();
-
- $result = mysql_query("select s.ID, s.Title,
- time_format(s.Start, '%l:%i %p') as Start,
- time_format(s.Stop, '%l:%i %p') as Stop,
- s.Day, s.MeetByMonthDays, s.ShowItem, s.Type
- from Series s
- group by s.Day, s.Start, s.Title;");
-
- //Process each row.
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- $data[] = new BibleStudy($row['ID'], $row['Title'],
- $row['Start'], $row['Stop'],$row['Day'], $row['MeetByMonthDays'],
- $row['Type']);
- }
-
- return $data;
- }
-
-
- // Return a list of current studies
- // ID, Timestamp, Title
-
- function &GetCurrentStudies() {
- $data = Array();
-
- $result = mysql_query("select s.ID, s.Title,
- time_format(s.Start, '%l:%i %p') as Start,
- time_format(s.Stop, '%l:%i %p') as Stop,
- s.Day, s.MeetByMonthDays, s.ShowItem, s.Type
- from Series s
- where s.ShowItem > 0
- group by s.Day, s.Start, s.Title;");
-
- //Process each row.
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- $data[] = new BibleStudy($row['ID'], $row['Title'],
- $row['Start'], $row['Stop'],$row['Day'], $row['MeetByMonthDays'],
- $row['Type']);
- }
-
- return $data;
- }
- }
-
- /**
- * <b>Database Container Class : Study</b><br />
- * Contains all of the important data for a study.
- * @author Kristen O'Brien <kristen_paristemi-com>
- * @copyright Copyright 2004, Kristen O'Brien
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- * @link http://www.paristemi.com Paristemi Main Site
- * @package paristemi
- * @subpackage paristemi_database_layer
- * @since Build 0.5
- * @version Build 0.7
- * @filesource
- */
- class BibleStudy {
- var $ID;
- var $Title;
- var $Start;
- var $Stop;
- var $Day;
- var $MeetByMonth;
- var $Type;
-
- function BibleStudy($id, $title, $start, $stop, $day, $meet, $type) {
- $this->ID = $id;
- $this->Title = $title;
- $this->Start = $start;
- $this->Stop = $stop;
- $this->Day = $day;
- $this->MeetByMonth = $meet;
- $this->Type = $type;
- }
- }
-
- /**
- * <b>Database Container Class : Study</b><br />
- * Contains all of the date specific data for a study.
- * @author Kristen O'Brien <kristen_paristemi-com>
- * @copyright Copyright 2004, Kristen O'Brien
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- * @link http://www.paristemi.com Paristemi Main Site
- * @package paristemi
- * @subpackage paristemi_database_layer
- * @since Build 0.5
- * @version Build 0.7
- * @filesource
- */
- class BibleStudyDate {
- var $ID;
- var $iTitle;
- var $iID;
- var $iStart;
- var $Recursive;
- var $RecDay;
- var $RecEnd;
-
- function BibleStudyDate($id, $ititle, $iid, $istart,$rec,$recday,$recend) {
- $this->iTitle = $ititle;
- $this->iID = $iid;
- $this->iStart = $istart;
- $this->Recursive = $rec;
- $this->RecDay = $recday;
- $this->RecEnd = $recend;
- }
- }
-
- /**
- * <b>Database Container Class : Study Detailed</b><br />
- * Contains all of the specific data for a study.
- * @author Kristen O'Brien <kristen_paristemi-com>
- * @copyright Copyright 2004, Kristen O'Brien
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- * @link http://www.paristemi.com Paristemi Main Site
- * @package paristemi
- * @subpackage paristemi_database_layer
- * @since Build 0.5
- * @version Build 0.7
- * @filesource
- */
- class BibleStudyDetailed {
- var $ID;
- var $Title;
- var $InstanceTitle;
- var $iID;
- var $iStart;
- var $Start;
- var $Stop;
- var $Day;
- var $MeetByMonth;
- var $Description;
- var $Type;
- var $LocationName;
- var $LocationID;
-
- function BibleStudyDetailed($id, $title, $instancetitle, $iid, $istart,
- $start, $stop, $day, $meetbymonth, $description, $type, $locationname, $locationid) {
- $this->ID = $id;
- $this->Title = $title;
- $this->InstanceTitle = $instancetitle;
- $this->iID = $iid;
- $this->iStart = $istart;
- $this->Start = $start;
- $this->Stop = $stop;
- $this->Day = $day;
- $this->MeetByMonth = $meetbymonth;
- $this->Description = $description;
- $this->Type = $type;
- $this->LocationName = $locationname;
- $this->LocationID = $locationid;
- }
- }
-
-
- ?>