TinyPortal

Development => Block Codes => Topic started by: Ianedres on May 04, 2009, 09:14:17 AM

Title: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 04, 2009, 09:14:17 AM
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 (http://www.php.net/manual/en/function.strtotime.php) 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 (http://www.tinyportal.net/index.php/topic,29466.msg235188.html#msg235188) 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>';
}
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 04, 2009, 09:18:57 AM
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.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Zetan on May 04, 2009, 09:28:30 AM
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:



?>

Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: ZarPrime on May 04, 2009, 04:08:07 PM
Looks like a very useful Snippet.  Good Job. :up:

ZarPrime
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Renegd98 on May 04, 2009, 05:33:56 PM
yes, nice script...
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 04, 2009, 06:10:06 PM
Z2 with R-factor rating!

Most excellent $foo brewing... ::)
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Zetan on May 04, 2009, 09:40:43 PM
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>';
}
}

Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 05, 2009, 02:23:19 AM
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.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Zetan on May 05, 2009, 02:52:32 AM
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>';
}
}

Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 05, 2009, 03:10:18 AM
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.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Zetan on May 05, 2009, 03:17:24 AM
Almost.. well, it fixed the layout  :P


Parse error: syntax error, unexpected ')' in /home/*******/public_html/Sources/Load.php(1759) : eval()'d code(979) : eval()'d code on line 28
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Zetan on May 05, 2009, 03:39:10 AM
Good stuff, got there in the end :)

Z
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 05, 2009, 03:49:17 AM
Crazy day, with a rush to try to fix things. It is stuff like that this that makes coding so much fun!

Corrected the exit routine and posted the fix into the original post at the top of the thread. Also made the change to the similar block snippet for posting to a board.

Thanks for finding the issue; should be good to go now. :coolsmiley:
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: IchBin on May 05, 2009, 05:12:55 AM
Good thing you caught that.... I almost $foo-- on you. :P

Nice snippet though! :up:
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: jirsh on May 15, 2009, 03:56:46 PM
Is there a way to make the displayed events clickable/linkable to the calendar or post? Is there a way to make it show birthdays and holidays? Thanks.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 15, 2009, 04:17:44 PM
Will look into the clickable events, as that would follow typical use anyway.

As for birthdays and holidays, can look into that but I thought there was some existing snippets for such things.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: jirsh on May 15, 2009, 04:21:43 PM
Thanks, I mean for the Holidays and Birhtdays to show in your block
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: jirsh on May 15, 2009, 04:32:34 PM
Also, is there an easy way to change the font sizes?
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 15, 2009, 04:49:38 PM
Changing the font sizes is set with the $css_event variable; match it to something in your theme's style.css file for your site.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 15, 2009, 06:28:48 PM
Rushed my answer today; the events were already clickable if they have a posted topic.

Also, will be updating the original code in the first post of this topic to accommodate the request for birthdays and holidays to be included with any scheduled events. You can select what to display in any combination of the three choices.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: zapiy on May 17, 2009, 12:51:41 AM
Hi brilliant mod, any chance it could show a rolling 4 week period? Hence its always an upto date block.

Cheers
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 17, 2009, 02:39:45 AM
Have you looked at the aforementioned strtotime php function on php.net mentioned in the thread?

It is very versatile in selecting a range.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: zapiy on May 17, 2009, 01:14:35 PM
I will take a look but its a mine field to me lol..
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on May 17, 2009, 07:01:22 PM
View the strtotime function (http://www.php.net/manual/en/function.strtotime.php) and set the $end variable in this line:

$end = date("Y-m-d", strtotime("next Sunday")); // see php.net for strtotime function examples; very robust!


"next Sunday" can be altered to "+4 weeks" or "next month" to accomplish a sliding range to look forward into your calendar. It may not detect everything you try, but you should be able find something that will work for you there.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: zapiy on May 18, 2009, 10:54:42 PM
Cheers fella i will have a play.
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Mick on July 16, 2009, 11:12:34 PM
Very cool.   I dig this ;)


The php.net thread was a mile long so i had to take a guess on how to make this event code to look a whole year ahead.

I replaced  "next Sunday"   for "+1 year" and it seems to work.

http://www.chevyavalancheclub.com/index.php?page=292

Several questions for ya,....

-One thing i noticed,i see a "?" before the event name.  Any ideas why?
-Is there a way to only show 1 line of a 4-5 day event instead of showing the entire days?

Thanx,
Mick.  
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on July 19, 2009, 09:25:01 PM
Quote from: bluedevil on July 16, 2009, 11:12:34 PM
Very cool.   I dig this ;)

Thanks, and glad to hear it worked for you.
QuoteSeveral questions for ya,....

-One thing i noticed,i see a "?" before the event name.  Any ideas why?
-Is there a way to only show 1 line of a 4-5 day event instead of showing the entire days?

The question mark I think is the browser not displaying the non-breaking space properly in the lines with '&nbsp;' in the echo string. Change the portion in between the single quotes to a single space or a dash to keep the indent...

For one line to be displayed in multi-day events, it would probably require a significant change to the code...
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: pimpas on July 28, 2009, 01:12:23 AM
Quote from: Ianedres on May 04, 2009, 09:14:17 AM

// Calendar Event Extraction : Version 1.5 (php Article)
// set range; must be in YYYY-DD-MM format
$start = date("Y-m-d"); // defaults to current SERVER time (no offsets)


1st - You're the man! Excellent work :D

2nd - How can i set the offset time ? Is it possible?

Best regards
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: JPDeni on July 28, 2009, 01:38:04 AM
I know that Iandres is pretty busy these days, so he might not see this.

You could do something like this:


$now = $time() - 5 ; //alter this to the number of hours that you want to offset.
$start = date("Y-m-d",$now);
Title: Re: [Block] Calendar Event Extraction (php Article)
Post by: Ianedres on August 09, 2009, 05:45:35 AM
Thanks for filling in and getting a fix in motion there.

And, yes, work is very busy lately.  :-\