You are here

webform_civicrm.module in Webform CiviCRM Integration 7

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
 */

/**
 * Implements 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;
}

/**
 * Implements hook_admin_paths().
 */
function webform_civicrm_admin_paths() {
  return array(
    'node/*/civicrm' => TRUE,
  );
}

/**
 * Implements 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') {
    if (substr($form['form_key']['#default_value'], 0, 7) == 'civicrm') {
      module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
      _webform_civicrm_webform_component_form_alter($form);
    }
  }
  elseif (strpos($form_id, 'webform_client_form_') !== FALSE) {
    if (isset($form['#node']->webform_civicrm) && arg(2) != 'done' && arg(2) != 'webform') {
      module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
      _webform_civicrm_webform_frontend_form_alter($form);
    }
  }
}

/**
 * Implements hook_node_load().
 */
function webform_civicrm_node_load($nodes, $types) {
  $db = db_query('SELECT * FROM {webform_civicrm_forms} WHERE nid IN(:nids)', array(
    ':nids' => array_keys($nodes),
  ));
  foreach ($db as $settings) {
    $nid = $settings->nid;
    unset($settings->nid);
    $nodes[$nid]->webform_civicrm = (array) $settings;
  }
}

/**
 * Implements hook_node_insert().
 */
function webform_civicrm_node_insert($node) {

  // For compatibility with node_clone module
  if (arg(2) == 'clone') {
    $db = db_query('SELECT * FROM {webform_civicrm_forms} WHERE nid = :nid', array(
      ':nid' => arg(1),
    ));
    foreach ($db as $settings) {
      $settings->nid = $node->nid;
      drupal_write_record('webform_civicrm_forms', $settings);
      return;
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function webform_civicrm_node_delete($node) {
  if ($node->webform) {
    db_delete('webform_civicrm_forms')
      ->condition('nid', $node->nid)
      ->execute();

    // Submissions have already been deleted from webform_submissions table, so we'll do the opposite of a join to find them
    db_delete('webform_civicrm_submissions')
      ->where('sid NOT IN (SELECT sid FROM {webform_submissions})')
      ->execute();
  }
}

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

/**
 * Implements hook_webform_submission_presave().
 */
function webform_civicrm_webform_submission_presave($node, &$submission) {
  if (isset($node->webform_civicrm)) {
    module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
    webform_civicrm_contact_match($node, $submission);
  }
}

/**
 * Implements hook_webform_submission_insert().
 */
function webform_civicrm_webform_submission_insert($node, $submission) {
  if (isset($node->webform_civicrm)) {
    module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
    webform_civicrm_process_submission($node, $submission);
  }
}

/**
 * Implements hook_webform_submission_delete().
 */
function webform_civicrm_webform_submission_delete($node, $submission) {
  db_delete('webform_civicrm_submissions')
    ->condition('sid', $submission->sid)
    ->execute();
}

/**
 * Implements 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 (:sids)', array(
    ':sids' => array_keys($submissions),
  ));
  $contacts = array();
  foreach ($db as $row) {
    $sid = $row->sid;
    unset($row->sid);
    $submissions[$sid]->civicrm = (array) $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']];
      }
    }
  }
}

/**
 * Implements 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 (!empty($new_id) && !empty($old_id) && $type == 'sqls') {
    db_update('webform_civicrm_submissions')
      ->fields(array(
      'contact_id' => $new_id,
    ))
      ->condition('contact_id', $old_id)
      ->execute();
  }
}

/**
 * Implements 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 nl2br(file_get_contents(drupal_get_path('module', 'webform_civicrm') . '/README.txt'));
      break;
  }
}