Advertisement
Guest User

Untitled

a guest
Jul 15th, 2013
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class CompetitionRegistration extends Page {
  3.  
  4.     public static $db = array(
  5.     );
  6.  
  7.     public static $has_one = array(
  8.     );
  9.  
  10. }
  11. class CompetitionRegistration_Controller extends Page_Controller {
  12.  
  13.     public static $allowed_actions = array ('RegistrationForm', 'doNewRegForm', 'results');
  14.    
  15.    
  16.  
  17.     public function init() {
  18.         parent::init();
  19.  
  20.         // Note: you should use SS template require tags inside your templates
  21.         // instead of putting Requirements calls here.  However these are
  22.         // included so that our older themes still work
  23.         Requirements::themedCSS('reset');
  24.         Requirements::themedCSS('layout');
  25.         Requirements::themedCSS('typography');
  26.         Requirements::themedCSS('form');
  27.  
  28.        
  29.         //CSS
  30.         Requirements::css("autocomplete/css/autocomplete.css");    
  31.        
  32.         //JS
  33.         Requirements::javascript("autocomplete/javascript/jquery.autocomplete-min.js");
  34.  
  35.     }
  36.  
  37.    
  38.     function getCompetitions() {
  39.         if($Competitions = Competition::get()->filter(array('EntryStatus' => 'Closed','EntryClosingDateTime:greaterthan' => 'NOW'))){
  40.             return $Competitions->map('ID', 'CompetitionSummary', 'Please Select');
  41.         } else {
  42.             return array('No Open Competitions Found');
  43.         }
  44.     }
  45.    
  46.     function getMembers() {
  47.         if($Members = Member::get()){
  48.             return $Members->map('ID', 'FirstName', 'Please Select a Competition');
  49.         } else {
  50.             return array('No Members Found');
  51.         }
  52.     }
  53.    
  54.    
  55.     public function NewRegForm(){
  56.         return new RegForm($this, 'doNewRegForm');
  57.     }
  58.    
  59.     public function doNewRegForm($data){
  60.        
  61.         // Create Team and name it
  62.  
  63.  
  64.             $team = new Team();
  65.             $team->write();
  66.  
  67.        
  68.         // Link team to the selected Competition
  69.         if($data['Competitions'] != ''){
  70.             $comp = Competition::get()->byID($data['Competitions']);
  71.             $team->Competitions()->add($comp);
  72.         }else{
  73.             die('no comp entered');
  74.         }
  75.  
  76.         // Add team captain
  77.         $captain = Member::get()->byID(Member::currentUserID());
  78.         $team->Members()->add($captain);
  79.        
  80.        
  81.         // Loop through each of the additional members and add them to the team
  82.         $formfields = array('1','2','3','4');
  83.  
  84.         foreach ($formfields as $value) {
  85.             if($data['ExistingMemberName' . $value] != ''){
  86.                 // If they selected an existing member look them up and select the first in the list
  87.                 // This assumes that only 1 person by that name is in the system...
  88.                 $member = Member::get()
  89.                     ->filter(array(
  90.                         'FirstName' => $data['ExistingMemberName' . $value]))
  91.                     ->first();
  92.                 // Adds the exisiting member to the team
  93.                 $team->Members()->add($member);
  94.            
  95.             }elseif($data['NewMemberName' . $value] != ''){
  96.                 // If they selected to create a new member it's done here
  97.                 $member = new Member();
  98.                 $member->FirstName = $data['NewMemberName' . $value];
  99.                 $member->Email = $data['NewMemberEmail' . $value];
  100.                 // Password is set to a random number
  101.                 $tempPassword = substr(number_format(time() * rand(),0,'',''),0,7);
  102.                 $member->Password = $tempPassword;
  103.                 // Set an expiry so that they have to change their password on login
  104.                 $member->PasswordExpiry = '2000-01-01';
  105.                 $member->write();
  106.                 $team->Members()->add($member);
  107.    
  108.                 // Send email to new user          
  109.                 $From = '';
  110.                 $To = $data['NewMemberEmail' . $value];
  111.                 $Subject = 'Registration';
  112.                 $email = new Email($From, $To, $Subject);
  113.                
  114.                 // Which email template to use
  115.                 $email->setTemplate('NewMemberEmail');
  116.                 $emailDetails = array(
  117.                     'Captain'   =>  $captain->FirstName,
  118.                     'NewMemberName' => $data['NewMemberName' . $value],
  119.                     'NewMemberEmail' => $data['NewMemberEmail' . $value],
  120.                     'Password'  => $tempPassword
  121.                 );
  122.                 $email->populateTemplate($emailDetails);
  123.        
  124.                 $email->send();
  125.             }else{
  126.                 // No Member entered
  127.             }  
  128.         }
  129.         Controller::redirectBack();
  130.        
  131.     }
  132.    
  133.     public function results()
  134.     {      
  135.         if($query = $this->getSearchQuery())
  136.         {
  137.             $query = Convert::raw2xml($query);
  138.            
  139.             //Search for our query - Pretty basic example here
  140.             $Results = Member::get()->filter(array('FirstName:PartialMatch' => $query));
  141.             //$Members = $Results->map('ID', 'FirstName', 'Email');
  142.            
  143.                                    
  144.             $Suggestions = "['" . implode("', '", $Results->column('FirstName')) . "']";
  145.            
  146.             return $json = "{
  147.                 query : '$query',
  148.                 suggestions : $Suggestions
  149.                 }";
  150.         }
  151.  
  152.         Director::redirect($this->Link());
  153.     }
  154.    
  155.     function getSearchQuery()
  156.     {
  157.         if($this->request)
  158.             return $this->request->getVar("query");
  159.     }
  160. }
  161.  
  162. class RegForm extends Form {
  163.      
  164.    
  165.      public function __construct($controller, $name) {
  166.         if($Competitions = Competition::get()->filter(array(
  167.             'EntryStatus' => 'Open',
  168.             'EntryClosingDateTime:greaterthan' => 'NOW')
  169.         )){
  170.             $comps = $Competitions->map('ID', 'CompetitionSummary', 'Please Select');
  171.         } else {
  172.             $comps = array('No Open Competitions Found');
  173.         }
  174.            
  175.             $fields = new FieldList(
  176.                 new DropdownField('Competitions', 'Comps', $comps),
  177.                 new TextField('TeamName', 'TeamName'),
  178.                
  179.                 new TextField('ExistingMemberName1', 'Search', $controller->getSearchQuery()),
  180.                 TextField::create('NewMemberName1', 'Name')->setAttribute('required', 'true'),
  181.                 new EmailField('NewMemberEmail1', 'Email'),
  182.                 new DropdownField('Degree1', 'Degree1', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
  183.                 new TextField('CurrentMajor1', 'Current Major'),
  184.                 new TextField('StudentID1', 'StudentID'),
  185.                
  186.                 new TextField('ExistingMemberName2', 'Search', $controller->getSearchQuery()),
  187.                 new TextField('NewMemberName2', 'Name'),
  188.                 new EmailField('NewMemberEmail2', 'Email'),
  189.                 new DropdownField('Degree2', 'Degree2', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
  190.                 new TextField('CurrentMajor2', 'Current Major'),
  191.                 new TextField('StudentID2', 'StudentID'),
  192.                
  193.                 new TextField('ExistingMemberName3', 'Search', $controller->getSearchQuery()),
  194.                 new TextField('NewMemberName3', 'Name'),
  195.                 new EmailField('NewMemberEmail3', 'Email'),
  196.                 new DropdownField('Degree3', 'Degree3', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
  197.                 new TextField('CurrentMajor3', 'Current Major'),
  198.                 new TextField('StudentID3', 'StudentID'),
  199.                
  200.                 new TextField('ExistingMemberName4', 'Search', $controller->getSearchQuery()),
  201.                 new TextField('NewMemberName4', 'Name'),
  202.                 new EmailField('NewMemberEmail4', 'Email'),
  203.                 new DropdownField('Degree4', 'Degree4', array('Bcom', 'Bcom(hons)', 'BA', 'LLB', 'BSc', 'Bcom/LLB', 'BA/LLB', 'BSc/LLB', 'Bcom', 'Masters', 'MBus', 'PGDipcom')),
  204.                 new TextField('CurrentMajor4', 'Current Major'),
  205.                 new TextField('StudentID4', 'StudentID')
  206.             );
  207.  
  208.             $actions = new FieldList(
  209.                  new FormAction('submit', 'Submit')
  210.             );
  211.  
  212.             parent::__construct($controller, $name, $fields, $actions);
  213.      }
  214.  
  215.      public function forTemplate() {
  216.             return $this->renderWith(array(
  217.                  $this->class,
  218.                  'Form'
  219.             ));
  220.      }
  221.  
  222.      public function submit($data, $form) {
  223.          
  224.      }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement