eventrepeat_nodeapi

Definition

eventrepeat_nodeapi(&$node, $op)
eventrepeat/eventrepeat.module, line 530

Description

hook_nodeapi implementation

Parameters

&$node The node the action is being performed on.

$op What kind of action is being performed.

Return value

This varies depending on the operation.

Related topics

Namesort iconDescription
Functions for nodeapi integration

Code

<?php
function eventrepeat_nodeapi(&$node, $op) {

  // only continue if this node is event and event_repeat enabled
  if (variable_get('event_nodeapi_'. $node->type, 'never') == 'never' &&
      variable_get('eventrepeat_nodeapi_'. $node->type, 0) == 0) {
    return;
  }

  //this holds the old start time of an event, for use in mass updates
  static $old_times;

  //for all ops except settings, make sure that the node is event & eventrepeat enabled before executing
  switch ($op) {

    case 'validate':
        // no break. both need a node with a formatted date and event_start
        // and event_end set, 'validate' for the previewing and 'submit' for
        // update/insert.

    case 'submit':

      // TODO: what if the user wants to "turn off" the repeating event
      // the user is trying to turn off repeating events for all future events
      if (($node->eventrepeat_FREQ == '' || $node->eventrepeat_FREQ == 'NONE') && ($node->eventrepeat_edit_type == 'future' || $node->eventrepeat_edit_type == 'all')) {
        form_set_error('eventrepeat_FREQ', t('Trying to remove a repeat setting from a repeating event is not currently supported. Please try deleting the events you don\'t want instead.'));
      }

      //validate the repeat end date
      _eventrepeat_validate_form_date('eventrepeat_end', t('Repeat end'), $node);

      // TODO: this would cause an odd error if the person wanted
      // to remove the repeating sequence so I removed it for now.
      //if they have some repeat options chosen,
      // make sure a repeat type is selected
      /*
      if ($node->eventrepeat_FREQ == 'NONE' &&
        ($node->eventrepeat_COUNT
         || $node->eventrepeat_INTERVAL > 1
         || $node->eventrepeat_BYDAY
         || $node->eventrepeat_BYWEEKNO
         || $node->eventrepeat_BYMONTH
         || $node->eventrepeat_BYMONTHDAY
         || $node->eventrepeat_BYYEARDAY
         || $node->eventrepeat_EXDATE)
      ) {
        form_set_error('eventrepeat_FREQ', t('You must select a Repeat Type if you want this event to repeat.'));
      }
      */

      //warn if user entered values for both repeat end and COUNT
      if ($node->eventrepeat_end && $node->eventrepeat_COUNT) {
        form_set_error('eventrepeat_end', t('\'Repeat end date\' and \'Count\' cannot both be set--select only one to provide a valid end point for the sequence'));
      }

      // TODO: if this is unsupported, can we just use end date after the initial render?
      //warn if user tries to edit a sequence based on COUNT--this is currently not supported
      if ($node->eventrepeat_rid && $node->eventrepeat_COUNT) {
        form_set_error('eventrepeat_COUNT', t('Editing a sequence which uses the  \'Count\' parameter is not currently supported. You may need to delete this event.'));
      }

      break;

    case 'delete':
        static $mass_delete;
        global $form_values;
        //determine what kind of delete we're doing here.  check $form_values to see if it's a mass delete
        //and mark it as such if so.  otherwise just delete the node from the detail table
        if ($form_values['eventrepeat_delete_type'][$node->nid] &&
          $form_values['eventrepeat_delete_type'][$node->nid] != 'this') {

          $mass_delete = TRUE;
          _eventrepeat_delete_nodes($form_values['eventrepeat_delete_type'][$node->nid], $node);
          //this line necessary to clean up mods after eventrepeat
          node_invoke_nodeapi($node, 'delete');
        } else {
          db_query("DELETE FROM {event_repeat_nodes} WHERE nid = %d", $node->nid);

          //if not a mass delete, check to see if this is the last repeat event in this sequence.  if so, then
          //delete the sequence
          if (!$mass_delete) {
            if (!db_num_rows(db_query('SELECT rid FROM {event_repeat_nodes} WHERE rid = %d', $node->eventrepeat_rid))) {
              db_query("DELETE FROM {event_repeat} WHERE rid = %d", $node->eventrepeat_rid);
            }
          }
        }


      break;
    // TODO: is this legacy now? deletions seem to work fine
    /*

    THIS SECTION IS ON HOLD UNTIL DELETE PRE IS IN CORE

      case 'delete pre':

      //also check here to see if the node is part of a repeat sequence
      if (variable_get('event_nodeapi_'. $node->type, 'never') != 'never' &&
        (variable_get('eventrepeat_nodeapi_'. $node->type, 0) == 1) && $node->eventrepeat_rid) {

        //create the option array
        $options = array('this' => t('This occurrence only'),
        'future' => t('This occurrence and all future occurrences'),
        'all' => t('All occurrences')
        );

        //construct the radio buttons.  NOTE: in order to prevent name collisions in the admin
        //delete process for multiple nodes, these form elements are named in array fashion, with nid to distinguish
        //the elements of the array
        $data_to_inject['eventrepeat_delete_type']['#tree'] = TRUE;
        $data_to_inject['eventrepeat_delete_type'][$node->nid] = array('#type' => 'radios', '#title' => t('Repeat event--delete the following'), '#default_value' => 'this', '#options' => $options, '#description' => t('\'This occurrence and all future occurrences\' will delete repeat events from the date of the selected node forward, \'All occurrences\' will delete repeat events after today\'s date.'));
        return $data_to_inject;
      }
      break;*/

    case 'load':

      if (variable_get('event_nodeapi_'. $node->type, 'never') != 'never') {

        //if the old start time hasn't been saved to the update code yet, then save it.  this is a total hack,
        //but i don't know any other way to do it
        if (!$old_times && $_POST['op'] == t('Submit')) {
          $old_times = db_query('SELECT e.event_start, e.event_end, e.timezone FROM {event} e WHERE e.nid = %d', $node->nid);
          if (db_num_rows($old_times)) {
            $old_times = db_fetch_object($old_times);
            $old_times->eventrepeat_old_times = TRUE;
            _eventrepeat_update_nodes($old_times, NULL);
          }
        }

        //if it's a repeat node, grab the repeat data for the node
        if (variable_get('eventrepeat_nodeapi_'. $node->type, 0) == 1) {
          $object = db_fetch_object(db_query('SELECT e_r.rid, repeat_RRULE, repeat_end
            FROM {event_repeat} e_r INNER JOIN {event_repeat_nodes} e_r_n ON
            e_r.rid = e_r_n.rid WHERE e_r_n.nid = %d', $node->nid));

          //if this node is in a repeat sequence, parse the RRULE, and return repeat data to the node
          if ($object->rid) {

            //append the repeat tag to the title if it's an event page
            if (arg(0) == 'event') {
              $node->title = theme("eventrepeat_title_tag", $node->title);
            }
            $items = _eventrepeat_parse_ical($object->repeat_RRULE);
            return array('eventrepeat_rid' => $object->rid,
                         'eventrepeat_FREQ' => $items[0]['FREQ'],
                         'eventrepeat_COUNT' => $items[0]['COUNT'],
                         'eventrepeat_INTERVAL' => $items[0]['INTERVAL'],
                         'eventrepeat_BYDAY' => $items[0]['BYDAY'],
                         'eventrepeat_BYWEEKNO' => $items[0]['BYWEEKNO'],
                         'eventrepeat_BYMONTH' => $items[0]['BYMONTH'],
                         'eventrepeat_BYMONTHDAY' => $items[0]['BYMONTHDAY'],
                         'eventrepeat_BYYEARDAY' => $items[0]['BYYEARDAY'],
                         'eventrepeat_EXDATE' => $items[0]['EXDATE'],
                         'eventrepeat_end' => $object->repeat_end, // placeholder in case the end date processing changes
                         'eventrepeat_endyear' => $object->repeat_end ? _event_date('Y', $object->repeat_end) : 0,
                         'eventrepeat_endmonth' => $object->repeat_end ? _event_date('n', $object->repeat_end) : 0,
                         'eventrepeat_endday' => $object->repeat_end ? _event_date('d', $object->repeat_end) : 0
            );
          }
        }
      }
      break;

    case 'insert':

        // add a new exception to the list if needed
        _eventrepeat_form_add_exception($node);

        // if this node has eventrepeat_FREQ info
        // remember that we strip this for new instances
        // in _eventrepeat_render_nodes
        if($node->eventrepeat_FREQ != '' && $node->eventrepeat_FREQ != 'NONE'){
          _eventrepeat_save_repeat($node);
        }

      break;


    case 'update':
      //setting a static indicator here--this case will more than likely get called
      //multiple times as nodes are updated, but we only want the repeat update
      //itself to run once per page load.
      static $update_run = NULL;
      if (!isset($update_run)) {
        $update_run = TRUE;
        // add a new exception to the list if needed
        _eventrepeat_form_add_exception($node);

        //determine what kind of update we're doing here.  it's either all (from current date
        //forward), future (current node and all future nodes) or just an individual node update
        //pass all and future to the mass edit code
        if ($node->eventrepeat_edit_type == 'future') {
          //we have to update the existing nodes first before we update the repeat pattern
          //only update the repeat pattern if the node update was successful.
          if (_eventrepeat_update_nodes($node, $node->event_start) === TRUE) {
            _eventrepeat_save_repeat($node);
          }
        }
        elseif ($node->eventrepeat_edit_type == 'all') {
          //we have to update the existing nodes first before we update the repeat pattern
          //only update the repeat pattern if the node update was successful.
          if (_eventrepeat_update_nodes($node, time()) === TRUE) {
            _eventrepeat_save_repeat($node);
          }
        }
        elseif ($node->eventrepeat_edit_type == 'this') {
          //if it's an individual edit, and admin has selected that they are to be removed from sequences,
          //then delete from detail table
          if (!variable_get('eventrepeat_single_edit_in_sequence', FALSE)) {
            db_query("DELETE FROM {event_repeat_nodes} WHERE nid = %d", $node->nid);
          }
        }
        // the user is turning a non-repeat event into a repeat event
        elseif(($node->eventrepeat_FREQ != '' && $node->eventrepeat_FREQ != 'NONE') && !$node->eventrepeat_rid){
          _eventrepeat_save_repeat($node);
        }
      }
      break;


    case 'view':
      break;
  }
}
?>