You are here

function _webform_update_7401_batch in Webform 7.4

Utility function to update all the locations that use tokens.

2 calls to _webform_update_7401_batch()
webform_update_7401 in ./webform.install
Rewrite token replacement system to use D7 tokens.
webform_update_7411 in ./webform.install
Remove [submission:values:x] token use of :nolabel and add :withlabel.

File

./webform.install, line 1319
Webform module install/schema hooks.

Code

function _webform_update_7401_batch(&$sandbox, $patterns, $replacements, $dpatterns, $dreplacements, $limit) {

  // Set up the initial batch process.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['last_nid_processed'] = -1;
    $sandbox['max'] = db_select('webform')
      ->countQuery()
      ->execute()
      ->fetchField();

    // Update tokens in variables.
    $variables = array(
      'webform_default_subject',
      'webform_default_from_name',
      'webform_default_from_address',
    );
    foreach ($variables as $variable) {
      $value = variable_get($variable, NULL);
      if ($value !== NULL) {
        $value = str_replace($patterns, $replacements, $value);
        $value = preg_replace($dpatterns, $dreplacements, $value);
        variable_set($variable, $value);
      }
    }
  }
  $webforms = db_select('webform', 'w')
    ->fields('w')
    ->condition('nid', $sandbox['last_nid_processed'], '>')
    ->orderBy('nid', 'ASC')
    ->range(0, $limit)
    ->execute()
    ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  foreach ($webforms as $nid => $webform) {

    // Update the webform record itself.
    $original = $webform;
    $parts = array(
      'confirmation',
      'redirect_url',
    );
    foreach ($parts as $part) {
      $webform[$part] = str_replace($patterns, $replacements, $webform[$part]);
      $webform[$part] = preg_replace($dpatterns, $dreplacements, $webform[$part]);
    }
    if ($webform != $original) {
      drupal_write_record('webform', $webform, array(
        'nid',
      ));
    }

    // Update tokens in component configurations.
    $result = db_select('webform_component', 'wc', array(
      'fetch' => PDO::FETCH_ASSOC,
    ))
      ->fields('wc')
      ->condition('wc.nid', $nid)
      ->execute();
    foreach ($result as $component) {
      $original_extra = $component['extra'];
      $original_value = $component['value'];
      $component['extra'] = unserialize($component['extra']);
      if (isset($component['extra']['description'])) {
        $description = str_replace($patterns, $replacements, $component['extra']['description']);
        $description = preg_replace($dpatterns, $dreplacements, $description);
        $component['extra']['description'] = $description;
      }
      $component['extra'] = serialize($component['extra']);
      $value = str_replace($patterns, $replacements, $component['value']);
      $value = preg_replace($dpatterns, $dreplacements, $value);
      $component['value'] = $value;
      if ($component['extra'] != $original_extra || $component['value'] != $original_value) {
        drupal_write_record('webform_component', $component, array(
          'nid',
          'cid',
        ));
      }
    }

    // Update tokens in e-mail configurations.
    $result = db_select('webform_emails', 'we', array(
      'fetch' => PDO::FETCH_ASSOC,
    ))
      ->fields('we')
      ->condition('we.nid', $nid)
      ->execute();
    foreach ($result as $email) {
      $parts = array(
        'template',
        'subject',
        'from_name',
      );
      $original = $email;
      foreach ($parts as $part) {
        $email[$part] = str_replace($patterns, $replacements, $email[$part]);
        $email[$part] = preg_replace($dpatterns, $dreplacements, $email[$part]);
      }
      if ($email != $original) {
        drupal_write_record('webform_emails', $email, array(
          'nid',
          'eid',
        ));
      }
    }

    // Update the last processed NID.
    $sandbox['last_nid_processed'] = $nid;
    $sandbox['progress']++;
  }
  return count($webforms);
}