| 1 |
<?php // $Id: votesmart_factory.inc,v 1.1 2008/08/06 05:47:08 vauxia Exp $ |
<?php // $Id: votesmart_factory.inc,v 1.2 2008/08/10 01:42:51 vauxia Exp $ |
| 2 |
|
|
| 3 |
function votesmart_factory() { |
function votesmart_factory() { |
| 4 |
require_once(drupal_get_path('module', 'votesmart') .'/votesmart.inc'); |
require_once(drupal_get_path('module', 'votesmart') .'/votesmart.inc'); |
| 9 |
$tables = votesmart_tables(); |
$tables = votesmart_tables(); |
| 10 |
$lists = votesmart_lists(); |
$lists = votesmart_lists(); |
| 11 |
|
|
| 12 |
|
//print_r(votesmart_api('Election.getElectionByYearState', array('year' => 2008))); |
| 13 |
|
//print_r(votesmart_api('Office.getOfficesByBranchLevel', array('branchId' => 'L', 'levelId' => 'L'))); |
| 14 |
|
//print_r(votesmart_get_record('election', 639)); |
| 15 |
|
//print_r(votesmart_api('Leadership.getPositions', array())); |
| 16 |
|
//print_r(votesmart_api('Candidates.getByElection', array('electionId' => 639))); |
| 17 |
|
//print_r(votesmart_api('Candidates.getByElection', array('electionId' => 639))); |
| 18 |
|
//print_r(votesmart_api('CandidateBio.getBio', array('candidateId' => 9490))); |
| 19 |
|
|
| 20 |
|
$states = votesmart_get_list('states'); |
| 21 |
|
$form['election'] = array( |
| 22 |
|
'#type' => 'fieldset', |
| 23 |
|
'#title' => t('Import election data'), |
| 24 |
|
'#tree' => TRUE, |
| 25 |
|
'#collapsible' => TRUE, |
| 26 |
|
'#collapsed' => TRUE, |
| 27 |
|
); |
| 28 |
|
|
| 29 |
|
$year = date('Y'); |
| 30 |
|
$years = array(); |
| 31 |
|
foreach (range($year - 5, $year + 5) as $y) $years[$y] = $y; |
| 32 |
|
$form['election']['year'] = array( |
| 33 |
|
'#type' => 'select', |
| 34 |
|
'#multiple' => TRUE, |
| 35 |
|
'#options' => $years, |
| 36 |
|
'#default_value' => $year, |
| 37 |
|
); |
| 38 |
|
|
| 39 |
|
$form['election']['states'] = array( |
| 40 |
|
'#type' => 'select', |
| 41 |
|
'#multiple' => TRUE, |
| 42 |
|
'#options' => array_merge(array('NA' => 'Federal elections'), $states), |
| 43 |
|
); |
| 44 |
|
|
| 45 |
foreach ($tables as $name => $table) { |
foreach ($tables as $name => $table) { |
| 46 |
// Incomplete table definition. Ignore. |
// Incomplete table definition. Ignore. |
| 47 |
if (!$table['fields']) continue; |
if (!$table['fields']) continue; |
| 48 |
|
|
| 49 |
$form[$name] = array( |
$form['tables'][$name] = array( |
| 50 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 51 |
); |
); |
| 52 |
|
|
| 53 |
$form[$name]['selected'] = array( |
$form['tables'][$name]['selected'] = array( |
| 54 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 55 |
'#title' => $table['description'], |
'#title' => $table['description'], |
| 56 |
); |
); |
| 57 |
|
|
| 58 |
$form[$name]['type_name'] = array( |
$form['tables'][$name]['type_name'] = array( |
| 59 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 60 |
'#title' => t('Type'), |
'#title' => t('Type'), |
| 61 |
'#description' => t('The machine-readable name of this content type.'), |
'#description' => t('The machine-readable name of this content type.'), |
| 63 |
); |
); |
| 64 |
|
|
| 65 |
foreach ($table['fields'] as $field_name => $field) { |
foreach ($table['fields'] as $field_name => $field) { |
| 66 |
$form[$name]['fields'][$field_name] = array( |
$form['tables'][$name]['fields'][$field_name] = array( |
| 67 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 68 |
'#title' => $field['description'], |
'#title' => $field['description'], |
| 69 |
); |
); |
| 80 |
if ($form_state['values'][$name]['selected']) { |
if ($form_state['values'][$name]['selected']) { |
| 81 |
$type_name = $form_state['values'][$name]['type_name']; |
$type_name = $form_state['values'][$name]['type_name']; |
| 82 |
$fields = $form_state['values'][$name]['fields']; |
$fields = $form_state['values'][$name]['fields']; |
| 83 |
votesmart_factory_import_node_type($type_name, $table, $fields); |
votesmart_factory_import_node_type($type_name, $name, $fields); |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
|
if ($form_state['values']['election']['states']) { |
| 87 |
|
foreach ($form_state['values']['election']['year'] as $year) { |
| 88 |
|
foreach ($form_state['values']['election']['states'] as $state) { |
| 89 |
|
$values = array('StateId' => $state, 'year' => $year); |
| 90 |
|
$election = votesmart_api('Election.getElectionByYearState', $values, 'election'); |
| 91 |
|
|
| 92 |
|
$s = (array) $election['stage']; |
| 93 |
|
//$values = array('electionId' => $election['electionId'], 'stageId' => $s['stageId']); |
| 94 |
|
$values = array('electionId' => $election['electionId'], 'stageId' => 'G'); |
| 95 |
|
|
| 96 |
|
//$candidates = votesmart_api('Election.getStageCandidates', $values, 'stagecandidates.election'); |
| 97 |
|
//$candidates = votesmart_api('Election.getStageCandidates', $values, 'candidate'); |
| 98 |
|
$candidates = votesmart_api('Candidates.getByElectionli', $values, 'candidate'); |
| 99 |
|
module_load_include('inc', 'node', 'node.pages'); |
| 100 |
|
foreach ($candidates as $c) { |
| 101 |
|
print_r($c); continue; |
| 102 |
|
$c = (array) $c; |
| 103 |
|
$values = array( |
| 104 |
|
'type' => 'candidate', |
| 105 |
|
'field_candidate_id' => $c['candidateId'], |
| 106 |
|
'title' => $c['firstName'] .' '. $c['lastName'], |
| 107 |
|
'op' => t('Save'), |
| 108 |
|
'name' => 'admin', |
| 109 |
|
); |
| 110 |
|
$node = (object) $values; |
| 111 |
|
$form_state = array('values' => $values); |
| 112 |
|
drupal_execute('candidate_node_form', $form_state, $node); |
| 113 |
|
} |
| 114 |
|
} |
| 115 |
|
die; |
| 116 |
} |
} |
| 117 |
} |
} |
| 118 |
} |
} |
| 119 |
|
|
| 120 |
function votesmart_factory_import_node_type($type_name, $table, $fields) { |
function votesmart_factory_import_node_type($type_name, $table_name, $fields) { |
| 121 |
|
$table = current(votesmart_tables($table_name)); |
| 122 |
$votesmart_factory_types = variable_get('votesmart_factory_types', array()); |
$votesmart_factory_types = variable_get('votesmart_factory_types', array()); |
| 123 |
|
|
| 124 |
include_once(drupal_get_path('module', 'content_copy') . '/content_copy.module'); |
include_once(drupal_get_path('module', 'content_copy') . '/content_copy.module'); |
| 127 |
'type' => array( |
'type' => array( |
| 128 |
'name' => $table['description'], |
'name' => $table['description'], |
| 129 |
'type' => $type_name, |
'type' => $type_name, |
| 130 |
'description' => '', |
'description' => t('@type information provided by Project Vote Smart', array('@type' => $table['description'])), |
| 131 |
'title_label' => '', |
'title_label' => 'Name', |
| 132 |
'body_label' => '', |
'has_title' => TRUE, |
| 133 |
|
'body_label' => 'Description', |
| 134 |
|
'has_body' => FALSE, |
| 135 |
'help' => '', |
'help' => '', |
| 136 |
'node_options' => array( |
'node_options' => array( |
| 137 |
'status' => true, |
'status' => true, |
| 150 |
'fields' => array(), |
'fields' => array(), |
| 151 |
); |
); |
| 152 |
|
|
| 153 |
|
// Field mappings to the Vote Smart API |
| 154 |
|
$map = array(); |
| 155 |
|
|
| 156 |
foreach ($fields as $field_name => $enabled) { |
foreach ($fields as $field_name => $enabled) { |
| 157 |
if (!$enabled) continue; |
if (!$enabled) continue; |
| 158 |
|
|
| 159 |
$content['fields'][] = _votesmart_factory_field($table, $field_name); |
if ($field = _votesmart_factory_field($table, $field_name, $table_name, $content, $map)) { |
| 160 |
|
$content['fields'][$field_name] = $field; |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
|
| 164 |
|
// Save the default field mappings to the votesmart field. |
| 165 |
|
if ($id = $content['votesmart_identifier']) { |
| 166 |
|
$content['fields'][$id]['votesmart_map'][$type_name] = $map; |
| 167 |
} |
} |
| 168 |
|
|
| 169 |
$form_state = array( |
$form_state = array( |
| 181 |
variable_set('votesmart_factory_types', $votesmart_factory_types); |
variable_set('votesmart_factory_types', $votesmart_factory_types); |
| 182 |
} |
} |
| 183 |
|
|
| 184 |
function _votesmart_factory_field($table, $field_name) { |
function _votesmart_factory_field($table, $name, $table_name, &$content, &$map) { |
| 185 |
$table_field = $table['fields'][$field_name]; |
$table_field = $table['fields'][$name]; |
| 186 |
|
|
| 187 |
// Default field definition. |
// Default field definition. |
| 188 |
|
$field_name = 'field_'. $name; |
| 189 |
$field = array( |
$field = array( |
| 190 |
'label' => $table_field['description'], |
'label' => $table_field['description'], |
| 191 |
'field_name' => 'field_'. $field_name, // TODO not specific enough. |
'field_name' => $field_name, // TODO not specific enough. |
| 192 |
'type' => 'text', |
'type' => 'text', |
| 193 |
'widget_type' => 'text_textfield', |
'widget_type' => 'text_textfield', |
| 194 |
'required' => '0', |
'required' => '0', |
| 197 |
'widget_module' => 'text', |
'widget_module' => 'text', |
| 198 |
); |
); |
| 199 |
|
|
| 200 |
|
// Default map placement. |
| 201 |
|
$field_map[$field_name] = array('value' => $name); |
| 202 |
|
|
| 203 |
|
// Set as title. |
| 204 |
|
if (in_array($name, array('name', 'title'))) { |
| 205 |
|
$field_map['title'] = $name; |
| 206 |
|
$content['title_label'] = $table_field['description']; |
| 207 |
|
unset($field, $field_map[$field_name]); |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
// Set as body. |
| 211 |
|
if (in_array($name, array('body'))) { |
| 212 |
|
$field_map['body'] = $name; |
| 213 |
|
$content['body_label'] = $table_field['description']; |
| 214 |
|
$content['has_body'] = TRUE; |
| 215 |
|
unset($field, $field_map[$field_name]); |
| 216 |
|
} |
| 217 |
|
|
| 218 |
// Convert to numeric field if we've got the number module. |
// Convert to numeric field if we've got the number module. |
| 219 |
if (module_exists('number')) { |
if (module_exists('number')) { |
| 220 |
|
$changed = false; |
| 221 |
if ($table_field['type'] == 'integer') { |
if ($table_field['type'] == 'integer') { |
| 222 |
$field['type'] = 'integer'; |
$changed = $field['type'] = 'number_integer'; |
| 223 |
} |
} |
| 224 |
elseif ($table_field['type'] == 'float') { |
elseif ($table_field['type'] == 'float') { |
| 225 |
$field['type'] = 'decimal'; |
$changed = $field['type'] = 'number_float'; |
| 226 |
} |
} |
| 227 |
|
if ($changed) { |
| 228 |
|
$field['widget_type'] = 'number'; |
| 229 |
|
$field['module'] = $field['widget_module'] = 'number'; |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
// Convert boolean items to checkboxes. |
| 233 |
|
if ($table_field['callback'] == '_votesmart_boolean' && module_exists('optionwidgets')) { |
| 234 |
|
$field['widget_type'] = 'optionwidgets_onoff'; |
| 235 |
|
$field['widget_module'] = 'optionwidgets'; |
| 236 |
|
$field['allowed_values'] = "0|False\n1|". $field['label']; |
| 237 |
|
} |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
// Convert "exploded" values to multi-value fields. |
| 241 |
|
if ($table_field['callback'] == '_votesmart_split_newline') { |
| 242 |
|
$field['multiple'] = '1'; |
| 243 |
|
} |
| 244 |
|
|
| 245 |
|
// Add input formatting to long text fields. |
| 246 |
|
if ($table_field['type'] == 'long') { |
| 247 |
|
$field['widget_type'] = 'text_textarea'; |
| 248 |
|
$field['text_processing'] = 1; |
| 249 |
} |
} |
| 250 |
|
|
| 251 |
// Convert to image field if we've got the imagefield module. |
// Convert to image field if we've got the imagefield module. |
| 252 |
if ($field['type'] == 'image' && module_exists('imagefield')) { |
if ($name == 'photo' && module_exists('imagefield')) { |
| 253 |
} |
} |
| 254 |
|
|
| 255 |
// Convert to link field if we've got the link module. |
// Convert to link field if we've got the link module. |
| 256 |
if ($field['type'] == 'link' && module_exists('link')) { |
if (($name == 'url' || substr($name, -4) == '_url') && module_exists('link')) { |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
// Convert multiple fields to postal field if we've got the postal module. |
| 260 |
|
|
| 261 |
|
// Convert to a votesmart field if it's an identifier. |
| 262 |
|
if ($name == $table_name .'_id') { |
| 263 |
|
$field['type'] = 'votesmart_id'; |
| 264 |
|
$field['widget_type'] = 'votesmart_id'; |
| 265 |
|
$field['module'] = $field['widget_module'] = 'votesmart_field'; |
| 266 |
|
$field['votesmart_table'] = $table_name; |
| 267 |
|
$content['votesmart_identifier'] = $name; |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
if ($field_map) { |
| 271 |
|
$map = array_merge($map, $field_map); |
| 272 |
} |
} |
| 273 |
|
|
| 274 |
return $field; |
return $field; |