You are here

function webform_civicrm_update_7401 in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 webform_civicrm.install \webform_civicrm_update_7401()

Move civicrm entity ids into a serialized array for each submission.

File

./webform_civicrm.install, line 730
Webform CiviCRM module's install, uninstall and upgrade code.

Code

function webform_civicrm_update_7401(&$batch) {

  // Initialize batch api and create new field
  if (!isset($batch['progress'])) {
    $field = array(
      'type' => 'text',
      'serialize' => TRUE,
      'description' => 'Array of entity ids for this submission',
    );
    db_add_field('webform_civicrm_submissions', 'civicrm_data', $field);
    $batch['progress'] = $batch['current_sid'] = 0;
    $batch['max'] = db_query('SELECT COUNT(sid) FROM {webform_civicrm_submissions}')
      ->fetchField();
  }

  // Process the next group of submissions.
  $result = db_query("SELECT * FROM {webform_civicrm_submissions} WHERE sid > {$batch['current_sid']} ORDER BY sid LIMIT 500");
  foreach ($result as $row) {
    $data = array();
    if ($row->activity_id) {
      $data['activity'] = array(
        1 => array(
          'id' => $row->activity_id,
        ),
      );
    }
    if ($row->contribution_id) {
      $data['contribution'] = array(
        1 => array(
          'id' => $row->contribution_id,
        ),
      );
    }
    db_update('webform_civicrm_submissions')
      ->condition('sid', $row->sid)
      ->fields(array(
      'civicrm_data' => serialize($data),
    ))
      ->execute();
    $batch['current_sid'] = $row->sid;
    $batch['progress']++;
  }

  // Update batch progress bar
  $batch['#finished'] = $batch['progress'] >= $batch['max'] ? TRUE : $batch['progress'] / $batch['max'];

  // Drop old fields when done
  if ($batch['#finished'] === TRUE) {
    db_drop_field('webform_civicrm_submissions', 'activity_id');
    db_drop_field('webform_civicrm_submissions', 'contribution_id');
  }
}