Merge pull request #260 from 3Dgoo/feature/before-update-cms-fields

Wrapping getCMSFields fields with beforeUpdateCMSFields
This commit is contained in:
Will Rossiter 2015-04-28 11:02:46 +12:00
commit 7badef5c35
2 changed files with 133 additions and 123 deletions

View File

@ -59,114 +59,118 @@ class UserDefinedForm extends Page {
/** /**
* @return FieldList * @return FieldList
*/ */
public function getCMSFields() { public function getCMSFields() {
// call updateCMSFields after userforms
SiteTree::disableCMSFieldsExtensions();
$fields = parent::getCMSFields();
SiteTree::enableCMSFieldsExtensions();
// define tabs
$fields->findOrMakeTab('Root.FormContent', _t('UserDefinedForm.FORM', 'Form'));
$fields->findOrMakeTab('Root.FormOptions', _t('UserDefinedForm.CONFIGURATION', 'Configuration'));
$fields->findOrMakeTab('Root.Submissions', _t('UserDefinedForm.SUBMISSIONS', 'Submissions'));
// field editor
$fields->addFieldToTab("Root.FormContent", new FieldEditor("Fields", 'Fields', "", $this ));
// text to show on complete $self = $this;
$onCompleteFieldSet = new CompositeField(
$label = new LabelField('OnCompleteMessageLabel',_t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')),
$editor = new HtmlEditorField( "OnCompleteMessage", "", _t('UserDefinedForm.ONCOMPLETEMESSAGE', $this->OnCompleteMessage))
);
$onCompleteFieldSet->addExtraClass('field');
$editor->setRows(3); $this->beforeUpdateCMSFields(function($fields) use ($self) {
$label->addExtraClass('left');
// define tabs
// Set the summary fields of UserDefinedForm_EmailRecipient dynamically via config system $fields->findOrMakeTab('Root.FormContent', _t('UserDefinedForm.FORM', 'Form'));
Config::inst()->update( $fields->findOrMakeTab('Root.FormOptions', _t('UserDefinedForm.CONFIGURATION', 'Configuration'));
'UserDefinedForm_EmailRecipient', $fields->findOrMakeTab('Root.Submissions', _t('UserDefinedForm.SUBMISSIONS', 'Submissions'));
'summary_fields',
array( // field editor
'EmailAddress' => _t('UserDefinedForm.EMAILADDRESS', 'Email'), $fields->addFieldToTab('Root.FormContent', new FieldEditor('Fields', 'Fields', '', $self ));
'EmailSubject' => _t('UserDefinedForm.EMAILSUBJECT', 'Subject'),
'EmailFrom' => _t('UserDefinedForm.EMAILFROM', 'From'), // text to show on complete
) $onCompleteFieldSet = new CompositeField(
); $label = new LabelField('OnCompleteMessageLabel',_t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')),
$editor = new HtmlEditorField( 'OnCompleteMessage', '', _t('UserDefinedForm.ONCOMPLETEMESSAGE', $self->OnCompleteMessage))
// who do we email on submission );
$emailRecipients = new GridField("EmailRecipients", _t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $this->EmailRecipients(), GridFieldConfig_RecordEditor::create(10));
$emailRecipients->getConfig()->getComponentByType('GridFieldAddNewButton')->setButtonName( $onCompleteFieldSet->addExtraClass('field');
_t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
); $editor->setRows(3);
$label->addExtraClass('left');
$fields->addFieldsToTab("Root.FormOptions", $onCompleteFieldSet);
$fields->addFieldToTab("Root.FormOptions", $emailRecipients); // Set the summary fields of UserDefinedForm_EmailRecipient dynamically via config system
$fields->addFieldsToTab("Root.FormOptions", $this->getFormOptions()); Config::inst()->update(
'UserDefinedForm_EmailRecipient',
'summary_fields',
// view the submissions array(
$submissions = new GridField( 'EmailAddress' => _t('UserDefinedForm.EMAILADDRESS', 'Email'),
"Submissions", 'EmailSubject' => _t('UserDefinedForm.EMAILSUBJECT', 'Subject'),
_t('UserDefinedForm.SUBMISSIONS', 'Submissions'), 'EmailFrom' => _t('UserDefinedForm.EMAILFROM', 'From'),
$this->Submissions()->sort('Created', 'DESC') )
); );
// make sure a numeric not a empty string is checked against this int column for SQL server // who do we email on submission
$parentID = (!empty($this->ID)) ? $this->ID : 0; $emailRecipients = new GridField('EmailRecipients', _t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $self->EmailRecipients(), GridFieldConfig_RecordEditor::create(10));
$emailRecipients->getConfig()->getComponentByType('GridFieldAddNewButton')->setButtonName(
// get a list of all field names and values used for print and export CSV views of the GridField below. _t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
$columnSQL = <<<SQL );
$fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet);
$fields->addFieldToTab('Root.FormOptions', $emailRecipients);
$fields->addFieldsToTab('Root.FormOptions', $self->getFormOptions());
// view the submissions
$submissions = new GridField(
'Submissions',
_t('UserDefinedForm.SUBMISSIONS', 'Submissions'),
$self->Submissions()->sort('Created', 'DESC')
);
// make sure a numeric not a empty string is checked against this int column for SQL server
$parentID = (!empty($self->ID)) ? $self->ID : 0;
// get a list of all field names and values used for print and export CSV views of the GridField below.
$columnSQL = <<<SQL
SELECT "Name", "Title" SELECT "Name", "Title"
FROM "SubmittedFormField" FROM "SubmittedFormField"
LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID" LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID"
WHERE "SubmittedForm"."ParentID" = '$parentID' WHERE "SubmittedForm"."ParentID" = '$parentID'
ORDER BY "Title" ASC ORDER BY "Title" ASC
SQL; SQL;
$columns = DB::query($columnSQL)->map(); $columns = DB::query($columnSQL)->map();
$config = new GridFieldConfig();
$config->addComponent(new GridFieldToolbarHeader());
$config->addComponent($sort = new GridFieldSortableHeader());
$config->addComponent($filter = new UserFormsGridFieldFilterHeader());
$config->addComponent(new GridFieldDataColumns());
$config->addComponent(new GridFieldEditButton());
$config->addComponent(new GridState_Component());
$config->addComponent(new GridFieldDeleteAction());
$config->addComponent(new GridFieldPageCount('toolbar-header-right'));
$config->addComponent($pagination = new GridFieldPaginator(25));
$config->addComponent(new GridFieldDetailForm());
$config->addComponent($export = new GridFieldExportButton());
$config->addComponent($print = new GridFieldPrintButton());
/**
* Support for {@link https://github.com/colymba/GridFieldBulkEditingTools}
*/
if(class_exists('GridFieldBulkManager')) {
$config->addComponent(new GridFieldBulkManager());
}
$sort->setThrowExceptionOnBadDataType(false);
$filter->setThrowExceptionOnBadDataType(false);
$pagination->setThrowExceptionOnBadDataType(false);
// attach every column to the print view form
$columns['Created'] = 'Created';
$filter->setColumns($columns);
// print configuration $config = new GridFieldConfig();
$print->setPrintHasHeader(true); $config->addComponent(new GridFieldToolbarHeader());
$print->setPrintColumns($columns); $config->addComponent($sort = new GridFieldSortableHeader());
$config->addComponent($filter = new UserFormsGridFieldFilterHeader());
// export configuration $config->addComponent(new GridFieldDataColumns());
$export->setCsvHasHeader(true); $config->addComponent(new GridFieldEditButton());
$export->setExportColumns($columns); $config->addComponent(new GridState_Component());
$config->addComponent(new GridFieldDeleteAction());
$submissions->setConfig($config); $config->addComponent(new GridFieldPageCount('toolbar-header-right'));
$fields->addFieldToTab("Root.Submissions", $submissions); $config->addComponent($pagination = new GridFieldPaginator(25));
$fields->addFieldToTab("Root.FormOptions", new CheckboxField('DisableSaveSubmissions',_t('UserDefinedForm.SAVESUBMISSIONS',"Disable Saving Submissions to Server"))); $config->addComponent(new GridFieldDetailForm());
$config->addComponent($export = new GridFieldExportButton());
$this->extend('updateCMSFields', $fields); $config->addComponent($print = new GridFieldPrintButton());
/**
* Support for {@link https://github.com/colymba/GridFieldBulkEditingTools}
*/
if(class_exists('GridFieldBulkManager')) {
$config->addComponent(new GridFieldBulkManager());
}
$sort->setThrowExceptionOnBadDataType(false);
$filter->setThrowExceptionOnBadDataType(false);
$pagination->setThrowExceptionOnBadDataType(false);
// attach every column to the print view form
$columns['Created'] = 'Created';
$filter->setColumns($columns);
// print configuration
$print->setPrintHasHeader(true);
$print->setPrintColumns($columns);
// export configuration
$export->setCsvHasHeader(true);
$export->setExportColumns($columns);
$submissions->setConfig($config);
$fields->addFieldToTab('Root.Submissions', $submissions);
$fields->addFieldToTab('Root.FormOptions', new CheckboxField('DisableSaveSubmissions', _t('UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server')));
});
$fields = parent::getCMSFields();
return $fields; return $fields;
} }
@ -405,7 +409,7 @@ SQL;
} }
$stageVersion = Versioned::get_versionnumber_by_stage('UserDefinedForm', 'Stage', $this->ID); $stageVersion = Versioned::get_versionnumber_by_stage('UserDefinedForm', 'Stage', $this->ID);
$liveVersion = Versioned::get_versionnumber_by_stage('UserDefinedForm', 'Live', $this->ID); $liveVersion = Versioned::get_versionnumber_by_stage('UserDefinedForm', 'Live', $this->ID);
$isModified = ($stageVersion && $stageVersion != $liveVersion); $isModified = ($stageVersion && $stageVersion != $liveVersion);
@ -667,7 +671,7 @@ class UserDefinedForm_Controller extends Page_Controller {
// watch out for multiselect options - radios and check boxes // watch out for multiselect options - radios and check boxes
if(is_a($formFieldWatch, 'EditableDropdown')) { if(is_a($formFieldWatch, 'EditableDropdown')) {
$fieldToWatch = "$(\"select[name='".$dependency['ConditionField']."']\")"; $fieldToWatch = "$(\"select[name='".$dependency['ConditionField']."']\")";
$fieldToWatchOnLoad = $fieldToWatch; $fieldToWatchOnLoad = $fieldToWatch;
} }
// watch out for checkboxs as the inputs don't have values but are 'checked // watch out for checkboxs as the inputs don't have values but are 'checked
@ -748,7 +752,7 @@ class UserDefinedForm_Controller extends Page_Controller {
case 'ValueGreaterThanEqual': case 'ValueGreaterThanEqual':
$expression = '$(this).val() >= parseFloat("'. $dependency['Value'] .'")'; $expression = '$(this).val() >= parseFloat("'. $dependency['Value'] .'")';
break; break;
default: // ==HasNotValue default: // ==HasNotValue
if ($checkboxField) { if ($checkboxField) {
$expression = '!$(this).prop("checked")'; $expression = '!$(this).prop("checked")';
@ -866,7 +870,7 @@ JS
* @return Redirection * @return Redirection
*/ */
public function process($data, $form) { public function process($data, $form) {
Session::set("FormInfo.{$form->FormName()}.data",$data); Session::set("FormInfo.{$form->FormName()}.data",$data);
Session::clear("FormInfo.{$form->FormName()}.errors"); Session::clear("FormInfo.{$form->FormName()}.errors");
foreach($this->Fields() as $field) { foreach($this->Fields() as $field) {
@ -952,7 +956,7 @@ JS
if($file->getAbsoluteSize() < 1024*1024*1){ if($file->getAbsoluteSize() < 1024*1024*1){
$attachments[] = $file; $attachments[] = $file;
} }
} }
} }
} }
@ -1013,7 +1017,7 @@ JS
$submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name); $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);
if($submittedFormField && is_string($submittedFormField->Value)) { if($submittedFormField && is_string($submittedFormField->Value)) {
$email->setTo($submittedFormField->Value); $email->setTo($submittedFormField->Value);
} }
} }
@ -1040,7 +1044,7 @@ JS
$email->sendPlain(); $email->sendPlain();
} }
else { else {
$email->send(); $email->send();
} }
} }
} }
@ -1265,7 +1269,7 @@ class UserDefinedForm_SubmittedFormEmail extends Email {
parent::__construct($submittedFields = null); parent::__construct($submittedFields = null);
} }
/** /**
* Set the "Reply-To" header with an email address rather than append as * Set the "Reply-To" header with an email address rather than append as
* {@link Email::replyTo} does. * {@link Email::replyTo} does.
* *

View File

@ -50,23 +50,29 @@ class SubmittedForm extends DataObject {
* @return FieldList * @return FieldList
*/ */
public function getCMSFields() { public function getCMSFields() {
$self = $this;
$this->beforeUpdateCMSFields(function($fields) use ($self) {
$fields->removeByName('Values');
$fields->dataFieldByName('SubmittedByID')->setDisabled(true);
$values = new GridField(
'Values',
'SubmittedFormField',
$self->Values()->sort('Created', 'ASC')
);
$config = new GridFieldConfig();
$config->addComponent(new GridFieldDataColumns());
$config->addComponent(new GridFieldExportButton());
$config->addComponent(new GridFieldPrintButton());
$values->setConfig($config);
$fields->addFieldToTab('Root.Main', $values);
});
$fields = parent::getCMSFields(); $fields = parent::getCMSFields();
$fields->removeByName('Values');
$fields->dataFieldByName('SubmittedByID')->setDisabled(true);
$values = new GridField(
"Values",
"SubmittedFormField",
$this->Values()->sort('Created', 'ASC')
);
$config = new GridFieldConfig();
$config->addComponent(new GridFieldDataColumns());
$config->addComponent(new GridFieldExportButton());
$config->addComponent(new GridFieldPrintButton());
$values->setConfig($config);
$fields->addFieldToTab('Root.Main', $values);
return $fields; return $fields;
} }