You are here

function ac_update_copy_states in Availability Calendars 7.2

1 call to ac_update_copy_states()
ac_update_copy_data in ac_update/ac_update.module

File

ac_update/ac_update.module, line 561
Availability Calendars update: admin update code

Code

function ac_update_copy_states(&$result) {
  $old_states = availability_calendars_get_states();
  $current_states = availability_calendar_get_states();
  $new_states = $current_states;
  $merged = 0;
  $renamed = 0;
  foreach ($old_states as $old_state) {

    // Get current state, based on css_class equivalence
    $found = FALSE;
    foreach ($current_states as $sid => $current_state) {
      if ($old_state['css_class'] === $current_state['css_class']) {
        $found = TRUE;
        $merged++;
        break;
      }
      else {
        if ($old_state['css_class'] === str_replace('-', '', $current_state['css_class'])) {
          $found = TRUE;
          $renamed++;
          break;
        }
      }
    }

    // If there is a equivalent, update the state, otherwise create a new.
    if ($found) {
      $new_states[$sid] = array(
        'sid' => $sid,
        'css_class' => $old_state['css_class'],
        'label' => $old_state['label'],
        'weight' => $old_state['weight'],
        'is_available' => $old_state['is_available'] ? 1 : 0,
      );
    }
    else {
      $new_states[] = array(
        'sid' => NULL,
        'css_class' => $old_state['css_class'],
        'label' => $old_state['label'],
        'weight' => $old_state['weight'],
        'is_available' => $old_state['is_available'] ? 1 : 0,
      );
    }
  }
  availability_calendar_update_states($new_states);
  $count = count($new_states);
  $result['messages'][] = array(
    'message' => "Copied {$count} states.",
    'type' => 'status',
  );
}