Index: eventrepeat.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/eventrepeat/eventrepeat.module,v
retrieving revision 1.39
diff -u -F^f -r1.39 eventrepeat.module
--- eventrepeat.module	11 Sep 2006 22:36:09 -0000	1.39
+++ eventrepeat.module	23 Sep 2006 01:13:27 -0000
@@ -380,11 +380,11 @@ function eventrepeat_form_alter($form_id
 
       // If this is a new node and this user
       // doesn't have access to create repeating events
-      // just return. 
+      // just return.
       if(!$node->nid && !user_access('create repeat events')){
         return;
       }
-      // If, however, this is an existing event that has   
+      // If, however, this is an existing event that has
       // a repeating pattern attached to it, we need to
       // show the form because otherwise the user would
       // end up breaking this node out of the repeat.
@@ -505,24 +505,24 @@ function eventrepeat_nodeapi(&$node, $op
         // 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, 
+      //if they have some repeat options chosen,
       // make sure a repeat type is selected
       /*
-      if ($node->eventrepeat_FREQ == 'NONE' && 
+      if ($node->eventrepeat_FREQ == 'NONE' &&
         ($node->eventrepeat_COUNT
          || $node->eventrepeat_INTERVAL > 1
          || $node->eventrepeat_BYDAY
@@ -546,12 +546,12 @@ function eventrepeat_nodeapi(&$node, $op
       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; 
+        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] &&
@@ -649,7 +649,7 @@ function eventrepeat_nodeapi(&$node, $op
         }
       }
       break;
-      
+
     case 'insert':
 
         // add a new exception to the list if needed
@@ -661,12 +661,17 @@ function eventrepeat_nodeapi(&$node, $op
         if($node->eventrepeat_FREQ != '' && $node->eventrepeat_FREQ != 'NONE'){
           _eventrepeat_save_repeat($node);
         }
-        
+
       break;
-    
-    
-    case 'update':
 
+
+    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);
 
@@ -674,39 +679,34 @@ function eventrepeat_nodeapi(&$node, $op
         //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') {
-          // TODO: I thought I would need to call this, but I guess I don't?
-          //_eventrepeat_save_repeat($node);
-          // _eventrepeat_update_nodes seems to update the time, title and body
-          // which is all we are supporting as of this patch
-          _eventrepeat_save_repeat($node);
-          _eventrepeat_update_nodes($node, $node->event_start);
-        } 
+          //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') {
-          // TODO: I thought I would need to call this, but I guess I don't?
-          // _eventrepeat_update_nodes seems to update the time, title and body
-          // which is all we are supporting as of this patch
-          //_eventrepeat_save_repeat($node);
-          _eventrepeat_save_repeat($node);
-          _eventrepeat_update_nodes($node, time());
-        } 
+          //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);          
+          _eventrepeat_save_repeat($node);
         }
-        
-
-        
-        
+      }
       break;
-      
-      
+
+
     case 'view':
       if (user_access('create repeat events') && arg(0) == 'node' && arg(2) == 'repeat') {
         $node->body .= t('Need help creating a repeat pattern? Click ') . l(t('here'), 'repeat/help');
@@ -942,11 +942,11 @@ function theme_eventrepeat_form($node) {
       'all' => t('All occurrences')
     );
     $form['eventrepeat_edit_type'] = array(
-      '#type' => 'radios', 
-      '#title' => t('Apply edit(s) to'), 
-      '#default_value' => 'future', 
-      '#options' => $options, 
-      '#description' => t('\'This occurrence and all future occurrences\' will edit repeat events from the date of the selected node forward, \'All occurrences\' will edit repeat events after today\'s date. <br>Note: editing a single occurrence will remove it from the repeat sequence.'), 
+      '#type' => 'radios',
+      '#title' => t('Apply edit(s) to'),
+      '#default_value' => 'future',
+      '#options' => $options,
+      '#description' => t('\'This occurrence and all future occurrences\' will edit repeat events from the date of the selected node forward, \'All occurrences\' will edit repeat events after today\'s date. <br>Note: editing a single occurrence will remove it from the repeat sequence.'),
       '#weight' => -12
     );
   }
@@ -962,7 +962,7 @@ function theme_eventrepeat_form($node) {
                        ) ? FALSE: TRUE;
   $collapsed = TRUE;
   if(($node->eventrepeat_FREQ != '' && $node->eventrepeat_FREQ != 'NONE')
-     || $exception_collapsed == FALSE 
+     || $exception_collapsed == FALSE
      || $advanced_collapsed == FALSE
   ){
     $collapsed = FALSE;
@@ -985,28 +985,28 @@ function theme_eventrepeat_form($node) {
   	'YEARLY' => t('Yearly')
   );
   $form['eventrepeat']['eventrepeat_FREQ'] = array(
-    '#type' => 'select', 
-    '#title' => t('Repeat type'), 
-    '#default_value' => $node->eventrepeat_FREQ ? $node->eventrepeat_FREQ : 'NONE', 
-    '#options' => $options, 
+    '#type' => 'select',
+    '#title' => t('Repeat type'),
+    '#default_value' => $node->eventrepeat_FREQ ? $node->eventrepeat_FREQ : 'NONE',
+    '#options' => $options,
     '#description' => t('select \'none\' to disable repeats for this event')
   );
 
   //put end controls in a group to make them less confusing
   $form['eventrepeat']['end_controls'] = array(
-    '#type' => 'fieldset', 
+    '#type' => 'fieldset',
     '#title' => t('End Settings'),
-    '#collapsible' => TRUE, 
+    '#collapsible' => TRUE,
     '#description' => t('Select either the end date or the number of times you want this event to repeat.')
   );
 
   //date box for end date
   $form['eventrepeat']['end_controls']['end_date'] = array(
-    '#type' => 'eventrepeat_date', 
-    '#title' => t('Repeat end date'), 
+    '#type' => 'eventrepeat_date',
+    '#title' => t('Repeat end date'),
     '#process' => array('_eventrepeat_form_date' => array($node, 'eventrepeat_end'))
   );
-    
+
   // the - or - markup
   $form['eventrepeat']['end_controls']['or'] = array('#type' => 'markup', '#value' => '<b>---' . t('OR') . '---</b>');
 
@@ -1017,10 +1017,10 @@ function theme_eventrepeat_form($node) {
     $options[$i] = $i;
   }
   $form['eventrepeat']['end_controls']['eventrepeat_COUNT'] = array(
-    '#type' => 'select', 
-    '#title' => t('Count'), 
-    '#default_value' => $node->eventrepeat_COUNT ? $node->eventrepeat_COUNT : 0, 
-    '#options' => $options, 
+    '#type' => 'select',
+    '#title' => t('Count'),
+    '#default_value' => $node->eventrepeat_COUNT ? $node->eventrepeat_COUNT : 0,
+    '#options' => $options,
     '#description' => t('Determines the number of repeat nodes that will be created for the repeat sequence')
   );
 
@@ -1067,13 +1067,13 @@ function theme_eventrepeat_form_advanced
       $options[$i] = $i;
     }
     $form['eventrepeat_advanced']['eventrepeat_INTERVAL'] = array(
-      '#type' => 'select', 
-      '#title' => t('Interval'), 
-      '#default_value' => ($node->eventrepeat_INTERVAL) ? $node->eventrepeat_INTERVAL : 1, '#options' => $options, 
+      '#type' => 'select',
+      '#title' => t('Interval'),
+      '#default_value' => ($node->eventrepeat_INTERVAL) ? $node->eventrepeat_INTERVAL : 1, '#options' => $options,
       '#description' => t('Frequency of repeat: 1 = every, 2 = every other, 3 = every 3rd, etc.')
     );
   }
-  
+
   //set days of the week array, and copy it into $DOW
   $options = array(
     'SU' => t('Sunday'),
@@ -1110,16 +1110,16 @@ function theme_eventrepeat_form_advanced
   //BYDAY param select box
   if($show_advanced['eventrepeat_BYDAY']){
     $form['eventrepeat_advanced']['eventrepeat_BYDAY'] = array(
-      '#type' => 'select', 
-      '#title' => t('Day(s)'), 
-      '#default_value' => $node->eventrepeat_BYDAY ? $node->eventrepeat_BYDAY : '', 
-      '#options' => $options, '#description' => t('Determines what day(s) of the week/month this event repeats on (by day of the week).  Lots of options available, scroll down!'), 
-      '#attributes' => array('size' => '7'), 
+      '#type' => 'select',
+      '#title' => t('Day(s)'),
+      '#default_value' => $node->eventrepeat_BYDAY ? $node->eventrepeat_BYDAY : '',
+      '#options' => $options, '#description' => t('Determines what day(s) of the week/month this event repeats on (by day of the week).  Lots of options available, scroll down!'),
+      '#attributes' => array('size' => '7'),
       '#multiple' => TRUE
     );
   }
-    
-    
+
+
   //BYMONTH param select box
   if($show_advanced['eventrepeat_BYMONTH']){
     $options = array(
@@ -1137,16 +1137,16 @@ function theme_eventrepeat_form_advanced
       12 => t('December'),
     );
     $form['eventrepeat_advanced']['eventrepeat_BYMONTH'] = array(
-      '#type' => 'select', 
-      '#title' => t('Month(s)'), 
-      '#default_value' => $node->eventrepeat_BYMONTH ? $node->eventrepeat_BYMONTH : '', 
-      '#options' => $options, 
-      '#description' => t('Selects what month(s) of the year this event repeats on'), 
+      '#type' => 'select',
+      '#title' => t('Month(s)'),
+      '#default_value' => $node->eventrepeat_BYMONTH ? $node->eventrepeat_BYMONTH : '',
+      '#options' => $options,
+      '#description' => t('Selects what month(s) of the year this event repeats on'),
       '#multiple' => TRUE,
       '#size' => 5,
     );
   }
-    
+
   //BYMONTHDAY param select box
   if($show_advanced['eventrepeat_BYMONTHDAY']){
     $options = array ();
@@ -1157,16 +1157,16 @@ function theme_eventrepeat_form_advanced
       $options[$i] = $i;
     }
     $form['eventrepeat_advanced']['eventrepeat_BYMONTHDAY'] = array(
-      '#type' => 'select', 
-      '#title' => t('Day(s) of the Month'), 
-      '#default_value' => $node->eventrepeat_BYMONTHDAY ? $node->eventrepeat_BYMONTHDAY : '', 
-      '#options' => $options, 
-      '#description' => t('Determines what day(s) of the month this event repeats on (the actual day number in the month).  Negative numbers count from the end of the month.'), 
+      '#type' => 'select',
+      '#title' => t('Day(s) of the Month'),
+      '#default_value' => $node->eventrepeat_BYMONTHDAY ? $node->eventrepeat_BYMONTHDAY : '',
+      '#options' => $options,
+      '#description' => t('Determines what day(s) of the month this event repeats on (the actual day number in the month).  Negative numbers count from the end of the month.'),
       '#multiple' => TRUE,
       '#size' => 5,
     );
   }
-  
+
   //BYYEARDAY param select box
   if($show_advanced['eventrepeat_BYYEARDAY']){
     $options = array ();
@@ -1177,16 +1177,16 @@ function theme_eventrepeat_form_advanced
       $options[$i] = $i;
     }
     $form['eventrepeat_advanced']['eventrepeat_BYYEARDAY'] = array(
-      '#type' => 'select', 
-      '#title' => t('Day(s) of the Year'), 
-      '#default_value' => $node->eventrepeat_BYYEARDAY ? $node->eventrepeat_BYYEARDAY : '', 
-      '#options' => $options, 
-      '#description' => t('Determines what day(s) of the year this event repeats on. Negative numbers count from the end of the year.'), 
+      '#type' => 'select',
+      '#title' => t('Day(s) of the Year'),
+      '#default_value' => $node->eventrepeat_BYYEARDAY ? $node->eventrepeat_BYYEARDAY : '',
+      '#options' => $options,
+      '#description' => t('Determines what day(s) of the year this event repeats on. Negative numbers count from the end of the year.'),
       '#multiple' => TRUE,
       '#size' => 5,
     );
   }
-   
+
   //BYWEEKNO param select box
   if($show_advanced['eventrepeat_BYWEEKNO']){
     $options = array ();
@@ -1197,28 +1197,28 @@ function theme_eventrepeat_form_advanced
       $options[$i] = $i;
     }
     $form['eventrepeat_advanced']['eventrepeat_BYWEEKNO'] = array(
-      '#type' => 'select', 
-      '#title' => t('Week Number(s)'), 
-      '#default_value' => $node->eventrepeat_BYWEEKNO ? $node->eventrepeat_BYWEEKNO : '', 
-      '#options' => $options, 
-      '#description' => t('Selects what week(s) of the year this event repeats on. Negative numbers count from the end of the year.'), 
+      '#type' => 'select',
+      '#title' => t('Week Number(s)'),
+      '#default_value' => $node->eventrepeat_BYWEEKNO ? $node->eventrepeat_BYWEEKNO : '',
+      '#options' => $options,
+      '#description' => t('Selects what week(s) of the year this event repeats on. Negative numbers count from the end of the year.'),
       '#multiple' => TRUE,
       '#size' => 5,
     );
   }
-    
+
   return $form;
 
 }
 
 /**
- * theme the exception fieldset of form elements 
+ * theme the exception fieldset of form elements
  */
 function theme_eventrepeat_form_exception($node, $exception_collapsed){
 
 	$edit = $_POST['edit'];
 	$form = array();
-		
+
     // start the exception fieldset
     $form['eventrepeat_exceptions'] = array(
       '#type' => 'fieldset',
@@ -1324,7 +1324,7 @@ function _eventrepeat_form_date($element
   $element[$prefix.'day'] = array('#type' => 'select', '#default_value' => $node->{$prefix.'day'} ? $node->{$prefix.'day'} : 0, '#options' => $days);
   $element['comma'] = array('#type' => 'markup', '#value' => ', ');
   $element[$prefix.'year'] = array('#type' => 'select', '#default_value' => $node->{$prefix.'year'} ? $node->{$prefix.'year'} : 0, '#options' => $years);
-  
+
   return $element;
 
 }
@@ -1347,20 +1347,20 @@ function _eventrepeat_form_add_exception
   foreach($exceptions_temp as $e){
   	$removecheck = 'remove_exception_'.$e;
   	if(!$_POST[$removecheck]){
-  	  $exceptions[] = $e; 
+  	  $exceptions[] = $e;
   	}
   }
 
-  // we are only re-processing this string if we have a new exception date in the dropdowns 
+  // we are only re-processing this string if we have a new exception date in the dropdowns
   if($node->eventrepeat_EXDATE_edityear && $node->eventrepeat_EXDATE_editmonth && $node->eventrepeat_EXDATE_editday){
 
     // validate the new exception date via the new
     // year, month and day and turn it into a timestamp
     _eventrepeat_validate_form_date('eventrepeat_EXDATE_edit', t('Exception'), $node);
-  
+
     // only proceed if a valid date was supplied
-    if($node->eventrepeat_EXDATE_edit){  
-  
+    if($node->eventrepeat_EXDATE_edit){
+
       // re-calculate the new exception timestamp by
       // 1. calculating the hour, minute, duration, and offset for the node
       // 2. correcting for GMT day offsets based on timezone
@@ -1372,11 +1372,11 @@ function _eventrepeat_form_add_exception
 
       // If this one does not exist in the current list, add it
       if (!in_array($node->eventrepeat_EXDATE_edit, $exceptions)) {
-        $exceptions[] = $node->eventrepeat_EXDATE_edit;       
+        $exceptions[] = $node->eventrepeat_EXDATE_edit;
       }
-      
-      // TODO: This does not seem to be working here, 
-      // and part of me likes the fact that it remembers 
+
+      // TODO: This does not seem to be working here,
+      // and part of me likes the fact that it remembers
       // for when I am adding multiple exception dates.
       // Empty out the new exception date dropdowns.
       $node->eventrepeat_EXDATE_edityear = '';
@@ -1384,12 +1384,12 @@ function _eventrepeat_form_add_exception
       $node->eventrepeat_EXDATE_editday = '';
       $node->eventrepeat_EXDATE_edit = '';
     }
-  
+
   }
 
   // make sure our exceptions are sorted correctly,
   // then implode them back to a string
-  asort($exceptions); 
+  asort($exceptions);
   $node->eventrepeat_EXDATE = implode(',', $exceptions);
 
 }
@@ -1408,14 +1408,14 @@ function theme_eventrepeat_date($element
 
 /**
  * Executes the repeat form
- * 
- * Note that $node is not a reference here so we can 
+ *
+ * Note that $node is not a reference here so we can
  * adjust it before we serialize it.
  *
  * @ingroup eventrepeat_support
  * @param $node The node object built from the submitted form.
  */
-//TODO: this used to be _eventrepeat_form_submit. 
+//TODO: this used to be _eventrepeat_form_submit.
 //I left the definition here for referecent.
 //function _eventrepeat_form_submit($form_id, $form_values) {
 function _eventrepeat_save_repeat($node) {
@@ -1504,7 +1504,7 @@ function _eventrepeat_save_repeat($node)
         $end_time = $start_time + $duration;
 
         //update the event start and end time
-        // TODO: event times are getting adjusted here (possible cause of double shift)        
+        // TODO: event times are getting adjusted here (possible cause of double shift)
         db_query("UPDATE {event} SET event_start = %d, event_end = %d WHERE nid = %d", $start_time, $end_time,
           $old_date->nid);
         $last_rendered = $new_date->date_stamp;
@@ -1859,7 +1859,7 @@ function _eventrepeat_render_nodes($rid 
 
           //unset nid to trigger new node insertion
           unset($repeat_data->nid);
-          
+
           // unset the repeat frequency so that we don't infinitely spawn instances
           // TODO: check to see if there are more things we need to unset here. Seems to be fine so far
           unset($repeat_data->eventrepeat_FREQ);
@@ -1974,7 +1974,7 @@ function _eventrepeat_settings_page_subm
     variable_set('eventrepeat_render_support', $form_values['eventrepeat_render_support']);
   }
   variable_set('eventrepeat_showadvanced', $form_values['eventrepeat_showadvanced']);
-  
+
   drupal_set_message(t('The settings have been updated.'));
 }
 
@@ -2065,20 +2065,20 @@ function _eventrepeat_update_nodes($node
   //unset eventrepeat_edit_type so this function isn't called multiple times
   unset($node->eventrepeat_edit_type);
 
-  // check here to make sure the user didn't edit the node to a whole different day.  
+  // check here to make sure the user didn't edit the node to a whole different day.
   // if so, warn them and reset the day to the original value.
   // TODO: need to go back and look at this. if I comment this
-  // section out, you can move the whole string of repeating events, 
+  // section out, you can move the whole string of repeating events,
   // but they them seem to fall out of the sequence and act unpredictably
   // I think there should be a way to allow this kind of edit.
-  // TODO: If you change the node to the same day as another node in the sequence, 
+  // TODO: If you change the node to the same day as another node in the sequence,
   // it looks like you will end up deleting the other one before this one gets
   // reset. This is probably because of _eventrepeat_save_repeat.
   $offset = event_get_offset($node->timezone, $node->event_start);
   if (_event_date('Y-m-d', $node->event_start, $offset) != _event_date('Y-m-d', $old_event_times->event_start, $offset)) {
     db_query('UPDATE {event} SET event_start = %d, event_end = %d WHERE nid = %d', $old_event_times->event_start, $old_event_times->event_end, $node->nid);
     form_set_error('', t('You cannot edit an event start time to a day different than the one defined in the repeat pattern in this manner. This is not currently supported.'));
-    return;
+    return FALSE;
   }
 
   //pull all of the nids for nodes that are to be updated from this sequence
@@ -2101,11 +2101,11 @@ function _eventrepeat_update_nodes($node
         $edited_node->title = $node->title;
         $edited_node->body = $node->body;
         $edited_node->teaser = $node->teaser;
-        
+
         //here we generate new start and end timestamps for the updated node, based on the date of the
         //node we're editing, and the start and end time of the node we updated.  this is done in case
         //the event start or end time was edited in the node we updated
-        // TODO: event times are getting adjusted here (possible cause of double shift)        
+        // TODO: event times are getting adjusted here (possible cause of double shift)
         $edited_node->event_start = $nid->event_start + $time_diff;
         $edited_node->event_end = $edited_node->event_start + $duration;
 
@@ -2121,9 +2121,10 @@ function _eventrepeat_update_nodes($node
   $repeat_data = serialize($edited_node);
   db_query("UPDATE {event_repeat} SET repeat_data = '%s' WHERE rid = %d", $repeat_data,
     $node->eventrepeat_rid);
+  return TRUE;
 }
 
-// TODO: Check and see if this text needs to be updated 
+// TODO: Check and see if this text needs to be updated
 // both in terms of the repeat form existing in the node form
 // and the new admin ability to turn different advanced options on and off
 /**
@@ -2151,7 +2152,7 @@ function _eventrepeat_validate_form_date
   $prefix_year = $prefix.'year';
   $prefix_month = $prefix.'month';
   $prefix_day = $prefix.'day';
-  
+
   if ($node->$prefix_year != 0 && $node->$prefix_month != 0 && $node->$prefix_day != 0) {
 
     //translate the input values to the end of the day in local time.  we use local and not GMT time here
@@ -2167,7 +2168,7 @@ function _eventrepeat_validate_form_date
     }
     $node->$prefix = gmmktime(23, 59, 59, $node->$prefix_month, $node->$prefix_day, $node->$prefix_year);
 
-    //if the date is after the beginning of the node in question, 
+    //if the date is after the beginning of the node in question,
     // or an exception date, then validate otherwise warn
     if (($node->$prefix > $event_start) || $type == t('Exception')) {
       return TRUE;
