NodeAPI

Events happening in the community are now at Drupal community events on www.drupal.org.
personxx@groups.drupal.org-gdo's picture

Hi everyone has anyone here worked with nodeapi before?

I'm busy writing a module that will add a download link to a VCS (vCalendar file) for each node that has a event enabled.

I started by looking at the documentation at:
hook_nodeapi
hook_form_alter

I then looked at the following example:
nodeapi_example

But it seems like the example code is a bit wonky at best and doesn't seem to save anything to the database.

I then started to disassemble the upload module which ships with Drupal core..

And wrote this module... (Beware it's a bit dirty and definately ugly code)

<?php
function vcalendar_help($section='') {
 
$output = '';
  switch(
$section) {
    case
"admin/help#vcalendar":
     
$output = '<p>' . t("Adds a vcalendar download link to event nodes") . '</p>';
    break;
  }
  return
$output;
}
// function vcalendar_help


function vcalendar_perm() {
  return array(
'access vcalendar content', 'administer vcalendar');
}
// function vcalendar_perm


function vcalendar_menu() {
 
$items = array();

 
$items[] = array(
   
'path' => 'export_vcs',
   
'title' => 'Export VCS',
   
'type' => MENU_CALLBACK,
   
'callback' => 'vcalendar_export',
   
'access' => user_access('administer vcalendar'),
  );
  return
$items;
}


function
vcalendar_form_alter($form_id, &$form) {
  if (
$form_id == 'node_type_form' && isset($form['identity']['type'])) {
   
$form['workflow']['vcalendar'] = array(
     
'#type' => 'radios',
     
'#title' => t('vCalendar'),
     
'#default_value' => variable_get('vcalendar_'. $form['#node_type']->type, 1),
     
'#options' => array(t('Disabled'), t('Enabled')),
    );
  }

  if (isset(
$form['type'])) {
   
$node = $form['#node'];
    if (
$form['type']['#value'] .'<em>node_form' == $form_id && variable_get("vcalendar</em>$node->type", TRUE)) {

     
// VCalendar fieldset
     
$form['vcalendar'] = array(
       
'#type' => 'fieldset',
       
'#access' => user_access('administer vcalendar'),
       
'#title' => t('vCalendar'),
       
'#collapsible' => TRUE,
       
'#collapsed' => empty($node->files),
       
'#description' => t('Would you like to display a vCalendar file for this event?'),
       
'#prefix' => '<div class="attachments">',
       
'#suffix' => '</div>',
       
'#weight' => 30,
      );
     
$form['vcalendar']['display'] = array(
       
'#type' => 'radios',
       
'#title' => 'Display vCalendar download link?',
       
'#options' => array(t(Yes), t(No)),
      );
    }
  }
}

function
vcalendar_nodeapi(&$node, $op, $teaser) {
  switch (
$op) {
    case
'view':
      if (
variable_get("vcalendar_$node->type", 1) == 1) {
       
$node->content['vcalendar'] = array(
         
'#value' => theme('vcalendar_file', $node->nid, $node->title),
         
'#weight' => 50,
        );
      }
    break;
  }
  return array();
}

function
theme_vcalendar_file($nid, $title) {
 
$header = array(t('vCalendar File'));
 
$href = "export_vcs/" . $nid;
 
$rows = array();
 
$text = $title . " " . $nid . ".vcs";
 
$rows[] = array(l($text, $href), l('Download', $href));
  return
theme('table', $header, $rows, array('id' => 'vCalendar'));
}

function
vcalendar_export() {
 
$page_content = '';
 
$nid = arg(1);

 
$filename = "RealIRM_Event_" . $nid . ".vcs";

 
$query = "SELECT * FROM node, node_revisions, event WHERE node.nid = event.nid AND node_revisions.nid = node.nid AND node.nid = " . $nid;
 
$result = db_query($query);
 
$row = db_fetch_array($result);

 
$tag_strip = strip_tags($row['body']);

 
$description = str_replace("\r", "=0D=0A=", $tag_strip);

 
drupal_set_header('Content-Type: text/x-vCalendar');
 
drupal_set_header('Content-Disposition: inline; filename=' . $filename);

 
$page_content .= "BEGIN:VCALENDAR\n";
 
$page_content .= "VERSION:1.0\n";
 
$page_content .= "PRODID:Realirm Calendar\n";
 
$page_content .= "TZ:-07\n";
 
$page_content .= "BEGIN:VEVENT\n";
 
$page_content .= "SUMMARY: " . $row['title'] . "\n";
 
$page_content .= "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" . $description . "\n";
 
$page_content .= "DTSTART: " . date('Ymd\THi00', $row['event_start']) . "\n";
 
$page_content .= "DTEND: " . date('Ymd\THi00', $row['event_end']) . "\n";
 
$page_content .= "END:VEVENT\n";
 
$page_content .= "END:VCALENDAR\n";

  print
$page_content;
  exit();
}
?>

Which seems to get me on the right track...

Can anyone maybe explain to me why the nodeapi_example code doesn't work?
And if I really am on the right track...

I'm not a Uber Drupal Coder yet ;) So any help or links to documentation better documentation would be greatly appreciated.

Regards Julz

Comments

It doesn't...work?

burningdog's picture

Hi Julz

Congrats on starting to play with drupal module code - it's fun! I'm sure you'll get better at it over time :)

That's a lot of code to walk through - anyone who is reading this post is probably not going to take the time to do so. If you can answer these questions, that will put people more in a place to help you:

  • What is it you are trying to do?
  • What did you expect to see?
  • What did you see instead?
  • What have you tried to fix it?

Got it all working

personxx@groups.drupal.org-gdo's picture

Hi Burningdog

I eventualy got nodeapi working... It seems like the example on drupal.org is slightly outdated...

There was some minor changes I had to do get it working under Drupal 5.

The module basicaly allows you to add a vcalendar link (VCF file) to any node type...
On the node creation and edit pages/forms It adds a little form which allows you to add a vcalendar link to the node.
When the link is clicked it downloads a generated VCF file which you can import into MS OUTLOOK.

The event module allows you to download a ics iCalendar file but it contains all the events on the site. My module basicaly extends the event module without making changes to event module itself... And allows you to download a vCalendar file only for the event associated with the node... (Hope this makes sense).

My clients requirements was that they wanted to specifically wanted a vCalendar download and only for the current event. I am busy modifying my module so you can have a iCal file per event as well...

I want to contribute this module... But is a bit confused on how to go about with the CVS stuf... I managed to get a CVS acc on cvs.drupal.org but is still slightly confused on how to branch the module and how the issue cue works etc.

I was thinking maybe at the next Drupal meetup someone could help me with this... I really enjoy coding drupal modules and have quite a few custom modules under my belt and would love to contribute a few to the Drupal.org community...

Regards personxx

CVS accounts

burningdog's picture

Cool - glad you got it working :)

