You are here

function webform_component_delete in Webform 7.4

Same name and namespace in other branches
  1. 5.2 webform_components.inc \webform_component_delete()
  2. 6.3 includes/webform.components.inc \webform_component_delete()
  3. 6.2 webform_components.inc \webform_component_delete()
  4. 7.3 includes/webform.components.inc \webform_component_delete()

Delete a Webform component.

3 calls to webform_component_delete()
webform_component_delete_form_submit in includes/webform.components.inc
Submit handler for webform_component_delete_form().
webform_node_delete in ./webform.module
Implements hook_node_delete().
webform_node_update in ./webform.module
Implements hook_node_update().

File

includes/webform.components.inc, line 861
Webform module component handling.

Code

function webform_component_delete($node, $component) {

  // Check if a delete function is available for this component. If so,
  // load all submissions and allow the component to delete each one.
  webform_component_include($component['type']);
  $delete_function = '_webform_delete_' . $component['type'];
  if (function_exists($delete_function)) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');
    $submissions = webform_get_submissions($node->nid);
    foreach ($submissions as $submission) {
      if (isset($submission->data[$component['cid']])) {
        webform_component_invoke($component['type'], 'delete', $component, $submission->data[$component['cid']]);
      }
    }
  }

  // Remove database entries.
  db_delete('webform_component')
    ->condition('nid', $node->nid)
    ->condition('cid', $component['cid'])
    ->execute();
  db_delete('webform_submitted_data')
    ->condition('nid', $node->nid)
    ->condition('cid', $component['cid'])
    ->execute();

  // Delete any conditionals dependent on this component.
  module_load_include('inc', 'webform', 'includes/webform.conditionals');
  foreach ($node->webform['conditionals'] as $rgid => &$conditional) {
    $specs = array(
      array(
        'field' => 'rules',
        'table' => 'webform_conditional_rules',
        'component' => 'source',
        'component_type' => 'source_type',
        'index' => 'rid',
      ),
      array(
        'field' => 'actions',
        'table' => 'webform_conditional_actions',
        'component' => 'target',
        'component_type' => 'target_type',
        'index' => 'aid',
      ),
    );
    foreach ($specs as $spec) {
      $deleted = array();
      $field = $spec['field'];
      foreach ($conditional[$field] as $key => $thing) {
        if ($thing[$spec['component_type']] === 'component' && $thing[$spec['component']] == $component['cid']) {
          $deleted[$key] = $key;
          unset($conditional[$field][$key]);
        }
      }
      if ($spec['field'] == 'rules') {

        // Rules deleted because of the source component being deleted may have left
        // empty sub-conditionals. Delete them, and then the entire rule group if
        // there aren't any rules left.
        $deleted += webform_delete_empty_subconditionals($conditional);
      }

      // Delete the conditional if this component is the only source / target.
      if (empty($conditional[$field])) {
        webform_conditional_delete($node, $conditional);

        // Also delete the conditional from the $node so it is not re-created
        // later on in webform_node_update().
        unset($node->webform['conditionals'][$conditional['rgid']]);

        // Loop exit.
        break;
      }

      // Remove the deleted rules / actions from the database.
      foreach ($deleted as $key) {
        db_delete($spec['table'])
          ->condition('nid', $node->nid)
          ->condition('rgid', $rgid)
          ->condition($spec['index'], $key)
          ->execute();
      }
    }
  }

  // Delete all elements under this element.
  $result = db_select('webform_component', 'c')
    ->fields('c')
    ->condition('nid', $node->nid)
    ->condition('pid', $component['cid'])
    ->execute();
  foreach ($result as $row) {
    $child_component = $node->webform['components'][$row->cid];
    webform_component_delete($node, $child_component);
  }

  // Post-delete actions.
  module_invoke_all('webform_component_delete', $component);
}