You are here

function webform_update_7403 in Webform 7.4

Convert per-component conditionals to new more flexible conditional system.

File

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

Code

function webform_update_7403(&$sandbox) {

  // Set up the initial batch process.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['last_nid_processed'] = -1;
    $sandbox['converted_count'] = 0;
    $sandbox['max'] = db_select('webform')
      ->countQuery()
      ->execute()
      ->fetchField();
  }
  $limit = webform_variable_get('webform_update_batch_size');
  $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 tokens in component configurations.
    $result = db_select('webform_component', 'wc', array(
      'fetch' => PDO::FETCH_ASSOC,
    ))
      ->fields('wc')
      ->condition('wc.nid', $nid)
      ->execute();
    $rgid = 0;
    foreach ($result as $component) {

      // For each component, check if it has conditional properties that need
      // to be removed and/or migrated. Because these properties may be in any
      // order, copy the original extra array for comparison.
      $component['extra'] = unserialize($component['extra']);
      $original_extra = $component['extra'];

      // Remove conditional properties if present.
      if (isset($component['extra']['conditional_component'])) {
        unset($component['extra']['conditional_component']);
      }
      if (isset($component['extra']['conditional_operator'])) {
        unset($component['extra']['conditional_operator']);
      }
      if (isset($component['extra']['conditional_values'])) {
        unset($component['extra']['conditional_values']);

        // If the component has conditional values specified, that indicates
        // that this component was conditionally shown. Convert it to a new
        // conditional with multiple rules if needed.
        if (strlen(trim($original_extra['conditional_values'])) && !empty($original_extra['conditional_operator']) && !empty($original_extra['conditional_component'])) {
          $conditional_values = explode("\n", $original_extra['conditional_values']);
          $rules = array();
          $rule = array(
            'nid' => $nid,
            'rgid' => $rgid,
            'rid' => NULL,
            'source_type' => 'component',
            'source' => $original_extra['conditional_component'],
            'operator' => 'equal',
            'value' => NULL,
          );
          foreach ($conditional_values as $value) {
            $value = trim($value);
            if ($value) {
              $new_rule = $rule;
              $new_rule['rid'] = count($rules);
              $new_rule['value'] = $value;
              $rules[] = $new_rule;
            }
          }
          if (count($rules)) {
            $conditional = array(
              'nid' => $nid,
              'rgid' => $rgid,
              'andor' => 'or',
              'action' => $original_extra['conditional_operator'] === '=' ? 'show' : 'hide',
              'target_type' => 'component',
              'target' => $component['cid'],
              'weight' => 0,
            );

            // Cannot use drupal_write_record for webform_conditional because
            // the current schema has fewer fields than the schema in use during
            // this hook_update_N function.
            db_insert('webform_conditional')
              ->fields($conditional)
              ->execute();
            foreach ($rules as $rule) {
              drupal_write_record('webform_conditional_rules', $rule);
            }
            $sandbox['converted_count']++;
            $rgid++;
          }
        }
      }

      // Update the component with the conditional properties removed.
      if ($component['extra'] != $original_extra) {
        $component['extra'] = serialize($component['extra']);
        drupal_write_record('webform_component', $component, array(
          'nid',
          'cid',
        ));
      }
    }

    // Update the last processed NID.
    $sandbox['last_nid_processed'] = $nid;
    $sandbox['progress']++;
  }

  // If less than limit was processed, the update process is finished.
  if (count($webforms) < $limit || $sandbox['progress'] == $sandbox['max']) {
    $finished = TRUE;
  }

  // If there's no max value then there's nothing to update and we're finished.
  if (empty($sandbox['max']) || isset($finished)) {
    return t('@count webforms using conditionals updated to the new conditional system.', array(
      '@count' => $sandbox['converted_count'],
    ));
  }
  else {

    // Indicate our current progress to the batch update system.
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  }
}