1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

class RootController extends Extension {

	static $allowed_actions = array(
		"RegistrationForm"
	);

	public function onBeforeInit() {
	
	        $themeFolder = $this->owner->ThemeDir();
	        
			$cssFiles = array(
			    $themeFolder . '/css/normalize.css',
			    $themeFolder . '/css/app.css'
			);
			
			$jsFiles = array(
			   'framework/thirdparty/jquery/jquery.js',
			    $themeFolder.'/javascript/foundation/foundation.js',
				$themeFolder.'/javascript/foundation/foundation.alerts.js',
				$themeFolder.'/javascript/foundation/foundation.cookie.js',
				$themeFolder.'/javascript/foundation/foundation.placeholder.js',
				$themeFolder.'/javascript/foundation/foundation.reveal.js',
				"mysite/javascript/jquery.validate.min.js",
			    'mysite/javascript/functions.js'
			    
			);
			
			Requirements::set_combined_files_folder($this->owner->ThemeDir().'/combined');
			Requirements::combine_files("common.css", $cssFiles);
			Requirements::combine_files("common.js", $jsFiles);
					
	   }
	   
	   function RegistrationForm()   {
        $fields = new FieldList(
            new TextField('FirstName', 'First Name'),
            new TextField('Surname', 'Surname'),
            new EmailField('Email', 'E-mail Address'),
            new PasswordField('Password', 'Password')            
        );
         
        // Create action
        $actions = new FieldList(
            new FormAction('doRegister', 'Sign Up')
        );
        // Create action
        $validator = new RequiredFields('FirstName', 'Surname', 'Email', 'Password');
 		
 		$form = new Form($this->owner, 'RegistrationForm', $fields, $actions, $validator);
 		//$form->addExtraClass("formee");
 		
 		$fields->dataFieldByName('FirstName')->addExtraClass('required');
 		$fields->dataFieldByName('Surname')->addExtraClass('required');
 		$fields->dataFieldByName('Email')->addExtraClass('required');
 		$fields->dataFieldByName('Password')->addExtraClass('required');
 		
        return $form;     
    }  
    
    //Submit the registration form
    function doRegister ( $data, $form ) {
    
        //Check for existing member email address
        if( $member = DataObject::get_one("Member", "`Email` = '". Convert::raw2sql($data['Email']) . "'") ) {
        
            //Set error message
            $form->AddErrorMessage('Email', "Sorry, that email address already exists.", 'bad');
            
            //Set form data from submitted values
            Session::set("FormInfo.Form_RegistrationForm.data", $data);    
            
            //Return back to form
            return $this->owner->redirectBack();
        }  
 
        //Otherwise create new member and log them in
        $Member = new Member();
        $form->saveInto($Member); 
        
		// Add to General Members Group
		$group = DataObject::get_one('Group', "ID = 3");
        
        // Logo user system info to detect duplicate accounts
        $Member->System = $_SERVER['HTTP_USER_AGENT'];
		$Member->IP = $_SERVER['REMOTE_ADDR'];
        
        // write and login the member
        $Member->write();
        $Member->login();
        $Member->Groups()->add($group);
         
        // set response and redirect to my account page
        $this->owner->setFlash("Thanks for signing up, you can now vote!","success");        
		return $this->owner->redirect("/");
    }
    
    function setFlash($message,$type) {
   		$array = array("Message" => $message, "Type" => $type);
   		Session::set("Flash", $array);
    }
   
   function getFlash() {
   		if ($array = Session::get("Flash")) {
   			Session::clear("Flash");
   			return new ArrayData($array);
   		}    			
   		return false;
   }
  

}