Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CompetitionRegistration extends Page {
- public static $db = array(
- );
- public static $has_one = array(
- );
- }
- class CompetitionRegistration_Controller extends Page_Controller {
- public static $allowed_actions = array ('RegistrationForm', 'doNewRegForm', 'results');
- public function init() {
- parent::init();
- // Note: you should use SS template require tags inside your templates
- // instead of putting Requirements calls here. However these are
- // included so that our older themes still work
- Requirements::themedCSS('reset');
- Requirements::themedCSS('layout');
- Requirements::themedCSS('typography');
- Requirements::themedCSS('form');
- //CSS
- Requirements::css("autocomplete/css/autocomplete.css");
- //JS
- Requirements::javascript("autocomplete/javascript/jquery.autocomplete-min.js");
- }
- function getCompetitions() {
- if($Competitions = Competition::get()->filter(array('EntryStatus' => 'Closed','EntryClosingDateTime:greaterthan' => 'NOW'))){
- return $Competitions->map('ID', 'CompetitionSummary', 'Please Select');
- } else {
- return array('No Open Competitions Found');
- }
- }
- function getMembers() {
- if($Members = Member::get()){
- return $Members->map('ID', 'FirstName', 'Please Select a Competition');
- } else {
- return array('No Members Found');
- }
- }
- public function NewRegForm(){
- return new RegForm($this, 'doNewRegForm');
- }
- public function doNewRegForm($data){
- // Create Team and name it
- $team = new Team();
- $team->write();
- // Link team to the selected Competition
- if($data['Competitions'] != ''){
- $comp = Competition::get()->byID($data['Competitions']);
- $team->Competitions()->add($comp);
- }else{
- die('no comp entered');
- }
- // Add team captain
- $captain = Member::get()->byID(Member::currentUserID());
- $team->Members()->add($captain);
- // Loop through each of the additional members and add them to the team
- $formfields = array('1','2','3','4');
- foreach ($formfields as $value) {
- if($data['ExistingMemberName' . $value] != ''){
- // If they selected an existing member look them up and select the first in the list
- // This assumes that only 1 person by that name is in the system...
- $member = Member::get()
- ->filter(array(
- 'FirstName' => $data['ExistingMemberName' . $value]))
- ->first();
- // Adds the exisiting member to the team
- $team->Members()->add($member);
- }elseif($data['NewMemberName' . $value] != ''){
- // If they selected to create a new member it's done here
- $member = new Member();
- $member->FirstName = $data['NewMemberName' . $value];
- $member->Email = $data['NewMemberEmail' . $value];
- // Password is set to a random number
- $tempPassword = substr(number_format(time() * rand(),0,'',''),0,7);
- $member->Password = $tempPassword;
- // Set an expiry so that they have to change their password on login
- $member->PasswordExpiry = '2000-01-01';
- $member->write();
- $team->Members()->add($member);
- // Send email to new user
- $From = '';
- $To = $data['NewMemberEmail' . $value];
- $Subject = 'Registration';
- $email = new Email($From, $To, $Subject);
- // Which email template to use
- $email->setTemplate('NewMemberEmail');
- $emailDetails = array(
- 'Captain' => $captain->FirstName,
- 'NewMemberName' => $data['NewMemberName' . $value],
- 'NewMemberEmail' => $data['NewMemberEmail' . $value],
- 'Password' => $tempPassword
- );
- $email->populateTemplate($emailDetails);
- $email->send();
- }else{
- // No Member entered
- }
- }
- Controller::redirectBack();
- }
- public function results()
- {
- if($query = $this->getSearchQuery())
- {
- $query = Convert::raw2xml($query);
- //Search for our query - Pretty basic example here
- $Results = Member::get()->filter(array('FirstName:PartialMatch' => $query));
- //$Members = $Results->map('ID', 'FirstName', 'Email');
- $Suggestions = "['" . implode("', '", $Results->column('FirstName')) . "']";
- return $json = "{
- query : '$query',
- suggestions : $Suggestions
- }";
- }
- Director::redirect($this->Link());
- }
- function getSearchQuery()
- {
- if($this->request)
- return $this->request->getVar("query");
- }
- }
- class RegForm extends Form {
- public function __construct($controller, $name) {
- if($Competitions = Competition::get()->filter(array(
- 'EntryStatus' => 'Open',
- 'EntryClosingDateTime:greaterthan' => 'NOW')
- )){
- $comps = $Competitions->map('ID', 'CompetitionSummary', 'Please Select');
- } else {
- $comps = array('No Open Competitions Found');
- }
- $fields = new FieldList(
- new DropdownField('Competitions', 'Comps', $comps),
- new TextField('TeamName', 'TeamName'),
- new TextField('ExistingMemberName1', 'Search', $controller->getSearchQuery()),
- TextField::create('NewMemberName1', 'Name')->setAttribute('required', 'true'),
- new EmailField('NewMemberEmail1', 'Email'),
- new DropdownField('Degree1', 'Degree1', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
- new TextField('CurrentMajor1', 'Current Major'),
- new TextField('StudentID1', 'StudentID'),
- new TextField('ExistingMemberName2', 'Search', $controller->getSearchQuery()),
- new TextField('NewMemberName2', 'Name'),
- new EmailField('NewMemberEmail2', 'Email'),
- new DropdownField('Degree2', 'Degree2', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
- new TextField('CurrentMajor2', 'Current Major'),
- new TextField('StudentID2', 'StudentID'),
- new TextField('ExistingMemberName3', 'Search', $controller->getSearchQuery()),
- new TextField('NewMemberName3', 'Name'),
- new EmailField('NewMemberEmail3', 'Email'),
- new DropdownField('Degree3', 'Degree3', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
- new TextField('CurrentMajor3', 'Current Major'),
- new TextField('StudentID3', 'StudentID'),
- new TextField('ExistingMemberName4', 'Search', $controller->getSearchQuery()),
- new TextField('NewMemberName4', 'Name'),
- new EmailField('NewMemberEmail4', 'Email'),
- new DropdownField('Degree4', 'Degree4', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
- new TextField('CurrentMajor4', 'Current Major'),
- new TextField('StudentID4', 'StudentID')
- );
- $actions = new FieldList(
- new FormAction('submit', 'Submit')
- );
- parent::__construct($controller, $name, $fields, $actions);
- }
- public function forTemplate() {
- return $this->renderWith(array(
- $this->class,
- 'Form'
- ));
- }
- public function submit($data, $form) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement