Page 1 of 1

How to use Events

Posted: Sat Aug 05, 2006 2:03 am
by Dr.CSS
I've upgraded to 1.0beta3 and wanted to use the Events module or whatever it's called.

Can't seem to find any thing to tell me how, I looked in Events there is a lot of core events but nothing on how to use them and the help, little blue button, is no help just a list of parameters with no way of knowing how to use them....

NewsArticleAdded

Sent when an article is added.
Parameters

    * \"news_id\" - Id of the news article
    * \"category_id\" - Id of the category for this article
    * \"title\" - Title of the article
    * \"content\" - Content of the article
    * \"summary\" - Summary of the article
    * \"status\" - Status of the article ("draft" or "publish")
    * \"start_time\" - Date the article should start being displayed
    * \"end_time\" - Date the article should stop being displayed

How is it sent? and to whom? and the Help in the upper left corner that sends me to the Wiki lands on There is currently no text in this page.

Re: How to use Events

Posted: Sat Aug 05, 2006 2:18 am
by calguy1000
Events are basically "broadcasted" by the module, or by the core when some significant thing happens.  The Events Manager allows you to add either user defined tags, or compatible modules as handlers for those broadcasts.

The params are the data that's attached to each broadcast.  In the case illustrated above: "NewsArticleAdded" it's the details of the new news article that was just added.

To use events you simply need to write a user defined tag that does something with the parameters of the events, and then add this user defined tag into the handlers for that event.

i.e:  Here's a UDT I have for adding a news item when a new file is uploaded to the Uploads module:

Here's the info about the event I'm handling:

OnUpload

An event generated when a new file is uploaded via the admin or frontend interfaces
Parameters

    * category - The category name
    * name - The name of the uploaded file
    * size - The size of the uploaded file
    * summary - The short description for the uploaded file (may be empty)
    * description - The long description for the uploaded file (may be empty)
    * author - The author of the uploaded file (if available)
    * ip_address - The internet address of the client that uploaded the file

Here's the UDT:

Code: Select all

global $gCms;
$news = $gCms->modules['News']['object'];
if( !$news )
  {
     echo "<H1>ERROR: Couldn't get the news module</H1>";
     return false;
  }
$author = 'somebody';
if( isset( $params['author'] ) )
  {   
     $author = $params['author'];
  }
$summary = "$author has just uploaded ".$params['name'];
$summary .= " ".$params['summary'];
$result = $news->AddNewArticle( $params['category'],
               'New file upload', $summary );
return $result;
You'll note that this UDT simply extracts the paremters from the event, and then calls the $news->AddNewArticle method in the news module.

Interestingly enough, this AddNewArticle method would inturn generate a "NewsArticleAdded" event.

Perhaps if you gave an example of what you want to do with events..... Or were you just stirring up the pot :) )

Re: How to use Events

Posted: Sat Aug 05, 2006 5:47 am
by Dr.CSS
I wanted to stir the pot  :D just kidding.

I'd like to add an event that will be triggered by the addition of a blog article to populate a calendar with the appropriate date so it, the date, can be clicked and take you to the article.

Re: How to use Events

Posted: Sat Aug 05, 2006 2:22 pm
by calguy1000
Well, then no problem.  We already have 1/2 the problem solved..... Which Event to trap

The next thing is to figure out how to add an event into the Calendar module (I'll help you with this, but won't do it for you).  You'll have to look through the calendar module to see
a) is there a nice convenient API function that we can call for doing this
    or
b) do we have to go to the database directly

if the answer is b) then we'll have to know the structure of the database, and make up the appropriate SQL INSERT statement(s).  But this isn't difficult because the installation procedure for the calendar module will give us the structure of the database tables the calendar module uses.

Re: How to use Events

Posted: Sat Aug 05, 2006 4:19 pm
by tsw
calguy1000 wrote: b) do we have to go to the database directly

if the answer is b) then we'll have to know the structure of the database, and make up the appropriate SQL INSERT statement(s).  But this isn't difficult because the installation procedure for the calendar module will give us the structure of the database tables the calendar module uses.
but if calendar db schema changes in future version event would stop working.. so it might be even better to ask calendar module author to create those api functions ;)