I don't have a CVS account but will hopefully be getting one within the next month or so. I haven't worked with CVS before so no doubt there'll be a learning curve. I'll also be learning about how to do this and would love to find someone who can help me through this.

I'm in Cape Town - I know there's fairly regular meetups in Joburg - I'm trying to gauge interest for a Cape Town one...

There's lots of stuff about how to work with CVS - I've read a lot of it and it's been helpful. Start here: Drupal and CVS

Thanks Burning Dog

personxx@groups.drupal.org-gdo's picture

I think I should get it right with the info from the provided link.

Will let you know how it goes ;)

keen to hear how this

petednz's picture

keen to hear how this progressed - have searched and searched and am failing to find what seems to be an obvious simple need - namely to have an iCal export option for each node that is a particular conteny type (namely an event or a meeting)

I have set up various Calendar views that have iCal feeds - but if these were working properly then I would expect them to download all the events on that particular calendar view

What I am wanting and what you seem to have described above is the option to have this iCal export on the actual node

maybe i am missing a really simple step with FeedAPI or iCal parser etc but .....

Am using D6, Views, CCK, Date API, Calendar etc,

Any pointers really appreciated.

pete davis : fuzion : connect + campaign + communicate

Hi

personxx@groups.drupal.org-gdo's picture

I have written a module that works. It still has a few bugs etc...

I'm still not exactly sure how to properly create branches for this module. As soon as I figgure this out there should be a download link for the development version at http://drupal.org/project/vcalendar

At the current moment I'm just trying to cope with my workload. And get some of my working projects of my shoulders. When all of that is finnished I will try and get this module branched and stable.

I will update this thread as I progress and keep everyone who is interested updated on my progress.

sounds good project but my

petednz's picture

sounds good project but my inquiry was to do with Calendar not Event - but of course we can always look at helping extend it

pete davis : fuzion : connect + campaign + communicate

South Africa

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: