| 1 |
<?php // $Id$ |
<?php // $Id: votesmart_field.module,v 1.1 2008/08/06 05:47:08 vauxia Exp $ |
| 2 |
|
|
| 3 |
|
/** |
| 4 |
|
* Implementation of hook_elements(). |
| 5 |
|
*/ |
| 6 |
|
function votesmart_field_elements() { |
| 7 |
|
return array( |
| 8 |
|
); |
| 9 |
|
} |
| 10 |
|
|
| 11 |
/** |
/** |
| 12 |
* Implementation of hook_field_info(). |
* Implementation of hook_field_info(). |
| 13 |
*/ |
*/ |
| 14 |
function votesmart_field_field_info() { |
function votesmart_field_field_info() { |
| 15 |
return array( |
return array( |
| 16 |
'votesmart_reference' => array( |
'votesmart_id' => array( |
| 17 |
'label' => 'Votesmart record', |
'label' => 'Votesmart record', |
| 18 |
'callbacks' => array( |
'callbacks' => array(), |
|
'tables' => CONTENT_CALLBACK_DEFAULT, |
|
|
'arguments' => CONTENT_CALLBACK_DEFAULT, |
|
|
), |
|
| 19 |
), |
), |
| 20 |
'votesmart_list' => array( |
'votesmart_list' => array( |
| 21 |
'label' => 'Votesmart list', |
'label' => 'Votesmart list', |
| 22 |
'callbacks' => array( |
'callbacks' => array(), |
|
'tables' => CONTENT_CALLBACK_DEFAULT, |
|
|
'arguments' => CONTENT_CALLBACK_DEFAULT, |
|
|
), |
|
| 23 |
), |
), |
| 24 |
); |
); |
| 25 |
} |
} |
| 28 |
* Implementation of hook_field_settings(). |
* Implementation of hook_field_settings(). |
| 29 |
*/ |
*/ |
| 30 |
function votesmart_field_field_settings($op, $field) { |
function votesmart_field_field_settings($op, $field) { |
| 31 |
|
require_once(drupal_get_path('module', 'votesmart') .'/votesmart.inc'); |
| 32 |
switch ($op) { |
switch ($op) { |
| 33 |
case 'form': |
case 'form': |
| 34 |
|
$form = array(); |
| 35 |
|
|
| 36 |
|
if ($field['type'] == 'votesmart_id') { |
| 37 |
|
// One id per customer, please. |
| 38 |
|
//$form['multiple'] = array('#type' => 'value', '#value' => 0); |
| 39 |
|
|
| 40 |
|
$tables = array(); |
| 41 |
|
foreach (votesmart_tables() as $name => $table) { |
| 42 |
|
if (!isset($table['fields'])) continue; |
| 43 |
|
$tables[$name] = $table['description']; |
| 44 |
|
} |
| 45 |
|
$form['votesmart_table'] = array( |
| 46 |
|
'#type' => 'select', |
| 47 |
|
'#title' => t('Vote Smart record type'), |
| 48 |
|
'#multiple' => FALSE, |
| 49 |
|
'#options' => $tables, |
| 50 |
|
'#default_value' => $field['votesmart_table'], |
| 51 |
|
); |
| 52 |
|
|
| 53 |
|
// Initialize the widget settings form. |
| 54 |
|
$form['votesmart_map'] = _votesmart_field_map_form($field['type_name'], $field); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
return $form; |
| 58 |
|
|
| 59 |
case 'save': |
case 'save': |
| 60 |
|
return array('votesmart_table', 'votesmart_map'); |
| 61 |
|
|
| 62 |
case 'database columns': |
case 'database columns': |
| 63 |
|
return array( |
| 64 |
|
'id' => array('type' => 'varchar', 'length' => 100), |
| 65 |
|
'table' => array('type' => 'varchar', 'length' => 100), |
| 66 |
|
); |
| 67 |
|
} |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
/** |
| 71 |
|
* Implementation of hook_field(). |
| 72 |
|
*/ |
| 73 |
|
function votesmart_field_field($op, &$node, $field, &$items, $teaser, $page) { |
| 74 |
|
switch ($op) { |
| 75 |
|
case 'validate': |
| 76 |
|
return; |
| 77 |
|
|
| 78 |
|
case 'presave': |
| 79 |
|
if ($map = $field['votesmart_map'][$node->type]) { |
| 80 |
|
foreach ($items as $item) { |
| 81 |
|
if (votesmart_field_content_is_empty($item, $field)) continue; |
| 82 |
|
$data = votesmart_get_record($field['votesmart_table'], $item); |
| 83 |
|
foreach ($map as $field_name => $token) { |
| 84 |
|
_votesmart_field_map_data($node, $field_name, $token, $data); |
| 85 |
|
} |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
|
return; |
| 89 |
} |
} |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
/** |
/** |
| 93 |
|
* Implementation of hook_field_content_is_empty(). |
| 94 |
|
*/ |
| 95 |
|
function votesmart_field_content_is_empty($item, $field) { |
| 96 |
|
return empty($item['id']); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
/** |
| 100 |
* Implementation of hook_widget_info(). |
* Implementation of hook_widget_info(). |
| 101 |
*/ |
*/ |
| 102 |
function votesmart_field_widget_info() { |
function votesmart_field_widget_info() { |
| 103 |
return array( |
return array( |
| 104 |
'votesmart_link' => array( |
'votesmart_id' => array( |
| 105 |
'label' => 'Link to remote Votesmart data', |
'label' => 'Link to remote Vote Smart data', |
| 106 |
'field types' => array('votesmart'), |
'field types' => array('votesmart_id'), |
| 107 |
'multiple values' => CONTENT_HANDLE_CORE, |
), |
| 108 |
'callbacks' => array( |
'votesmart_list' => array( |
| 109 |
'default value' => CONTENT_CALLBACK_DEFAULT, |
'label' => 'List of Vote Smart data', |
| 110 |
), |
'field types' => array('votesmart_id'), |
| 111 |
), |
), |
| 112 |
); |
); |
| 113 |
} |
} |
| 115 |
/** |
/** |
| 116 |
* Implementation of hook_widget(). |
* Implementation of hook_widget(). |
| 117 |
*/ |
*/ |
| 118 |
function votesmart_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
function votesmart_field_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
| 119 |
$element = array( |
$element = array( |
| 120 |
|
'#field' => $field, |
| 121 |
'#type' => $field['widget']['type'], |
'#type' => $field['widget']['type'], |
| 122 |
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL, |
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL, |
| 123 |
); |
); |
| 124 |
|
//TODO temporary |
| 125 |
|
$element = array( |
| 126 |
|
'#type' => 'textfield', |
| 127 |
|
'#input' => TRUE, |
| 128 |
|
'#columns' => array('id', 'table'), |
| 129 |
|
); |
| 130 |
return $element; |
return $element; |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
|
function _votesmart_field_map_data(&$node, $field_name, $token, $data) { |
| 134 |
|
if (is_array($token)) { |
| 135 |
|
foreach($token as $col => $tok) { |
| 136 |
|
if ($tok && isset($data[$tok])) { |
| 137 |
|
$node->{$field_name}[0][$col] = $data[$tok]; |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
else { |
| 142 |
|
if ($token && isset($data[$token])) $node->$field_name = $value; |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
function _votesmart_field_map_form($node_type, $field) { |
| 147 |
|
if (!$table = $field['votesmart_table']) return; |
| 148 |
|
|
| 149 |
|
$values = $field['votesmart_map'] ? $field['votesmart_map'] : array(); |
| 150 |
|
$tokens = current(votesmart_token_list('votesmart_'. $table)); |
| 151 |
|
$tokens = array_merge(array(0 => t('Do not populate')), $tokens); |
| 152 |
|
$supported_types = array('text', 'number_integer', 'number_float', 'number_decimal', 'image'); |
| 153 |
|
|
| 154 |
|
$form = array( |
| 155 |
|
'#type' => 'fieldset', |
| 156 |
|
'#title' => t('Vote Smart field mappings'), |
| 157 |
|
'#collapsible' => FALSE, |
| 158 |
|
'#tree' => TRUE, |
| 159 |
|
); |
| 160 |
|
|
| 161 |
|
foreach(array('title' => 'Title', 'body' => 'Body') as $name => $v) { |
| 162 |
|
$form[$node_type][$name] = array( |
| 163 |
|
'#type' => 'select', |
| 164 |
|
'#title' => t($v), |
| 165 |
|
'#multiple' => FALSE, |
| 166 |
|
'#options' => $tokens, |
| 167 |
|
'#default_value' => $values[$node_type][$name], |
| 168 |
|
); |
| 169 |
|
} |
| 170 |
|
foreach (content_fields(NULL, $node_type) as $name => $info ) { |
| 171 |
|
if ($info['type_name'] != $node_type) continue; |
| 172 |
|
if (!in_array($info['type'], $supported_types)) continue; |
| 173 |
|
|
| 174 |
|
$form[$node_type][$name] = array( '#tree' => TRUE ); |
| 175 |
|
|
| 176 |
|
foreach ($info['columns'] as $column_name => $column_info) { |
| 177 |
|
$form[$node_type][$name][$column_name] = array( |
| 178 |
|
'#type' => 'select', |
| 179 |
|
'#title' => $info['widget']['label'], |
| 180 |
|
'#multiple' => FALSE, |
| 181 |
|
'#options' => $tokens, |
| 182 |
|
'#default_value' => $values[$node_type][$name][$column_name], |
| 183 |
|
); |
| 184 |
|
} |
| 185 |
|
} |
| 186 |
|
return $form; |
| 187 |
|
} |