| 1 |
<?php // $Id: votesmart.module,v 1.2 2008/08/10 01:42:51 vauxia Exp $
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_menu().
|
| 5 |
*/
|
| 6 |
function votesmart_menu() {
|
| 7 |
return array(
|
| 8 |
'admin/build/votesmart' => array(
|
| 9 |
'title' => 'Project Vote Smart',
|
| 10 |
'page callback' => 'drupal_get_form',
|
| 11 |
'page arguments' => array('votesmart_settings'),
|
| 12 |
'access arguments' => array('administer votesmart'),
|
| 13 |
'file' => 'votesmart.inc',
|
| 14 |
),
|
| 15 |
'admin/build/votesmart/overview' => array(
|
| 16 |
'title' => 'API information',
|
| 17 |
'weight' => -10,
|
| 18 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 19 |
),
|
| 20 |
);
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_perm().
|
| 25 |
*/
|
| 26 |
function votesmart_perm() {
|
| 27 |
return array('administer votesmart');
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Effect a function call to the Vote Smart API.
|
| 32 |
*/
|
| 33 |
function votesmart_api() {
|
| 34 |
module_load_include('api.inc', 'votesmart');
|
| 35 |
$args = func_get_args();
|
| 36 |
return call_user_func_array('_votesmart_api', $args);
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Return a name->value list array from the Vote Smart API.
|
| 41 |
*/
|
| 42 |
function votesmart_get_list($list, $values = array(), $sort = TRUE) {
|
| 43 |
$list = current(votesmart_lists($list));
|
| 44 |
$request = _votesmart_set_query($list['votesmart'], $values);
|
| 45 |
$data = call_user_func_array('votesmart_api', $request);
|
| 46 |
$items = array();
|
| 47 |
foreach ($data as $item) {
|
| 48 |
$item = (array) $item;
|
| 49 |
$items[$item[$list['id']]] = $item[$list['name']];
|
| 50 |
}
|
| 51 |
if ($sort) ksort($items);
|
| 52 |
return $items;
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Return a single record from the Vote Smart API.
|
| 57 |
*/
|
| 58 |
function votesmart_get_record($table, $id, $extra = array()) {
|
| 59 |
$extra = array_merge($extra, array($table .'_id' => $id));
|
| 60 |
$table = current(votesmart_tables($table));
|
| 61 |
$request = _votesmart_set_query($table['votesmart'], $extra);
|
| 62 |
$data = call_user_func_array('votesmart_api', $request);
|
| 63 |
return _votesmart_field_values($table, $data);
|
| 64 |
}
|
| 65 |
|
| 66 |
function votesmart_lists($filter = NULL) {
|
| 67 |
module_load_include('inc', 'votesmart');
|
| 68 |
return _votesmart_lists($filter);
|
| 69 |
}
|
| 70 |
|
| 71 |
function votesmart_tables($filter = NULL) {
|
| 72 |
module_load_include('inc', 'votesmart');
|
| 73 |
return _votesmart_tables($filter);
|
| 74 |
}
|
| 75 |
|
| 76 |
function votesmart_token_list($type = 'all') {
|
| 77 |
if ($type == 'all' || substr($type, 0, 10) == 'votesmart_') {
|
| 78 |
$tokens = array();
|
| 79 |
$filter = ($type == 'all') ? NULL : str_replace('votesmart_', '', $type);
|
| 80 |
foreach (votesmart_tables($filter) as $table_name => $table) {
|
| 81 |
if (!isset($table['fields'])) return;
|
| 82 |
$tokens['votesmart_'. $table_name] = array();
|
| 83 |
foreach ($table['fields'] as $field_name => $field) {
|
| 84 |
$tokens['votesmart_'. $table_name][$field_name] = $field['description'];
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
return $tokens;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
function votesmart_token_values($type, $object = NULL, $options = array()) {
|
| 93 |
$object = (object) $object;
|
| 94 |
$table = str_replace('votesmart_', '', $type);
|
| 95 |
|
| 96 |
if ($object->votesmart_id) {
|
| 97 |
return votesmart_get_record($table, $object->votesmart_id);
|
| 98 |
}
|
| 99 |
}
|