Skip to content

Instantly share code, notes, and snippets.

@jaspertandy
Created October 4, 2011 14:15
Show Gist options
  • Save jaspertandy/1261742 to your computer and use it in GitHub Desktop.
Save jaspertandy/1261742 to your computer and use it in GitHub Desktop.
<?php
class Competition_Validation extends Validation {
protected $competition;
protected $guess;
public function init(){
$this->add_rule( 'name' , 'required' );
$this->add_rule( 'email' , 'required' );
$this->add_rule( 'email' , 'email' );
$this->add_rule( 'answer' , array( $this , 'valid_competition' ) );
$this->add_rule( 'email' , array( $this , 'ip_cant_enter' ) );
}
public function valid_competition( Validation $v , $field ){
$this->competition = Competitions::factory( $this->d() )->get_latest_live();
if ( !$this->competition->loaded() ) return false;
foreach ( $this->competition->answers() as $answer ) {
if ( (int) $answer['id'] === (int) $v->$field ) $this->guess = $answer;
}
return $this->guess ? true : false;
}
public function ip_cant_enter( Validation $v , $field ){
if ( !$this->competition ) return false;
$entries = $this->d()
->db
->query('select count(*) c
from competitionGuesses
where ip = :ip and competitionId = :competitionid' , array(
'ip' => $_SERVER['REMOTE_ADDR'],
'competitionid' => $this->competition->id
))
->shift();
return $entries['c'] > 0 ? false : true;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment