From 0ad6198307996d80e1f91c45d5594c97f2fb0f09 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 7 Feb 2008 03:00:25 +0000 Subject: [PATCH] #1951 - Fix newsletter subscription form git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@49174 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/Newsletter/SubscribeForm.php | 25 ++++++++++++------------- code/PageTypes/UserDefinedForm.php | 4 ++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/code/Newsletter/SubscribeForm.php b/code/Newsletter/SubscribeForm.php index 45b1f936..4f22f885 100755 --- a/code/Newsletter/SubscribeForm.php +++ b/code/Newsletter/SubscribeForm.php @@ -64,14 +64,19 @@ class SubscribeForm extends UserDefinedForm { if( !empty( $typeValue ) ) { $newField->prepopulate( $typeValue ); } - - $newField->setField('ID', "new-" . $count); - + + $newField->ParentID = $this->ID; $newField->Sort = $count; - $f->addWithoutWrite($newField); + $newField->write(); $count++; } } + + public function write($showDebug = false, $forceInsert = false, $forceWrite = false) { + $isNew = (!$this->ID); + parent::write($showDebug, $forceInsert, $forceWrite); + if($isNew) $this->addDefaultFields(); + } public function Newsletters() { $components = $this->getComponents('Newsletters'); @@ -179,7 +184,6 @@ class SubscribeForm_SubscribeEmail extends Email_Template { class SubscribeForm_Controller extends UserDefinedForm_Controller { function process( $data, $form ) { - // Add the user to the mailing list $member = Object::create("Member"); @@ -188,7 +192,6 @@ class SubscribeForm_Controller extends UserDefinedForm_Controller { // map the editables to the data foreach( $this->Fields() as $editable ) { - $field = $editable->CustomParameter; if( !$field ) continue; @@ -198,14 +201,14 @@ class SubscribeForm_Controller extends UserDefinedForm_Controller { // if( $member->hasField( $field ) ) $member->$field = $data[$editable->Name]; } - + // need to write the member record before adding the user to groups $member->write(); $newsletters = array(); // Add member to the selected newsletters - if( $data['Newsletters'] ) foreach( $data['Newsletters'] as $listID ) { + if( isset($data['Newsletters'])) foreach( $data['Newsletters'] as $listID ) { if( !is_numeric( $listID ) ) continue; @@ -259,13 +262,9 @@ class SubscribeForm_Controller extends UserDefinedForm_Controller { $newsletters = array(); - // Debug::show($newsletterList); - // get the newsletter types to display on the form foreach( $newsletterList as $newsletter ) - $newsletters[] = $newsletter; - - // Debug::show( $newsletters ); + $newsletters[$newsletter->ID] = $newsletter->Title; $form->Fields()->push( new CheckboxSetField( 'Newsletters', 'Subscribe to lists', $newsletters ) ); diff --git a/code/PageTypes/UserDefinedForm.php b/code/PageTypes/UserDefinedForm.php index 84b26120..010d4876 100755 --- a/code/PageTypes/UserDefinedForm.php +++ b/code/PageTypes/UserDefinedForm.php @@ -232,7 +232,7 @@ class UserDefinedForm_Controller extends Page_Controller { if( $field->hasMethod( 'getValueFromData' ) ) $submittedField->Value = $field->getValueFromData( $data ); else - $submittedField->Value = $data[$field->Name]; + if(isset($data[$field->Name])) $submittedField->Value = $data[$field->Name]; $submittedField->write(); $submittedFields->push($submittedField); @@ -278,7 +278,7 @@ class UserDefinedForm_Controller extends Page_Controller { $values[$field->Title] = Convert::linkIfMatch($field->getValueFromData( $data )); } else { - $values[$field->Title] = Convert::linkIfMatch($data[$field->Name]); + if(isset($data[$field->Name])) $values[$field->Title] = Convert::linkIfMatch($data[$field->Name]); } }