just my 2 cents

Re: How to use Events

Posted: Sat Aug 05, 2006 7:08 pm
by calguy1000
Yep, you're correct.... who's the maintainer of Calendar....

Re: How to use Events

Posted: Sat Aug 05, 2006 7:47 pm
by Elijah Lofgren
calguy1000 wrote: Yep, you're correct.... who's the maintainer of Calendar....
Looking at like http://dev.cmsmadesimple.org/scm/?group_id=20 it looks like akrabat (Rob Allen) and katon (Victor Katolyk) were the most active. I don't think they have been active on calendar for over 4 months though.
Calendar may be in need of a new maintainer.

Re: How to use Events

Posted: Sun Aug 06, 2006 2:17 pm
by Dr.CSS
So the event that Calendar uses that I'm looking to harvest would be this one?

I'm looking to have an event that is put in this data base line eg,. a new article is added to this category would be added to the appropriate date in calendar.

$sql = 'SELECT * FROM ' . $this->events_table_name .' WHERE event_id = ' . $event_id;
$rs = $db->Execute($sql);
if($rs->RecordCount() > 0)
{
$result = $rs->FetchRow();
$result['categories'] = array();
// now pick up categories
$sql = 'SELECT category_id FROM ' . $this->events_to_categories_table_name . ' WHERE event_id = ' . $event_id;
$rs = $db->Execute($sql);
if($rs)
{
while($row = $rs->FetchRow())
{
$result['categories'][] = $row['category_id'];
}
}
}
else
{
// create an empty record
$result = array();
$result['event_id'] = -1;
$result['event_title'] = '';
$result['event_summary'] = '';
$result['event_details'] = '';
$result['event_date_start'] = NULL;
$result['event_date_end'] = NULL;
$result['event_created_by'] = -1;
$result['event_create_date'] = NULL; this would be triggered by some thing being added to 'categories' by the UDT event handler
$result['event_modified_date'] = NULL;
$result['categories'] = array();
}

The blog add event has 2 events I want to capture I think these are them, the category may not be important I may just need the date it was added to go to the calendars date data base to add it to the calendar which I could do a custom calendar for the page i want to show it on as having a category of blog entries and that, the category, is set in Calendar, am I getting close?

$usedcategory = '';
if (isset($params['category']))
{
$usedcategory = $params['category'];
}

$postdate = time();
if (isset($params['postdate_Month']))
{
$postdate = mktime($params['postdate_Hour'], $params['postdate_Minute'], $params['postdate_Second'], $params['postdate_Month'], $params['postdate_Day'], $params['postdate_Year']);
}


BTW: I had to change Calendar to work in 1.0-beta3 "Hawaii" should I give this file to someone to be added to the module, in any case I'll attach it here.
It should be tested by someone else to insure compatability/stability.  of course remove the .txt to get a .php file  ;)

[attachment deleted by admin]

Re: How to use Events

Posted: Sun Aug 06, 2006 2:34 pm
by calguy1000
I may look at taking on Calendar for the purpose of adding events, and 1.0 compatibility.

Mark.  Are you interested in a) having all of the blog entries go to one category in the calendar module, or b) having the categories synchronized in some way.  and/or would you want all news articles to go the calendar module as well, or just certain categories.

Re: How to use Events

Posted: Sun Aug 06, 2006 4:21 pm
by Dr.CSS
He chooses...

a) all blog entries go to a category in calendar so i can use calendar in other ways/pages and have it populate the days in calendar with just the date, small calendar, of the calendar link to the blog entry for that day.

Say I make a new article in Blog on june 10th and submit it, would like the Event handler to pick up on it and add the link to that article into that day in Calendar.

Sample of page with little calendar with dates linked to blog entry http://debrasalonen.com/blog/2006/06/ this is the lady I'm making/redesigning site for, new site http://www.multiintech.com/debra/index.php?page=blog , not done still tweaking.