View source
<?php
module_load_include('inc', 'webform_matrix_component', '/components/matrix');
module_load_include('inc', 'webform', '/includes/webform.components');
function webform_matrix_component_theme() {
return array(
'webform_matrix_table' => array(
'render element' => 'element',
),
);
}
function webform_matrix_component_webform_component_info() {
$components = array();
$components['matrix'] = array(
'label' => t('Matrix'),
'description' => t('Basic matrix type. Add One More Element in Row'),
'features' => array(
'default_value' => FALSE,
'description' => TRUE,
'title' => TRUE,
'title_display' => TRUE,
'title_inline' => FALSE,
'conditional' => TRUE,
'group' => FALSE,
'csv' => TRUE,
),
'file' => 'components/matrix.inc',
'file path' => drupal_get_path('module', 'webform_matrix_component'),
);
return $components;
}
function webform_matrix_component_form_webform_component_edit_form_alter(&$form, &$form_state, $formid) {
if ($form['type']['#value'] == 'matrix') {
$component = $form_state['build_info']['args'][1];
$matrix_form = _webform_matrix_component_get_column_form($form, $form_state, $component);
$form = array_merge($form, $matrix_form);
}
}
function webform_matrix_component_form_webform_admin_settings_alter(&$form, &$form_state, $formid) {
$form['matrix_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Matrix Component Settings'),
'#collapsible' => 1,
'#collapsed' => 1,
);
$form['matrix_fieldset']['webform_matrix_component_webform_matrix_rows'] = array(
'#type' => 'textfield',
'#title' => t('Matrix Rows'),
'#description' => t('Enter the max rows in matrix component'),
'#default_value' => variable_get('webform_matrix_component_webform_matrix_rows', 20),
'#element_validate' => array(
'_webform_matrix_component_validate_number',
),
);
$form['matrix_fieldset']['webform_matrix_component_webform_matrix_cols'] = array(
'#type' => 'textfield',
'#title' => t('Matrix Cols'),
'#description' => t('Enter the max cols in matrix component'),
'#default_value' => variable_get('webform_matrix_component_webform_matrix_cols', 10),
'#element_validate' => array(
'_webform_matrix_component_validate_number',
),
);
$form['matrix_fieldset']['webform_matrix_component_webform_matrix_title_required'] = array(
'#type' => 'checkbox',
'#title' => t('Is Matrix Title Required'),
'#return_value' => 1,
'#description' => t('Make Matrix title required or not'),
'#default_value' => variable_get('webform_matrix_component_webform_matrix_title_required', 1),
);
}
function webform_matrix_component_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'webform_client_form_') === 0) {
module_load_include('inc', 'webform', '/components/date');
foreach ($form['#node']->webform['components'] as $cid => $component) {
if ($component['type'] == 'matrix' && $form['details']['page_num']['#value'] == $component['page_num']) {
$nid = $component['nid'];
$value = array();
if (empty($submission) && !empty($form_state['build_info']['args'][1]->sid)) {
$submission = webform_get_submission($nid, $form_state['build_info']['args'][1]->sid);
if (!empty($submission->data[$cid])) {
$value = $submission->data[$cid];
}
}
$formkey = $component['form_key'];
$pid = $component['pid'];
$parents = webform_component_parent_keys($form['#node'], $component);
$element = drupal_array_get_nested_value($form['submitted'], $parents);
$element['#attached']['css'][] = drupal_get_path('module', 'webform_matrix_component') . '/webform_matrix_component.css';
if (empty($value) && isset($element['#component_value'])) {
$value = $element['#component_value'];
}
$datepicker = FALSE;
if (isset($component['extra']['matrix_col'])) {
$editvalue = NULL;
if (!empty($value[0])) {
$editvalue = unserialize($value[0]);
foreach (array_keys($editvalue) as $key) {
$empty = TRUE;
for ($i = 1; $i <= $component['extra']['matrix_col']; $i++) {
if (!empty($editvalue[$key][$i])) {
$empty = FALSE;
break;
}
}
if ($empty) {
unset($editvalue[$key]);
}
}
}
$matrix_col = $component['extra']['matrix_col'];
$matrix_row = !empty($editvalue) ? count($editvalue) : $component['extra']['matrix_row'];
if (empty($form_state[$formkey]['temp_matrix_rows'])) {
$form_state[$formkey]['temp_matrix_rows'] = $matrix_row;
}
if ($component['extra']['title_display'] !== 'none') {
$element['#matrix_header'] = $element['#title'];
}
for ($prow = 1; $prow <= $form_state[$formkey]['temp_matrix_rows']; $prow++) {
if (isset($component['extra']['element'])) {
$headers = array();
foreach ($component['extra']['element'] as $elekey => $elevalue) {
list($j, $i) = explode('-', $elekey);
$eletype = $elevalue['type'];
$title = isset($elevalue['title']) ? $elevalue['title'] : $elekey;
$title = _webform_filter_xss($title);
$title_display = "invisible";
$required = FALSE;
if ($prow == 1) {
$required = $component['mandatory'] ? TRUE : FALSE;
if (!$required) {
$required = $elevalue['mandatory'] ? TRUE : FALSE;
}
}
$htitle = $title;
if ($component['mandatory'] || $elevalue['mandatory']) {
$htitle .= theme_form_required_marker($title);
}
$headers[] = $htitle;
switch ($eletype) {
case 'textfield':
$elevalue['default_value'] = isset($elevalue['default_value']) ? $elevalue['default_value'] : "";
$element[$prow][$i] = array(
'#type' => 'textfield',
'#title' => check_plain($title),
'#size' => !empty($elevalue['size']) ? $elevalue['size'] : 15,
'#maxlength' => !empty($elevalue['maxlength']) ? $elevalue['maxlength'] : 50,
'#default_value' => isset($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : $elevalue['default_value'],
'#required' => $required,
'#title_display' => $title_display,
'#validated' => TRUE,
'#webform_validated' => FALSE,
);
break;
case 'select':
$select_type = isset($elevalue['select_type']) ? $elevalue['select_type'] : 'list';
$is_multiple = isset($elevalue['multiple']) ? $elevalue['multiple'] : 0;
$hide_title = isset($elevalue['hide_title']) ? $elevalue['hide_title'] : 0;
$options = $elevalue['option'];
$option_array = array();
$option_array = _webform_matrix_component_select_option_from_text($options);
switch ($select_type) {
case 'list':
$element[$prow][$i] = array(
'#type' => 'select',
'#title' => check_plain($title),
'#options' => $option_array,
'#required' => $required,
'#title_display' => $title_display,
'#empty_option' => t('Select'),
'#empty_value' => '',
'#default_value' => isset($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : $elevalue['default_value'],
);
if ($is_multiple) {
$element[$prow][$i]['#multiple'] = TRUE;
}
break;
case 'radio':
$element[$prow][$i] = array(
'#title' => check_plain($title),
'#options' => $option_array,
'#required' => $required,
'#title_display' => $title_display,
'#child_title_display' => $hide_title ? 'invisible' : 'after',
);
if ($is_multiple) {
$element[$prow][$i]['#type'] = 'checkboxes';
$element[$prow][$i]['#default_value'] = is_array($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : array(
$editvalue[$prow][$i],
);
}
else {
$element[$prow][$i]['#type'] = 'radios';
if (isset($editvalue[$prow][$i]) && $editvalue[$prow][$i]) {
$element[$prow][$i]['#default_value'] = $editvalue[$prow][$i];
}
}
$element[$prow][$i]['#validated'] = TRUE;
$element[$prow][$i]['#webform_validated'] = FALSE;
break;
}
break;
case 'date':
$datepicker = isset($elevalue['datepicker']) ? $elevalue['datepicker'] : FALSE;
$element[$prow][$i] = array(
'#type' => 'date',
'#title' => check_plain($title),
'#title_display' => $title_display,
'#required' => $required,
'#start_date' => !empty($elevalue['startdate']) ? $elevalue['startdate'] : '-2 years',
'#end_date' => !empty($elevalue['enddate']) ? $elevalue['enddate'] : '+2 years',
'#year_textfield' => 0,
'#default_value' => !empty($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : $elevalue['default_date'],
'#timezone' => 'user',
'#theme' => 'webform_date_matrix',
'#element_validate' => array(
'webform_validate_date',
),
'#datepicker' => $datepicker,
'#reference_timestamp' => NULL,
'#exclude' => array(),
'#webform_component' => array(
'cid' => $cid,
'type' => 'date',
),
);
break;
case 'label':
foreach ($elevalue['label_name'] as $delta => $label_element) {
$element[$prow][$i][$delta] = array(
'#type' => 'item',
'#title' => check_plain($title),
'#markup' => webform_filter_xss($label_element),
'#attributes' => array(
'class' => array(
'form-item',
),
),
'#default_value' => isset($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : "",
'#title_display' => $title_display,
);
$element[$prow][$i]['#webform_matrix_component_labels'] = TRUE;
}
if (isset($form_state['triggering_element']['#parents']) && strstr(end($form_state['triggering_element']['#parents']), 'add_row')) {
$added_rows = $form_state[$formkey]['temp_matrix_rows'] - count($elevalue['label_name']);
for ($added_row = 0; $added_row < $added_rows; $added_row++) {
$element[$prow][$i][] = array(
'#type' => 'item',
'#title' => check_plain($title),
'#markup' => check_plain($title),
'#attributes' => array(
'class' => array(
'form-item',
),
),
'#title_display' => $title_display,
);
}
}
break;
case 'textarea':
$element[$prow][$i] = array(
'#type' => 'textarea',
'#title' => check_plain($title),
'#rows' => !empty($elevalue['rows']) ? $elevalue['rows'] : 1,
'#cols' => !empty($elevalue['cols']) ? $elevalue['cols'] : 15,
'#resizable' => TRUE,
'#title_display' => $title_display,
'#default_value' => isset($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : "",
'#title' => check_plain($title),
'#required' => $required,
'#validated' => TRUE,
'#webform_validated' => FALSE,
);
break;
}
drupal_alter('webform_render_matrix', $component, $elevalue, $element[$prow][$i]);
}
}
if (isset($component['extra']['remove_row_feature']) && $component['extra']['remove_row_feature'] == 2) {
$element['remove_row_line_' . $formkey . "_" . $prow] = array(
'#type' => 'submit',
'#name' => 'remove_row_line' . $formkey . "_" . $prow,
'#value' => isset($component['extra']['remove_row_button_text']) ? t($component['extra']['remove_row_button_text']) : t('Remove a row'),
'#submit' => array(
'_webform_matrix_component_get_remove_row_line_submit',
),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => '_webform_matrix_component_get_remove_row_line',
'wrapper' => "edit-{$formkey}-{$pid}",
),
);
}
}
}
$element['#headers'] = $headers;
$element['#temp_max_row'] = $form_state[$formkey]['temp_matrix_rows'];
if (isset($component['extra']['add_row_feature']) && $component['extra']['add_row_feature'] == 1) {
$element['add_row_' . $formkey] = array(
'#type' => 'submit',
'#name' => 'add_row_' . $formkey,
'#value' => isset($component['extra']['add_row_button_text']) ? t($component['extra']['add_row_button_text']) : t('Add a row'),
'#submit' => array(
'_webform_matrix_component_get_add_row_submit',
),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => '_webform_matrix_component_get_add_row',
'wrapper' => "edit-{$formkey}-{$pid}",
),
);
}
if (isset($component['extra']['remove_row_feature']) && $component['extra']['remove_row_feature'] == 1) {
$element['remove_row_' . $formkey] = array(
'#type' => 'submit',
'#name' => 'remove_row_' . $formkey,
'#value' => isset($component['extra']['remove_row_button_text']) ? t($component['extra']['remove_row_button_text']) : t('Remove a row'),
'#submit' => array(
'_webform_matrix_component_get_remove_row_submit',
),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => '_webform_matrix_component_get_remove_row',
'wrapper' => "edit-{$formkey}-{$pid}",
),
);
}
if ($datepicker) {
$element['#attached']['library'] = array(
array(
'system',
'ui.datepicker',
),
);
}
$parents = _webform_matrix_component_fieldset_parents($component['cid'], $form['submitted']);
$parents[] = $formkey;
drupal_array_set_nested_value($form['submitted'], $parents, $element);
$parents[] = '#theme';
drupal_array_set_nested_value($form['submitted'], $parents, array(
'webform_matrix_table',
));
}
}
}
}
function _webform_matrix_component_fieldset_parents($cid, $submitted, &$parents = array(), &$found = FALSE) {
foreach (element_children($submitted) as $component_name) {
if (isset($submitted[$component_name]['#webform_component']['type'])) {
if ($submitted[$component_name]['#webform_component']['type'] == 'fieldset') {
$parents[] = $component_name;
_webform_matrix_component_fieldset_parents($cid, $submitted[$component_name], $parents, $found);
if ($found) {
return $parents;
}
}
elseif ($submitted[$component_name]['#webform_component']['cid'] == $cid) {
$found = TRUE;
return $parents;
}
}
}
array_pop($parents);
}
function theme_webform_matrix_table($variables) {
$element = $variables['element'];
$component = $element['#webform_component'];
$form_key = $component['form_key'];
$pid = $component['pid'];
$id = "edit-{$form_key}-{$pid}";
$matrix_col = $component['extra']['matrix_col'];
$above = !empty($element['#webform_component']['extra']['description_above']);
$matrix_row = $element['#temp_max_row'];
$headers = $element['#headers'];
foreach ($headers as $key => $header) {
$headers[$key] = t($header);
}
$rows = array();
$is_remove_line = false;
for ($i = 1; $i <= $matrix_row; $i++) {
$cols = array();
for ($j = 1; $j <= $matrix_col; $j++) {
foreach (element_children($element[$i][$j]) as $key) {
if (isset($element[$i][$j]['#child_title_display'])) {
$element[$i][$j][$key]['#title_display'] = $element[$i][$j]['#child_title_display'];
}
}
if (isset($element[$i][$j]['#type']) && $element[$i][$j]['#type'] === 'select') {
foreach ($element[$i][$j]['#options'] as $key => $option) {
$element[$i][$j]['#options'][$key] = t('!option', array(
'!option' => $option,
));
}
}
if (isset($element[$i][$j]['#webform_matrix_component_labels']) && $element[$i][$j]['#webform_matrix_component_labels'] == TRUE) {
$cols[] = drupal_render($element[$i][$j][$i]);
}
else {
$cols[] = drupal_render($element[$i][$j]);
}
}
if (isset($element['remove_row_line_' . $element['#webform_component']['form_key'] . "_" . $i])) {
$cols[] = drupal_render($element['remove_row_line_' . $element['#webform_component']['form_key'] . "_" . $i]);
$is_remove_line = true;
}
$rows[] = $cols;
}
if ($is_remove_line) {
$headers[] = isset($component['extra']['remove_row_header_text']) ? t($component['extra']['remove_row_header_text']) : t('Remove action');
}
$rows[] = array(
array(
'data' => drupal_render($element['add_row']),
'colspan' => $matrix_col,
),
);
$form_class = str_replace('_', '-', $form_key);
$nested_level = $element['#parents'][0] == 'submitted' ? 1 : 0;
$parents = str_replace('_', '-', implode('--', array_slice($element['#parents'], $nested_level)));
$output = "<div class='form-item webform-component webform-component-matrix webform-component--{$form_class} webform-component--{$parents}' id='{$id}'>";
$caption = !empty($element['#matrix_header']) ? $element['#matrix_header'] : NULL;
if ($above) {
$caption .= !empty($element['#description']) ? '<div>' . $element['#description'] . "</div>" : "";
}
$output .= theme('table', array(
'caption' => $caption,
'header' => $headers,
'rows' => $rows,
'attributes' => array(
'id' => array(
"{$id}-table",
),
),
));
$add_row_id = 'add_row_' . $element['#webform_component']['form_key'];
$remove_row_id = 'remove_row_' . $element['#webform_component']['form_key'];
$output .= drupal_render_children($element, array(
$add_row_id,
));
$output .= drupal_render_children($element, array(
$remove_row_id,
));
if (!$above) {
$output .= !empty($element['#description']) ? '<div>' . $element['#description'] . "</div>" : "";
}
$output .= "</div>";
return $output;
}
function _webform_matrix_component_get_add_row_submit($form, &$form_state) {
end($form_state['triggering_element']['#parents']);
$form_key = prev($form_state['triggering_element']['#parents']);
$form_state[$form_key]['temp_matrix_rows']++;
$form_state['rebuild'] = TRUE;
}
function _webform_matrix_component_get_add_row($form, &$form_state) {
$parents = $form_state['triggering_element']['#parents'];
array_pop($parents);
return drupal_array_get_nested_value($form, $parents);
}
function _webform_matrix_component_get_remove_row_submit($form, &$form_state) {
end($form_state['triggering_element']['#parents']);
$form_key = prev($form_state['triggering_element']['#parents']);
if ($form_state[$form_key]['temp_matrix_rows'] > 1) {
$form_state[$form_key]['temp_matrix_rows']--;
$form_state['rebuild'] = TRUE;
}
else {
form_set_error($form_key, 'This is last row cannot delete.');
}
}
function _webform_matrix_component_get_remove_row($form, &$form_state) {
$parents = $form_state['triggering_element']['#parents'];
array_pop($parents);
return drupal_array_get_nested_value($form, $parents);
}
function _webform_matrix_component_get_remove_row_line_submit($form, &$form_state) {
end($form_state['triggering_element']['#parents']);
$form_key = prev($form_state['triggering_element']['#parents']);
$triggering_row = $form_state['input']['_triggering_element_name'];
if ($form_state[$form_key]['temp_matrix_rows'] > 1) {
$remove_line_array = explode("_", $triggering_row);
$size_array = count($remove_line_array);
$remove_line = $remove_line_array[$size_array - 1];
$rowKeys = array_values(array_filter(array_keys($form['submitted'][$form_key]), 'is_int'));
$shifted = false;
foreach ($rowKeys as $key) {
if ($shifted) {
$form_state["input"]["submitted"][$form_key][$key - 1] = $form_state["input"]["submitted"][$form_key][$key];
$form['submitted'][$form_key][$key - 1] = $form['submitted'][$form_key][$key];
unset($form['submitted'][$form_key][$key]);
unset($form_state["input"]["submitted"][$form_key][$key]);
}
else {
if ($key == $remove_line) {
$shifted = true;
unset($form['submitted'][$form_key][$key]);
unset($form_state["input"]["submitted"][$form_key][$key]);
}
}
}
$form_state[$form_key]['temp_matrix_rows']--;
$form_state['rebuild'] = TRUE;
}
else {
form_set_error($form_key, 'This is last row cannot delete.');
}
}
function _webform_matrix_component_get_remove_row_line($form, &$form_state) {
$parents = $form_state['triggering_element']['#parents'];
array_pop($parents);
return drupal_array_get_nested_value($form, $parents);
}