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: 458
  • Total: 458

[Block] Calendar Event Extraction / Post To Boards (php Article)

Started by tim antley, May 04, 2009, 09:42:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ianedres

This is a php article to retrieve a list of events from the SMF calendar, then post directly into a specific message board. Technically, it is not a snippet, but a script. You will need either a cron scheduling utility for your server, or manually run this as you need it.

This script will create a list of events for the current date ranging to a flexible ending date as a formatted message in a board. Detailed instructions for setting the range are in the following message, or available on php.net for the strtotime function.

It uses the official SMF API to create the message in the board you specify.

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 similiar script.

Comments or $foo contributions are appreciated.

05/04/09 : Corrected exit routine for zero articles. (Thnx Zetan!)

<?php

// Calendar Event Extraction & Post
// May 4, 2009
// Tim Antley | www.BayouMX.com | Ianedres
// making $foo

// Utility to pull events from calendar for time period and post to specific message board

// check for SSI.php to load SMF API to run script as stand-alone
if (!defined('SMF')) require_once('../SSI.php');

// if SMF is still not available, terminate with prejudice ;)
if (!defined('SMF')) die('Hacking attempt...');

// set range; must be in YYYY-DD-MM format
$start date("Y-m-d");
$end date("Y-m-d"strtotime("next Sunday")); // see php.net for strtotime function examples; very robust!

// posting variables needing to be set for your site
$board_number '1'// msg board to post to
$member_name "Calendar Script"// name to post as
$topic_subject 'Events Scheduled Until ' date("l, M jS, Y"strtotime($end)); // subject line for msg

// should not need to edit below this line...

// set and load Calendar.php
global $sourcedir;
require_once(
$sourcedir '/Calendar.php');
require_once(
$sourcedir '/Subs-Post.php');
require_once(
$sourcedir '/Load.php');

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

if(
count($events) < 1
{
echo 'No events located in calendar for date range provided.';
return;
}

// container for body of message
$body '';

foreach(
$events as $key => $event_day)
{
// insert day information and start bbcode list
$body .= date("l, M jS, Y"strtotime($key)) . '[hr] [list]';

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'];

// insert event information
$body .= '[li]' $link '[/li]';

}

// end of list
$body .= '[/list][hr]';

}

// not recommended to change anything below here
$topic ''// leave blank
$topic_prefix ''// set to prepend header ahead of dynamic data
$topic_icon 'xx'// icon selection (advanced)

$member_email ''// leave blank
$member_id ''// leave blank
$member_ip ''// leave blank

// SMF's createPost arrays
$msgOptions = array( 'id' => 0,
'body' => $topic_prefix $body,
'icon' => $topic_icon,
'subject' => $topic_subject,
);

$topicOptions = array( 'board' => $board_number,
'id' => $topic,
'lock_mode' => 0,
'mark_as_read' => false,
'poll' => 0,
'sticky_mode' => 0,
);

$posterOptions = array( 'email' => $member_email,
'id' => $member_id,
'ip' => $member_ip,
'name' => $member_name,
'update_post_count' => true,
);

// call SMF to insert post
createPost($msgOptions$topicOptions$posterOptions);
 
?>

Ianedres

There are a few variables to set in this script beforehand.

$board_number is the SMF message board to which the message should be posted; you may wish to create a new board to test your script with initially until you decide how much or how little to display. The createPost function is used to insert the message through the SMF API code.

$member_name will be the name that displays as the message poster; this does not have to be an authentic member, so 'News' or 'Calendar' is permitted, but will display as a guest would.

$topic_subject is the subject line with the ending date generated automatically. You can adjust that to what you want, of course.

Using the strtotime function 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. If you set it for a negative range (why?), you should get nothing.

It does require access to 'Calendar.php', 'Subs-Post.php', 'Load.php' and calls for all through the SMF $sourcedir location. It does not make any checks for the viewer's permissions whatsoever, so any events found are displayed to whoever can read the message.

I do use a check to see if SMF is running and if not, tries to load the SSI.php. You may have to set this to your site's directory structure should it not load. I store all my scripts on my server and call them through the 'include' function in articles. I do this for several reasons- 1) I can edit the scripts easily with another program and 2) the scripts remain if my SMF database gets scrambled.

One thing comes to mind: if you leave this script in a public directory, it will run each time it is requested and insert the message. So take the precautions to limit access to spiders and unauthorized access. You can delete the messages, but it would not take long to run a few hundred times.

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.

ZarPrime

Nice Bro'.  You've been working hard, looks very useful. :up:

ZarPrime

Ianedres

Quote from: ZarPrime on May 04, 2009, 04:14:32 PMNice Bro'.  You've been working hard, looks very useful. :up:

Thanks so much. Explaining it took longer than the code (I think?) since I try to answer questions beforehand and I forget so much these days when I work on a script after the fact!

This website is proudly hosted on Crocweb Cloud Website Hosting.