You are here

function ac_update_remove_split_states in Availability Calendars 7.2

Return value

array with keys: success: bool messages: array of array with keys: type: string message: string

1 string reference to 'ac_update_remove_split_states'
ac_update_form2 in ac_update/ac_update.module
Retrieve form callback for the admin/config/content/availability-calendars/update/2 page.

File

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

Code

function ac_update_remove_split_states() {
  $result = array(
    'success' => TRUE,
    'messages' => array(),
  );

  // Get all stored states.
  $states = array_keys(availability_calendars_get_states());
  $stored_states = db_select('availability_calendars_day', 'acd')
    ->distinct()
    ->fields('acd', array(
    'status',
  ))
    ->execute()
    ->fetchCol();
  $non_default_states = array_diff($stored_states, $states);

  // Create an update map for the found values.
  $update_map = array();
  foreach ($non_default_states as $stored_state) {
    $haystack = " {$stored_state} ";
    $found = FALSE;
    foreach ($states as $state) {
      if (strpos($haystack, " {$state}-pm ") !== FALSE) {
        $update_map[$stored_state] = $state;
        $found = TRUE;
        break;
      }
    }
    if (!$found) {
      foreach ($states as $state) {
        if (strpos($haystack, " {$state} ") !== FALSE) {
          $update_map[$stored_state] = $state;
          $found = TRUE;
          break;
        }
      }
    }
    if (!$found) {
      $result['success'] = FALSE;
      $result['messages'][] = array(
        'message' => "Found a (split) state that cannot be mapped: {$stored_state}",
        'type' => 'error',
      );
    }
  }
  if ($result['success']) {
    $count = 0;
    foreach ($update_map as $current_state => $new_state) {
      $count += db_update('availability_calendars_day')
        ->fields(array(
        'status' => $new_state,
      ))
        ->condition('status', $current_state)
        ->execute();
    }
    if ($count === 0) {
      $result['messages'][] = array(
        'message' => t('No split days were found. Updates were not necessary'),
        'type' => 'status',
      );
    }
    else {
      $result['messages'][] = array(
        'message' => t("{$count} records were updated. Split days have been removed from the database."),
        'type' => 'status',
      );
    }
  }
  return $result;
}