FIX: save a null value instead of upload data. (Fixes #87)

This commit is contained in:
Will Rossiter 2013-01-29 21:44:00 +13:00
parent 22c4081e89
commit 6f671d304d
2 changed files with 94 additions and 48 deletions

View File

@ -40,7 +40,7 @@ class UserDefinedForm extends Page {
); );
/** /**
* @var Array * @var array
*/ */
public static $has_many = array( public static $has_many = array(
"Fields" => "EditableFormField", "Fields" => "EditableFormField",
@ -49,9 +49,7 @@ class UserDefinedForm extends Page {
); );
/** /**
* Setup the CMS Fields for the User Defined Form * @return FieldList
*
* @return FieldSet
*/ */
public function getCMSFields() { public function getCMSFields() {
$fields = parent::getCMSFields(); $fields = parent::getCMSFields();
@ -99,7 +97,7 @@ class UserDefinedForm extends Page {
/** /**
* When publishing copy the editable form fields to the live database * When publishing copy the editable form fields to the live database
* Not going to version emails and submissions as they are likely to * Not going to version emails and submissions as they are likely to
* persist over multiple versions * persist over multiple versions.
* *
* @return void * @return void
*/ */
@ -235,7 +233,7 @@ class UserDefinedForm extends Page {
* *
* @return Page * @return Page
*/ */
public function duplicate($doWrite = true) { public function duplicate($doWrite = true) {
$page = parent::duplicate($doWrite); $page = parent::duplicate($doWrite);
// the form fields // the form fields
@ -286,18 +284,21 @@ class UserDefinedForm extends Page {
*/ */
public function getIsModifiedOnStage() { public function getIsModifiedOnStage() {
// new unsaved pages could be never be published // new unsaved pages could be never be published
if($this->isNew()) return false; if($this->isNew()) {
return false;
}
$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);
if (!$isModified) {
if(!$isModified) {
if($this->Fields()) { if($this->Fields()) {
foreach($this->Fields() as $field) { foreach($this->Fields() as $field) {
if ($field->getIsModifiedOnStage()) { if($field->getIsModifiedOnStage()) {
$isModified = true; $isModified = true;
break; break;
} }
} }
} }
@ -330,9 +331,9 @@ class UserDefinedForm_Controller extends Page_Controller {
/** /**
* Using $UserDefinedForm in the Content area of the page shows * Using $UserDefinedForm in the Content area of the page shows
* where the form should be rendered into. If it does not exist * where the form should be rendered into. If it does not exist
* then default back to $Form * then default back to $Form.
* *
* @return Array * @return array
*/ */
public function index() { public function index() {
if($this->Content && $form = $this->Form()) { if($this->Content && $form = $this->Form()) {
@ -354,14 +355,16 @@ class UserDefinedForm_Controller extends Page_Controller {
/** /**
* Keep the session alive for the user. * Keep the session alive for the user.
*
* @return int
*/ */
function ping() { public function ping() {
return 1; return 1;
} }
/** /**
* Get the form for the page. Form can be modified by calling {@link updateForm()} * Get the form for the page. Form can be modified by calling {@link updateForm()}
* on a UserDefinedForm extension * on a UserDefinedForm extension.
* *
* @return Form|false * @return Form|false
*/ */
@ -392,9 +395,9 @@ class UserDefinedForm_Controller extends Page_Controller {
/** /**
* Get the form fields for the form on this page. Can modify this FieldSet * Get the form fields for the form on this page. Can modify this FieldSet
* by using {@link updateFormFields()} on an {@link Extension} subclass which * by using {@link updateFormFields()} on an {@link Extension} subclass which
* is applied to this controller * is applied to this controller.
* *
* @return FieldSet * @return FieldList
*/ */
public function getFormFields() { public function getFormFields() {
$fields = new FieldList(); $fields = new FieldList();
@ -451,7 +454,7 @@ class UserDefinedForm_Controller extends Page_Controller {
* *
* @todo Make form actions editable via their own field editor. * @todo Make form actions editable via their own field editor.
* *
* @return FieldSet * @return FieldList
*/ */
public function getFormActions() { public function getFormActions() {
$submitText = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit'); $submitText = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit');
@ -529,7 +532,7 @@ JS
/** /**
* Generate the javascript for the conditional field show / hiding logic. * Generate the javascript for the conditional field show / hiding logic.
* Allows complex rules to be created *
* @return void * @return void
*/ */
public function generateConditionalJavascript() { public function generateConditionalJavascript() {
@ -703,21 +706,24 @@ JS
* @return JSON * @return JSON
*/ */
public function array2json($array) { public function array2json($array) {
foreach($array as $key => $value) foreach($array as $key => $value) {
if(is_array( $value )) { if(is_array( $value )) {
$result[] = "$key:" . $this->array2json($value); $result[] = "$key:" . $this->array2json($value);
} else { } else {
$value = (is_bool($value)) ? $value : "\"$value\""; $value = (is_bool($value)) ? $value : "\"$value\"";
$result[] = "$key:$value"; $result[] = "$key:$value";
} }
}
return (isset($result)) ? "{\n".implode( ', ', $result ) ."\n}\n": '{}'; return (isset($result)) ? "{\n".implode( ', ', $result ) ."\n}\n": '{}';
} }
/** /**
* Process the form that is submitted through the site * Process the form that is submitted through the site
* *
* @param Array Data * @param array $data
* @param Form Form * @param Form $form
*
* @return Redirection * @return Redirection
*/ */
public function process($data, $form) { public function process($data, $form) {
@ -739,6 +745,7 @@ JS
if(Session::get("FormInfo.{$form->FormName()}.errors")){ if(Session::get("FormInfo.{$form->FormName()}.errors")){
Controller::curr()->redirectBack(); Controller::curr()->redirectBack();
return; return;
} }
@ -747,7 +754,9 @@ JS
$submittedForm->ParentID = $this->ID; $submittedForm->ParentID = $this->ID;
// if saving is not disabled save now to generate the ID // if saving is not disabled save now to generate the ID
if(!$this->DisableSaveSubmissions) $submittedForm->write(); if(!$this->DisableSaveSubmissions) {
$submittedForm->write();
}
$values = array(); $values = array();
$attachments = array(); $attachments = array();
@ -755,8 +764,9 @@ JS
$submittedFields = new ArrayList(); $submittedFields = new ArrayList();
foreach($this->Fields() as $field) { foreach($this->Fields() as $field) {
if(!$field->showInReports()) {
if(!$field->showInReports()) continue; continue;
}
$submittedField = $field->getSubmittedFormField(); $submittedField = $field->getSubmittedFormField();
$submittedField->ParentID = $submittedForm->ID; $submittedField->ParentID = $submittedForm->ID;
@ -766,9 +776,10 @@ JS
// save the value from the data // save the value from the data
if($field->hasMethod('getValueFromData')) { if($field->hasMethod('getValueFromData')) {
$submittedField->Value = $field->getValueFromData($data); $submittedField->Value = $field->getValueFromData($data);
} } else {
else { if(isset($data[$field->Name])) {
if(isset($data[$field->Name])) $submittedField->Value = $data[$field->Name]; $submittedField->Value = $data[$field->Name];
}
} }
if(!empty($data[$field->Name])){ if(!empty($data[$field->Name])){
@ -799,7 +810,9 @@ JS
} }
} }
if(!$this->DisableSaveSubmissions) $submittedField->write(); if(!$this->DisableSaveSubmissions) {
$submittedField->write();
}
$submittedFields->push($submittedField); $submittedFields->push($submittedField);
} }
@ -815,9 +828,13 @@ JS
$email->populateTemplate($emailData); $email->populateTemplate($emailData);
if($attachments){ if($attachments){
foreach($attachments as $file){ foreach($attachments as $file) {
if($file->ID != 0) { if($file->ID != 0) {
$email->attachFile($file->Filename, $file->Filename, HTTP::get_mime_type($file->Filename)); $email->attachFile(
$file->Filename,
$file->Filename,
HTTP::get_mime_type($file->Filename)
);
} }
} }
} }
@ -879,9 +896,8 @@ JS
} }
/** /**
* This action handles rendering the "finished" message, * This action handles rendering the "finished" message, which is
* which is customisable by editing the ReceivedFormSubmission.ss * customizable by editing the ReceivedFormSubmission template.
* template.
* *
* @return ViewableData * @return ViewableData
*/ */
@ -906,7 +922,7 @@ JS
*/ */
class UserDefinedForm_EmailRecipient extends DataObject { class UserDefinedForm_EmailRecipient extends DataObject {
static $db = array( public static $db = array(
'EmailAddress' => 'Varchar(200)', 'EmailAddress' => 'Varchar(200)',
'EmailSubject' => 'Varchar(200)', 'EmailSubject' => 'Varchar(200)',
'EmailFrom' => 'Varchar(200)', 'EmailFrom' => 'Varchar(200)',
@ -916,15 +932,14 @@ class UserDefinedForm_EmailRecipient extends DataObject {
'HideFormData' => 'Boolean' 'HideFormData' => 'Boolean'
); );
static $has_one = array( public static $has_one = array(
'Form' => 'UserDefinedForm', 'Form' => 'UserDefinedForm',
'SendEmailFromField' => 'EditableFormField', 'SendEmailFromField' => 'EditableFormField',
'SendEmailToField' => 'EditableFormField' 'SendEmailToField' => 'EditableFormField'
); );
/** /**
* Return the fields to edit this email. * @return FieldList
* @return FieldSet
*/ */
public function getCMSFields() { public function getCMSFields() {
@ -997,28 +1012,45 @@ class UserDefinedForm_EmailRecipient extends DataObject {
return $fields; return $fields;
} }
/**
* @param Member
*
* @return boolean
*/
public function canView($member = null) { public function canView($member = null) {
return $this->Form()->canView(); return $this->Form()->canView();
} }
/**
* @param Member
*
* @return boolean
*/
public function canEdit($member = null) { public function canEdit($member = null) {
return $this->Form()->canEdit(); return $this->Form()->canEdit();
} }
/**
* @param Member
*
* @return boolean
*/
public function canDelete($member = null) { public function canDelete($member = null) {
return $this->Form()->canDelete(); return $this->Form()->canDelete();
} }
} }
/** /**
* Email that gets sent to the people listed in the Email Recipients * Email that gets sent to the people listed in the Email Recipients when a
* when a submission is made * submission is made.
* *
* @package userforms * @package userforms
*/ */
class UserDefinedForm_SubmittedFormEmail extends Email { class UserDefinedForm_SubmittedFormEmail extends Email {
protected $ss_template = "SubmittedFormEmail"; protected $ss_template = "SubmittedFormEmail";
protected $data; protected $data;
public function __construct($submittedFields = null) { public function __construct($submittedFields = null) {

View File

@ -2,19 +2,20 @@
/** /**
* A file uploaded on a {@link UserDefinedForm} and attached to a single * A file uploaded on a {@link UserDefinedForm} and attached to a single
* {@link SubmittedForm} * {@link SubmittedForm}.
* *
* @package userforms * @package userforms
*/ */
class SubmittedFileField extends SubmittedFormField { class SubmittedFileField extends SubmittedFormField {
static $has_one = array( public static $has_one = array(
"UploadedFile" => "File" "UploadedFile" => "File"
); );
/** /**
* Return the value of this field for inclusion into things such as reports * Return the value of this field for inclusion into things such as
* reports.
* *
* @return string * @return string
*/ */
@ -24,23 +25,36 @@ class SubmittedFileField extends SubmittedFormField {
$title = _t('SubmittedFileField.DOWNLOADFILE', 'Download File'); $title = _t('SubmittedFileField.DOWNLOADFILE', 'Download File');
if($link) { if($link) {
return sprintf('%s - <a href="%s" target="_blank">%s</a>', $name, $link, $title); return sprintf(
'%s - <a href="%s" target="_blank">%s</a>',
$name, $link, $title
);
} }
return false; return false;
} }
/** /**
* Return the value for this field in the CSV export * Return the value for this field in the CSV export.
* *
* @return String * @return string
*/ */
public function getExportValue() { public function getExportValue() {
return ($link = $this->getLink()) ? $link : ""; return ($link = $this->getLink()) ? $link : "";
} }
/** /**
* Return the link for the file attached to this submitted form field * Return the value for the database, link to the file is stored as a
* relation so value for the field can be null.
*
* @return string
*/
public function getValueFromData() {
return null;
}
/**
* Return the link for the file attached to this submitted form field.
* *
* @return string * @return string
*/ */
@ -62,4 +76,4 @@ class SubmittedFileField extends SubmittedFormField {
return $this->UploadedFile()->Name; return $this->UploadedFile()->Name;
} }
} }
} }