function _wf_crm_frontend_form_alter in Webform CiviCRM Integration 7.3
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.
Parameters
$form: FAPI form array
$form_state: FAPI form_state array
1 call to _wf_crm_frontend_form_alter()
- webform_civicrm_form_alter in ./
webform_civicrm.module - Implements hook_form_alter().
File
- ./
webform_civicrm_forms.inc, line 22
Code
function _wf_crm_frontend_form_alter(&$form, &$form_state) {
civicrm_initialize();
$form['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_forms.js';
$form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_forms.css';
$form['#validate'][] = 'wf_crm_validate';
array_unshift($form['#submit'], 'wf_crm_storage');
$config = CRM_Core_Config::singleton();
$node = $form['#node'];
$settings = $node->webform_civicrm;
$data = $settings['data'];
$id['cid'] = $id = $info = array();
// JS Cache eliminates the need for most ajax state/province callbacks
foreach ($data['contact'] as $c) {
if (!empty($c['number_of_address'])) {
$js_vars = array(
'defaultCountry' => $config->defaultContactCountry,
'defaultStates' => wf_crm_get_options('state_province', $config->defaultContactCountry),
'noCountry' => t('- First Choose a Country -'),
'callbackPath' => url('webform-civicrm/js', array(
'alias' => TRUE,
)),
);
$form['#attached']['js'][] = array(
'data' => array(
'webform_civicrm' => $js_vars,
),
'type' => 'setting',
);
break;
}
}
$enabled = wf_crm_enabled_fields($node);
// Keep track of cids across multipage forms
if (!empty($form_state['values']['submitted']) && wf_crm_aval($form_state, 'webform:page_count') > 1) {
foreach ($enabled as $k => $v) {
if (substr($k, -8) == 'existing' && !empty($form_state['values']['submitted'][$v])) {
list(, $c) = explode('_', $k);
$cid_data["cid{$c}"] = $form_state['values']['submitted'][$v];
}
}
if (!empty($cid_data)) {
$form['#attributes']['data-civicrm-ids'] = json_encode($cid_data);
}
}
// Early return if the form (or page) was already submitted
if (wf_crm_aval($form_state, 'triggering_element:#id') == 'edit-previous' || empty($form_state['rebuild']) && !empty($form_state['storage'])) {
wf_crm_fill_form($form['submitted'], $node);
return;
}
// If this is an edit op, use the original IDs and return
if (isset($form['#submission']->sid)) {
if (isset($form['#submission']->civicrm)) {
$form_state['civicrm']['id']['cid'] = $form['#submission']->civicrm['contact_id'];
$form_state['civicrm']['id']['act'][1] = $form['#submission']->civicrm['activity_id'];
foreach ($form_state['civicrm']['id']['cid'] as $c => $cid) {
$info['contact'][$c]['contact'][1]['existing'] = $cid;
}
}
wf_crm_fill_form($form['submitted'], $node, $info);
return;
}
// If this form is already in-process, IDs will be stored
if (!empty($form_state['civicrm'])) {
$id = $form_state['civicrm']['id'];
}
else {
$count = count($data['contact']);
for ($i = 1; $i <= $count; ++$i) {
if ($existing_component = wf_crm_aval($enabled, "civicrm_{$i}_contact_1_contact_existing")) {
module_load_include('inc', 'webform_civicrm', 'contact_component');
wf_crm_find_contact($node, $node->webform['components'][$existing_component], $id['cid']);
}
}
}
// Lookup activity if passed in url
if (!empty($_GET['aid']) && !empty($id['cid']) && is_numeric($_GET['aid'])) {
$result = wf_civicrm_api('activity', 'get', array(
'activity_id' => $_GET['aid'],
'return.target_contact_id' => 1,
'return.assignee_contact_id' => 1,
));
if (isset($result['values'][$_GET['aid']])) {
$act = $result['values'][$_GET['aid']];
// Verify that this activity is the right type and that our contacts have some involvement in it
if ($act['activity_type_id'] == $data['activity'][1]['activity'][1]['activity_type_id']) {
foreach ($id['cid'] as $cid) {
if ($act['source_contact_id'] == $cid || in_array($cid, $act['target_contact_id']) || in_array($cid, $act['assignee_contact_id'])) {
$activity = array(
'activity' => array(
1 => $result['values'][$_GET['aid']],
),
);
$custom = wf_crm_get_custom($_GET['aid'], 'activity');
$info['activity'] = array(
1 => $activity + $custom,
);
$id['act'][1] = $_GET['aid'];
if (!empty($data['case'][1])) {
$result = wf_civicrm_api('case', 'get', array(
'activity_id' => $id['act'][1],
));
if ($id['case'][1] = wf_crm_aval($result, 'id')) {
$custom = wf_crm_get_custom($id['case'][1], 'case');
$info['case'] = array(
1 => array(
'case' => array(
1 => $result['values'][$result['id']],
),
) + $custom,
);
}
}
break;
}
}
}
}
}
else {
if (!empty($data['case'][1])) {
$case = wf_crm_case_find($data['case'][1], $id);
if ($case) {
$id['case'][1] = $case['id'];
$custom = wf_crm_get_custom($case['id'], 'case');
// Add case to $info in standard webform_civicrm format
$info['case'] = array(
1 => array(
'case' => array(
1 => $case,
),
) + $custom,
);
}
}
if (!empty($id['cid'][1]) && empty($data['case'][1]) || !empty($data['case'][1]) && !empty($id['case'][1])) {
if (!empty($data['activity'][1]['existing_activity_status'])) {
$params = array(
'activity_type_id' => $data['activity'][1]['activity'][1]['activity_type_id'],
'status_id' => $data['activity'][1]['existing_activity_status'],
);
if (!empty($data['case'][1])) {
$params['case_id'] = $id['case'][1];
}
else {
$params['contact_id'] = $id['cid'][1];
}
$id['act'][1] = wf_crm_activity_find($params);
if ($id['act'][1]) {
$result = wf_civicrm_api('activity', 'get', array(
'activity_id' => $id['act'][1],
));
$activity = array(
'activity' => array(
1 => $result['values'][$id['act'][1]],
),
);
$custom = wf_crm_get_custom($id['act'][1], 'activity');
$info['activity'] = array(
1 => $activity + $custom,
);
}
}
}
}
// Form alterations for unknown contacts
if (empty($id['cid'][1])) {
if ($settings['prefix_unknown']) {
$form['#prefix'] = wf_crm_aval($form, '#prefix', '') . '<div class="webform-civicrm-prefix contact-unknown">' . nl2br($settings['prefix_unknown']) . '</div>';
}
if ($settings['block_unknown_users']) {
$form['submitted']['#access'] = $form['actions']['#access'] = FALSE;
drupal_set_message(t('Sorry, you do not have permission to access this form.'), 'warning', FALSE);
return;
}
}
// Check if events are open to registration and take appropriate action
$events = array();
$reg = wf_crm_aval($data, 'reg_options', array());
if (!empty($data['participant_reg_type'])) {
// Fetch events set in back-end
$data += array(
'participant' => array(),
);
foreach ($data['participant'] as $e => $par) {
if (!empty($par['participant'])) {
foreach ($par['participant'] as $n => $p) {
if (!empty($p['event_id'])) {
// Handle multi-valued event selection
foreach ((array) $p['event_id'] as $eid) {
if ($eid = (int) $eid) {
$events[$eid]['ended'] = TRUE;
$events[$eid]['title'] = t('this event');
$events[$eid]['count'] = wf_crm_aval($events, "{$eid}:count", 0) + 1;
$events[$eid]['form'][] = array(
'contact' => $e,
'num' => $n,
'eid' => NULL,
);
}
}
}
}
}
}
// Add events exposed to the form
foreach ($enabled as $field => $fid) {
if (strpos($field, 'participant_event_id')) {
foreach (wf_crm_exposed_options($node, $fid) as $p => $label) {
list($eid) = explode('-', $p);
$events[$eid]['ended'] = TRUE;
$events[$eid]['title'] = $label;
list(, $e, , $n) = explode('_', $field);
$events[$eid]['form'][] = array(
'contact' => $e,
'num' => $n,
'eid' => $p,
);
}
}
}
if ($events && (!empty($reg['show_remaining']) || !empty($reg['block_form']))) {
wf_crm_event_info($events);
foreach ($events as $eid => $event) {
if ($event['ended']) {
if (!empty($reg['show_remaining']) && empty($form_state['input']) && empty($form_state['storage'])) {
drupal_set_message(t('Sorry, %event has ended.', array(
'%event' => $event['title'],
)), 'warning', FALSE);
}
}
elseif ($event['full']) {
if (!empty($reg['show_remaining']) && empty($form_state['input']) && empty($form_state['storage'])) {
drupal_set_message('<em>' . $event['title'] . '</em>: ' . $event['full_message'], 'warning', FALSE);
}
}
else {
$reg['block_form'] = FALSE;
if ($event['max_participants'] && empty($form_state['input']) && empty($form_state['storage']) && ($reg['show_remaining'] == 'always' || intval($reg['show_remaining']) >= $event['remaining'])) {
drupal_set_message(format_plural($event['remaining'], '%event has 1 remaining space.', '%event has @count remaining spaces.', array(
'%event' => $event['title'],
)), 'status', FALSE);
}
}
}
if ($reg['block_form']) {
$form['submitted']['#access'] = $form['actions']['#access'] = FALSE;
return;
}
}
}
// Form alterations for known contacts
foreach ($data['contact'] as $c => $contact) {
if ($cid = wf_crm_aval($id['cid'], $c)) {
// Retrieve contact data
$info['contact'][$c] = wf_crm_contact_get($node, $enabled, $c, $id['cid']);
$info['contact'][$c]['contact'][1]['existing'] = $cid;
// Retrieve participant data
if ($events && ($c == 1 || $data['participant_reg_type'] == 'separate')) {
$select = array(
'id',
'event_id',
'role_id',
'status_id',
);
if (in_array('CiviCampaign', $config->enableComponents, TRUE)) {
$select[] = 'campaign_id';
}
$dao =& CRM_Core_DAO::executeQuery('SELECT ' . implode(',', $select) . " FROM civicrm_participant WHERE contact_id = {$cid} AND event_id IN (" . implode(',', array_keys($events)) . ") AND status_id IN (SELECT id FROM civicrm_participant_status_type WHERE class <> 'Negative')");
while ($dao
->fetch()) {
$par = array();
foreach ($select as $sel) {
$par['participant'][1][$sel] = $dao->{$sel};
}
$par += wf_crm_get_custom($dao->id, 'Participant');
foreach ($events[$dao->event_id]['form'] as $event) {
if ($event['contact'] == $c) {
$n = $event['contact'];
$i = $event['num'];
// Support multi-valued form elements
$event_ids = wf_crm_aval($info, "participant:{$n}:participant:{$i}:event_id", array());
if ($event['eid']) {
$event_ids[] = $event['eid'];
}
foreach ($par as $k => $v) {
$info['participant'][$n][$k][$i] = $v[1];
}
$info['participant'][$n]['participant'][$i]['event_id'] = $event_ids;
}
}
}
}
}
}
if (!empty($id['cid'][1])) {
if ($settings['prefix_known']) {
$form['#prefix'] = wf_crm_aval($form, '#prefix', '') . '<div class="webform-civicrm-prefix contact-known">' . nl2br(wf_crm_replace_tokens($settings['prefix_known'], $info['contact'][1]['contact'][1])) . '</div>';
}
if ($settings['message'] && empty($form_state['input']) && empty($form_state['storage'])) {
_wf_crm_set_message($settings['message'], $info['contact'][1]['contact'][1]);
}
}
// Store ids
$form_state['civicrm']['id'] = $id;
// Set default values and other attributes for CiviCRM form elements
// Passing $submitted helps avoid overwriting values that have been entered on a multi-step form
$submitted = wf_crm_aval($form_state, 'values:submitted', array());
wf_crm_fill_form($form['submitted'], $node, $info, $submitted);
}