TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 884
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 452
  • Total: 452

[Block] Calendar Event Extraction (php Article)

Started by tim antley, May 04, 2009, 09:14:17 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ianedres

This is a php article to display a list of events from the SMF calendar.

This script is intended to organize the generated output into a list format of events for the current date ranging to a flexible ending date. Detailed instructions in the following message, or available on php.net for the strtotime function.

The list is dynamically generated each time the article is viewed, thus any changes to the calendar are reflected immediately. It does not modify or alter any database tables.

Any changes or corrections to the script will be posted in this message; i.e. this will be the most current version of the snippet.

See this snippet for a similar script that post directly to your message boards.

Comments or $foo++ contributions are appreciated.

05/15/09 : Added request to include birthdays and/or holidays in the list.
05/04/09 : Corrected exit routine for zero articles. (Thnx Zetan!)

// Calendar Event Extraction : Version 1.5 (php Article)
// May 15, 2009
// Tim Antley | www.BayouMX.com | Ianedres
// making $foo

// Utility to extract events, birthdays, and holidays from calendar for selected time period

// set and load Calendar.php
global $sourcedir, $scripturl;
require_once($sourcedir . '/Calendar.php');

// set range; must be in YYYY-DD-MM format
$start = date("Y-m-d"); // defaults to current SERVER time (no offsets)
$end = date("Y-m-d", strtotime("next Sunday")); // see php.net for strtotime function examples; very robust!

// set following variables to 'true' or 'false' to display events, birthdays, or holidays
$show_events = true;
$show_bdays = true;
$show_holidays = true;

// set to 'true' or 'false' to show message at end of script for empty ranges
$show_errors = true;

// css classes for title and events
$css_cat = 'catbg';
$css_day = 'titlebg';
$css_event = 'windowbg';

// get arrays and sort
if($show_events == true)
{
$events = calendarEventArray($start, $end);
ksort($events);
}

if($show_bdays == true)
{
$bdays = calendarBirthdayArray($start, $end);
ksort($bdays);
}

if($show_holidays == true)
{
$holidays = calendarHolidayArray($start, $end);
ksort($holidays);
}

// events
if(!empty($events) && $show_events == true)
{
echo '<div class="' . $css_cat . '">' . 'Events' . '</div>';
foreach($events as $key => $event_day)
{
// display day and date in css class
echo '<div class = "' . $css_day . '">' . date("l, M jS, Y", strtotime($key)) . '</div>';

foreach($event_day as $event)
{
// setting variables for all elements of array {for future use ;) }
$id = $event['id'];
$title = $event['title'];
$can_edit = $event['can_edit'];
$modify_href = $event['modify_href'];
$href = $event['href'];
$link = $event['link'];
$start_date = $event['start_date'];
$end_date = $event['end_date'];
$is_last = $event['is_last'];

// display event info in css class
echo '<div class="' . $css_event . '">';
echo '&nbsp;' . $link . '<br />'; // using non-breaking space for slight indent
echo '</div>';
}
}

echo '<hr />';
}

// birthdays
if(!empty($bdays) && $show_bdays == true)
{
echo '<div class="' . $css_cat . '">' . 'Birthdays' . '</div>';
foreach($bdays as $key => $event_day)
{
// display day and date in css class
echo '<div class = "' . $css_day . '">' . date("l, M jS, Y", strtotime($key)) . '</div>';

foreach($event_day as $event)
{
$id = $event['id'];
$name = $event['name'];
$age = $event['age'];
$is_last = $event['is_last'];

// display event info in css class
echo '<div class="' . $css_event . '">';
echo '&nbsp;' . '<a href="' . $scripturl . '?action=profile;u=' . $id . '">';
echo $name . '</a >' . ($age != 0 ? ' ('. $age . ')' : '') . '<br />';
echo '</div>';
}
}

echo '<hr />';
}

// holidays
if(!empty($holidays) && $show_holidays == true)
{
echo '<div class="' . $css_cat . '">' . 'Holidays' . '</div>';

foreach($holidays as $key => $event_day)
{
// display day and date in css class; holiday name afterwards
echo '<div class = "' . $css_day . '">' . date("l, M jS, Y", strtotime($key)) . ' - ';

$name = $event_day['0'];

echo $name . '</div>';
}

echo '<hr />';
}

if($show_errors == true)
{
if(empty($events) && $show_events == true) echo '<div class="error">' . 'No events scheduled in selected range.' . '</div>';
if(empty($bdays) && $show_bdays == true) echo '<div class="error">' . 'No birthdays scheduled in selected range.' . '</div>';
if(empty($holidays) && $show_holidays == true) echo '<div class="error">' . 'No holidays scheduled in selected range.' . '</div>';
}

Ianedres

#1
This script will display calendar entries from a selected range. You should use this as a php article or block. It does not alter the database, nor does it check permissions so all results are displayed to whoever is viewing.

It uses the strtotime function which allows robust flexibility in deciding when the ending range is set. By using such language as 'next Thursday', '+2 days', '+4 weeks', you can have it sliding the end point without having to rework the code.

There are some good examples on the php.net site for the function definition; I have not tried to break this script by inserting any odd variables, but it should meet the needs of most sites easily.

