View source
<?php
module_load_include('inc', 'webform_civicrm', 'includes/utils');
variable_set('webform_table', TRUE);
class wf_crm_admin_component {
private $form;
private $form_state;
private $node;
private $data;
private $component;
private $field;
private $hasOptionsTable;
function __construct(&$form, &$form_state) {
civicrm_initialize();
$this->form =& $form;
$this->form_state =& $form_state;
$this->node = node_load($form['nid']['#value']);
$this->data = wf_crm_aval($this->node, 'webform_civicrm:data');
$this->component = wf_crm_aval($this->node, 'webform:components:' . $this->form['cid']['#value']);
}
public function alterForm() {
$fid = $this->form['form_key']['#default_value'];
$pieces = wf_crm_explode_key($fid);
if (!$this->data || !$pieces) {
return;
}
list(, $i, $ent, $n, $table, $name) = $pieces;
$this->field = wf_crm_aval(wf_crm_get_fields(), $table . '_' . $name);
if (isCustomGroupFieldset($fid)) {
$this->form['form_key']['#disabled'] = TRUE;
$this->form['form_key']['#value'] = $fid;
}
if (!$this->field && $table != 'fieldset') {
return;
}
$this->form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_admin.css';
if (empty($this->form['clone']['#value']) || $name == 'fieldset') {
$this->form['form_key']['#disabled'] = TRUE;
$this->form['form_key']['#value'] = $fid;
$this->form['form_key']['#description'] = NULL;
$this->form['form_key']['#attributes']['title'] = t('Automatically set for use by CiviCRM processing.');
if (!empty($this->form['clone']['#value'])) {
$this->form['actions']['submit']['#value'] = t('Clone Contact');
$new = count($this->data['contact']) + 1;
$this->form['form_key']['#value'] = implode('_', [
'civicrm',
$new,
$ent,
$n,
$table,
$name,
]);
$this->form['name']['#default_value'] = wf_crm_contact_label($new, [], 'plain');
++$this->form['position']['weight']['#default_value'];
array_unshift($this->form['#submit'], 'wf_crm_contact_clone');
if (empty($this->form_state['input'])) {
$types = wf_crm_get_contact_types();
$type = $types[0][$this->data['contact'][$i]['contact'][1]['contact_type']];
drupal_set_message(t('Press the "Clone Contact" button below and a new @type will be added to the form with all the settings for !contact. All fields within this fieldset will be cloned (be careful, that may include non-contact fields).', [
'@type' => $type,
'!contact' => wf_crm_contact_label($i, $this->data),
]), 'status', FALSE);
}
}
}
elseif (empty($this->form_state['input'])) {
drupal_set_message(t('You are cloning a CiviCRM component. Refer to the Webform CiviCRM instructions for how to set the key if you want the new field to be processed by CiviCRM. You can also clone an entire contact by clicking the clone button by their fieldset.'), 'status', FALSE);
}
if ($table === 'address' && $name === 'state_province_id') {
$this->form['validation']['maxlength']['#type'] = 'hidden';
$this->form['validation']['maxlength']['#value'] = 5;
$this->form['value']['#type'] = 'select';
unset($this->form['value']['#size'], $this->form['value']['#maxlength'], $this->form['value']['#description']);
$this->form['value']['#options'] = wf_crm_get_states();
$this->form['value']['#empty_value'] = '';
}
elseif ($name == 'contribution_page_id') {
$this->form['value']['#access'] = FALSE;
$this->form['#prefix'] = '<p>' . t('This is a placeholder for a Contribution Page.') . '</p>';
}
elseif ($this->component['type'] === 'hidden' || $this->component['type'] === 'select') {
$this->hasOptionsTable = $this
->buildOptionsTable();
}
if (!$this->hasOptionsTable && $this->component['type'] === 'select') {
$this
->alterOptionsElement();
}
if (!$this->hasOptionsTable && wf_crm_aval($this->field, 'data_type') == 'Money') {
$this
->moneyOptions();
}
elseif ($this->component['type'] === 'autocomplete') {
$this->form['options']['#access'] = FALSE;
}
elseif (($name === 'contact_id' || $name === 'external_identifier') && $this->component['type'] === 'hidden') {
$this->form['value']['#value'] = '';
$this->form['value']['#access'] = FALSE;
$this->form['#prefix'] = '<p>' . t('There are no configuration options for this hidden field. You can use it for post processing, for example to include a link to the CiviCRM contact in an email.') . '</p><p>' . t('The webform token for this field is:') . '<br /><strong>' . $this
->componentToken($this->form['cid']['#value']) . '</strong><br /><em>' . t('Note: this token will change if you move the field in or out of a fieldset.') . '</em></p>';
}
elseif ($name == 'cs') {
$this->form['value']['#title'] = t('Checksum Lifespan');
$this->form['value']['#required'] = TRUE;
$this->form['value']['#type'] = 'textfield';
$this->form['value']['#description'] = t('Enter the number of days for which this checksum will be valid. Enter 0 to never expire.');
$this->form['#validate'][] = 'wf_crm_cs_validate';
$enabled = wf_crm_enabled_fields($this->node);
$this->form['#prefix'] = '<p>' . t('This field will generate a checksum for !contact after the form is submitted. You could use its webform token to create a personalized link for anonymous users.', [
'!contact' => wf_crm_contact_label($i, $this->data),
]) . '</p><p>';
if ($cid_field = wf_crm_aval($enabled, 'civicrm_' . $i . '_contact_1_contact_contact_id')) {
$this->form['#prefix'] .= t('Example: to link users back to this form, place this line in a webform-generated email:') . '<br /><code>' . url('node/' . $this->node->nid, [
'absolute' => TRUE,
]) . '?cid' . $i . '=';
$this->form['#prefix'] .= $this
->componentToken($cid_field) . '&cs' . $i . '=' . $this
->componentToken($this->form['cid']['#value']) . '</code></p>';
$this->form['#prefix'] .= '<p>' . t('Example: to redirect to a contribution page, paste this into the redirect field on the Webform "Form Settings" tab (change id from 1 to the real id of your contribution page):') . '<br /><code>' . 'civicrm/contribute/transact?reset=1&id=1' . '&cid=' . $this
->componentToken($cid_field) . '&cs=' . $this
->componentToken($this->form['cid']['#value']) . '</code></p>';
}
else {
$this->form['#prefix'] .= t('Consider enabling the hidden contact_id field - hashed links usually require this value.') . '</p>';
}
}
if ($this->form['type']['#value'] == 'civicrm_contact') {
$this->form['display']['#weight'] = -1;
}
elseif ($this->field && !in_array($this->field['type'], [
'civicrm_contact',
'file',
]) && empty($_GET['type'])) {
$unusable_widgets = [
'civicrm_contact',
'pagebreak',
'markup',
'layout_box',
'fieldset',
];
$widgets = array_diff_key(webform_component_options(), array_flip($unusable_widgets));
if ($this->form['type']['#value'] == 'textfield' && $table === 'address') {
if ($name === 'state_province_id' || $name === 'county_id') {
$widgets['textfield'] = t('Textfield / AJAX Select');
}
}
$this->form['widget'] = [
'#type' => 'fieldset',
'#title' => t('Widget: %type', [
'%type' => wf_crm_aval($widgets, $this->form['type']['#value'], t('Unknown')),
]),
'#description' => t('The default widget for this %type field is %widget. You may change it if you wish - for example, a hidden field allows you to set the value without exposing this field to the form.<br />Note: Not all widgets may be compatible with this CiviCRM field - test your form after changing widgets.', [
'%type' => str_replace('#', '', $this->field['name']),
'%widget' => wf_crm_aval($widgets, $this->field['type'], t('Disabled')),
]),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$this->form['widget']['type'] = [
'#type' => 'select',
'#options' => $widgets,
'#default_value' => $this->form['type']['#value'],
];
$this->form['widget']['change_widget'] = [
'#type' => 'submit',
'#value' => t('Change Widget'),
'#submit' => [
'webform_component_edit_form_submit',
'wf_crm_change_widget',
],
'#states' => [
'invisible' => [
'select[name="widget[type]"]' => [
'value' => $this->form['type']['#value'],
],
],
'disabled' => [
'select[name="widget[type]"]' => [
'value' => $this->form['type']['#value'],
],
],
],
];
}
}
public function adjustPageBreak() {
$components = $this->node->webform['components'];
foreach ($components as $cid => $component) {
if ($component['form_key'] == 'civicrm_1_contribution_1_contribution_contribution_page_id') {
while ($component['pid']) {
$component = $components[$component['pid']];
}
$min = $component['weight'];
}
}
if (isset($min)) {
$weight = $this->form['position']['weight']['#default_value'];
$this->form['position']['weight'] = [
'#type' => 'value',
];
if ($weight >= $min) {
$weight = $min - 1;
}
$this->form['position']['weight']['#value'] = $weight;
}
}
private function buildOptionsTable() {
$options = $sort = wf_crm_field_options($this->component, 'component_edit', $this->data);
if (!$options) {
return FALSE;
}
$this->form['#attached']['js'] = [
drupal_get_path('module', 'webform_civicrm') . '/js/webform_civicrm_options.js',
];
$defaults_selected = [];
if (isset($this->component['value']) && strlen($this->component['value'])) {
foreach (explode(',', trim($this->component['value'])) as $v) {
$defaults_selected[] = '_web_civi_option_selected_' . $v;
}
}
unset($this->form['items']);
$this->form['value']['#type'] = 'hidden';
$this->form['civicrm_options_fieldset'] = [
'#type' => 'fieldset',
'#title' => t('Options'),
'#id' => 'wf-crm-options-fieldset',
'#theme' => 'webform_civicrm_options_table',
];
$option_keys = [];
foreach ($options as $k => $v) {
$option_keys['_web_civi_option_selected_' . $k] = '';
$this->form['civicrm_options_fieldset']['civicrm_option_name_' . $k] = [
'#markup' => '<span class="civicrm-option-name">' . $v . '</span>',
];
}
if ($this->component['type'] === 'select') {
$this->form['civicrm_options_fieldset']['civicrm_live_options'] = [
'#type' => 'radios',
'#options' => [
t('<strong>Static Options</strong> (fully configurable)'),
t('<strong>Live Options</strong> (update automatically)'),
],
'#default_value' => (int) (!empty($this->component['extra']['civicrm_live_options'])),
'#parents' => [
'extra',
'civicrm_live_options',
],
];
$this->form['civicrm_options_fieldset']['intro'] = [
'#markup' => '<p><div class="live-options-hide">' . t('Drag the arrows to re-order these options. Click the "enabled" checkbox to show/remove an item from the form. Set the label as you want it to appear on the form.') . '</div><div class="live-options-show">' . t('You cannot control the presentation of live options. They will be loaded from the CiviCRM database every time the form is displayed.') . '</div><div>' . t('Check the "default" box for an option to be selected by default when a user views the form.') . '</div></p>',
];
if (wf_crm_aval($this->field, 'data_type') == 'ContactReference') {
$this->form['civicrm_options_fieldset']['intro'] = [
'#markup' => '<p>' . t('This is a contact reference field. It points to another contact on the webform. You can configure how that contact is presented by editing their "Existing Contact" field.') . '</p>' . '<p>' . t("Note: In most cases it is not desirable to have the selection of webform contacts exposed to the end-user so you may wish to set this field's value on the CiviCRM tab instead.") . '</p>',
];
}
$options_selected = wf_crm_str2array($this->component['extra']['items']);
$option_keys = $option_selected_keys = [];
foreach ($options_selected as $k => $v) {
if (isset($options[$k])) {
$option_keys['_web_civi_option_selected_' . $k] = '';
$option_selected_keys[] = '_web_civi_option_selected_' . $k;
unset($sort[$k]);
}
}
foreach ($sort as $k => $v) {
$option_keys['_web_civi_option_selected_' . $k] = '';
}
$this->form['extra']['items']['#type'] = 'hidden';
$this->form['extra']['items']['#required'] = FALSE;
$this->form['extra']['items']['#value'] = $this->component['extra']['items'];
$this->form['extra']['options_source']['#access'] = FALSE;
$this->form['civicrm_options_fieldset']['civicrm_options'] = [
'#type' => 'checkboxes',
'#required' => TRUE,
'#options' => $option_keys,
'#default_value' => $option_selected_keys,
];
$w = 0;
foreach ($option_keys as $k => $v) {
$k = str_replace('_web_civi_option_selected_', '', $k);
$this->form['civicrm_options_fieldset']['civicrm_option_label_' . $k] = [
'#type' => 'textfield',
'#size' => 30,
'#default_value' => !empty($options_selected[$k]) ? $options_selected[$k] : $options[$k],
];
$this->form['civicrm_options_fieldset']['civicrm_option_weight_' . $k] = [
'#type' => 'textfield',
'#size' => 3,
'#default_value' => ++$w,
];
}
}
$this->form['civicrm_options_fieldset']['civicrm_defaults'] = [
'#type' => 'checkboxes',
'#options' => [
'' => '',
] + $option_keys,
'#default_value' => $defaults_selected,
];
if (empty($this->field['extra']['multiple']) && $this->component['type'] === 'select') {
$this->form['extra']['multiple']['#type'] = 'hidden';
$this->form['extra']['multiple']['#value'] = 0;
}
elseif ($this->component['type'] === 'select') {
$this->form['extra']['multiple']['#type'] = 'checkbox';
$this->form['extra']['multiple']['#title'] = t('Multiple');
$this->form['extra']['multiple']['#description'] = t('Check this option if the user should be allowed to choose multiple values.');
$this->form['extra']['multiple']['#default_value'] = !empty($this->component['extra']['multiple']);
}
else {
$this->form['extra']['multiple']['#type'] = 'hidden';
$this->form['extra']['multiple']['#value'] = (int) (!empty($this->field['extra']['multiple']));
}
array_unshift($this->form['#submit'], 'wf_crm_process_options_selection');
return TRUE;
}
private function alterOptionsElement() {
$this->form['items']['options']['#key_type_toggle'] = t('Show CiviCRM values (keys)');
$this->form['items']['options']['#key_type_toggled'] = TRUE;
}
private function moneyOptions() {
form_load_include($this->form_state, 'inc', 'webform_civicrm', 'includes/wf_crm_admin_component');
$this->form['#validate'][] = 'wf_crm_money_validate';
if ($this->component['type'] === 'grid') {
$this->form['options']['#title'] = t('Amounts (columns)');
$this->form['options']['#description'] = t('Options to select across the top. Usually these are quantities. Keys must be numeric');
$this->form['questions']['#title'] = t('Items (rows)');
$this->form['questions']['#description'] = t('Items down the side of the grid. Usually the keys represent item price, which will be multiplied with the amount column.') . '<br />' . t('Use non-numeric keys to use the price from the amount column as-is with no multiplication.');
}
elseif ($this->component['type'] === 'select') {
$this->form['items']['options']['option_settings']['options_source']['#access'] = FALSE;
$this->form['items']['#description'] = t('Enter item labels and prices. If you allow the user to select multiple items their amounts will be added together.');
}
foreach ([
'items',
'options',
'questions',
] as $field) {
if (!empty($this->form[$field]['options']['#key_type_toggle'])) {
$this->form[$field]['options']['#key_type_toggle'] = t('Show Prices');
$this->form[$field]['options']['#key_type_toggled'] = TRUE;
}
}
$symbol = wf_crm_get_civi_setting('defaultCurrencySymbol', '$');
drupal_add_css('
.option-key-cell:before {
content:"' . $symbol . '";
}
.options-widget .form-text.option-key {
width: 50%;
margin-left: .3em;
}', 'inline');
}
public function postProcess() {
$vals =& $this->form_state['values'];
$vals['value'] = '';
foreach ($vals['civicrm_options_fieldset']['civicrm_defaults'] as $k) {
if ($k) {
$vals['value'] .= ($vals['value'] ? ',' : '') . str_replace('_web_civi_option_selected_', '', $k);
}
}
if (isset($vals['civicrm_options_fieldset']['civicrm_options'])) {
if (empty($vals['extra']['civicrm_live_options'])) {
$items = [];
foreach ($vals['civicrm_options_fieldset']['civicrm_options'] as $k) {
if ($k) {
$v = str_replace('_web_civi_option_selected_', '', $k);
if (!($label = $vals['civicrm_options_fieldset']['civicrm_option_label_' . $v])) {
$label = $this->form['civicrm_options_fieldset']['civicrm_option_name_' . $v]['#value'];
}
$items[$vals['civicrm_options_fieldset']['civicrm_option_weight_' . $v]] = $v . '|' . $label;
}
}
ksort($items);
$vals['extra']['items'] = implode("\n", $items);
if (count($items) == 1 && empty($vals['extra']['aslist'])) {
$vals['extra']['multiple'] = 1;
}
}
else {
$items = wf_crm_field_options($vals, 'live_options', $this->data);
$items += wf_crm_str2array($vals['extra']['items']);
$vals['extra']['items'] = wf_crm_array2str($items);
}
}
}
private function componentToken($cid) {
module_load_include('inc', 'webform', 'includes/webform.components');
$component = $this->node->webform['components'][$cid];
$parents = webform_component_parent_keys($this->node, $component);
return '[submission:values:' . implode(':', $parents) . ':nolabel]';
}
public static function preprocessComponentsForm(&$form, &$rows, $node) {
$enabled = $data = [];
if (!empty($node->webform_civicrm)) {
civicrm_initialize();
$enabled = wf_crm_enabled_fields($node);
$data = $node->webform_civicrm['data'];
$form['components']['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_admin.css';
}
foreach ($rows as &$row) {
if (!empty($row[0]['colspan'])) {
if (empty($node->webform['components']) && wf_crm_admin_access($node)) {
$form['components']['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/js/webform_tab.js';
$row[0]['data'] = t('Add a webform component below, or click the <a class="webform-civicrm-tab-link" href="!url">CiviCRM tab</a> to add CRM fields.', [
'!url' => url("node/{$node->nid}/civicrm"),
]);
}
}
elseif (in_array('webform-add-form', $row['class'])) {
unset($form['add']['type']['#options']['civicrm_contact']);
$form['add']['type']['#printed'] = FALSE;
$row['data'][1] = drupal_render($form['add']['type']);
}
elseif (!empty($row['data-cid']) && isset($node->webform['components'][$row['data-cid']])) {
$cid = $row['data-cid'];
$component = $node->webform['components'][$cid];
$type =& $row['data'][2]['data'];
if ($component['type'] == 'civicrm_contact') {
$types = [
'autocomplete' => t('Contact - Autocomplete'),
'select' => t('Contact - Select List'),
'hidden' => $component['extra']['show_hidden_contact'] ? t('Contact - Static') : t('Contact - Hidden'),
];
$type = $types[$component['extra']['widget']];
}
elseif ($component['type'] == 'select') {
if ($component['extra']['aslist']) {
$type = $component['extra']['multiple'] ? t('Multi-select') : t('Select');
}
else {
$type = $component['extra']['multiple'] ? t('Checkboxes') : t('Radio buttons');
}
}
if (in_array($cid, $enabled)) {
$fields = wf_crm_get_fields();
$sets = wf_crm_get_fields('sets');
$class = 'civi-icon';
list(, $c, $ent, $n, $table, $name) = explode('_', $component['form_key'], 6);
$field = wf_crm_aval($fields, $table . '_' . $name, [
'type' => 'fieldset',
]);
$row['data'][7]['data'] = '';
if ($component['type'] == 'fieldset' && $ent == 'contact') {
$title = t('Contact !num', [
'!num' => $c,
]);
$type = t('Fieldset for !contact', [
'!contact' => wf_crm_contact_label($c, $data),
]);
$row['data'][7]['data'] = l(t('Clone Contact'), "node/{$node->nid}/webform/components/{$cid}/clone", [
'attributes' => [
'title' => t('Add a new contact to the form with the same fields and settings'),
],
]);
$class .= ' fieldset';
}
elseif ($ent == 'contact') {
$field_type = $table == 'contact' || $table == 'other' ? $field['name'] : $sets[$table]['label'];
$title = t('!type Field for !contact', [
'!contact' => wf_crm_contact_label($c, $data),
'!type' => $field_type,
]);
}
elseif ($component['type'] != 'fieldset') {
$title = t('Field for !type', [
'!type' => $sets[$table]['label'],
]);
}
else {
$title = $type = t('Fieldset for !type !num', [
'!type' => $sets[$ent]['label'],
'!num' => $c,
]);
}
if ($table === 'address' && $component['type'] === 'textfield') {
if ($name === 'state_province_id' || $name === 'county_id') {
$type = t('AJAX Select');
}
}
if ($component['type'] == 'civicrm_contact' || $component['type'] == 'fieldset') {
$class .= ' ' . ($ent == 'contact' ? $node->webform_civicrm['data']['contact'][$c]['contact'][1]['contact_type'] : $ent);
}
if ($component['type'] == 'select') {
$type .= ' (' . (empty($component['extra']['civicrm_live_options']) ? t('static') : t('live')) . ')';
}
if ($component['type'] == 'civicrm_contact') {
if ($component['extra']['default'] == 'contact_id') {
$row['data'][3]['data'] = check_plain(wf_crm_display_name($component['extra']['default_contact_id']));
}
elseif ($component['extra']['default'] == 'user') {
$row['data'][3]['data'] = t('Current User');
}
elseif ($component['extra']['default'] == 'auto') {
$row['data'][3]['data'] = t('Auto - From Filters');
}
elseif ($component['extra']['default'] == 'relationship' && $component['extra']['default_relationship']) {
$relationship_labels = [];
$relationship_types = wf_crm_get_relationship_types();
foreach ($component['extra']['default_relationship'] as $default_relationship) {
list($rid, $rtype) = explode('_', $default_relationship);
$label_type = $rtype == 'b' ? 'b_a' : 'a_b';
$relationship_labels[] = $relationship_types[$rid]["label_{$label_type}"] . ' ' . wf_crm_contact_label($component['extra']['default_relationship_to'], $data);
}
$row['data'][3]['data'] = implode(' ' . ts('or') . ' ', $relationship_labels);
}
}
elseif (isset($component['value']) && strlen($component['value']) && ($field['type'] == 'select' || !empty($field['expose_list']))) {
if ($component['type'] == 'select') {
$items = wf_crm_str2array($component['extra']['items']);
}
else {
$items = wf_crm_field_options($component, 'components_form', $node->webform_civicrm['data']);
}
$val = '';
foreach (explode(',', $component['value']) as $v) {
if (isset($items[trim($v)])) {
$val .= ($val ? ', ' : '') . $items[trim($v)];
}
}
$row['data'][3]['data'] = $val;
}
if ($name == 'contribution_page_id') {
$type = t('CiviCRM Billing Fields');
$class .= ' contribution';
$row['data'][6]['data'] = l(t('Configure'), 'civicrm/admin/contribute/settings', [
'query' => [
'reset' => 1,
'action' => 'update',
'id' => $component['value'],
],
'attributes' => [
'title' => t('Edit Contribution Page in CiviCRM'),
],
]);
}
$type = '<span class="' . $class . '"> </span>' . $type;
$row['data'][2]['title'] = $title;
}
}
}
}
public static function checkBillingPagination($node) {
civicrm_initialize();
$enabled = wf_crm_enabled_fields($node);
$field_ids = [
'membership_membership_type_id',
'membership_num_terms',
'membership_fee_amount',
'participant_fee_amount',
'participant_event_id',
];
foreach ($node->webform['components'] as $cid => $component) {
if (in_array($cid, $enabled)) {
list(, , , , $key) = explode('_', $component['form_key'], 5);
if (in_array($key, $field_ids)) {
$payment_fields[$cid] = $component;
}
elseif ($key == 'contribution_contribution_page_id') {
$contribution_page_page = $component['page_num'];
}
}
}
$message = '';
if (isset($contribution_page_page) && !empty($payment_fields)) {
foreach ($payment_fields as $component) {
if ($component['page_num'] >= $contribution_page_page) {
$message .= '<li>' . $component['name'] . '</li>';
}
}
}
if ($message) {
$message = t('In order for line-items to be displayed correctly to the user, it is recommended to add a page break in-between Contribution Billing Fields and the following:') . '<ul>' . $message . '</ul>';
drupal_set_message($message, 'warning', FALSE);
}
}
}
function theme_webform_civicrm_options_table($variables) {
$element = $variables['element'];
$element['civicrm_defaults']['']['#attributes']['class'][] = 'select-all-civi-defaults';
$default_box = drupal_render($element['civicrm_defaults']['']);
$select_box = '<input class="select-all-civi-options" type="checkbox" checked="checked" title="' . t('Select All') . '"> ';
$table = [
'rows' => [],
'attributes' => [
'id' => 'civicrm-options-table',
],
'sticky' => FALSE,
];
if (empty($element['civicrm_options'])) {
$table['header'] = [
t('Item'),
$default_box . t('Selected'),
];
}
else {
$table['header'] = [
t('Item'),
t('Weight'),
[
'data' => $select_box . t('Enabled'),
'class' => [
'live-options-hide',
],
],
[
'data' => t('Label'),
'class' => [
'live-options-hide',
],
],
$default_box . t('Default'),
];
drupal_add_tabledrag('civicrm-options-table', 'order', 'self', 'civicrm-option-weight');
}
foreach (element_children($element['civicrm_defaults']) as $k) {
if ($k) {
$v = str_replace('_web_civi_option_selected_', '', $k);
$row = [
drupal_render($element['civicrm_option_name_' . $v]),
];
if (!empty($element['civicrm_options'])) {
$element['civicrm_option_weight_' . $v]['#attributes']['class'] = [
'civicrm-option-weight',
];
$element['civicrm_options'][$k]['#attributes']['class'] = [
'civicrm-enabled',
];
$element['civicrm_option_label_' . $v]['#attributes']['class'] = [
'civicrm-label',
];
$row[] = drupal_render($element['civicrm_option_weight_' . $v]);
$row[] = [
'data' => drupal_render($element['civicrm_options'][$k]),
'class' => [
'live-options-hide',
],
];
$row[] = [
'data' => drupal_render($element['civicrm_option_label_' . $v]),
'class' => [
'live-options-hide',
],
];
}
$element['civicrm_defaults'][$k]['#attributes']['class'] = [
'civicrm-default',
];
$row[] = drupal_render($element['civicrm_defaults'][$k]);
$table['rows'][] = [
'data' => $row,
'class' => [
'draggable',
],
];
}
}
return drupal_render_children($element) . theme('table', $table);
}
function wf_crm_cs_validate($form, &$form_state) {
if (!is_numeric($form_state['values']['value']) || $form_state['values']['value'] < 0) {
form_error($form['value'], t('Please enter a valid number of days.'));
}
}
function wf_crm_money_validate($form, &$form_state) {
$vals = $form_state['values'];
if (!empty($vals['value']) && !is_numeric($vals['value']) && $vals['type'] != 'formula') {
$isTokenString = token_replace($vals['value'], [], [
'clear' => true,
]);
if (!empty($isTokenString)) {
form_error($form['value'], t('This is a CiviCRM currency field. @field must be a number.', [
'@field' => $form['value']['#title'],
]));
}
}
foreach ([
'items',
'options',
] as $field) {
if (!empty($vals['extra'][$field])) {
foreach (wf_crm_str2array($vals['extra'][$field]) as $key => $val) {
if (!is_numeric($key)) {
form_error($form, t('This is a CiviCRM currency field. @field keys must be numeric.', [
'@field' => $form[$field]['#title'],
]));
break;
}
}
}
}
}
function wf_crm_change_widget($form, &$form_state) {
unset($_SESSION['messages']['status']);
drupal_set_message(t('Click "Save component" to change this field to %type (or go back to cancel). Test your form to ensure that the new widget works with this CiviCRM field.', [
'%type' => $form['widget']['type']['#options'][$form_state['values']['widget']['type']],
]));
$form_state['redirect'] = [
$_GET['q'],
[
'query' => [
'type' => $form_state['values']['widget']['type'],
],
],
];
unset($_GET['destination']);
}
function wf_crm_process_options_selection($form, &$form_state) {
$admin_form = new wf_crm_admin_component($form, $form_state);
$admin_form
->postProcess();
}
function wf_crm_components_form_validate($form, &$form_state) {
$components = wf_crm_aval($form_state, 'values:components', []);
foreach ($components as $cid => $component) {
$component += $form['#node']->webform['components'][$cid];
if ($component['form_key'] == 'civicrm_1_contribution_1_contribution_contribution_page_id') {
while ($component['pid']) {
$component = $components[$component['pid']];
}
$weight = $component['weight'];
}
}
if (isset($weight)) {
foreach ($components as $cid => $component) {
$component += $form['#node']->webform['components'][$cid];
if ($component['type'] == 'pagebreak' && $component['weight'] > $weight) {
form_error($form['components'], t('CiviCRM Contribution Billing Fields <em>must</em> be on the last page of the form.'));
}
}
}
}
function isCustomGroupFieldset($fid) {
$pieces = wf_crm_explode_key($fid);
if ($pieces[0] == 'civicrm' && strpos($pieces[4], 'cg') !== FALSE && $pieces[5] == 'fieldset') {
return true;
}
return false;
}