View source
<?php
function webform_draggable_list_webform_component_info() {
$components = array();
$components['draggable_list'] = array(
'label' => t('Draggable list'),
'description' => t('Allow users to order a list with drag and drop and save the order'),
'features' => array(
'required' => FALSE,
),
);
return $components;
}
function _webform_defaults_draggable_list() {
$defaults = array(
'name' => '',
'form_key' => NULL,
'mandatory' => 0,
'pid' => 0,
'weight' => 0,
'extra' => array(
'rows' => '',
'title_display' => 0,
'description' => '',
'private' => 0,
'hide_toggle' => 0,
'hide_message' => 0,
'show_weight' => 0,
'custom_row_keys' => 0,
),
);
if (module_exists('webform_draggable_list_formbuilder')) {
webform_component_include('select');
$defaults['extra']['rows'] = _webform_select_options_to_text(array(
t('Item 1'),
t('Item 2'),
t('Item 3'),
));
}
return $defaults;
}
function _webform_render_draggable_list($component, $value = NULL, $filter = TRUE) {
webform_component_include('select');
$form_item = array(
'#type' => 'draggable_list',
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#weight' => $component['weight'],
'#webform_component' => $component,
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
'#theme_wrappers' => array(
'webform_element',
),
'#theme' => array(
'draggable_list',
),
'#prefix' => '<div class="webform-component-draggable-list" id="webform-component-draggable-list-' . $component['form_key'] . '-wrapper">',
'#wrapper_id' => 'webform-component-draggable-list-' . $component['form_key'] . '-wrapper',
'#suffix' => '</div>',
'#draggable_list_rows' => _webform_select_options_from_text($component['extra']['rows']),
'#draggable_list_draggable' => TRUE,
);
_webform_draggable_list_expand_draggable_list($form_item, $component, $value);
drupal_alter('draggable_list', $form_item, $component, $value);
return $form_item;
}
function _webform_draggable_list_expand_draggable_list(&$draggable_list, $component, $value) {
if (count($draggable_list['#draggable_list_rows'])) {
$weight = range(1, count($draggable_list['#draggable_list_rows']), 1);
$weight = array_combine($weight, $weight);
$default_weight = 1;
foreach ($draggable_list['#draggable_list_rows'] as $key => $row) {
$draggable_list[$key] = array(
'#type' => 'select',
'#options' => $weight,
'#default_value' => $default_weight,
'#title' => 'Weight',
'#attributes' => array(
'class' => array(
"my-weight-{$component['form_key']}",
),
),
);
if (!empty($value)) {
$draggable_list[$key]['#default_value'] = $value[$key];
$draggable_list[$key]['#weight'] = $value[$key];
}
$default_weight++;
}
}
}
function _webform_theme_draggable_list() {
return array(
'draggable_list' => array(
'render element' => 'element',
),
);
}
function theme_draggable_list($variables) {
$form = $variables['element'];
if ($form['#webform_component']['extra']['hide_message']) {
drupal_add_css("#{$form['#wrapper_id']} .warning { display:none !important; }", array(
'type' => 'inline',
));
}
if ($form['#webform_component']['extra']['hide_toggle']) {
drupal_add_css("#{$form['#wrapper_id']} .tabledrag-toggle-weight-wrapper { display:none !important; }", array(
'type' => 'inline',
));
}
setrawcookie('Drupal.tableDrag.showWeight', rawurlencode($form['#webform_component']['extra']['show_weight']), REQUEST_TIME + 31536000, base_path());
$rows = $form['#draggable_list_rows'];
$draggable = $form['#draggable_list_draggable'];
$i = 0;
$element_id = $variables['element']['#id'];
$table_rows = array();
foreach (element_children($form) as $key) {
$row = $rows[$key];
$this_row = array();
$item = array(
"item{$i}" => array(
'#markup' => $row,
),
);
$this_row[] = array(
'data' => drupal_render($item),
);
if ($draggable) {
$this_row[] = array(
'data' => drupal_render($form[$key]),
);
}
$table_rows[] = array(
'data' => $this_row,
'class' => array(
'draggable',
),
);
$i++;
}
if ($draggable) {
drupal_add_tabledrag($element_id, 'order', 'sibling', "my-weight-{$variables['element']['#webform_component']['form_key']}");
}
$form = array(
'#type' => 'markup',
'#markup' => theme('table', array(
'rows' => $table_rows,
'attributes' => array(
'id' => $element_id,
),
)),
'#weight' => '1',
);
$output = drupal_render($form);
return $output;
}
function _webform_display_draggable_list($component, $value, $format = 'html') {
webform_component_include('select');
$draggable_list = array(
'#title' => $component['name'],
'#weight' => $component['weight'],
'#description' => _webform_filter_descriptions($component['extra']['description']),
'#theme_wrappers' => $format == 'html' ? array(
'webform_element',
) : array(
'webform_element_text',
),
'#post_render' => array(
'webform_element_wrapper',
),
'#webform_component' => $component,
'#format' => $format,
'#draggable_list_rows' => _webform_select_options_from_text($component['extra']['rows']),
'#wrapper_id' => 'webform-component-draggable-list-' . $component['form_key'] . '-wrapper',
);
if ($format == 'html') {
$draggable_list += array(
'#draggable_list_draggable' => FALSE,
'#theme' => array(
'draggable_list',
),
);
_webform_draggable_list_expand_draggable_list($draggable_list, $component, $value);
drupal_alter('draggable_list', $draggable_list, $component, $value);
}
else {
asort($value, SORT_NUMERIC);
$items = array();
if (!empty($value)) {
foreach ($value as $key => $val) {
$items[] = "-{$draggable_list['#draggable_list_rows'][$key]}: {$val}";
}
}
$draggable_list += array(
'#type' => 'item',
'#markup' => implode("\n", $items),
);
}
return $draggable_list;
}
function _webform_edit_draggable_list($component) {
webform_component_include('select');
if (module_exists('options_element')) {
$form['rows'] = array(
'#type' => 'fieldset',
'#title' => t('Rows'),
'#collapsible' => TRUE,
'#description' => t('Values of the list item. One value per line.'),
'#attributes' => array(
'class' => array(
'webform-options-element',
),
),
'#element_validate' => array(
'_webform_edit_validate_options',
),
);
$form['rows']['options'] = array(
'#type' => 'options',
'#options' => _webform_select_options_from_text($component['extra']['rows'], TRUE),
'#optgroups' => FALSE,
'#default_value' => FALSE,
'#key_type' => 'mixed',
'#key_type_toggle' => t('Customize row keys (Advanced)'),
'#key_type_toggled' => $component['extra']['custom_row_keys'],
);
}
else {
$form['extra']['rows'] = array(
'#type' => 'textarea',
'#title' => t('Items'),
'#default_value' => $component['extra']['rows'],
'#description' => t('Values of the list item. One row per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>') . theme('webform_token_help'),
'#cols' => 60,
'#rows' => 5,
'#weight' => -2,
'#required' => TRUE,
'#wysiwyg' => FALSE,
'#element_validate' => array(
'_webform_edit_validate_select',
),
);
}
$form['extra']['hide_toggle'] = array(
'#type' => 'checkbox',
'#title' => t('Hide toggle row weights link'),
'#description' => t('Hide the link above the list that allows users to show/hide row weights.'),
'#default_value' => $component['extra']['hide_toggle'],
);
$form['extra']['hide_message'] = array(
'#type' => 'checkbox',
'#title' => t('Hide warning message'),
'#description' => t('Hide the warning message "*Changes made in this table will not be saved until the form is submitted." when a user rearragnes the list.'),
'#default_value' => $component['extra']['hide_message'],
);
$form['extra']['show_weight'] = array(
'#type' => 'checkbox',
'#title' => t('Show row weights by default'),
'#description' => t('Show the row weights by default.'),
'#default_value' => $component['extra']['show_weight'],
);
return $form;
}
function _webform_analysis_draggable_list($component, $sids = array()) {
webform_component_include('select');
$draggable_list_rows = _webform_select_options_from_text($component['extra']['rows'], TRUE);
$query = db_select('webform_submitted_data', 'wsd')
->fields('wsd', array(
'no',
'data',
))
->condition('nid', $component['nid'])
->condition('cid', $component['cid'])
->condition('data', '', '<>')
->groupBy('no')
->groupBy('data');
$query
->addExpression('COUNT(sid)', 'datacount');
if (count($sids)) {
$query
->condition('sid', $sids, 'IN');
}
$result = $query
->execute();
$counts = array();
$weights = array();
foreach ($result as $data) {
$counts[$data->no][$data->data] = $data->datacount;
$weights[$data->data] = $data->data;
}
ksort($weights);
$rows = array();
$header = array(
'',
);
foreach ($weights as $weight) {
$header[] = t('Weight: !weight', array(
'!weight' => $weight,
));
}
foreach ($draggable_list_rows as $draggable_list_rowkey => $draggable_list_row) {
$row = array(
_webform_filter_xss($draggable_list_row),
);
foreach ($weights as $weight) {
$row[] = !empty($counts[$draggable_list_rowkey][$weight]) ? $counts[$draggable_list_rowkey][$weight] : 0;
}
$rows[] = $row;
}
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'class' => array(
'webform-grid',
),
),
));
return array(
array(
array(
'data' => $output,
'colspan' => 2,
),
),
);
}
function _webform_table_draggable_list($component, $value) {
webform_component_include('select');
$rows = _webform_select_options_from_text($component['extra']['rows'], TRUE);
$output = '';
if (is_array($value)) {
asort($value);
foreach ($value as $item => $value) {
if ($value !== '') {
$output .= _webform_filter_xss($rows[$item]) . '<br />';
}
}
}
else {
$output = !isset($value[0]) ? '' : _webform_filter_xss($value[0]);
}
return $output;
}
function _webform_csv_headers_draggable_list($component, $export_options) {
webform_component_include('select');
$header = array();
$header[0] = array(
'',
);
$header[1] = array(
$component['name'],
);
$items = _webform_select_options_from_text($component['extra']['rows'], TRUE);
$count = 0;
foreach ($items as $key => $item) {
if ($count != 0) {
$header[0][] = '';
$header[1][] = '';
}
$header[2][] = $component['name'] . " " . (string) ($count + 1);
$count++;
}
return $header;
}
function _webform_csv_data_draggable_list($component, $export_options, $value) {
webform_component_include('select');
$rows = _webform_select_options_from_text($component['extra']['rows'], TRUE);
$return = array();
if (is_array($value)) {
asort($value);
foreach ($value as $key => $val) {
if (isset($rows[$key])) {
$return[] = $export_options['select_keys'] ? $key : $rows[$key];
}
else {
$return[] = '';
}
}
}
return $return;
}