I've been kinda quiet around here lately.
But, I've gone and done it. I've added some stuff so that you can specify which category or categories you would like to include in your calendar. This is done thus:
- Code: Select all
[[ calendar category="category-one,category-two,category-three,etc." ]]
Of course, you would use the names of your categories (
not the display names, the "internal" names). For instance, if you had a category with the display name
My Ramblings you would use
category="my-ramblings". If you do not use the parameter, every category will be displayed, just as it worked before this update.
Hopefully, I haven't stepped on any toes doing this.
Here's the code:
- Code: Select all
<?php
// - Extension: Calendar
// - Version: 0.8 - development
// - Author: Khevor/Kay Hermann/Stingray
// - Email: khevor@khevorslanding.net
// - Site: http://www.khevorslanding.net/
// - Updatecheck: n/a
// - Description: a calendar widget
// - Date: 2009-01-25
// - Identifier: calendar
global $calendar_config;
$calendar_config = array(
'calendar_folder' => "calendar",
'week_start_day' => 1
);
/**
* Adds the hook for calendarAdmin()
*
* @see calendarAdmin()
*/
$this->addHook(
'configuration_add',
'calendar',
array("calendarAdmin", "Calendar")
);
/**
* Adds the hook for the actual widget. We just use the same
* as the snippet, in this case.
*
* @see smarty_calendar()
*/
$this->addHook(
'widget',
'calendar',
"smarty_calendar"
);
/**
* Add the calendar.css to the header..
*/
$this->addHook(
'after_parse',
'insert_before_close_head',
"<!-- calendar -->
<link href='[[pivotx_dir]]extensions/calendar/calendar.css' rel='stylesheet' type='text/css' />
<script type='text/javascript'>
// <![CDATA[
function showCalendarDiv(dday) {
var divDay = 'day' + dday;
document.getElementById('dlCalendarEntries').style.display='block';
document.getElementById('dlCalendarEntries').innerHTML = ' ';
document.getElementById('dlCalendarEntries').innerHTML = document.getElementById(divDay).innerHTML;
}
// ]]>
</script>"
);
// Register 'calendar' as a smarty tag.
$PIVOTX['template']->register_function('calendar', 'smarty_calendar');
/**
* Output
*/
function smarty_calendar($params) {
global $PIVOTX, $calendar_config;
$params = clean_params($params);
$iWeekStart = get_default($PIVOTX['config']->get('week_start_day'), $calendar_config['week_start_day']);
$iTimeZone = $PIVOTX['config']->get('timeoffset');
$iTimeZoneUnit = $PIVOTX['config']->get('timeoffset_unit');
// Stingray addition, to limit categories.
if ($params['category']) {
$categories = explode(",", $params['category']);
}
// Translate TimeZone Unit to full English
switch ($iTimeZoneUnit) {
case "i":
$iTZU = " minutes";
break;
case "d":
$iTZU = " days";
break;
case "m":
$iTZU = " months";
break;
case "y":
$iTZU = " years";
break;
default:
$iTZU = " hours";
}
// Get the current date
$aStoredDate = getdate(strtotime($iTimeZone . $iTZU));
// Were the month and year passed and if so, change the $aCurrentDate to that month/year
// unless it matches the current month and year, then use the date stored in $aStoredDate.
if (isset($_REQUEST['m'])) {
$sCalledMonth = $_REQUEST['m'];
$bMonOk = preg_match("/^[0-9]{1,2}$/", $sCalledMonth) > 0 ? true : false;
if ($sCalledMonth < 1 || $sCalledMonth > 13) { $bMonOk = false; }
}
if (isset($_REQUEST['y'])) {
$sCalledYear = $_REQUEST['y'];
$bYearOk = preg_match("/^[0-9]{4}$/", $sCalledYear) > 0 ? true : false;
}
if ($bMonOk && $bYearOk) {
if ( $sCalledMonth == $aStoredDate['mon'] && $sCalledYear == $aStoredDate['year']) {
$aCurrentDate = $aStoredDate;
} else {
$aCurrentDate = getdate(mktime(0, 0, 0, $sCalledMonth, 1, $sCalledYear));
}
} else {
$aCurrentDate = $aStoredDate;
}
// Get number of days in the month
switch ($aCurrentDate['mon']) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
$iNumDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
$iNumDays = 30;
break;
case 2:
$iNumDays = date("L", mktime(0, 0, 0, $aCurrentDate['mon'], 1, $aCurrentDate['year'])) ? 29 : 28;
}
// Determine the day of the week the month starts at.
$iStartDay = date("w", mktime(0, 0, 0, $aCurrentDate['mon'], 1, $aCurrentDate['year']));
if($iStartDay == 0) $iStartDay = 7;
$j = $iStartDay;
$aMonth = array_fill(1, $iNumDays, 0);
for($i = 1; $i < $iNumDays + 1; $i++) {
if ($j > 7) { $j = 1; }
$aMonth[$i] = $j;
$j++;
}
/******************************
* Get Entries For the Month *
******************************/
$dStartDate = date("Y-m-d-H-i", mktime(0, 0, 0, $aCurrentDate['mon'], 1, $aCurrentDate['year']));
$dStopDate = date("Y-m-d-H-i", mktime(23, 59, 59, $aCurrentDate['mon'], $iNumDays, $aCurrentDate['year']));
$aParams = array ("start" => $dStartDate, "end" => $dStopDate, "status" => "publish");
$aListEntriesRead = $PIVOTX['db']->read_entries($aParams);
$aListEntries = array();
// STINGRAY ADDITION
/*********************************************************
* Limits the entried displayed to the desired categories *
*********************************************************/
if ($categories) {
$categoriescount = count($categories);
$entriescount = count($aListEntriesRead);
for ($i=0;$i<$entriescount;$i++) {
$match = 0;
for ($ii=0;$ii<$categoriescount;$ii++) {
if (in_array($categories[$ii], $aListEntriesRead[$i]['category'])) {
$match++;
}
}
if ($match > 0) {
$aListEntries[] = $aListEntriesRead[$i];
}
}
} else {
$aListEntries = $aListEntriesRead;
}
/******************************
* Start building the calendar *
******************************/
// Build variables for previous and next month links
$prevMonth = $aCurrentDate['mon'] - 1;
$prevYear = $aCurrentDate['year'];
if ($prevMonth < 1) { $prevMonth = 12; $prevYear--; }
$nextMonth = $aCurrentDate['mon'] + 1;
$nextYear = $aCurrentDate['year'];
if ($nextMonth > 12) { $nextMonth = 1; $nextYear++; }
// Escape sequences are only to make it look tidy and serve no other real purpose.
// Build the Month/Year header and the Days of the Week Header
$output = "<a name=\"dlCName\"></a><div class=\"dlCalendarTable\"><table class=\"dlCalendar\">\n\t<tbody>\n";
$output .= "\t<tr><th class=\"previous\"><a href=\"?m=" . $prevMonth . "&y=" . $prevYear . "#dlCName\" title=\"Previous Month\">«</a></th><th colspan=\"5\" class=\"dlCalendarHead\">" . $aCurrentDate['month'] . " " . $aCurrentDate['year'] . "</th><th class=\"next\"><a href=\"?m=" . $nextMonth . "&y=" . $nextYear . "#dlCName\" title=\"Next Month\">»</a></th></tr>\n";
$output .= "\t<tr class=\"dlCalendarSubHead\">\n";
$j = $iWeekStart;
for ($i = 0; $i < 7; $i++) {
switch ($j) {
case 1:
$output .= "\t\t<td>Mo</td>\n";
break;
case 2:
$output .= "\t\t<td>Tu</td>\n";
break;
case 3:
$output .= "\t\t<td>We</td>\n";
break;
case 4:
$output .= "\t\t<td>Th</td>\n";
break;
case 5:
$output .= "\t\t<td>Fr</td>\n";
break;
case 6:
$output .= "\t\t<td>Sa</td>\n";
break;
case 7:
$output .= "\t\t<td>Su</td>\n";
}
$j++;
if ($j > 7) { $j = 1; }
}
//$output .= "\t</tr>\n\t<tr>\n";
/******************************
* Build the actual calendar *
******************************/
// Insert blank days for the first week if necessary.
if ($iWeekStart <= $iStartDay) {
$iBlankDays = $iStartDay - $iWeekStart;
} else {
$iBlankDays = 7 - ($iWeekStart - $iStartDay);
}
for ($i = 0; $i < $iBlankDays; $i++) {
$output .= "\t\t<td> </td>\n";
}
// Insert links for the published entries during the iteration of days.
$aDivInfo = array_fill(1, $iNumDays, "");
for ($i = 1; $i < $iNumDays + 1; $i++) {
if ($aMonth[$i] == $iWeekStart) {
$output .= "\t</tr>\n\t<tr>\n";
}
$dNow = date("Y-m-d", mktime(23, 59, 59, $aCurrentDate['mon'], $i, $aCurrentDate['year']));
$dStoredNow = date("Y-m-d", mktime(23, 59, 59, $aStoredDate['mon'], $aStoredDate['mday'], $aStoredDate['year']));
$bCounter = false;
$sDivInfo = "";
foreach ($aListEntries as $a) {
if ( $dNow == substr($a['publish_date'], 0, 10)) {
$sDivInfo .= '<li><a href="' . $a['link'] . '" title="' . $a['title'] . '">' . $a['title'] . '</a></li>';
$bCounter = true;
}
}
if ($bCounter) {
if ($dNow == $dStoredNow) {
$output .= "\t\t<td class=\"dlCalendarCurrentDay\"><a class=\"dlCalendarDivLink\" onclick=\"showCalendarDiv(" . $i . ");\">" . $i . "</a></td>\n";
} else {
$output .= "\t\t<td><a class=\"dlCalendarDivLink\" onclick=\"showCalendarDiv(" . $i . ");\">" . $i . "</a></td>\n";
}
} else {
if ($dNow == $dStoredNow) {
$output .= "\t\t<td class=\"dlCalendarCurrentDay\">" . $i . "</td>\n";
} else {
$output .= "\t\t<td>" . $i . "</td>\n";
}
}
$aDivInfo[$i] = $sDivInfo;
}
// Insert blank days for the last week if necessary.
$iEndDay = $aMonth[$iNumDays];
$iWeekEnd = $iWeekStart - 1;
if ($iWeekEnd < 1 ) { $iWeekEnd = 7; }
if ($iEndDay <= $iWeekEnd) {
$iBlankDays = $iWeekEnd - $iEndDay;
} else {
$iBlankDays = 7 - ($iEndDay - $iWeekEnd);
}
for ($i = 0; $i < $iBlankDays; $i++ ) {
$output .= "\t\t<td> </td>\n";
}
// Close off the calendar construction
$output .= "\t</tr>\n";
$output .= "\t</tbody>\n</table>\n";
// Build the Entry Div (where the entry titles are to be displayed)
$output .= "<div id=\"dlCalendarEntries\"> </div>\n";
// Build sub Divs where the all entries for that month are hidden
// for use with the javascript.
for ($i = 1; $i < $iNumDays + 1; $i++) {
if ($aDivInfo[$i] != "" ) {
$output .= "<div id=\"day" . $i . "\" style=\"display:none;\"><ul>" . $aDivInfo[$i] . "</ul></div>\n";
}
}
// Close the Entry Div.
$output .= "</div>\n";
// Output the calendar
return $output;
}
/**
* The configuration screen for calendar
*
* @param unknown_type $form_html
*/
function calendarAdmin(&$form_html) {
global $PIVOTX, $calendar_config;
$form = $PIVOTX['extensions']->getAdminForm('calendar');
$form->add( array(
'type' => 'text',
'name' => 'week_start_day',
'label' => "Week Start Day",
'value' => '1',
'error' => 'That\'s not a valid start day for the week!',
'text' => "Enter a numerical value for the day of the week you wish the calendar to start at. 1 = Monday, 2 = Tuesday, ... 7 = Sunday.",
'size' => 8,
'isrequired' => 1,
'validation' => 'string|minlen=1|maxlen=1'
));
/**
* Add the form to our (referenced) $form_html. Make sure you use the same key
* as the first parameter to $PIVOTX['extensions']->getAdminForm
*/
$form_html['calendar'] = $PIVOTX['extensions']->getAdminFormHtml($form, $calendar_config);
}
?>
Remember to rid yourselves of the unwanted spaces if copying the code (as mentioned elsewhere in this thread).