Move pledge data parsing to separate file.
This commit is contained in:
73
pledge-update/parse_pledge_data.php
Normal file
73
pledge-update/parse_pledge_data.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
function parse_pledge_data($candidates_files) {
|
||||
$candidate_data = [];
|
||||
foreach ($candidates_files as $key => $file) {
|
||||
$config_file = dirname($file)."/config.json";
|
||||
$config_string = file_get_contents($config_file);
|
||||
|
||||
if ($config_string !== FALSE) {
|
||||
$config = json_decode($config_string, true);
|
||||
} else {
|
||||
error_log("Error opening config.json.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (($handle = fopen($file, "r")) !== FALSE) {
|
||||
$headers = fgetcsv($handle);
|
||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||
$candidate = [];
|
||||
$candidate['Pledge'] = 'n';
|
||||
$candidate['Picture'] = "";
|
||||
$candidate['image_url'] = "";
|
||||
$candidate['image_id'] = "";
|
||||
foreach ($headers as $key => $value) {
|
||||
$candidate[$value] = $data[$key];
|
||||
}
|
||||
$candidate['Council'] = $config['councilName'];
|
||||
$candidate['Path'] = dirname($file);
|
||||
$media_desc = $candidate['Path']."/".
|
||||
$candidate['Picture'].".json";
|
||||
if (file_exists($media_desc)) {
|
||||
$media_string = file_get_contents($media_desc);
|
||||
if ($media_string !== FALSE) {
|
||||
$media = json_decode($media_string, true);
|
||||
} else {
|
||||
error_log("Error opening image descriptor.");
|
||||
exit(1);
|
||||
}
|
||||
/* Get photo URL and ID */
|
||||
$candidate['image_url'] = $media['url'];
|
||||
$candidate['image_id'] = $media['id'];
|
||||
}
|
||||
$candidate_data[$candidate['Candidate Name']] = $candidate;
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening candidates file');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Override pledge columns if pledges.csv is present */
|
||||
$pledges_file = dirname($file)."/pledges.csv";
|
||||
if (!file_exists($pledges_file)) continue;
|
||||
|
||||
if (($handle = fopen($pledges_file, "r")) !== FALSE) {
|
||||
$headers = fgetcsv($handle);
|
||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||
$candidate = [];
|
||||
foreach ($headers as $key => $value) {
|
||||
$candidate[$value] = $data[$key];
|
||||
}
|
||||
$candidate_data[$candidate['Candidate Name']]['Pledge'] =
|
||||
$candidate['Pledge'];
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening pledges file');
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
return $candidate_data;
|
||||
}
|
||||
Reference in New Issue
Block a user