/[drupal]/contributions/modules/votesmart/votesmart.module
ViewVC logotype

Contents of /contributions/modules/votesmart/votesmart.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sun Aug 17 15:36:33 2008 UTC (15 months, 1 week ago) by vauxia
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +30 -15 lines
File MIME type: text/x-php
Latest batch of changes -
 - When linked to a Vote Smart record, all CCK, body, and title fields can be
   populated with values from the remote records.
 - Added format callbacks for certain fields, e.g. 't'/'f' can become 1/0
   boolean values, newline-delimited lists can become multivalued fields, etc.
 - Handling of "list" values, such as office type, office branch, state, etc.
 - Reformatted API input array, which should hopefully make things clearer.
 - Continued populating 'tables', to include candidate records.
 - Experimental factory/bulk import working.  For example, you can now import
   all candidates for a particular state election.
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 }

  ViewVC Help
Powered by ViewVC 1.1.2