forked from chaunceyt/lawmakers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlawmakers.drush.inc
More file actions
48 lines (42 loc) · 1.03 KB
/
lawmakers.drush.inc
File metadata and controls
48 lines (42 loc) · 1.03 KB
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
<?php
/**
* @file
* Lawmaker Drush script.
*/
/**
* Implements hook_drush_command().
*/
function lawmakers_drush_command() {
$items = array();
$items['import-lawmakers'] = array(
'callback' => 'lawmakers_import_csv_data',
'description' => dt('Import Lawmakers from csv file.'),
);
return $items;
}
/**
* Drush callback.
*/
function lawmakers_import_csv_data() {
$lawmakers = _lawmakers_parse_csv();
foreach ($lawmakers as $lawmaker) {
$lawmaker = entity_create('lawmakers', $lawmaker);
$username = $lawmaker->firstname . '_' . $lawmaker->lastname;
$lawmaker->username = $username;
lawmakers_save($lawmaker);
}
drush_log('Import complete', 'success');
}
/**
* Helper function to parse csv.
*/
function _lawmakers_parse_csv() {
$items = array();
$file = drupal_get_path('module', 'lawmakers') . "/data/legislators.csv";
$rows = array_map('str_getcsv', file($file));
$header = array_shift($rows);
foreach ($rows as $row) {
$items[] = array_combine($header, $row);
}
return $items;
}