View source
<?php
define('WEBFROM_CIVICRM_CIVICRM_VERSION_MIN', '4.1');
define('WEBFROM_CIVICRM_CIVICRM_VERSION_MAX', '4.4');
define('WEBFROM_CIVICRM_WEBFORM_VERSION', '3');
function webform_civicrm_menu() {
$items = array();
$items['node/%webform_menu/civicrm'] = array(
'title' => 'CiviCRM',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'wf_crm_configure_form',
1,
),
'access callback' => 'wf_crm_admin_access',
'access arguments' => array(
1,
),
'file' => 'webform_civicrm_admin.inc',
'weight' => 3,
'type' => MENU_LOCAL_TASK,
);
$items['webform-civicrm/js/%'] = array(
'page callback' => 'wf_crm_ajax',
'file' => 'contact_component.inc',
'access callback' => TRUE,
'page arguments' => array(
2,
),
'type' => MENU_CALLBACK,
);
return $items;
}
function wf_crm_admin_access($node) {
return node_access('update', $node) && user_access('access CiviCRM');
}
function webform_civicrm_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_component_edit_form' && substr($form['form_key']['#default_value'], 0, 7) == 'civicrm') {
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_admin');
_wf_crm_component_form_alter($form, $form_state);
}
elseif (strpos($form_id, 'webform_client_form_') !== FALSE && !empty($form['#node']->webform_civicrm)) {
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
_wf_crm_frontend_form_alter($form, $form_state);
}
elseif ($form_id == 'webform_components_form') {
$form['#theme'] = array(
'webform_civicrm_components_form',
);
}
}
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) {
$node =& $nodes[$settings->nid];
$settings->data = unserialize($settings->data);
$node->webform_civicrm = (array) $settings;
if (!empty($_GET['type']) && arg(0) == 'node' && arg(1) == $node->nid && arg(3) == 'components') {
if (!empty($node->webform['components'][arg(4)]) && array_key_exists($_GET['type'], webform_components())) {
$node->webform['components'][arg(4)]['type'] = $_GET['type'];
webform_component_defaults($node->webform['components'][arg(4)]);
if ($_GET['type'] == 'select') {
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
civicrm_initialize();
$node->webform['components'][arg(4)]['extra']['items'] = wf_crm_field_options($node->webform['components'][arg(4)], 'component_insert', $node->webform_civicrm['data']);
}
}
}
}
}
function webform_civicrm_node_insert($node) {
if (isset($node->webform_civicrm)) {
$node->webform_civicrm['nid'] = $node->nid;
drupal_write_record('webform_civicrm_forms', $node->webform_civicrm);
}
}
function webform_civicrm_node_delete($node) {
if (!empty($node->webform)) {
db_delete('webform_civicrm_forms')
->condition('nid', $node->nid)
->execute();
db_delete('webform_civicrm_submissions')
->where('sid NOT IN (SELECT sid FROM {webform_submissions})')
->execute();
}
}
function webform_civicrm_theme() {
$theme = array(
'webform_civicrm_components_form' => array(
'render element' => 'form',
'file' => 'webform_civicrm_admin.inc',
),
'webform_civicrm_options' => array(
'render element' => 'element',
),
'display_civicrm_contact' => array(
'render element' => 'element',
'file' => 'contact_component.inc',
),
'static_contact_element' => array(
'render element' => 'element',
'file' => 'contact_component.inc',
),
);
return $theme;
}
function webform_civicrm_webform_component_info() {
return array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'description' => t('Choose existing contact.'),
'features' => array(
'email_name' => TRUE,
),
'file' => 'contact_component.inc',
),
);
}
function webform_civicrm_webform_submission_presave($node, &$submission) {
if (!empty($node->webform_civicrm)) {
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
wf_crm_process_submission($node, $submission, 'presave');
}
}
function webform_civicrm_webform_submission_insert($node, $submission) {
if (!empty($node->webform_civicrm)) {
wf_crm_process_submission($node, $submission, 'insert');
}
}
function webform_civicrm_webform_submission_update($node, $submission) {
if (!empty($node->webform_civicrm)) {
wf_crm_process_submission($node, $submission, 'update');
}
}
function webform_civicrm_webform_submission_delete($node, $submission) {
db_delete('webform_civicrm_submissions')
->condition('sid', $submission->sid)
->execute();
}
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();
foreach ($db as $row) {
$sid = $row->sid;
unset($row->sid);
if ($cid = $row->contact_id) {
$cid = explode('-', rtrim($cid, '-'));
unset($cid[0]);
$row->contact_id = $cid;
$contacts[$cid[1]] = '';
}
$submissions[$sid]->civicrm = (array) $row;
}
if ($contacts) {
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 (isset($s->civicrm['contact_id'])) {
$s->civicrm['display_name'] = $contacts[$s->civicrm['contact_id'][1]];
}
}
}
}
function webform_civicrm_webform_submission_render_alter(&$sub) {
if (!empty($sub['#submission']->civicrm['display_name']) && empty($sub['#email']) && $sub['#format'] == 'html') {
drupal_set_title(t('Submission #!num by @name', array(
'!num' => $sub['#submission']->sid,
'@name' => $sub['#submission']->civicrm['display_name'],
)));
}
}
function webform_civicrm_webform_submission_actions($node, $submission) {
$actions = array();
if (!empty($node->webform_civicrm) && !empty($submission->civicrm) && webform_results_access($node) && user_access('access CiviCRM')) {
$data = $submission->civicrm;
if (!empty($data['display_name'])) {
$actions['civicrm_action contact_view'] = array(
'title' => t('View @name', array(
'@name' => $data['display_name'],
)),
'href' => 'civicrm/contact/view',
'query' => array(
'reset' => 1,
'cid' => $data['contact_id'][1],
),
);
}
if (!empty($data['activity_id'])) {
$actions['civicrm_action activity_view'] = array(
'title' => t('View Activity'),
'href' => 'civicrm/activity',
'query' => array(
'action' => 'view',
'reset' => 1,
'cid' => $data['contact_id'][1],
'id' => $data['activity_id'],
),
);
}
}
return $actions;
}
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')
->expression('contact_id', 'REPLACE(contact_id, :old, :new)', array(
':old' => '-' . $old_id . '-',
':new' => '-' . $new_id . '-',
))
->condition('contact_id', '%-' . $old_id . '-%', 'LIKE')
->execute();
db_query("UPDATE {webform_submitted_data} d, {webform_component} c SET d.data = :new\n WHERE d.data = :old AND d.cid = c.cid AND d.nid = c.nid AND c.type = 'civicrm_contact'", array(
':new' => $new_id,
':old' => $old_id,
));
}
}
function webform_civicrm_civicrm_post($op, $type, $id, $obj) {
if ($type === 'Activity' && $op === 'delete') {
db_update('webform_civicrm_submissions')
->fields(array(
'activity_id' => 0,
))
->condition('activity_id', $id)
->execute();
}
}
function wf_crm_configure_form_ajax($form, $form_state) {
return wf_crm_aval($form, $form_state['triggering_element']['#ajax']['pathstr']);
}
function webform_civicrm_admin_paths() {
return array(
'node/*/civicrm' => TRUE,
);
}
function webform_civicrm_help($section) {
switch ($section) {
case 'admin/help#webform_civicrm':
return nl2br(file_get_contents(drupal_get_path('module', 'webform_civicrm') . '/README.txt'));
break;
}
}
function webform_civicrm_webform_component_presave(&$component) {
if ($c = wf_crm_contact_clone_storage()) {
$component['form_key'] = str_replace($c['old'], $c['new'], $component['form_key']);
if ($component['type'] == 'civicrm_contact') {
if (wf_crm_aval($component, 'extra:default') == 'user') {
unset($component['extra']['default']);
}
}
}
}
function webform_civicrm_preprocess_webform_results_submissions(&$variables) {
if (count($variables['table']['#rows']) && !empty($variables['node']->webform_civicrm)) {
$access = user_access('access CiviCRM');
$temp = $variables['table']['#header'];
$variables['table']['#header'] = array();
foreach ($temp as $k => $v) {
$variables['table']['#header'][] = $v;
if ($k == 1) {
$variables['table']['#header'][] = t('Name');
}
}
foreach ($variables['table']['#rows'] as &$row) {
$name = '';
$sid = $row[0];
if (!empty($variables['submissions'][$sid]->civicrm['contact_id'][1])) {
$data = $variables['submissions'][$sid]->civicrm;
if (($name = $data['display_name']) !== '') {
if ($access) {
$name = l($name, 'civicrm/contact/view', array(
'query' => array(
'reset' => 1,
'cid' => $data['contact_id'][1],
),
'alias' => TRUE,
));
}
}
}
$temp = $row;
$row = array();
foreach ($temp as $k => $v) {
$row[] = $v;
if ($k == 1) {
$row[] = $name;
}
}
}
}
}
function wf_crm_aval($haystack, $keys, $default = NULL, $strict = FALSE) {
foreach (explode(':', $keys) as $key) {
if (is_object($haystack)) {
$haystack = (array) $haystack;
}
if (!is_array($haystack) || !isset($haystack[$key]) || empty($haystack[$key]) && $default !== NULL && !$strict) {
return $default;
}
$haystack = $haystack[$key];
}
return $haystack;
}
function wf_crm_contact_clone_storage($input = NULL) {
static $storage = NULL;
if ($input) {
$storage = $input;
}
return $storage;
}
function wf_crm_contact_clone($form, $form_state) {
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
$fid = $form['form_key']['#default_value'];
list(, $old, $ent, $n, $table, $key) = wf_crm_explode_key($fid);
$node = node_load($form['nid']['#value']);
$settings = $node->webform_civicrm;
$new = count($settings['data']['contact']) + 1;
$settings['data']['contact'][$new] = $settings['data']['contact'][$old];
$storage = array(
'old' => array(
"civicrm_{$old}_contact_",
),
'new' => array(
"civicrm_{$new}_contact_",
),
);
if (wf_crm_aval($settings['data'], 'participant_reg_type') == 'separate') {
$settings['data']['participant'][$new] = $settings['data']['participant'][$old];
$storage['old'][] = "civicrm_{$old}_participant_";
$storage['new'][] = "civicrm_{$new}_participant_";
}
drupal_write_record('webform_civicrm_forms', $settings, 'nid');
wf_crm_contact_clone_storage($storage);
}
function wf_crm_validate($form, &$form_state) {
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
civicrm_initialize();
require_once 'CRM/Utils/Type.php';
$node = $form['#node'];
$values = _webform_client_form_submit_flatten($node, wf_crm_aval($form_state, 'values:submitted'));
$submitted = wf_crm_enabled_fields($node, $values);
_wf_crm_validate($form['submitted'], $form_state, $submitted);
if (!empty($node->webform_civicrm['data']['reg_options']['validate']) && !empty($node->webform_civicrm['data']['participant']) && !empty($node->webform_civicrm['data']['participant_reg_type'])) {
if (!empty($form_state['storage']['submitted']) && wf_crm_aval($form_state, 'storage:page_num', 1) > 1) {
$values += $form_state['storage']['submitted'];
$submitted = wf_crm_enabled_fields($node, $values);
}
_wf_crm_participant_validate($form, $form_state, $submitted);
}
}
function wf_crm_storage($form, $form_state = NULL) {
static $storage = array();
if (is_array($form) && !empty($form['#node']->nid) && !empty($form_state['civicrm'])) {
$storage[$form['#node']->nid] = $form_state['civicrm'];
}
elseif (is_numeric($form)) {
return wf_crm_aval($storage, $form, array());
}
}
function _webform_civicrm_status() {
$status = array();
$status['webform_civicrm'] = FALSE;
$path = drupal_get_path('module', 'civicrm') . '/civicrm.info';
$civicrm = drupal_parse_info_file($path);
$path = drupal_get_path('module', 'webform') . '/webform.info';
$webform = drupal_parse_info_file($path);
$webform['version'] = str_replace('7.x-', '', $webform['version']);
if (version_compare($civicrm['version'], WEBFROM_CIVICRM_CIVICRM_VERSION_MIN, '>=') && version_compare($webform['version'], WEBFROM_CIVICRM_WEBFORM_VERSION, '>=')) {
$status['webform_civicrm'] = TRUE;
}
if (WEBFROM_CIVICRM_CIVICRM_VERSION_MAX && version_compare($civicrm['version'], WEBFROM_CIVICRM_CIVICRM_VERSION_MAX, '>=')) {
$status['webform_civicrm'] = FALSE;
}
return $status;
}