ENHANCEMENT Added CSV export functionality to the userforms module

This commit is contained in:
Sean Harvey 2008-09-29 05:33:43 +00:00
parent 1bb12dce72
commit 74d69a30fe
3 changed files with 105 additions and 14 deletions

View File

@ -5,15 +5,11 @@
* @subpackage pagetypes
*/
class UserDefinedForm extends Page {
static $add_action = "a contact form";
static $icon = "cms/images/treeicons/task";
// a list of groups that are permitted to create pages of this type.
/*static $can_create = array(
'Administrators'
);*/
static $need_permission = 'ADMIN';
static $db = array(
@ -167,6 +163,97 @@ class UserDefinedForm_Controller extends Page_Controller {
parent::init();
}
/**
* Export each of the form submissions for this UserDefinedForm
* instance into a CSV file.
*
* In order to run this export function, the user must be
* able to edit the page, so we check canEdit()
*/
function export() {
if(!$this->canEdit()) return false;
$now = Date("Y-m-d_h.i.s");
$fileName = "export-$now.csv";
$separator = ",";
// Get the UserDefinedForm to export data from the URL
$SQL_ID = Convert::raw2sql(Director::urlParam('ID'));
if($SQL_ID) {
$udf = DataObject::get_by_id("UserDefinedForm", $SQL_ID);
if($udf) {
$submissions = $udf->Submissions();
if($submissions && $submissions->Count() > 0) {
// Get all the submission IDs (so we know what names/titles to get - helps for sites with many UDF's)
$inClause = array();
foreach($submissions as $submission) {
$inClause[] = $submission->ID;
}
// Get the CSV header rows from the database
$tmp = DB::query("SELECT DISTINCT Name, Title FROM SubmittedFormField LEFT JOIN SubmittedForm ON SubmittedForm.ID = SubmittedFormField.ParentID WHERE SubmittedFormField.ParentID IN (" . implode(',', $inClause) . ")");
// Sort the Names and Titles from the database query into separate keyed arrays
foreach($tmp as $array) {
$csvHeaderNames[] = $array['Name'];
$csvHeaderTitle[] = $array['Title'];
}
// For every submission...
$i = 0;
foreach($submissions as $submission) {
// Get the rows for this submission (One row = one form field)
$dataRow = $submission->FieldValues();
$rows[$i] = array();
// For every row/field, get all the columns
foreach($dataRow as $column) {
// If the Name of this field is in the $csvHeaderNames array, get an array of all the places it exists
if($index = array_keys($csvHeaderNames, $column->Name)) {
if(is_array($index)) {
// Set the final output array for each index that we want to insert this value into
foreach($index as $idx) {
$rows[$i][$idx] = $column->Value;
}
}
}
}
$i++;
}
// CSV header row
$csvData = '"' . implode('","', $csvHeaderTitle) . '"' . "\n";
// For every row of data (one form submission = one row)
foreach($rows as $row) {
// Loop over all the names we can use
for($i=0;$i<count($csvHeaderNames);$i++) {
if(!$row[$i]) $csvData .= '"",'; // If there is no data for this column, output it as blank instead
else $csvData .= '"'.$row[$i].'",'; // Otherwise, output the value for this column
}
// Start a new row for each submission
$csvData .= "\n";
}
} else {
user_error("No submissions to export.", E_USER_ERROR);
}
HTTP::sendFileToBrowser($csvData, $fileName);
} else {
user_error("'$SQL_ID' is a valid type, but we can't find a UserDefinedForm in the database that matches the ID.", E_USER_ERROR);
}
} else {
user_error("'$SQL_ID' is not a valid UserDefinedForm ID.", E_USER_ERROR);
}
}
function Form() {
// Build fields
$fields = new FieldSet();

View File

@ -5,14 +5,6 @@
*/
class SubmittedFormReportField extends FormField {
/**
* Displays the form (without defaults) submitted as it appears on the front of the site
* Users will use this instance of the form to filter results
*/
function Form() {
/*return $this->form->Form();*/
}
function Field() {
Requirements::css(SAPPHIRE_DIR . "/css/SubmittedFormReportField.css");
@ -22,5 +14,12 @@ class SubmittedFormReportField extends FormField {
function Submissions() {
return $this->form->getRecord()->Submissions();
}
function ExportLink() {
if($this->Submissions() && $this->Submissions()->Count() > 0) {
return $this->form->getRecord()->Link() . 'export/' . $this->form->getRecord()->ID;
}
}
}
?>

View File

@ -2,6 +2,11 @@
$FilterForm
</div>
<div class="reports" id="FormSubmissions">
<% if ExportLink %>
<a href="$ExportLink" title="Export CSV file"><strong><% _t('EXPORTSUBMISSIONS', 'Export submissions to CSV') %></strong></a>
<% end_if %>
<% control Submissions %>
<div class="report">
<span class="submitted"><% _t('SUBMITTED', 'Submitted at') %> $Created.Nice <% if Recipient %>to $Recipient<% end_if %></span>