mirror of
https://github.com/silverstripe/silverstripe-reports
synced 2024-10-22 11:05:53 +02:00
40eb79da40
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@44135 467b73ca-7a2a-4603-9d3b-597d59a354a9
258 lines
8.4 KiB
PHP
Executable File
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;
|
|
}
|
|
}
|
|
?>
|