-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenreferral.module
213 lines (182 loc) · 5.68 KB
/
openreferral.module
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* @file
* Module file for openreferral.
*/
/**
* Implements hook_menu().
*/
function openreferral_menu() {
$items = array();
$items['admin/config/services/openreferral'] = array(
'title' => 'Open Referral',
'description' => 'Configure Open Referral Settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('openreferral_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'openreferral.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['openreferral/datapackage.json'] = array(
'page callback' => 'openreferral_json_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['openreferral/%'] = array(
'page callback' => 'openreferral_csv_page',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu callback for /openreferral/datapackage.json
*/
function openreferral_json_page() {
$base_url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
$name = $_SERVER['HTTP_HOST'] . '-open-referral-dataset';
$json = array(
'name' => $name,
'resources' => array(
array(
'path' => $base_url . '/openreferral/organizations.csv',
'schema' => array(
'fields' => openreferral_fields('schema', 'organizations')
)
),
array(
'path' => $base_url . '/openreferral/phones.csv',
'schema' => array(
'fields' => openreferral_fields('schema', 'phones')
)
),
array(
'path' => $base_url . '/openreferral/postal_addresses.csv',
'schema' => array(
'fields' => openreferral_fields('schema', 'address')
)
),
)
);
return drupal_json_output($json);
}
/**
* Menu callback for /openreferral/*.csv
*/
function openreferral_csv_page($csv_file) {
switch ($csv_file) {
case 'organizations.csv':
$table = 'organizations';
break;
case 'phones.csv':
$table = 'phones';
break;
case 'postal_addresses.csv':
$table = 'address';
break;
}
if (!empty($table)) {
if (!$csv = openreferral_get_openreferral_data($table)) {
echo 'Open Referral data not available.';
return;
}
drupal_add_http_header('Content-type', 'text/csv');
$out = fopen('php://output', 'w');
foreach ($csv as $line) {
fputcsv($out, $line);
}
fclose($out);
}
}
/**
* @param $mode
* 'field' or 'schema'
*
* @param $table
* table name or 'all'
*/
function openreferral_fields($mode, $table) {
$fields = array(
'organizations' => array(
'name', 'alternate_name', 'description', 'email', 'url', 'legal_status', 'tax_status', 'tax_id', 'year_incorporated', 'services', 'phones', 'locations', 'contact', 'details', 'program'
),
'phones' => array(
'number', 'locations', 'services', 'organizations', 'contacts', 'service_at_location_id', 'extension', 'type', 'language', 'description'
),
'address' => array(
'address_1', 'city', 'state_province', 'postal_code', 'region', 'country', 'attention', 'address_type', 'locations'
),
);
if ($mode == 'field') {
return $table == 'all' ? $fields : $fields[$table];
}
elseif ($mode == 'schema') {
return array_map(function ($val) {
return array('name' => $val, 'type' => 'string');
}, $fields[$table]);
}
}
function openreferral_get_openreferral_data($table) {
if (!$cache = variable_get('openreferral_cache', array())) {
openreferral_build_openreferral_data();
$cache = variable_get('openreferral_cache', array());
}
return isset($cache[$table]) ? $cache[$table] : NULL;
}
function openreferral_build_openreferral_data() {
$api_key = variable_get('openreferral_api_key', '');
$or_base_url = variable_get('openreferral_base_url', '');
if (!$api_key or !$or_base_url) return;
$tables = array('services', 'locations', 'organizations', 'phones', 'address', 'contact', 'program');
$fields = openreferral_fields('field', 'all');
$data = array();
foreach ($tables as $table) {
$airtable = drupal_http_request($or_base_url . "/$table", array('headers' => array('Authorization' => 'Bearer ' . $api_key)));
if ($airtable->code == 200 and $airtable->status_message == 'OK') {
$records = drupal_json_decode($airtable->data)['records'];
foreach ($records as $row) {
$data[$table][$row['id']] = $row['fields'];
}
}
}
// Replace record id with label (mostly name field except phone number) and
// build csv data to cache.
foreach (array_keys($fields) as $table) {
foreach ($data[$table] as $row) {
$item = array();
foreach ($row as $field => $value) {
if (is_array($value)) {
foreach ($value as $key => $rec_id) {
$val = NULL;
if (strpos($rec_id, 'rec') !== FALSE) {
if (isset($data[$field][$rec_id]['name'])) {
$val = $data[$field][$rec_id]['name'];
}
elseif (isset($data[$field][$rec_id]['number'])) {
$val = $data[$field][$rec_id]['number'];
}
}
if ($val) {
$value[$key] = $val;
}
}
$value = join(', ', $value);
}
$item[$field] = $value;
}
$openref[$table][] = $item;
}
foreach ($openref[$table] as $row) {
if (empty($csv_data[$table])) {
$csv_data[$table][] = array_values($fields[$table]);
}
$item = array();
foreach ($fields[$table] as $field) {
$item[] = isset($row[$field]) ? $row[$field] : '';
}
$csv_data[$table][] = $item;
}
}
variable_set('openreferral_cache', $csv_data);
}