diff --git a/README b/README index 133def2..0bafa3d 100644 --- a/README +++ b/README @@ -9,6 +9,7 @@ Will Rossiter (Nickname: wrossiter, willr) Requirements --------------------- SilverStripe 2.4.0+ +PHP 5 >= 5.1.0 (fputcsv) Documentation ----------------------------------------------- diff --git a/code/submissions/SubmittedFormReportField.php b/code/submissions/SubmittedFormReportField.php index 73b756b..afd206a 100755 --- a/code/submissions/SubmittedFormReportField.php +++ b/code/submissions/SubmittedFormReportField.php @@ -84,9 +84,25 @@ class SubmittedFormReportField extends FormField { if($udf) { $submissions = $udf->Submissions("", "\"ID\""); + + // Collect unique columns for use in the CSV. + // Do it separately as we need a fixed number of columns for the file. + // Include all fields that have ever existed in this form. + // Preserve the ordering: the most recent form setup should be dominant. + $inClause = array(); + foreach($submissions as $submission) { + $inClause[] = $submission->ID; + } + $csvHeaders = DB::query("SELECT DISTINCT \"Name\" , \"Title\" FROM + (( + SELECT \"Name\" , \"Title\" FROM \"SubmittedFormField\" + LEFT JOIN \"SubmittedForm\" ON \"SubmittedForm\".\"ID\" = \"SubmittedFormField\".\"ParentID\" IN (" . implode(',', $inClause) . ") + ORDER BY \"SubmittedFormField\".\"ParentID\" DESC, \"SubmittedFormField\".\"ID\" + ) AS \"tmp\")"); + if ($csvHeaders) $csvHeaders = $csvHeaders->map(); + if($submissions && $submissions->exists()) { $data = array(); - $csvHeaders = array(); // Create CSV rows out of submissions. Fields on those submissions will become columns. foreach($submissions as $submission) { @@ -94,14 +110,7 @@ class SubmittedFormReportField extends FormField { $row = array(); foreach($fields as $field) { - // Collect the data $row[$field->Name] = $field->Value; - - // Collect unique columns for use in the CSV - we must have a fixed number of columns, but we want to - // include all fields that have ever existed in this form. - // NOTE: even if the field was used long time ago and only once, it will be included, so expect to see - // some empty columns, especially on heavily-edited UDFs. - $csvHeaders[$field->Name] = $field->Title; } $row['Submitted'] = $submission->Created; @@ -122,7 +131,7 @@ class SubmittedFormReportField extends FormField { // Encode the row $fp = fopen('php://memory', 'r+'); - fputcsv($fp, $csvRowItems, ',', '"'); + fputcsv($fp, $csvRowItems); rewind($fp); $csvData .= fgets($fp); fclose($fp); diff --git a/tests/SubmittedFormTest.php b/tests/SubmittedFormTest.php index f987bba..52e7c37 100644 --- a/tests/SubmittedFormTest.php +++ b/tests/SubmittedFormTest.php @@ -54,22 +54,27 @@ class SubmittedFormTest extends FunctionalTest { $data = array(); foreach($exportLines as $line) { - $data[] = str_getcsv($line); + $fp = fopen('php://memory', 'w+'); + fputs($fp, $line); + rewind($fp); + $data[] = fgetcsv($fp); } - - // check the headers are fine and include legacy field + + // check the headers are fine and include every legacy field. They should also be ordered + // according to the latest form layout. $this->assertEquals($data[0], array( - 'Submitted Title','Submitted Title 2','Submitted' + 'First', 'Submitted Title 2', 'Submitted Title', 'Field 2', 'Field 1', 'File', 'Submitted' )); - + // check the number of records in the export $this->assertEquals(count($data), 12); - $this->assertEquals($data[1][0], 'Value 1'); - $this->assertEquals($data[1][1], ""); - - $this->assertEquals($data[2][0], ""); + $this->assertEquals($data[1][1], 'quote " and comma , test'); + $this->assertEquals($data[1][2], 'Value 1'); $this->assertEquals($data[2][1], 'Value 2'); + + $this->assertEquals($data[11][0], 'First'); + $this->assertEquals($data[11][1], 'Second'); } function testdeletesubmission() { diff --git a/tests/SubmittedFormTest.yml b/tests/SubmittedFormTest.yml index 7aefb01..f7cd529 100644 --- a/tests/SubmittedFormTest.yml +++ b/tests/SubmittedFormTest.yml @@ -60,17 +60,35 @@ SubmittedFormField: Testing until I cannot I love my testing - long-submitted-1: + long-submitted-1a: Name: Submitted Name Title: Submitted Title Parent: =>SubmittedForm.long-1 Value: Value 1 + + long-submitted-1b: + Name: Submitted Name 2 + Title: Submitted Title 2 + Parent: =>SubmittedForm.long-1 + Value: 'quote " and comma , test' long-submitted-2: Parent: =>SubmittedForm.long-2 Name: Submitted Name 2 Title: Submitted Title 2 Value: Value 2 + + long-submitted-11a: + Parent: =>SubmittedForm.long-11 + Name: First + Title: First + Value: First + + long-submitted-11b: + Parent: =>SubmittedForm.long-11 + Name: Submitted Name 2 + Title: Submitted Title 2 + Value: Second SubmittedFileField: submitted-file-1: @@ -79,4 +97,4 @@ SubmittedFileField: Parent: =>SubmittedForm.submitted-form-1 UploadedFile: =>File.uploaded-file - \ No newline at end of file +