Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 23, 2015
1 parent 35a2fe4 commit 86abb55
Show file tree
Hide file tree
Showing 27 changed files with 3,346 additions and 3,181 deletions.
300 changes: 153 additions & 147 deletions code/controllers/EventRegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,151 +5,157 @@
*
* @package silverstripe-eventmanagement
*/
class EventRegisterController extends Page_Controller {

public static $url_handlers = array(
'' => 'index'
);

public static $allowed_actions = array(
'RegisterForm',
'confirm'
);

protected $parent;
protected $datetime;

/**
* Constructs a new controller for creating a registration.
*
* @param ContentController $parent
* @param RegistrableDateTime $datetime
*/
public function __construct($parent, $datetime) {
$this->parent = $parent;
$this->datetime = $datetime;

parent::__construct($parent->data());
}

public function init() {
parent::init();

if ($this->datetime->Event()->RequireLoggedIn && !Member::currentUserID()) {
return Security::permissionFailure($this, array(
'default' => 'Please log in to register for this event.'
));
}

$form = $this->RegisterForm();
$expiry = $form->getExpiryDateTime();

if ($expiry && $expiry->InPast()) {
$form->getSession()->Registration()->delete();
$form->getSession()->delete();

$message = _t('EventManagement.REGSESSIONEXPIRED', 'Your'
. ' registration expired before it was completed. Please'
. ' try ordering your tickets again.');

$form->sessionMessage($message, 'bad');

$this->redirect($this->Link());
}
else {
$form->clearMessage();
}
}

public function index() {
$datetime = $this->datetime;
$exclude = null;

// If we have a current multiform ID, then exclude the linked
// registration from the capacity calculation.
if (isset($_GET['MultiFormSessionID'])) {
$exclude = $this->RegisterForm()->getSession()->RegistrationID;
}

if ($datetime->getStartDateTime()->getTimestamp() < time()) {
$data = array(
'Title' => $datetime->Event()->Title . ' Has Already Happened',
'Content' => '<p>You can no longer register for this event.</p>'
);
} elseif ($datetime->getRemainingCapacity($exclude)) {
$data = array(
'Title' => 'Register For ' . $datetime->Event()->Title,
'Form' => $this->RegisterForm()
);
} else {
$data = array(
'Title' => $datetime->Event()->Title . ' Is Full',
'Content' => '<p>There are no more places available at this event.</p>'
);
}

return $this->getViewer('index')->process($this->customise($data));
}

/**
* Handles a user clicking on a registration confirmation link in an email.
*/
public function confirm($request) {
$id = $request->param('ID');
$token = $request->getVar('token');

if (!$rego = DataObject::get_by_id('EventRegistration', $id)) {
return $this->httpError(404);
}

if ($rego->Token != $token) {
return $this->httpError(403);
}

if ($rego->Status != 'Unconfirmed') {
return $this->redirect($rego->Link());
}

try {
$rego->Status = 'Valid';
$rego->write();

EventRegistrationDetailsEmail::factory($rego)->send();
} catch (ValidationException $e) {
return array(
'Title' => 'Could Not Confirm Registration',
'Content' => '<p>' . $e->getResult()->message() . '</p>'
);
}

return array(
'Title' => $this->datetime->Event()->AfterConfirmTitle,
'Content' => $this->datetime->Event()->obj('AfterConfirmContent')
);
}

/**
* @return RegistrableDateTime
*/
public function getDateTime() {
return $this->datetime;
}

/**
* @return EventRegisterForm
*/
public function RegisterForm() {
return new EventRegisterForm($this, 'RegisterForm');
}

/**
* @param string $action
* @return string
*/
public function Link($action = null) {
return Controller::join_links(
$this->parent->Link(), 'register', $action
);
}

class EventRegisterController extends Page_Controller
{

public static $url_handlers = array(
'' => 'index'
);

public static $allowed_actions = array(
'RegisterForm',
'confirm'
);

protected $parent;
protected $datetime;

/**
* Constructs a new controller for creating a registration.
*
* @param ContentController $parent
* @param RegistrableDateTime $datetime
*/
public function __construct($parent, $datetime)
{
$this->parent = $parent;
$this->datetime = $datetime;

parent::__construct($parent->data());
}

public function init()
{
parent::init();

if ($this->datetime->Event()->RequireLoggedIn && !Member::currentUserID()) {
return Security::permissionFailure($this, array(
'default' => 'Please log in to register for this event.'
));
}

$form = $this->RegisterForm();
$expiry = $form->getExpiryDateTime();

if ($expiry && $expiry->InPast()) {
$form->getSession()->Registration()->delete();
$form->getSession()->delete();

$message = _t('EventManagement.REGSESSIONEXPIRED', 'Your'
. ' registration expired before it was completed. Please'
. ' try ordering your tickets again.');

$form->sessionMessage($message, 'bad');

$this->redirect($this->Link());
} else {
$form->clearMessage();
}
}

public function index()
{
$datetime = $this->datetime;
$exclude = null;

// If we have a current multiform ID, then exclude the linked
// registration from the capacity calculation.
if (isset($_GET['MultiFormSessionID'])) {
$exclude = $this->RegisterForm()->getSession()->RegistrationID;
}

if ($datetime->getStartDateTime()->getTimestamp() < time()) {
$data = array(
'Title' => $datetime->Event()->Title . ' Has Already Happened',
'Content' => '<p>You can no longer register for this event.</p>'
);
} elseif ($datetime->getRemainingCapacity($exclude)) {
$data = array(
'Title' => 'Register For ' . $datetime->Event()->Title,
'Form' => $this->RegisterForm()
);
} else {
$data = array(
'Title' => $datetime->Event()->Title . ' Is Full',
'Content' => '<p>There are no more places available at this event.</p>'
);
}

return $this->getViewer('index')->process($this->customise($data));
}

/**
* Handles a user clicking on a registration confirmation link in an email.
*/
public function confirm($request)
{
$id = $request->param('ID');
$token = $request->getVar('token');

if (!$rego = DataObject::get_by_id('EventRegistration', $id)) {
return $this->httpError(404);
}

if ($rego->Token != $token) {
return $this->httpError(403);
}

if ($rego->Status != 'Unconfirmed') {
return $this->redirect($rego->Link());
}

try {
$rego->Status = 'Valid';
$rego->write();

EventRegistrationDetailsEmail::factory($rego)->send();
} catch (ValidationException $e) {
return array(
'Title' => 'Could Not Confirm Registration',
'Content' => '<p>' . $e->getResult()->message() . '</p>'
);
}

return array(
'Title' => $this->datetime->Event()->AfterConfirmTitle,
'Content' => $this->datetime->Event()->obj('AfterConfirmContent')
);
}

/**
* @return RegistrableDateTime
*/
public function getDateTime()
{
return $this->datetime;
}

/**
* @return EventRegisterForm
*/
public function RegisterForm()
{
return new EventRegisterForm($this, 'RegisterForm');
}

/**
* @param string $action
* @return string
*/
public function Link($action = null)
{
return Controller::join_links(
$this->parent->Link(), 'register', $action
);
}
}
Loading

0 comments on commit 86abb55

Please sign in to comment.