silverstripe-cms/code/Newsletter/SubscribeForm.php
Normann Lou a3f9d9b6d3 switch externals to trunk.
Inform-merge: from the changeset:
r32477: Merge 2.0-inform from trunk previously
r32478: Merge 2.0-inform from trunk previously
r32481: merge 2.0infom with lastes chunk 
r32483: merge 2.0infom with lastes chunk 
r33526: Final styling of all forms and combined communication form add/alter javascript for height adjustment of First / Second block 
r33580: styling for combined form communication in myinfom pages 
r33706: styling of combined form (communication) in Email 
r33881: made compatible to $extraClass 
r33885: added defaultVal
r33887: fixed typo 
r34728: modified SmallFieldHolder?()
r34729: added "validationError"-class 
r34914: WIP3866: Factfinder: Hide "self emplyed" block 
r34964: Change current plan upto TraumaInsurance? 
r35038: disabled friggin field focus 
r35230: #1032 Fixed hash-link insertion 
r35887: conditionally setting parameters in sourceID() - to avoid empty overrides
r35892: Saving value in SQL-compatible format (YYYY-MM-DD instead of DD/MM/YYYY), with fallback for non-sql values (just passed through without conversion) 
r35928: Removed "create a" from PageType?-dropdown, sorting alphabetically, falling back to $singular_name
r35990: branched off for membertablefield
r35994: fix for membertablefield
r36024: added array-condition needed for DMYDateField 
r36083: fix bug for compositeField -> dropDatalessField
r36394: removed debug code 
r36826: change wrong indent format 
r36828: WIP 4262: Logging out of My Inform goes to blank page
r36858: Fixed error caused in r12472 while merging to Session-class
r37132: Merged partial changesets from branches/2.0-nzct, only adding childID to detailform when not in add-mode 
r40815: add an unsubscribe record when a member subscribe a newslettertype 
r41113: fix the bug described in http://support.silverstripe.com/info/ticket/31: CRM not showing search results
r43226: fixed search (partial merge from trunk) 
r43268: merged createNewPassword() from trunk, was referencing a non-existinent global function randomString()


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@45473 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-11-23 01:10:19 +00:00

258 lines
8.4 KiB
PHP
Executable File

