-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunlight_rules.rules.inc
133 lines (109 loc) · 3.3 KB
/
sunlight_rules.rules.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* implements hook_rules_action_info
* creates custom action for rules
*/
function sunlight_rules_rules_action_info() {
return array(
'sunlight_rules_get_state_legislators' => array(
'label' => t('Get state legislators by lat/lng coordinates'),
'group' => t('Sunlight Rules'),
'parameter' => array(
'latitude' => array(
'label' => t('Latitude'),
'type' => 'text',
),
'longitude' => array(
'label' => t('Longitude'),
'type' => 'text',
),
),
'provides' => array(
'sunlight_state_legislators_dump' => array(
'type' => 'text',
'label' => t('Legislators - Variable Dump'),
),
),
'access callback' => '_sunlight_rules_access_callback',
),
'sunlight_rules_get_state_legislator_districts' => array(
'label' => t('Get state legislator districts by lat/lng coordinates'),
'group' => t('Sunlight Rules'),
'parameter' => array(
'latitude' => array(
'label' => t('Latitude'),
'type' => 'text',
),
'longitude' => array(
'label' => t('Longitude'),
'type' => 'text',
),
),
'provides' => array(
'sunlight_state_lower_chamber_district' => array(
'type' => 'text',
'label' => t('Lower Chamber District'),
),
'sunlight_state_upper_chamber_district' => array(
'type' => 'text',
'label' => t('Upper Chamber District'),
),
),
'access callback' => '_sunlight_rules_access_callback',
),
);
}
/**
* callback function for getting state legislators
*/
function sunlight_rules_get_state_legislators($latitude, $longitude) {
$return = array(
'sunlight_state_legislators_dump' => null,
);
$state_legislators = sunlight('OpenStates','geo_lookup');
$state_legislators_object = $state_legislators->coords($latitude, $longitude)->get();
$return['sunlight_state_legislators_dump'] = print_r($state_legislators_object,1);
return $return;
}
/**
* callback function for getting state legislator districts
*/
function sunlight_rules_get_state_legislator_districts($latitude, $longitude) {
$return = array(
'sunlight_state_upper_chamber_district' => null,
'sunlight_state_lower_chamber_district' => null,
);
$state_legislators = sunlight('OpenStates','geo_lookup');
$state_legislators_object = $state_legislators->coords($latitude, $longitude)->get();
foreach($state_legislators_object as $state_legislator) {
if ( isset($state_legislator->chamber) && isset($state_legislator->district) ) {
if ( $state_legislator->chamber == 'upper') {
$return['sunlight_state_upper_chamber_district'] = $state_legislator->district;
} elseif ( $state_legislator->chamber == 'lower') {
$return['sunlight_state_lower_chamber_district'] = $state_legislator->district;
}
}
}
return $return;
}
/**
* helper function to provide access control
*/
function _sunlight_rules_access_callback() {
return user_access('use sunlight rules');
}
/**
* helper function to log requests
*/
function _sunlight_rules_log_request($log) {
/*
$object = new stdClass();
$object->timestamp = time();
$fields = array('hook','url','method','headers','data','callback','status_code',
'status_method','error','data_returned');
foreach ($fields as $field) {
$object->$field = isset($log[$field]) ? $log[$field] : null;
}
watchdog('rules_api', '<pre>'.print_r($object, 1).'</pre>');
*/
}