diff --git a/code/formfields/SubmittedFormReportField.php b/code/formfields/SubmittedFormReportField.php index 9016d9b..bd5ad52 100755 --- a/code/formfields/SubmittedFormReportField.php +++ b/code/formfields/SubmittedFormReportField.php @@ -12,35 +12,41 @@ class SubmittedFormReportField extends FormField { Requirements::javascript("userforms/javascript/UserForm.js"); return $this->renderWith("SubmittedFormReportField"); } - + /** * Return the submissions from the site * - * @return ComponentSet + * @return PaginatedList */ - public function Submissions() { - $pageStart = isset($_REQUEST['start']) && is_numeric($_REQUEST['start']) ? $_REQUEST['start'] : 0; - $pageLength = 10; - - $items = $this->form->getRecord()->getComponents('Submissions', null, "\"Created\" DESC")->limit($pageStart,$pageLength); - $formId = $this->form->getRecord()->ID; - - foreach(DB::query("SELECT COUNT(*) AS \"CountRows\" FROM \"SubmittedForm\" WHERE \"ParentID\" = $formId") as $r) $totalCount = $r['CountRows']; + public function getSubmissions($start = 0) { + $record = $this->form->getRecord(); + $submissions = $record->getComponents('Submissions', null, "\"Created\" DESC"); - //$items->setPageLimits($pageStart, $pageLength, $totalCount); - $items->NextStart = $pageStart + $pageLength; - $items->PrevStart = $pageStart - $pageLength; - $items->Start = $pageStart; - $items->StartPlusOffset = $pageStart+$pageLength; - $items->TotalCount = $totalCount; - - return $items; + $query = DB::query(sprintf(" + SELECT COUNT(*) AS \"CountRows\" + FROM \"SubmittedForm\" + WHERE \"ParentID\" = '%d'", $record->ID + )); + + foreach($query as $r) $totalCount = $r['CountRows']; + + $list = new PaginatedList($submissions); + $list->setPageStart($start); + $list->setPageLength(10); + $list->setTotalItems($totalCount); + + return $list; } - public function getSubmissions() { - return $this->customise(array( - 'Submissions' => $this->Submissions() - ))->renderWith(array('SubmittedFormReportField')); + /** + * @return string + */ + public function getMoreSubmissions() { + $start = ($start = $this->request->getVar('start')) ? (int) $start : 0; + + return $this->customise(new ArrayData(array( + 'Submissions' => $this->getSubmissions($start) + )))->renderWith(array('SubmittedFormReportField')); } /** diff --git a/code/model/UserDefinedForm.php b/code/model/UserDefinedForm.php index 6b0a736..ee1f4fd 100755 --- a/code/model/UserDefinedForm.php +++ b/code/model/UserDefinedForm.php @@ -331,7 +331,7 @@ class UserDefinedForm_Controller extends Page_Controller { */ public function Form() { $fields = $this->getFormFields(); - if(!$fields) return false; + if(!$fields || !$fields->exists()) return false; $actions = $this->getFormActions(); diff --git a/templates/SubmittedFormReportField.ss b/templates/SubmittedFormReportField.ss index 331ac39..77465c7 100644 --- a/templates/SubmittedFormReportField.ss +++ b/templates/SubmittedFormReportField.ss @@ -6,36 +6,36 @@
$Title | $FormattedValue |
style="display: none"<% end_if %>><% _t('NOSUBMISSIONS', 'No Submissions') %>
<% end_if %> - -style="display: none"<% end_if %>><% _t('NOSUBMISSIONS', 'No Submissions') %>
diff --git a/tests/SubmittedFormTest.php b/tests/SubmittedFormTest.php index 8c874c5..008d370 100644 --- a/tests/SubmittedFormTest.php +++ b/tests/SubmittedFormTest.php @@ -1,5 +1,8 @@ field->Submissions(); - - // test with 11 submissions. Should be over 2 pages. 10 per page. - // @todo add tests to ensure the order - $this->assertEquals($submissions->Count(), 10); + $submissions = $this->field->getSubmissions(); + $this->assertEquals($submissions->TotalPages(), 2); - $this->assertEquals($submissions->TotalItems(), 11); + $this->assertEquals($submissions->getTotalItems(), 11); } - function testGetSubmissionns() { - $template = $this->field->getSubmissions(); - + function testGetMoreSubmissions() { + $template = $this->field->getMoreSubmissions(); $parser = new CSSContentParser($template); - // check to ensure that the pagination exists $pagination = $parser->getBySelector('.userforms-submissions-pagination'); - + $this->assertEquals(str_replace("\n", ' ',(string) $pagination[0]->span), "Viewing rows 0 - 10 of 11 rows"); $this->assertEquals(str_replace("\n", ' ',(string) $pagination[0]->a), "Next page"); diff --git a/tests/UserDefinedFormTest.php b/tests/UserDefinedFormTest.php index bc3114b..e70f46b 100644 --- a/tests/UserDefinedFormTest.php +++ b/tests/UserDefinedFormTest.php @@ -10,8 +10,11 @@ class UserDefinedFormTest extends FunctionalTest { function testRollbackToVersion() { - // @todo rolling back functionality (eg fields) is not supported yet + $this->markTestSkipped( + 'UserDefinedForm::rollback() has not been implemented completely' + ); + // @todo $this->logInWithPermission('ADMIN'); $form = $this->objFromFixture('UserDefinedForm', 'basic-form-page'); @@ -125,7 +128,7 @@ class UserDefinedFormTest extends FunctionalTest { // should not have published the dropdown $liveDropdown = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $dropdown->ID"); - $this->assertFalse($liveDropdown); + $this->assertNull($liveDropdown); // when publishing it should have added it $form->doPublish(); @@ -164,7 +167,7 @@ class UserDefinedFormTest extends FunctionalTest { // unpublish $form->doUnpublish(); - $this->assertFalse(Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID")); + $this->assertNull(Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID")); $this->assertEquals(DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value(), 0); } @@ -172,35 +175,27 @@ class UserDefinedFormTest extends FunctionalTest { function testDoRevertToLive() { $this->logInWithPermission('ADMIN'); $form = $this->objFromFixture('UserDefinedForm', 'basic-form-page'); - $form->SubmitButtonText = 'Button Text'; - $form->doPublish(); - $text = $form->Fields()->First(); - - $form->SubmitButtonText = 'Edited Button Text'; - $form->write(); + $field = $form->Fields()->First(); - $text->Title = 'Edited title'; - $text->write(); + $field->Title = 'Title'; + $field->write(); + + $form->doPublish(); + + $field->Title = 'Edited title'; + $field->write(); // check that the published version is not updated - $liveText = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $text->ID"); - - $revertTo = $liveText->Title; - - $this->assertFalse($revertTo == $text->Title); + $live = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $field->ID"); + $this->assertEquals('Title', $live->Title); // revert back to the live data $form->doRevertToLive(); + $form->flushCache(); - $check = Versioned::get_one_by_stage("EditableFormField", "Stage", "\"EditableFormField\".\"ID\" = $text->ID"); + $check = Versioned::get_one_by_stage("EditableFormField", "Stage", "\"EditableFormField\".\"ID\" = $field->ID"); - $this->assertEquals($check->Title, $revertTo); - - // check the edited buttoned - $liveForm = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID"); - $revertedForm = Versioned::get_one_by_stage("UserDefinedForm", "Stage", "\"UserDefinedForm\".\"ID\" = $form->ID"); - - $this->assertEquals($liveForm->SubmitButtonText, $revertedForm->SubmitButtonText); + $this->assertEquals('Title', $check->Title); } function testDuplicatingForm() {