Skip to content

Instantly share code, notes, and snippets.

@labboy0276
Created October 16, 2014 17:56
Show Gist options
  • Save labboy0276/95d62ac0ae002d2ec4c4 to your computer and use it in GitHub Desktop.
Save labboy0276/95d62ac0ae002d2ec4c4 to your computer and use it in GitHub Desktop.
cvt batach
function cvt_management_report_census_form_submit($form, &$form_state) {
//POSTS - FORM INFO
$district_cvtid = $form_state['values']['district']; //hidden
$eff_month = $form_state['values']['eff_month'];
$plan_year = $form_state['values']['plan_year'];
//FORMAT THE MONTH/YEAR FOR THE API
if ($eff_month >= 10) {
//DOUBLE DIGIT MONTHS DO NOT NEED TO BE PADDED
$eff_month_padded = $eff_month;
//GET THE FIRST PART OF THE PLAN YEAR
$plan_year_CCYY = substr($plan_year, 0, 4);
} else {
//PAD SINGLE DIGIT MONTHS
$eff_month_padded = '0' . $eff_month;
//GET THE SECOND PART OF THE PLAN YEAR
$plan_year_CCYY = substr($plan_year, 4, 4); //correct line
//$plan_year_CCYY = substr($plan_year, 3, 4); //will cause an error
}
$effMonthYear = $eff_month_padded . $plan_year_CCYY;
//CALL API
$census = cvt_management_get_data_census_api($district_cvtid, $effMonthYear);
// Return JSON array based on response.
if ($census !== FALSE) {
if (isset($census['subscribers'])) {
// Batch Process plugin.
$batch = array(
'title' => t('Getting Report...'),
'operations' => array(
array('cvt_management_report_census', array($census, $effMonthYear)),
),
'init_message' => t('Commencing'),
'finished' => 'cvt_management_batch_finished',
);
batch_set($batch);
}
else {
// if http result does not have content
$msg = 'Census: No Subscribers index returned.';
watchdog('cvt', $msg, WATCHDOG_WARNING);
$message = array('status' => 'warning', 'message' => t('No subscribers for this district during the effective date.'));
drupal_set_message($message['message'], $message['status'], FALSE);
return '';
}
}
else {
$message = t('Census: Census for %district not returned.', array('%district' => $district_cvtid));
watchdog('cvt', $message, NULL, WATCHDOG_NOTICE);
$message = array('status' => 'error', 'message' => t('The census report was not able to be generated. Please contact support, 1-800-288-9870.'));
drupal_set_message($message['message'], $message['status'], FALSE);
return '';
}
}
function cvt_management_report_census($census, $effMonthYear, &$context) {
//Set Batch Counter
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
//Create ouptput stream
$output = fopen('php://output', 'w');
//Add header row
fputcsv($output, array(
t('District Name'),
t('Unit'),
t('Previous Unit'),
t('Last Name'),
t('First Name'),
t('M.I.'),
t('Gender'),
t('Zip Code'),
t('DOB'),
t('Marital Status'),
t('Status'),
t('Enrollment Type'),
t('# of Deps.'),
t('Deps. w/Health"'),
t('Deps. w/Dental'),
t('Deps. w/Vision'),
t('Deps. w/SupLife')
));
//Add data rows
foreach ($census['subscribers'] as $key => $row) {
$context['message'] = t('Now assimilation %key', array('%key' => $key));
fputcsv($output, array(
t($row['districtname']),
t($row['unit']),
t($row['previousunit']),
t($row['lastname']),
t($row['firstname']),
t($row['middleinitial']),
t($row['gender']),
t($row['zipcode']),
t($row['birthdate']),
t($row['maritalstatus']),
t($row['status']),
t($row['enrollType']),
t($row['numdeps']),
t($row['dephealth']),
t($row['depdental']),
t($row['depvision']),
t($row['depsuplife'])
));
$context['sandbox']['progress']++;
}
$test = '1';
//SET THE FILE NAME
$filename = $row['districtname'] . "_Census_" . $effMonthYear . "_" . date("M_d-Y");
//REMOVE PERIODS AND SPACES FORM FILE NAMES
$filename = str_replace("." ,"" ,$filename);
$filename = str_replace(" ", "", $filename);
drupal_add_http_header('Content-Type', 'text/csv; utf-8');
drupal_add_http_header('Content-Disposition', "attachment; filename = $filename.csv");
drupal_add_http_header('Pragma', 'no-cache');
drupal_add_http_header('Expires', '0');
//Close the output stream
fclose($output);
return TRUE;
}
function cvt_management_batch_finished($success, $results, $operations) {
$test = '1';
if ($success) {
$message = format_plural(count($results), 'One URL assimilated.', '@count drones assimilated.');
}
else {
$message = t('Assimilation was futile!');
}
drupal_set_message($message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment