This how-to explains how to integrate public kronolith calendars with your other websites through screen scraping with PHP. There are other methods of embedding kronolith calendars with javascript, but they don't provide the same level of functionality.
++ Assumptions
++ Setup
This setup uses two files, one to provide the necessary functions and another that you can put anywhere on your site (or multiple places) where you'd like the calendar displayed.
The file used to display the calendars is fairly simple, it includes a link to appropriate stylesheets, the call to the calendar with associated config variables and if necessary header and footer information. This example is designed to go inside the
tags on your page.'''Place this in the php file where you would like to have the calendar display'''
'''Place this file in your php include path''' or adjust the require_once call above
function post_calendar($url_base, $display_cals, $default_view) {
// date cases
// Day
if(isset($_REQUEST['mday']) && isset($_REQUEST['month']) && isset($_REQUEST['year'])) {
$time = 'month=' . $_REQUEST['month'] . '&mday=' . $_REQUEST['mday'] . '&year=' . $_REQUEST['year'];
}
// Week & Work Week
else if (isset($_REQUEST['week']) && isset($_REQUEST['year'])) {
$time = 'week=' . $_REQUEST['week'] . '&year=' . $_REQUEST['year'];
}
// Month
else if (isset($_REQUEST['month']) && isset($_REQUEST['year'])) {
$time = 'month=' . $_REQUEST['month'] . '&year=' . $_REQUEST['year'];
}
// Year
else if (isset($_REQUEST['year'])) {
$time = 'year=' . $_REQUEST['year'];
}
else if (isset($_REQUEST['timestamp'])) {
$time = 'timestamp=' . $_REQUEST['timestamp'];
} else {
$time = 'timestamp=' . time();
}
if(!isset($_REQUEST['view'])) {
$_REQUEST['view'] = $default_view;
}
#construct the URL we want to get content from
$view = $_REQUEST['view'];
foreach ($display_cals as $display_cal) {
$calendars .= '&display_cal[]=' . $display_cal;
}
if($_REQUEST['view'] == 'event') {
$targetfile = $url_base . $view . ".php?" . $time . '&calendar=' . $_REQUEST['calendar'] . '&eventID=' . $_REQUEST['eventID'];
} else {
$targetfile = $url_base . $view . ".php?" . $time . $calendars;
}
//print $targetfile;
$targetfile = str_replace(" ","%20",$targetfile);
$file = file($targetfile);
$calendarfile = implode('',$file);
$calendar_array = explode("", $calendarfile);
$calendar_array = explode("", $calendar_array[1]);
$calendarfile = $calendar_array[0];
// Convert day/week/month/year links to local links
$calendarfile = str_replace('/calendars/kronolith/day.php?', $_SERVER['SCRIPT_URL'] . '?view=day' . $calendars . '&',$calendarfile);
$calendarfile = str_replace('/calendars/kronolith/week.php?', $_SERVER['SCRIPT_URL'] . '?view=week' . $calendars . '&',$calendarfile);
$calendarfile = str_replace('/calendars/kronolith/workweek.php?', $_SERVER['SCRIPT_URL'] . '?view=workweek' . $calendars . '&',$calendarfile);
$calendarfile = str_replace('/calendars/kronolith/month.php?', $_SERVER['SCRIPT_URL'] . '?view=month' . $calendars . '&',$calendarfile);
$calendarfile = str_replace('/calendars/kronolith/year.php?', $_SERVER['SCRIPT_URL'] . '?view=year' . $calendars . '&',$calendarfile);
$calendarfile = str_replace('/calendars/kronolith/academicyear.php?', $_SERVER['SCRIPT_URL'] . '?view=academicyear' . $calendars . '&',$calendarfile);
// Deal with individual events
$calendarfile = str_replace('/calendars/kronolith/event.php?', $_SERVER['SCRIPT_URL'] . '?view=event' . '&',$calendarfile);
// Cleanup display
// Remove the category lines
$calendarfile = preg_replace('/.*\s*.*\s*.*\s*.*\s*.*\s*.*\s/', '' ,$calendarfile);
// Remove the owner lines
$calendarfile = preg_replace('/.*\s*.*\s*.*\s*.*\s*.*\s*/', '' ,$calendarfile);
// Remove the status lines
$calendarfile = preg_replace('/.*\s*.*\s*.*\s*.*\s*.*/', '' ,$calendarfile);
// Remove alarm and created lines
$calendarfile = preg_replace('/.*\s*.*\s*.*\s*.*\s*.*\s*.*\s*.*\s*.*\s*.*\s*.*/', ' ' ,$calendarfile);
// Clean up quotes
$calendarfile = stripslashes($calendarfile);
// Remove titles because the are broken
$calendarfile = preg_replace('/(title=")(.*?)(")/e', "", $calendarfile);
// Remove script files because they are broken too
$calendarfile = str_replace('<script type="text/javascript" src="/calendars/kronolith/js/QuickFinder.js"></script>', '', $calendarfile);
$calendarfile = str_replace('<script type="text/javascript" src="/calendars/kronolith/js/redbox.js"></script>', '', $calendarfile);
$calendarfile = str_replace('<script type="text/javascript" src="/calendars/kronolith/js/calendar-panel.js"></script>', '', $calendarfile);
?>
echo $calendarfile;
?>
}
++ Further Customization
If you would like to hide the menus for guests you can adjust the pages in kronolith that display the different views. Here's an example of the kronolith/day.php file. You'll want to adjust the month, week, workweek and year files as well.
require_once dirname(__FILE__) . '/lib/base.php';
$view = Kronolith::getView('Day');
$title = $view->getTime($prefs->getValue('date_format'));
$print_view = (bool)Util::getFormData('print');
Horde::addScriptFile('tooltip.js', 'horde', true);
if (!$print_view) {
Horde::addScriptFile('popup.js', 'horde', true);
}
require KRONOLITH_TEMPLATES . '/common-header.inc';
$user = Auth::getAuth();
if ($user != '') {
if ($print_view) {
require $registry->get('templates', 'horde') . '/javascript/print.js';
} else {
require KRONOLITH_TEMPLATES . '/menu.inc';
}
echo '';
}
else {
echo '';
}
if (!$print_view) {
Kronolith::tabs();
}
$view->html(KRONOLITH_TEMPLATES);
echo '';
if ($print_view) {
require KRONOLITH_TEMPLATES . '/calendar_titles.inc';
} else {
require KRONOLITH_TEMPLATES . '/panel.inc';
}
require $registry->get('templates', 'horde') . '/common-footer.inc';