Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AionVote{
- protected $data;
- protected $connection;
- function AionVote(){
- //theoriticly there should be database manager class to conect once and the only return instance of conection
- //but you can just initialize it here
- //this data should be in some configuration file
- $user = "aionrenegade";
- $password = "aiondevwashere";
- $database = "au_server_gs3";
- // Connect to the database
- $this->connection = mysql_connect("74.208.218.54",$user,$password) or die ('Could not connect: ' . mysql_error());
- mysql_select_db($database, $this->connection);
- }
- public function getData($account_id){
- //check if $account_id isn't account name it should be a number id of account
- $this->data = array();
- //feeding data about account character
- $query = mysql_query("SELECT id, name FROM players WHERE account_name ='$account_id'") or die("ERROR:".mysql_error());
- while($row = mysql_fetch_array( $query )){
- $this->data['account_characters'][]=$row;
- }
- //feeding data about avible prizes
- $query = mysql_query("SELECT prize_id, prize_name FROM vote_prizes") or die("ERROR:".mysql_error());
- while($row = mysql_fetch_array( $query )){
- $this->data['voting_prizes'][]=$row;
- }
- //feeding data about voting pages
- $query = mysql_query("SELECT id, link, img FROM vote_page_list") or die("ERROR:".mysql_error());
- while($row = mysql_fetch_array( $query )){
- $lastvoted_query = mysql_query("SELECT time,CURRENT_TIMESTAMP-time dif FROM vote_logs WHERE account_name='$account_id' AND link_id='$row[id]'") or die("ERROR:".mysql_error());
- if(mysql_num_rows($lastvoted_query)>0){
- $lastvoted = mysql_fetch_array( $lastvoted_query );
- }
- else{
- $lastvoted = array('time'=>'You may now vote.');
- }
- $this->data['voting_pages'][]=array_merge($row,$lastvoted);
- }
- return $this->data;
- }
- public function generateLinks(){
- $list='';
- for($i=0;$i<count($this->data['voting_pages']);$i++){
- $list.=$this->linkSchema($this->data['voting_pages'][$i]['id'],$this->data['voting_pages'][$i]['link'],$this->data['voting_pages'][$i]['img'],$this->data['voting_pages'][$i]['time']);
- }
- return $list;
- }
- protected function linkSchema($id,$link,$img,$last){
- return '<div class="vote"><a class="vote" onclick="return AionVote.open(this,'.$id.')" href="'.$link.'" target="_blank"><img width="65" height="42" src="'.$img.'"/></a><p>Last time voted:</p><span id="vote_comment_'.$id.'">'.$last.'</span></div>'."\n";
- }
- public function generateAccountCharacterHTMLOptions(){
- $options='';
- for($i=0;$i<count($this->data['account_characters']);$i++){
- $options.='<option value="'.$this->data['account_characters'][$i]['id'].'">'.$this->data['account_characters'][$i]['name'].'</option>';
- }
- return $options;
- }
- public function generatePrizeHTMLOptions(){
- $options='';
- for($i=0;$i<count($this->data['voting_prizes']);$i++){
- $options.='<option value="'.$this->data['voting_prizes'][$i]['prize_id'].'">'.$this->data['voting_prizes'][$i]['prize_name'].'</option>';
- }
- return $options;
- }
- public function saveVote($account_id,$link_id,$prize_id){
- //security here
- $link_id = intval($link_id);
- //$char_id = intval($char_id);
- $prize_id = intval($prize_id);
- if($account_id!=0&&$link_id!=0&&$prize_id!=0){
- //checking if player already voted
- $query = mysql_query("SELECT * FROM vote_logs WHERE account_name='$account_id' AND link_id='$link_id'")or die("ERROR:".mysql_error());
- if(mysql_num_rows($query)>0)//if there is already a row with such vote we will update it if the time from last update is more than 12h
- {
- mysql_query("UPDATE vote_logs SET prize_id='$prize_id', time=CURRENT_TIMESTAMP WHERE account_name='$account_id' AND link_id='$link_id' AND CURRENT_TIMESTAMP>DATE_ADD(time,INTERVAL 12 HOUR)")or die("ERROR:".mysql_error());
- if(mysql_affected_rows()==0){
- return "Last voting occurred less than 12h from now.";
- }
- }
- else//else we will insert new row
- {
- mysql_query("INSERT INTO vote_logs (account_name,link_id,prize_id) VALUES ('$account_id','$link_id','$prize_id')")or die("ERROR:".mysql_error());
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement