public function wf_crm_webform_preprocess::alterForm in Webform CiviCRM Integration 7.4
Same name and namespace in other branches
- 7.5 includes/wf_crm_webform_preprocess.inc \wf_crm_webform_preprocess::alterForm()
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.
File
- includes/
wf_crm_webform_preprocess.inc, line 38
Class
Code
public function alterForm() {
// Add css & js
$this
->addResources();
// Add validation handler
$this->form['#validate'][] = 'wf_crm_validate';
// If this form is already in-process, IDs will be stored
if (!empty($this->form_state['civicrm'])) {
$this->ent = $this->form_state['civicrm']['ent'];
}
$submitted_contacts = array();
// Keep track of cids across multipage forms
if (!empty($this->form_state['values']['submitted']) && wf_crm_aval($this->form_state, 'webform:page_count') > 1) {
foreach ($this->enabled as $k => $v) {
if (substr($k, -8) == 'existing' && isset($this->form_state['values']['submitted'][$v])) {
list(, $c) = explode('_', $k);
$val = $this->form_state['values']['submitted'][$v];
$cid_data["cid{$c}"] = $this->ent['contact'][$c]['id'] = (int) (is_array($val) ? $val[0] : $val);
$submitted_contacts[$c] = TRUE;
}
}
if (!empty($cid_data)) {
$this->form['#attributes']['data-civicrm-ids'] = json_encode($cid_data);
}
}
// Early return if the form (or page) was already submitted
if (wf_crm_aval($this->form_state, 'triggering_element:#id') == 'edit-previous' || empty($this->form_state['rebuild']) && !empty($this->form_state['storage']) && empty($this->form['#submission']->is_draft) || !empty($this->form['#submission']->is_draft) && empty($this->form_state['input'])) {
$this
->fillForm($this->form['submitted']);
return;
}
// If this is an edit op, use the original IDs and return
if (isset($this->form['#submission']->sid) && $this->form['#submission']->is_draft != '1') {
if (isset($this->form['#submission']->civicrm)) {
$this->form_state['civicrm']['ent'] = $this->form['#submission']->civicrm;
foreach ($this->form_state['civicrm']['ent']['contact'] as $c => $contact) {
$this->info['contact'][$c]['contact'][1]['existing'] = wf_crm_aval($contact, 'id', 0);
}
}
$this
->fillForm($this->form['submitted']);
return;
}
// Search for existing contacts
for ($c = 1; $c <= count($this->data['contact']); ++$c) {
$this->ent['contact'][$c] = wf_crm_aval($this->ent, "contact:{$c}", array());
$existing_component = $this
->getComponent("civicrm_{$c}_contact_1_contact_existing");
// Search for contact if the user hasn't already chosen one
if ($existing_component && empty($submitted_contacts[$c])) {
$this
->findContact($existing_component);
}
// Fill cid with '0' if unknown
$this->ent['contact'][$c] += array(
'id' => 0,
);
}
// Search for other existing entities
if (empty($this->form_state['civicrm'])) {
if (!empty($this->data['case']['number_of_case'])) {
$this
->findExistingCases();
}
if (!empty($this->data['activity']['number_of_activity'])) {
$this
->findExistingActivities();
}
if (isset($this->data['grant']['number_of_grant'])) {
$this
->findExistingGrants();
}
}
// Check if contact reload required: for case roles
for ($c = 1; $c <= count($this->data['contact']); ++$c) {
if (isset($this->ent['contact'][$c]['reload'])) {
$existing_component = $this
->getComponent("civicrm_{$c}_contact_1_contact_existing");
$this->ent['contact'][$c]['id'] = 0;
$this
->findContact($existing_component);
}
}
// Form alterations for unknown contacts
if (empty($this->ent['contact'][1]['id'])) {
if ($this->settings['prefix_unknown']) {
$this->form['#prefix'] = wf_crm_aval($this->form, '#prefix', '') . '<div class="webform-civicrm-prefix contact-unknown">' . nl2br($this->settings['prefix_unknown']) . '</div>';
}
if ($this->settings['block_unknown_users']) {
$this->form['submitted']['#access'] = $this->form['actions']['#access'] = FALSE;
$this
->setMessage(t('Sorry, you do not have permission to access this form.'), 'warning');
return;
}
}
if (!empty($this->data['participant_reg_type'])) {
$this
->populateEvents();
}
// Form alterations for known contacts
foreach ($this->ent['contact'] as $c => $contact) {
if (!empty($contact['id'])) {
// Retrieve contact data
$this->info['contact'][$c] = $this
->loadContact($c);
$this->info['contact'][$c]['contact'][1]['existing'] = $contact['id'];
// Retrieve participant data
if ($this->events && ($c == 1 || $this->data['participant_reg_type'] == 'separate')) {
$this
->loadParticipants($c);
}
// Membership
if (!empty($this->data['membership'][$c]['number_of_membership'])) {
$this
->loadMemberships($c, $contact['id']);
}
}
// Load events from url if enabled, this will override loadParticipants
if (!empty($this->data['reg_options']['allow_url_load'])) {
$this
->loadURLEvents($c);
}
}
// Prefill other existing entities
foreach (array(
'case',
'activity',
'grant',
) as $entity) {
if (!empty($this->ent[$entity])) {
$this
->populateExistingEntity($entity);
}
}
if (!empty($this->ent['contact'][1]['id'])) {
if ($this->settings['prefix_known']) {
$this->form['#prefix'] = wf_crm_aval($this->form, '#prefix', '') . '<div class="webform-civicrm-prefix contact-known">' . nl2br($this
->replaceTokens($this->settings['prefix_known'], $this->info['contact'][1]['contact'][1])) . '</div>';
}
if ($this->settings['message']) {
$this
->showNotYouMessage($this->settings['message'], $this->info['contact'][1]['contact'][1]);
}
}
// Store ids
$this->form_state['civicrm']['ent'] = $this->ent;
// 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($this->form_state, 'values:submitted', array());
$this
->fillForm($this->form['submitted'], $submitted);
}