You are here

webform_civicrm.module in Webform CiviCRM Integration 6

Webform CiviCRM Integration Module: Links webform submissions to contacts in a CiviCRM database. @author Coleman Watts

File

webform_civicrm.module
View source
<?php

/**
 * @file
 * Webform CiviCRM Integration Module:
 * Links webform submissions to contacts in a CiviCRM database.
 * @author Coleman Watts
 */

/**
 * Implementation of hook_menu().
 */
function webform_civicrm_menu() {
  $items = array();
  $items['node/%webform_menu/civicrm'] = array(
    'title' => 'CiviCRM',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'webform_civicrm_configure_form',
      1,
    ),
    'access callback' => 'node_access',
    'access arguments' => array(
      'update',
      1,
    ),
    'file' => 'webform_civicrm_forms.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['webform-civicrm/js/%/%'] = array(
    'page callback' => 'webform_civicrm_js_options',
    'file' => 'webform_civicrm_utils.inc',
    'access callback' => 'node_access',
    'access arguments' => array(
      'update',
      2,
    ),
    'page arguments' => array(
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
function webform_civicrm_form_alter(&$form, $form_state, $form_id) {

  // Alter back-end webform component edit forms
  if ($form_id == 'webform_component_edit_form') {
    require_once 'webform_civicrm_forms.inc';
    _webform_civicrm_webform_component_form_alter($form);
  }
  elseif (strpos($form_id, 'webform_client_form_') !== FALSE) {
    if (!empty($form['#node']->webform_civicrm) && arg(2) != 'done' && arg(2) != 'webform') {
      require_once 'webform_civicrm_forms.inc';
      _webform_civicrm_webform_frontend_form_alter($form);
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function webform_civicrm_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'load':
      $db = db_query('SELECT * FROM {webform_civicrm_forms} WHERE nid = %d', $node->nid);
      if ($settings = db_fetch_array($db)) {
        return array(
          'webform_civicrm' => $settings,
        );
      }
      break;
    case 'insert':

      // For compatibility with node_clone module
      if (arg(2) == 'clone') {
        $db = db_query('SELECT * FROM {webform_civicrm_forms} WHERE nid = %d', arg(1));
        if ($settings = db_fetch_array($db)) {
          $settings['nid'] = $node->nid;
          drupal_write_record('webform_civicrm_forms', $settings);
        }
      }
      break;
    case 'delete':
      if (isset($node->webform)) {
        db_query('DELETE FROM {webform_civicrm_forms} WHERE nid = %d', $node->nid);

        // Submissions have already been deleted from webform_submissions table, so we'll do the opposite of a join to find them
        db_query('DELETE FROM {webform_civicrm_submissions} WHERE sid NOT IN (SELECT sid FROM {webform_submissions})');
      }
      break;
  }
}

/**
 * Implementation of hook_theme().
 */
function webform_civicrm_theme() {
  $theme = array(
    // Override webform-results-submissions.tpl.php
    'webform_results_submissions' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'template' => 'webform-results-submissions',
    ),
  );
  return $theme;
}

/**
 * Implementation of hook_webform_submission_presave().
 */
function webform_civicrm_webform_submission_presave($node, &$submission) {
  if (!empty($node->webform_civicrm)) {
    require_once 'webform_civicrm_forms.inc';
    webform_civicrm_contact_match($node, $submission);
  }
}

/**
 * Implementation of hook_webform_submission_insert().
 */
function webform_civicrm_webform_submission_insert($node, $submission) {
  if (!empty($node->webform_civicrm)) {
    require_once 'webform_civicrm_forms.inc';
    webform_civicrm_process_submission($node, $submission);
  }
}

/**
 * Implementation of hook_webform_submission_delete().
 */
function webform_civicrm_webform_submission_delete($node, $submission) {
  db_query("DELETE FROM {webform_civicrm_submissions} WHERE sid = %d", $submission->sid);
}

/**
 * Implementation of hook_webform_submission_load().
 * Add CiviCRM contact info to submission objects.
 */
function webform_civicrm_webform_submission_load(&$submissions) {
  if (empty($submissions)) {
    return;
  }
  $db = db_query('SELECT * FROM {webform_civicrm_submissions} WHERE sid IN (' . implode(',', array_keys($submissions)) . ')');
  $contacts = array();
  while ($row = db_fetch_array($db)) {
    $sid = $row['sid'];
    unset($row['sid']);
    $submissions[$sid]->civicrm = $row;
    if ($cid = $row['contact_id']) {
      $contacts[$cid] = '';
    }
  }
  if ($contacts) {

    // Retrieve contact names and add to submission objects
    civicrm_initialize();
    $sql = 'SELECT id, display_name FROM civicrm_contact WHERE id IN (' . implode(',', array_keys($contacts)) . ')';
    $dao =& CRM_Core_DAO::executeQuery($sql);
    while ($dao
      ->fetch()) {
      $contacts[$dao->id] = $dao->display_name;
    }
    foreach ($submissions as &$s) {
      if (!empty($s->civicrm['contact_id'])) {
        $s->civicrm['display_name'] = $contacts[$s->civicrm['contact_id']];
      }
    }
  }
}

/**
 * Implementation of hook_civicrm_merge().
 * Update submission data to reflect new cid when contacts are merged.
 */
function webform_civicrm_civicrm_merge($type, $data, $new_id = NULL, $old_id = NULL, $tables = NULL) {
  if ($new_id && $old_id && $type == 'sqls') {
    db_query('UPDATE {webform_civicrm_submissions} SET contact_id = %d WHERE contact_id = %d', $new_id, $old_id);
  }
}

/**
 * Implementation of hook_help().
 */
function webform_civicrm_help($section) {
  switch ($section) {
    case 'admin/help#webform_civicrm':

      // Return a line-break version of the module README.txt
      return filter_filter('process', 1, NULL, file_get_contents(drupal_get_path('module', 'webform_civicrm') . '/README.txt'));
      break;
  }
}

Functions

Namesort descending Description
webform_civicrm_civicrm_merge Implementation of hook_civicrm_merge(). Update submission data to reflect new cid when contacts are merged.
webform_civicrm_form_alter Implementation of hook_form_alter().
webform_civicrm_help Implementation of hook_help().
webform_civicrm_menu Implementation of hook_menu().
webform_civicrm_nodeapi Implementation of hook_nodeapi().
webform_civicrm_theme Implementation of hook_theme().
webform_civicrm_webform_submission_delete Implementation of hook_webform_submission_delete().
webform_civicrm_webform_submission_insert Implementation of hook_webform_submission_insert().
webform_civicrm_webform_submission_load Implementation of hook_webform_submission_load(). Add CiviCRM contact info to submission objects.
webform_civicrm_webform_submission_presave Implementation of hook_webform_submission_presave().