Skip to content

Instantly share code, notes, and snippets.

@sam452
Last active August 29, 2015 14:26
Show Gist options
  • Save sam452/b023fc611820620dadce to your computer and use it in GitHub Desktop.
Save sam452/b023fc611820620dadce to your computer and use it in GitHub Desktop.
Can a csv be a drupal template with a parameter?
<?php
echo "year = " . $year;
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv"); // Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies
$objects = re_dashboard_sendCSV($year);
return toCSV($objects);
?>
unction re_dashboard_menu() {
$items = array();
$items['redashboardlogin'] = array('page callback' => 'drupal_get_form',
'page arguments' => array('re_dashboard_login_form'),
'access callback' => true
);
$items['redashboard/toCSV/%'] = array(
'title' => "Projects output",
'page arguments' => array(2),
'page callback' => '_re_dashboard_projects_toCSV',
'access arguments' => 'access content',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'file' => 're_dashboard_helper.inc',
);
return $items;
}
function re_dashboard_theme() {
return array(
're_dashboard_my_project_page' => array(
'template' => 'project_page',
'path' => drupal_get_path('module', 're_dashboard'),
'type' => 'module',
'variables' => array('results' => NULL,)
),
're_dashboard_csv' => array(
'template' => 'outputCSV',
'path' => drupal_get_path('module', 're_dashboard'),
'type' => 'module',
'variables' => array('results' => NULL,)
),
);
}
unction re_dashboard_all_list() {
re_dashboard_login_check();
dpm($_SESSION);
$table = array(
'header' => array(
'default' => array('data' => t("Year"), 'field' => 'expectedCompletionYear', 'sort' => 'asc'),
array('data' => t('Resident Name/Investigator'), 'field' => ''),
array('data' => t("Project Name/Question"), 'field' => 'projectName'),
array('data' => t('Mentor/Principal'), 'field' => 'projectMentor'),
),
'rows' => array(),
'attributes' => array('id' => 'redashboard-table', 'align' => 'center')
);
$no_yes = array("No", "Yes");
$query = db_select('domResidents', 'r');
$query->join('domProjects', 'p', 'r.residentID = p.residentID');
$query->fields('r', array('firstName', 'lastName', 'expectedCompletionYear'))
->fields('p', array('projectName', 'projectMentor', 'domProjectID', 'projectStatus'))
->orderBy('r.expectedCompletionYear', 'ASC')
->orderBy('r.lastName', 'ASC');
// ->extend('TableSort')
// ->orderBy('r.expectedCompletionYear');
// $query->groupBy('r.expectedCompletionYear');
$arr = array();
$result = $query->execute()->fetchAll();
//print_r($result);
$years = array();
$header = array('Name' => t('Resident Name/Investigator'), 'projectName' => t("Project Name/Question"), 'projectStatus' => t('Status'),'projectMentor' => t('Mentor/Principal'));
foreach ($result as $item) {
$rows[$item->expectedCompletionYear][] = array('Name' => $item->lastName . ', ' . $item->firstName , l($item->projectName, 'redashboard/project/' . $item->domProjectID), $item->projectStatus, 'projectMentor' => $item->projectMentor);
$years[] = $item->expectedCompletionYear;
}
$my_years = array_unique($years);
$theme = '';
foreach($my_years as $key => $value){
$theme .= '<h2>'. $value.' Residents</h2>';
$theme .= theme('table',array('header' => $header,'rows' => $rows[$value]));
$theme .= '<br>';
if (isset($_SESSION['isadmin']) && $_SESSION['isadmin']) {
$theme .= l('Download ' . $value . ' projects', 'redashboard/toCSV/' . $value) .'<br>';
}
}
$theme .= l('Back to Residents Dashboard', 'redashboard');
return $theme;
}
function re_dashboard_sendCSV($year) {
$query = db_select('domResidents', 'r');
$query->join('domProjects', 'p', 'r.residentID = p.residentID');
$query->fields('r', array('firstName', 'lastName', 'expectedCompletionYear'))
->condition('expectedCompletionYear', $year, '=')
->fields('p', array('projectName', 'projectMentor', 'domProjectID', 'projectStatus'))
->orderBy('r.expectedCompletionYear', 'ASC')
->orderBy('r.lastName', 'ASC');
$result = $query->execute()->fetchAll();
return $result;
}
function toCSV($objects) {
$delimiter = ", ";
if (count($objects) > 0) {
$output = fopen("php://output", "w");
$header = array_keys((array)$objects[0]);
fputcsv($output, $header, $delimiter);
foreach ($objects as $element) {
fputcsv($output, (array)$element, $delimiter);
}
}
}
function _re_dashboard_projects_toCSV($year) {
$output = array();
return theme('re_dashboard_csv', array('year' =>$year));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment