You are here

function webform_update_7407 in Webform 7.4

Remove orphaned conditional rules.

File

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

Code

function webform_update_7407() {

  // Delete entire conditions for deleted components that were the target.
  // This query is complicated by database compatibility since we're joining two
  // non-matching columns. See http://drupal.org/node/2026891.
  $result = db_query("SELECT rg.* FROM {webform_conditional} rg LEFT JOIN {webform_component} c ON rg.nid = c.nid AND rg.target_type = 'component' AND rg.target = RTRIM(CAST(c.cid AS CHAR(10))) WHERE c.cid IS NULL");
  foreach ($result as $row) {
    db_delete('webform_conditional')
      ->condition('nid', $row->nid)
      ->condition('rgid', $row->rgid)
      ->execute();
    db_delete('webform_conditional_rules')
      ->condition('nid', $row->nid)
      ->condition('rgid', $row->rgid)
      ->execute();
  }

  // Delete conditional rules for deleted components that were the source.
  $result = db_query("SELECT r.* FROM {webform_conditional_rules} r LEFT JOIN {webform_component} c ON r.nid = c.nid AND r.source_type = 'component' AND r.source = c.cid WHERE c.cid IS NULL");
  foreach ($result as $row) {
    db_delete('webform_conditional_rules')
      ->condition('nid', $row->nid)
      ->condition('rgid', $row->rgid)
      ->condition('rid', $row->rid)
      ->execute();
  }

  // Delete any conditions that no longer have any rules left in them.
  $result = db_query("SELECT rg.* FROM {webform_conditional} rg LEFT JOIN {webform_conditional_rules} r ON rg.nid = r.nid AND rg.rgid = r.rgid WHERE r.rgid IS NULL");
  foreach ($result as $row) {
    db_delete('webform_conditional')
      ->condition('nid', $row->nid)
      ->condition('rgid', $row->rgid)
      ->execute();
  }
}