You are here

function mailchimp_lists_update_7202 in Mailchimp 7.2

Convert block deltas from ID numbers to machine names.

File

modules/mailchimp_lists/mailchimp_lists.install, line 212
Install, update and uninstall functions for the mailchimp_lists module.

Code

function mailchimp_lists_update_7202() {
  $num_updated = 0;
  $num_deleted = 0;
  $query = db_select('block', 'b')
    ->fields('b', array(
    'bid',
    'delta',
    'module',
    'theme',
  ))
    ->condition('module', 'mailchimp_lists', '=')
    ->execute();
  $results = $query
    ->fetchAll();
  foreach ($results as $result) {
    if (is_numeric($result->delta)) {
      $list_query = db_select('mailchimp_lists', 'm')
        ->fields('m', array(
        'name',
      ))
        ->condition('id', $result->delta)
        ->execute();
      $list_results = $list_query
        ->fetchAll();
      if ($list_results) {
        $list_machine_name = $list_results[0]->name;

        // Check to see if a machine name delta already exists:
        $check_query = db_select('block', 'b')
          ->fields('b', array(
          'bid',
          'delta',
        ))
          ->condition('delta', $list_machine_name, '=')
          ->condition('module', $result->module, '=')
          ->condition('theme', $result->theme, '=')
          ->execute();
        $check_results = $check_query
          ->fetchAll();
        if (count($check_results)) {

          // Delete the numeric fields:
          $num_deleted += db_delete('block')
            ->condition('bid', $result->bid, '=')
            ->execute();
        }
        else {
          $num_updated += db_update('block')
            ->fields(array(
            'delta' => $list_machine_name,
          ))
            ->condition('bid', $result->bid, '=')
            ->execute();
        }
      }
    }
  }
  if ($num_deleted > 0) {
    return t('Deleted !num duplicate block(s).', array(
      '!num' => $num_deleted,
    ));
  }
  elseif ($num_updated > 0) {
    return t('Updated !num block(s).', array(
      '!num' => $num_updated,
    ));
  }
  else {
    return TRUE;
  }
}