<?php
class SubscribeForm extends UserDefinedForm {
static $add_action = "a newsletter subscription form";
static $required_fields = array(
'Email address' => 'EditableEmailField(CustomParameter=Email,CanDelete=0)',
'First name' => 'EditableTextField(CustomParameter=FirstName)',
'Last name' => 'EditableTextField(CustomParameter=Surname)',
'Address' => 'EditableTextField(Rows=3,CustomParameter=Address)',
'Job title' => 'EditableTextField(CustomParameter=JobTitle)',
'Organisation' => 'EditableTextField(CustomParameter=Organisation)',
'Mail format' => 'EditableRadioField(Options=1:HTML,Options=0:Text__only,CustomParameter=HTMLEmail)'
);
static $db = array(
'Subscribe' => 'Boolean',
'AllNewsletters' => 'Boolean',
'Subject' => 'Varchar'
);
static $defaults = array(
"OnCompleteMessage" => "<p>Thanks, you have been added to our mailing list.</p>",
);
static $has_many = array(
'Newsletters' => 'NewsletterType'
);
function __construct( $data = null, $isSingleton = false ) {
parent::__construct( $data, $isSingleton );
if( $data || $isSingleton )
return;
$this->addDefaultFields();
}
private function addDefaultFields() {
$f = $this->Fields();
// check that the required fields exist
$count = 1;
foreach( self::$required_fields as $defaultName => $typeString ) {
list( $type, $typeValue ) = $this->parseType( $typeString );
$newField = new $type();
$newField->Name = "Field" . (string)$count;
$newField->Title = $defaultName;
// TODO Map the field to a particular action
if( !empty( $typeValue ) ) {
$newField->prepopulate( $typeValue );
}
$newField->setField('ID', "new-" . $count);
$newField->Sort = $count;
$f->addWithoutWrite($newField);
$count++;
}
}
public function Newsletters() {
$components = $this->getComponents('Newsletters');
return $components;
}
public function customFormActions( $isReadonly = false ) {
$fields = parent::customFormActions( $isReadonly );
// get the newsletters in the system
$newsletterTypes = DataObject::get( 'NewsletterType' );
$availableNewsletters = array();
$nlCheckboxes = array();
foreach( $this->Newsletters() as $subscribeTo )
$availableNewsletters[] = $subscribeTo->ID;
// create a checkbox for each newsletter
if($newsletterTypes && is_object($newsletterTypes)) {
foreach( $newsletterTypes as $newsletterType )
$nlCheckboxes[] = new CheckboxField( "CustomNewsletters[{$newsletterType->ID}]", $newsletterType->Title, in_array( $newsletterType->ID, $availableNewsletters ) );
}
$fields->push( new OptionsetField( 'AllNewsletters', '', array(
1 => 'All newsletters',
0 => 'Specific newsletters'
),
$this->AllNewsletters
));
$fields->push( new CompositeField( $nlCheckboxes ));
$fields->push( new TextField( 'Subject', 'Subject line on confirmation', $this->Subject ) );
return $fields;
}
function parseType( $typeString ) {
if( preg_match('/^([A-Za-z_]+)\(([^)]+)\)$/', $typeString, $match ) )
return array( $match[1], $match[2] );
return array( $typeString, null );
}
/**
*
*/
public function processNewFormFields() {
}
/**
* Saves data related to any custom actions this form performs when submitted
*/
public function customFormSave( $data, $form ) {
$newsletters = $this->Newsletters();
$this->Subscribe = !empty( $data['Subscribe'] );
$this->AllNewsletters = !empty( $data['AllNewsletters'] );
$this->Subject = $data['Subject'];
// Note: $data['CustomNewsletters'] was changed to $_REQUEST['CustomNewsletters'] in order to fix
// to fix "'Specific newsletters' option in 'newsletter subscription form' page type does not work" bug
// See: http://www.silverstripe.com/bugs/flat/1675
if( !empty( $_REQUEST['CustomNewsletters'] ) ) {
/*foreach( $newsletters as $newsletter ) {
if( $data['CustomNewsletters'][$newsletter->ID] == 'on' ) {
$newsletters->add( $newsletter->ID );
} else {
$newsletters->remove( $newsletter->ID );
}
}*/
if( isset($_REQUEST['CustomNewsletters'][0]) )
unset( $_REQUEST['CustomNewsletters'][0] );
$newsletters->setByIDList( array_keys( $_REQUEST['CustomNewsletters'] ) );
} else {
$this->AllNewsletters = true;
$newsletters->removeAll();
}
}
}
class SubscribeForm_SubscribeEmail extends Email_Template {
protected $to = '$Email';
protected $subject = '$Subject';
protected $ss_template = 'SubscribeEmail';
protected $from = '';
}
class SubscribeForm_Controller extends UserDefinedForm_Controller {
function process( $data, $form ) {
// Add the user to the mailing list
$member = Object::create("Member");
// $_REQUEST['showqueries'] = 1;
// map the editables to the data
foreach( $this->Fields() as $editable ) {
$field = $editable->CustomParameter;
if( !$field )
continue;
// Debug::message( $editable->Name . '->' . $field );
// 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( !is_numeric( $listID ) )
continue;
// get the newsletter
$newsletter = DataObject::get_by_id( 'NewsletterType', $listID );
if( !$newsletter )
continue;
$newsletters[] = $newsletter;
// Debug::show( $newsletter->GroupID );
$member->Groups()->add( $newsletter->GroupID );
}
// email the user with their details
$templateData = array(
'Email' => $member->Email,
'FirstName' => $member->FirstName,
'Newsletters' => new DataObjectSet( $newsletters ),
'UnsubscribeLink' => Director::baseURL() . 'unsubscribe/' . $member->Email
);
$email = new SubscribeForm_SubscribeEmail();
$email->setFrom( Email::getAdminEmail() );
$email->setSubject( $this->Subject );
$email->populateTemplate( $templateData );
$email->send();
$parentHTML = parent::process( $data, $form );
$templateData['Link'] = $data['Referrer'];
$templateData['UnsubscribeLink'] = Director::baseURL() . 'unsubscribe';
$custom = $this->customise(array(
"Content" => $this->customise( $templateData )->renderWith('SubscribeSubmission')
));
return $custom->renderWith('Page');
}
function Form() {
$form = parent::Form();
if( $this->AllNewsletters )
$newsletterList = DataObject::get('NewsletterType');
else
$newsletterList = $this->Newsletters();
$newsletters = array();
// Debug::show($newsletterList);
// get the newsletter types to display on the form
foreach( $newsletterList as $newsletter )
$newsletters[] = $newsletter;
// Debug::show( $newsletters );
$form->Fields()->push( new CheckboxSetField( 'Newsletters', 'Subscribe to lists', $newsletters ) );
$validator = $form->getValidator();
$validator->addRequiredField( 'Newsletters' );
$form->setValidator( $validator );
return $form;
}
}
?>