From 74d69a30fe48ed9e895d05f6b18aa933ae8711e8 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 29 Sep 2008 05:33:43 +0000 Subject: [PATCH] ENHANCEMENT Added CSV export functionality to the userforms module --- code/UserDefinedForm.php | 99 ++++++++++++++++++++++-- code/editor/SubmittedFormReportField.php | 15 ++-- templates/SubmittedFormReportField.ss | 5 ++ 3 files changed, 105 insertions(+), 14 deletions(-) diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index cd3dfc8..decf565 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -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( @@ -137,7 +133,7 @@ class UserDefinedForm extends Page { public function customFormActions( $isReadonly = false ) { return new FieldSet( new TextField( "SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText ) ); } - + /** * Duplicate this UserDefinedForm page, and its form fields. * Submissions, on the other hand, won't be duplicated. @@ -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;$iform->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; + } + } + } ?> \ No newline at end of file diff --git a/templates/SubmittedFormReportField.ss b/templates/SubmittedFormReportField.ss index 9bcde60..45252b9 100644 --- a/templates/SubmittedFormReportField.ss +++ b/templates/SubmittedFormReportField.ss @@ -2,6 +2,11 @@ $FilterForm
+ +<% if ExportLink %> + <% _t('EXPORTSUBMISSIONS', 'Export submissions to CSV') %> +<% end_if %> + <% control Submissions %>