From 74442bfd895e80a642fb7092f2e6e8542aa842d8 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 14 Apr 2009 22:59:46 +0000 Subject: [PATCH] FEATURE: added ability to have a clear form button. MINOR: setting up functions for deleting submissions --- code/UserDefinedForm.php | 106 +++++++++++++++++------ code/editor/FieldEditor.php | 2 + code/editor/SubmittedForm.php | 13 ++- code/editor/SubmittedFormField.php | 1 + code/editor/SubmittedFormReportField.php | 23 ++++- 5 files changed, 115 insertions(+), 30 deletions(-) diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index 0e7d0b5..4300b42 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -41,6 +41,7 @@ class UserDefinedForm extends Page { "EmailOnSubmitSubject" => "Varchar(200)", "SubmitButtonText" => "Varchar", "OnCompleteMessage" => "HTMLText", + "ShowClearButton" => "Boolean" ); /** @@ -63,8 +64,6 @@ class UserDefinedForm extends Page { "EmailRecipients" => "UserDefinedForm_EmailRecipient" ); - protected $fields; - /** * Setup the CMS Fields for the User Defined Form * @@ -74,11 +73,36 @@ class UserDefinedForm extends Page { $fields = parent::getCMSFields(); // field editor - $fields->addFieldToTab("Root."._t('UserDefinedForm.FORM', 'Form'), new FieldEditor("Fields", 'Fields', "", $this )); + $fields->addFieldToTab("Root.Content."._t('UserDefinedForm.FORM', 'Form'), new FieldEditor("Fields", 'Fields', "", $this )); // view the submissions - $fields->addFieldToTab("Root."._t('UserDefinedForm.SUBMISSIONS','Submissions'), new SubmittedFormReportField( "Reports", _t('UserDefinedForm.RECEIVED', 'Received Submissions'), "", $this ) ); + $fields->addFieldToTab("Root.Content."._t('UserDefinedForm.SUBMISSIONS','Submissions'), new SubmittedFormReportField( "Reports", _t('UserDefinedForm.RECEIVED', 'Received Submissions'), "", $this ) ); + /* + Trying to get submissions working as a tablelistfield. close, but not close enough + $query = new SQLQuery(); + $query->from("SubmittedForm"); + $query->where("SubmittedForm.ParentID = '$this->ID'"); + $query->orderby("SubmittedForm.Created"); + $query->limit("20"); + $query->leftJoin("SubmittedFormField", "SubmittedFormField.ParentID = SubmittedForm.ID"); + + $myTableListField = new TableListField( + 'Submissions', + 'SubmittedForm' + ); + $myTableListField->setCustomQuery($query); + $fieldValues = array( + "Title" => "Title", + "Value" => "$Value", + "Name" => "$Name", + "ID" => "$ID", + "ParentID" => "$ParentID" + ); + $myTableListField->setFieldList($fieldValues); + + $fields->addFieldToTab("Root.Test", $myTableListField); + */ // who do we email on submission $emailRecipients = new HasManyComplexTableField($this, 'EmailRecipients', @@ -91,7 +115,7 @@ class UserDefinedForm extends Page { 'getCMSFields_forPopup' ); $emailRecipients->setAddTitle(_t('UserDefinedForm.AEMAILRECIPIENT', 'A Email Recipient')); - $fields->addFieldToTab("Root."._t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $emailRecipients); + $fields->addFieldToTab("Root.Content."._t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $emailRecipients); // text to show on complete $onCompleteFieldSet = new FieldSet( @@ -186,22 +210,35 @@ class UserDefinedForm extends Page { function ReportFilterForm() { return new SubmittedFormReportField_FilterForm( $this, 'ReportFilterForm' ); } - - function delete() { - // remove all the fields associated with this page - foreach( $this->Fields() as $field ) - $field->delete(); - - parent::delete(); - } + + /** + * Called on before delete remove all the fields from the database + */ + public function delete() { + foreach($this->Fields() as $field) { + $field->delete(); + } + parent::delete(); + } - public function customFormActions( $isReadonly = false ) { - return new FieldSet( new TextField( "SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText ) ); + /** + * Custom Form Actions for the form + * + * @param bool Is the Form readonly + * @return FieldSet + */ + public function customFormActions($isReadonly = false) { + return new FieldSet( + new TextField("SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText), + new CheckboxField("ShowClearButton", _t('UserDefinedForm.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton) + ); } /** * Duplicate this UserDefinedForm page, and its form fields. * Submissions, on the other hand, won't be duplicated. + * + * @return Page */ public function duplicate() { $page = parent::duplicate(); @@ -224,9 +261,8 @@ class UserDefinedForm extends Page { class UserDefinedForm_Controller extends Page_Controller { function init() { - Requirements::javascript(THIRDPARTY_DIR . 'jsparty/prototype-safe.js'); - Requirements::javascript(THIRDPARTY_DIR . 'jsparty/behaviour.js'); - + Requirements::javascript(THIRDPARTY_DIR . '/prototype.js'); + Requirements::javascript(THIRDPARTY_DIR . '/behaviour.js'); parent::init(); } @@ -323,6 +359,10 @@ class UserDefinedForm_Controller extends Page_Controller { } } + function ReportFilterForm() { + return new SubmittedFormReportField_FilterForm( $this, 'ReportFilterForm' ); + } + /** * User Defined Form. Feature of the user defined form is if you want the * form to appear in a custom location on the page you can use $UserDefinedForm @@ -353,19 +393,30 @@ class UserDefinedForm_Controller extends Page_Controller { $fields->push( new HiddenField( "Referrer", "", $referer ) ); // Build actions - $actions = new FieldSet( - new FormAction( "process", $this->SubmitButtonText ) + $actions = new FieldSet( + new FormAction("process", $this->SubmitButtonText) ); - // set the name of the form - $form = new Form( $this, "Form", $fields, $actions, new RequiredFields( $required ) ); + // Do we want to add a clear form. Should do this via js + if($this->ShowClearButton) { + $actions->push(new ResetFormAction("clearForm")); + } + + $form = new Form( $this, "Form", $fields, $actions, new RequiredFields($required)); $form->loadDataFrom($this->failover); return $form; } - - function ReportFilterForm() { - return new SubmittedFormReportField_FilterForm( $this, 'ReportFilterForm' ); + /** + * Clear a form action. This is the fall back to if it doesn't work + * with the javascript + * + * @param Array data + * @param Form The form to clear + */ + public function clearForm($data, $form) { + Session::clear('FormInfo.Form'); + return Director::redirectBack(); } /** @@ -521,6 +572,11 @@ class UserDefinedForm_Controller extends Page_Controller { return $templateData; } + function deletesubmissions() { + // delete all the submissions + + return true; + } } /** diff --git a/code/editor/FieldEditor.php b/code/editor/FieldEditor.php index b7f1d30..4cb8577 100755 --- a/code/editor/FieldEditor.php +++ b/code/editor/FieldEditor.php @@ -83,9 +83,11 @@ class FieldEditor extends FormField { $name = $this->name; $fieldSet = $record->$name(); + // @todo shouldn't we deal with customFormActions on that object? $record->EmailTo = $_REQUEST[$name]['EmailTo']; $record->EmailOnSubmit = isset( $_REQUEST[$name]['EmailOnSubmit'] ) ? "1" : "0"; $record->SubmitButtonText = $_REQUEST[$name]['SubmitButtonText']; + $record->ShowClearButton = isset($_REQUEST[$name]['ShowClearButton']) ? "1" : "0"; // store the field IDs and delete the missing fields // alternatively, we could delete all the fields and re add them diff --git a/code/editor/SubmittedForm.php b/code/editor/SubmittedForm.php index 1c6653d..6fd19e1 100755 --- a/code/editor/SubmittedForm.php +++ b/code/editor/SubmittedForm.php @@ -9,13 +9,18 @@ class SubmittedForm extends DataObject { "Parent" => "UserDefinedForm", ); - static $db = array( - "Recipient" => "Varchar(255)" - ); - static $has_many = array( "FieldValues" => "SubmittedFormField" ); + + /** + * Return the Link to DeleteLink + * + * @return String + */ + public function DeleteLink() { + return $this->Parent()->Link().'deletesubmission/'.$this->ID; + } function SubmitTime() { return $this->Created; diff --git a/code/editor/SubmittedFormField.php b/code/editor/SubmittedFormField.php index 8b0cb57..97aa313 100755 --- a/code/editor/SubmittedFormField.php +++ b/code/editor/SubmittedFormField.php @@ -14,5 +14,6 @@ class SubmittedFormField extends DataObject { static $has_one = array( "Parent" => "SubmittedForm" ); + } ?> \ No newline at end of file diff --git a/code/editor/SubmittedFormReportField.php b/code/editor/SubmittedFormReportField.php index 53c735a..f49ca44 100755 --- a/code/editor/SubmittedFormReportField.php +++ b/code/editor/SubmittedFormReportField.php @@ -1,7 +1,8 @@ renderWith("SubmittedFormReportField"); } + /** + * Return the submissions from the site + * + * @return ComponentSet + */ function Submissions() { return $this->form->getRecord()->Submissions(); } + /** + * Link to the export function of the controller + * + * @return String + */ function ExportLink() { if($this->Submissions() && $this->Submissions()->Count() > 0) { return $this->form->getRecord()->Link() . 'export/' . $this->form->getRecord()->ID; } } + /** + * Link to the delete the submission + * + * @return String + */ + function DeleteLink() { + if($this->Submissions() && $this->Submissions()->Count() > 0) { + return $this->form->getRecord()->Link() . 'deletesubmissions/' . $this->form->getRecord()->ID; + } + } } ?> \ No newline at end of file