vid] = $v->name;
}
if(isset($currfield['extra']['vocabulary'])){
$default_vocabulary = $currfield['extra']['vocabulary'];
} else {
$default_vocabulary = 0;
}
$edit_fields['extra']['vocabulary'] = array(
'#type' => 'select',
'#title' => t("Vocabulary"),
'#options' => $vocabularies,
'#default_value' => $default_vocabulary,
'#description' => t('The vocabulary set to present terms from.'),
'#weight' => -4,
'#required' => TRUE,
//emfluence
'#ahah' => array(
'event' => 'change',
'path' => 'node/' . $nid . '/edit/components/ahah/vocabulary/terms',
'wrapper' => 'webform-component-edit-form',
'method' => 'replace',
),
);
//emfluence
$default_terms = array();
if(isset($currfield['extra']['terms'])){
$default_terms = $currfield['extra']['terms'];
}
$edit_fields['extra']['terms'] = array(
'#type' => 'select',
'#title' => t('Terms to Display'),
'#options' => _webform_vocabulary_get_terms($default_vocabulary),
'#default_value' => $default_terms,
'#description' => t('The terms available to the user.'),
'#weight' => -3,
'#require' => TRUE,
'#multiple' => TRUE,
);
if(isset($currfield['extra']['depth'])){
$default_depth = $currfield['extra']['depth'];
} else {
$default_depth = 0;
}
$edit_fields['extra']['depth'] = array(
'#type' => 'textfield',
'#title' => t('Depth to Display'),
'#weight' => -2,
'#size' => 3,
'#default_value' => $default_depth,
'#required' => TRUE,
'#description' => t('The depth to display from each parent. Leave 0 to display all terms.'),
);
$edit_fields['extra']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t("Multiple"),
'#return_value' => 'Y',
'#default_value' => ($currfield['extra']['multiple']=='Y'?TRUE:FALSE),
'#description' => t('Check this option if the user should be allowed to choose multiple values.'),
);
$edit_fields['extra']['aslist'] = array(
'#type' => 'checkbox',
'#title' => t("Listbox"),
'#return_value' => 'Y',
'#default_value' => ($currfield['extra']['aslist']=='Y'?TRUE:FALSE),
'#description' => t('Check this option if you want the select component to be of listbox type instead of radiobuttons or checkboxes.'),
);
return $edit_fields;
}
//emfluence
function _webform_vocabulary_get_terms($vocabulary){
$options = array();
if($vocabulary == 0){
$result = db_query(db_rewrite_sql("SELECT vid, name FROM {vocabulary}"));
$vocabularies = array();
while ($v = db_fetch_object($result)) {
$vocabularies[$v->vid] = $v->name;
$tree = taxonomy_get_tree($v->vid);
foreach ($tree as $term) {
$options[$term->tid] = str_repeat('-', $term->depth). $term->name;
}
}
} else {
$tree = taxonomy_get_tree($vocabulary);
if ($tree && count($tree) > 0) {
foreach ($tree as $term) {
$options[$term->tid] = str_repeat('-', $term->depth). $term->name;
}
}
}
return $options;
}
//emfluence
function _webform_vocabulary_terms($vocabulary) {
if($vocabulary != "null") {
$term = array(
'#type' => 'select',
'#title' => t('Terms to Display'),
'#options' => _webform_vocabulary_get_terms($vocabulary),
'#description' => t('The terms available to the user.'),
'#weight' => -3,
'#require' => TRUE,
'#multiple' => TRUE,
);
$output = _webform_vocabulary_ahah_terms_render($term, 'extra');
print drupal_to_js(array('data' => $output, 'status' => true));
} else {
print drupal_to_js("
Error loading data.
");
}
exit();
}
/*
This function is largely based on the poll module, its been simplified for reuse.
$fields is the specific form elements you want to attach via ahah,
$name is the form fields array key... e.g. the name for $form['title'] is "title"
emfluence
*/
function _webform_vocabulary_ahah_terms_render($fields, $name) {
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Add the new element to the stored form. Without adding the element to the
// form, Drupal is not aware of this new elements existence and will not
// process it. We retreive the cached form, add the element, and resave.
$form = form_get_cache($form_build_id, $form_state);
$form[$name]['terms'] = $fields;
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
// Rebuild the form.
$form = form_builder($_POST['form_id'], $form, $form_state);
// Render the new output.
$new_form = $form;
return drupal_render($new_form);
}
// emfluence
function _webform_vocabulary_ahah($node, $function){
switch($function){
case "terms":
_webform_vocabulary_terms($_POST['extra']['vocabulary']);
break;
}
}
/**
* function webform_render_vocabulary
* Build a form item array containing all the properties of this component
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns An array of a form item to be displayed on the client-side webform
**/
function _webform_render_vocabulary($component) {
$form_item = array(
'#title' => htmlspecialchars($component['name'], ENT_QUOTES),
'#required' => $component['mandatory'],
'#weight' => $component['weight'],
'#description' => _webform_filter_descriptions($component['extra']['description']),
'#prefix' => '',
'#suffix' => '
',
);
if ($component['extra']['aslist'] == 'Y' && $component['extra']['multiple'] != 'Y') {
$options = array('' => t('select...'));
}
else {
$options = array();
}
// Extract terms from user-selected vocabulary
if ($component['extra']['depth'] == 0){
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
foreach ($tree as $child_term) {
if(!in_array($child_term->tid, $options)){
$options[$child_term->tid] = str_repeat('-', $child_term->depth + 1). $child_term->name;
}
}
}
} else {
foreach($component['extra']['terms'] as $term_id){
$term = taxonomy_get_term($term_id);
$options[$term->tid] = $term->name;
$tree = taxonomy_get_tree($term->vid, $term->tid, -1, $component['extra']['depth']);
if ($tree && count($tree) > 0) {
foreach ($tree as $child_term) {
if(!in_array($child_term->tid, $options)){
$options[$child_term->tid] = str_repeat('-', $child_term->depth + 1). $child_term->name;
}
}
}
}
}
/*
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
foreach ($tree as $term) {
$options[$term->tid] = str_repeat('-', $term->depth). $term->name;
}
}
*/
// Set the component options
$form_item['#options'] = $options;
// Set the default value
if ($default_value) {
// Convert default value to a list if necessary
if ($component['extra']['multiple'] == 'Y') {
if (strpos($default_value, ',')) {
$varray = explode(',', $default_value);
foreach ($varray as $key => $v) {
if (array_key_exists(_webform_safe_name($v), $options)) {
$form_item['#default_value'][] = _webform_safe_name($v);
}
else {
$form_item['#default_value'][] = $v;
}
}
}
else {
if (array_key_exists(_webform_safe_name($default_value), $options)) {
$form_item['#default_value'] = _webform_safe_name($default_value);
}
else {
$form_item['#default_value'] = $default_value;
}
}
}
else {
if (array_key_exists(_webform_safe_name($default_value), $options)) {
$form_item['#default_value'] = _webform_safe_name($default_value);
}
else {
$form_item['#default_value'] = $default_value;
}
}
}
if ($component['extra']['aslist'] == 'Y') {
// Set display as a select list:
$form_item['#type'] = 'select';
if ($component['extra']['multiple'] == 'Y') {
$form_item['#multiple'] = TRUE;
}
}
else {
if ($component['extra']['multiple'] == 'Y') {
// Set display as a checkbox set
$form_item['#type'] = 'checkboxes';
}
else {
// Set display as a radio set
$form_item['#type'] = 'radios';
}
}
return $form_item;
}
/**
* function _webform_submission_display_vocabulary
* Display the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions"
* @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns Textual output formatted for human reading.
**/
function _webform_submission_display_vocabulary($data, $component, $enabled = FALSE) {
$form_item = _webform_render_vocabulary($component);
if ($component['extra']['multiple'] == 'Y') {
$form_item['#default_value'] = array();
// Set the value as an array
foreach ((array)$data['value'] as $key => $value) {
if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
$form_item['#default_value'][] = _webform_safe_name($value);
}
else {
$form_item['#default_value'][] = $value;
}
}
}
else {
// Set the value as a single string
foreach ((array)$data['value'] as $value) {
if ($value !== '0') {
if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
$form_item['#default_value'] = _webform_safe_name($value);
}
else {
$form_item['#default_value'] = $value;
}
break;
}
}
}
$form_item['#disabled'] = !$enabled;
return $form_item;
}
/**
* DEPRICATED!!! Do not uncomment!
* function webform_submit_vocabulary
* Translates the submitted 'safe' form values back into their un-edited original form
* @param $data The POST data associated with the component
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns Nothing
**/
/*
function _webform_submit_vocabulary(&$data, $component) {
$value = _webform_filter_values($component['value']);
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
foreach ($tree as $term) {
$options[$term->tid] = str_repeat('-', $term->depth). $term->name;
}
}
if (is_array($data)) {
foreach ($data as $key => $value) {
if ($value) {
$data[$key] = $options[$key];
}
}
}
elseif (!empty($data)) {
$data = $options[$data];
}
}
*/
/**
* Module specific instance of hook_theme().
*/
function _webform_theme_vocabulary() {
return array(
'webform_mail_vocabulary' => array(
'arguments' => array('data' => NULL, 'component' => NULL),
),
);
}
/**
* theme_webform_mail_vocabulary
* Format the output of emailed data for this component
*
* @param mixed $data A string or array of the submitted data
* @param array $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns string Textual output to be included in the email
*/
function theme_webform_mail_vocabulary($data, $component) {
// Generate the output
if ($component['extra']['multiple']) {
$output = $component['name'] .":\n";
foreach ($data as $value) {
if ($value) {
$term = taxonomy_get_term($value);
$output .= " - " . $term->name . "\n";
}
}
}
else {
$term = taxonomy_get_term($data);
$output = $component['name'] .": ". $term->name ."\n";
}
return $output;
}
/**
* function _webform_help_vocabulary
* Module specific instance of hook_help
**/
function _webform_help_vocabulary($section) {
switch ($section) {
case 'admin/settings/webform#vocabulary_description':
$output = t("Allows creation of checkboxes, radio buttons, or select menus with taxonomy terms.");
break;
}
return $output;
}
/**
* function _webform_analysis_view_vocabulary
* Calculate and returns statistics about results for this component from all submission to this webform. The output of this function will be displayed under the "results" tab then "analysis"
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns An array of data rows, each containing a statistic for this component's submissions.
**/
function _webform_analysis_rows_vocabulary($component) {
$options = _webform_select_options($component['extra']['items']);
$query = 'SELECT data, count(data) as datacount '.
' FROM {webform_submitted_data} '.
' WHERE nid = %d '.
' AND cid = %d '.
" AND data != '0' AND data != '' ".
' GROUP BY data ';
$result = db_query($query, $component['nid'], $component['cid']);
$rows = array();
while ($data = db_fetch_array($result)) {
if (isset($options[$data['data']])) {
$display_option = $options[$data['data']];
}
else {
$display_option = $data['data'];
}
$term = taxonomy_get_term($display_option);
$rows[] = array($term->name, $data['datacount']);
}
return $rows;
}
/**
* function _webform_table_data_vocabulary
* Return the result of this component's submission for display in a table. The output of this function will be displayed under the "results" tab then "table"
* @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
* @returns Textual output formatted for human reading.
**/
function _webform_table_data_vocabulary($data) {
// Set the value as a single string
if (is_array($data['value'])) {
foreach ($data['value'] as $value) {
if ($value !== '0') {
$term = taxonomy_get_term($value);
$output .= check_plain($term->name) . "
";
}
}
}
else {
if(empty($data['value']['0'])){
$output = "";
} else {
$term = taxonomy_get_term($data['value']['0']);
$output .= check_plain($term->name) ."
";
}
}
return $output;
}
/**
* function _webform_csv_headers_vocabulary
* Return the header information for this component to be displayed in a comma seperated value file. The output of this function will be displayed under the "results" tab then "download"
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns An array of data to be displayed in the first three rows of a CSV file, not including either prefixed or trailing commas
**/
function _webform_csv_headers_vocabulary($component) {
$header = array();
$header[0] = '';
if ($component['extra']['multiple']) {
$header[1] = $component['name'];
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
foreach ($tree as $term) {
$items[] = $term->name;
}
}
foreach ($items as $item) {
$header[2] .= ",". $item;
}
// Remove the preceding extra comma
$header[2] = substr($header[2], 2);
}
else {
$header[2] = $component['name'];
}
return $header;
}
/**
* function _webform_csv_data_vocabulary
* Return the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions"
* @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
* @returns Textual output formatted for CSV, not including either prefixed or trailing commas
**/
function _webform_csv_data_vocabulary($data, $component) {
$value = _webform_filter_values($component['value']);
$options = array();
// Extract terms from user-selected vocabulary
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
foreach ($tree as $term) {
$options[$term->tid] = $term->name;
}
}
if ($component['extra']['multiple']) {
foreach ($options as $key => $item) {
if (in_array($key, (array)$data['value']) === true) {
$output .= ',Yes';
}
else {
$output .= ',No';
}
}
// Remove the preceding extra comma
$output = substr($output, 2);
}
else {
$output = $data['value'][0];
}
return $output;
}
?>