Variables:
Three variables exist to display events, birthdays, or holidays in any combination; a fourth variable will have the script show a simple message at the end of the script if a selected category did not have any results. These are all set to 'true' or 'false' and should be self-explanatory in the script.

Three CSS variables ($css_cat, $css_day, and $css_event) allow you to format the output of the list; simply use classes found in your theme's style.css file to alter the look of the list.

The script will require access to 'Calendar.php' and calls for it through the SMF $sourcedir location for maximum compatibility.

Clickable Stuff:
Events that have an associated message topic can be clicked to view the message. Names for birthdays are linked to their respective profile.

Birthdays will show the age, if year of birth exists in that member's profile.

I have commented this code to help with any questions. Any suggestions are welcome and I will try to accommodate practical requests for modifications to the code.

Zetan

Thanks Ianedres. Looks interesting and will be useful for my site :up:

Don't forget, remove the php tags at the beginning and end of the script, TP Articles have these already coded in and will return an error if used.

remove these..

top:



<?php




bottom:



?>


ZarPrime

Looks like a very useful Snippet.  Good Job. :up:

ZarPrime

Renegd98


Ianedres

Z2 with R-factor rating!

Most excellent $foo brewing... ::)

Zetan

It breaks the site layout:

http://www.slayingsteel.com/index.php?page=events

Notice the footer.. or a lack of one.

Code used:



// Calendar Event Extraction (php Article)
// May 4, 2009
// Tim Antley | www.BayouMX.com | Ianedres
// making $foo

// Utility to pull events from calendar for time period

// defacto standard security check for SMF
if (!defined('SMF')) die('Hacking attempt...');

// set and load Calendar.php
global $sourcedir;
require_once($sourcedir . '/Calendar.php');

// set range; must be in YYYY-DD-MM format
$start = date("Y-m-d"); // defaults to current SERVER time (no offsets)
$end = date("Y-m-d", strtotime("next Sunday")); // see php.net for strtotime function examples; very robust!

// css classes for title and events
$css_day = 'titlebg';
$css_event = '';

// get array of events and sort by key
$events = calendarEventArray($start, $end);
ksort($events);

// if no events exist, exit script
if(count($events) < 1) die('No events located in calendar for date range provided.');

foreach($events as $key => $event_day)
{
// display day and date in css class
echo '<div class = "' . $css_day . '">' . date("l, M jS, Y", strtotime($key)) . '</div>';

foreach($event_day as $event)
{
// setting variables for all elements of array {for future use ;) }
$id = $event['id'];
$title = $event['title'];
$can_edit = $event['can_edit'];
$modify_href = $event['modify_href'];
$href = $event['href'];
$link = $event['link'];
$start_date = $event['start_date'];
$end_date = $event['end_date'];
$is_last = $event['is_last'];

// display event info in css class
echo '<div class="' . $css_event . '">';
echo '�' . $link . '<br />'; // using non-breaking space for slight indent
echo '</div>';
}
}


Ianedres

Sigh. Done that before...

Change "die" to "exit" and have it like this:
if(count($events) < 1) exit;

Once you confirm that works, I'll adjust the code here and in the other snippet.

Zetan

Works fine with events in the calendar, but still not working properly without.
I think I may have discovered a bug in TP too. That article is using the default theme when logged in, but when guests view it, it's using the main site theme.




// Calendar Event Extraction (php Article)
// May 4, 2009
// Tim Antley | www.BayouMX.com | Ianedres
// making $foo

// Utility to pull events from calendar for time period

// defacto standard security check for SMF
if (!defined('SMF')) die('Hacking attempt...');

// set and load Calendar.php
global $sourcedir;
require_once($sourcedir . '/Calendar.php');

// set range; must be in YYYY-DD-MM format
$start = date("Y-m-d"); // defaults to current SERVER time (no offsets)
$end = date("Y-m-d", strtotime("next Sunday")); // see php.net for strtotime function examples; very robust!

// css classes for title and events
$css_day = 'titlebg';
$css_event = '';

// get array of events and sort by key
$events = calendarEventArray($start, $end);
ksort($events);

// if no events exist, exit script
if(count($events) < 1) exit;

foreach($events as $key => $event_day)
{
// display day and date in css class
echo '<div class = "' . $css_day . '">' . date("l, M jS, Y", strtotime($key)) . '</div>';

foreach($event_day as $event)
{
// setting variables for all elements of array {for future use ;) }
$id = $event['id'];
$title = $event['title'];
$can_edit = $event['can_edit'];
$modify_href = $event['modify_href'];
$href = $event['href'];
$link = $event['link'];
$start_date = $event['start_date'];
$end_date = $event['end_date'];
$is_last = $event['is_last'];

// display event info in css class
echo '<div class="' . $css_event . '">';
echo '�' . $link . '<br />'; // using non-breaking space for slight indent
echo '</div>';
}
}


Ianedres

OMG! What a day and that's what I get for rushing an answer on the way out the door...

Use return(); instead of exit(); Exit and die are functionally equivalent. :buck2:

// if no events exist, exit script
if(count($events) < 1) return();


JPDeni's $foo much too strong.

This website is proudly hosted on Crocweb Cloud Website Hosting.