Extending Cam Findlay's SilverStripe module for event registration. Adding an attendees list and plurilization support.
Write your perfect readme file here
| <?php |
| <?php | |
| class RegistrationExtension extends DataExtension { | |
| private static $db = array( | |
| 'EnableRegistrations' => 'Boolean', | |
| 'MaxRegistrations' => 'Int' | |
| ); | |
| private static $has_many = array( | |
| 'Registrations' => 'Registration' | |
| ); | |
| public function updateSettingsFields( FieldList $fields ) { | |
| $fields->addFieldToTab( | |
| 'Root.Registrations', | |
| TextField::create('MaxRegistrations', 'Maximum registrations?') | |
| ); | |
| $fields->addFieldToTab( | |
| 'Root.Registrations', | |
| FieldGroup::create( | |
| CheckboxField::create('EnableRegistrations', 'Allow registrations for this event') | |
| )->setTitle('Enable') | |
| ); | |
| } | |
| public function updateCMSFields( FieldList $fields ) { | |
| if ($this->owner->EnableRegistrations) { | |
| $fields->addFieldToTab( | |
| 'Root.Registrations', | |
| GridField::create( | |
| 'Registrations', | |
| 'Registrations', | |
| $this->owner->Registrations(), | |
| GridFieldConfig_RecordEditor::create() | |
| ) | |
| ); | |
| } | |
| } | |
| } | |
| class RegistrationControllerExtension extends DataExtension { | |
| private static $allowed_actions = array('RegistrationForm'); | |
| public function RegistrationForm(){ | |
| $fields = FieldList::create( | |
| TextField::create('Name'), | |
| TextField::create('Organisation'), | |
| EmailField::create('Email') | |
| ); | |
| $actions = FieldList::create( | |
| FormAction::create('doRegister', 'Send Registration') | |
| ); | |
| $validator = RequiredFields::create(array('Name','Email')); | |
| $form = Form::create($this->owner, 'RegistrationForm', $fields, $actions, $validator); | |
| return $form; | |
| } | |
| public function doRegister($data, Form $form){ | |
| if ($this->SpacesAvailable()) { | |
| $registration = Registration::create(); | |
| $form->saveInto($registration); | |
| $registration->PageID = $this->owner->ID; | |
| $registration->write(); | |
| $form->sessionMessage('Thanks for registering', 'good'); | |
| } else { | |
| $form->sessionMessage('Registrations are full', 'warning'); | |
| } | |
| $this->owner->redirectBack(); | |
| } | |
| public function SpacesAvailable(){ | |
| $registrations = Registration::get() | |
| ->filter('PageID', $this->owner->ID) | |
| ->exclude('Status', 'Applied') | |
| ->count(); | |
| $spacesleft = (int)($this->owner->MaxRegistrations - $registrations); | |
| return ($spacesleft > 0) ? $spacesleft : 0; | |
| } | |
| public function RegisteredPeeps(){ | |
| $registrations = Registration::get() | |
| ->filter('PageID', $this->owner->ID) | |
| ->exclude('Status', 'Applied'); | |
| return $registrations; | |
| } | |
| } |
| <?php | |
| class Registration extends DataObject { | |
| private static $db = array( | |
| 'Name' => 'Varchar(255)', | |
| 'Organisation' => 'Varchar(255)', | |
| 'Email' => 'Varchar(255)', | |
| 'Status' => 'Enum("Applied, Confirmed", "Applied")' | |
| ); | |
| private static $has_one = array( | |
| 'Page' => 'Page' | |
| ); | |
| private static $summary_fields = array( | |
| 'Name', | |
| 'Organisation', | |
| 'Email', | |
| 'Status' | |
| ); | |
| public function onAfterWrite(){ | |
| if($this->Status == 'Confirmed' && !Member::get()->filter('Email',$this->Email)->exists()){ | |
| $member = Member::create(); | |
| $member->FirstName = $this->FirstName; | |
| $member->Surname = $this->Surname; | |
| $member->Email = $this->Email; | |
| $member->write(); | |
| } | |
| parent::onAfterWrite(); | |
| } | |
| public function getFirstName(){ | |
| return $this->splitName(0); | |
| } | |
| public function getSurname(){ | |
| return $this->splitName(1); | |
| } | |
| public function splitName($id){ | |
| $result = explode(' ', $this->Name); | |
| return $result[$id]; | |
| } | |
| } |
| { | |
| "name": "wgodfrey/pageregistrations", | |
| "description": "Copied from Cam Findlay's lesson", | |
| "type": "silverstripe-module", | |
| "keywords": ["silverstripe", "cwp", "registration"], | |
| "license": "WTFPL", | |
| "authors": [{ | |
| "name": "Wilfred Godfrey", | |
| "email": "wilfred@silverstripe.com" | |
| }], | |
| "require": { | |
| "silverstripe/framework": "~3.1", | |
| "silverstripe/cms": "~3.1" | |
| } | |
| } |
| <% if $RegisteredPeeps.count() %> | |
| <div class="message info"> | |
| <h5> $RegisteredPeeps.count() <% if $RegisteredPeeps.count() == 1 %>person<% else %>people<% end_if %> going:</h5> | |
| <ul> | |
| <% loop $RegisteredPeeps %> | |
| <li>$Name<% if $Organisation %> - <em>$Organisation</em><% end_if %></li> | |
| <% end_loop %> | |
| </ul> | |
| </div> | |
| <% end_if %> |
| <% if EnableRegistrations %> | |
| <% if $SpacesAvailable %> | |
| <h2>Register</h2> | |
| <h3>$SpacesAvailable space<% if $SpacesAvailable != 1%>s<% end_if %> left</h3> | |
| <% include EventAttendees %> | |
| $RegistrationForm | |
| <% else %> | |
| <div class="message warning">No spaces left</div> | |
| <% include EventAttendees %> | |
| <% end_if %> | |
| <% end_if %> |
| <?php | |
| class RegistrationTest extends SapphireTest { | |
| public static $fixture_file = 'pageregistrations/tests/RegistrationTest.yml'; | |
| public function testNameSplit(){ | |
| $registration = $this->objFromFixture('Registration', 'JoeBloggs'); | |
| $this->assertEquals($registration->FirstName, 'Joe'); | |
| $this->assertEquals($registration->Surname,'Bloggs'); | |
| } | |
| } |
| Registration: | |
| JoeBloggs: | |
| Name: 'Joe Bloggs' | |
| Organisation: 'Example Co Ltd.' | |
| Email: 'joe@bloggs.com' |