<?php

$EventsLinks = array
(
  1 => array('http://wwpw.wowwiki.com/Midsummer_Fire_Festival', 'http://www.worldofwarcraft.com/info/events/midsummer/'),
  2 => array('http://www.wowwiki.com/Feast_of_Winter_Veil', 'http://www.worldofwarcraft.com/info/events/winterveil/'),
  3 => array('http://www.wowwiki.com/Darkmoon_Faire', 'http://www.worldofwarcraft.com/info/basics/factions/darkmoon/'),
  4 => array('http://www.wowwiki.com/Darkmoon_Faire', 'http://www.worldofwarcraft.com/info/basics/factions/darkmoon/'),
  5 => array('http://www.wowwiki.com/Darkmoon_Faire', 'http://www.worldofwarcraft.com/info/basics/factions/darkmoon/'),
  6 => array('http://www.wowwiki.com/New_Year', 'http://www.worldofwarcraft.com/info/events/newyears/'),
  7 => array('http://www.wowwiki.com/Lunar_Festival', 'http://www.worldofwarcraft.com/info/events/lunarnewyears/'),
  8 => array('http://www.wowwiki.com/Love_is_in_the_Air', 'http://www.worldofwarcraft.com/info/events/loveisintheair/'),
  9 => array('http://www.wowwiki.com/Noblegarden', 'http://www.worldofwarcraft.com/community/ingameevents.html#noblegarden'),
  10 => array('http://www.wowwiki.com/Children%27s_Week', 'http://www.worldofwarcraft.com/info/events/childrensweek/'),
  11 => array('http://www.wowwiki.com/Harvest_Festival', 'http://www.worldofwarcraft.com/info/events/harvestfestival/'),
  12 => array('http://www.wowwiki.com/Hallow%27s_End', 'http://www.worldofwarcraft.com/info/events/hallowsend/'),
  13 => array('http://www.wowwiki.com/Elemental_Invasions', 'http://www.worldofwarcraft.com/community/ingameevents.html#elemental'),
  14 => array('http://www.wowwiki.com/Stranglethorn_Fishing_Extravaganza', 'http://www.worldofwarcraft.com/info/events/fishing/'),
  15 => array('http://www.wowwiki.com/Stranglethorn_Fishing_Extravaganza', 'http://www.worldofwarcraft.com/info/events/fishing/'),
  16 => array('http://www.wowwiki.com/Gurubashi_Arena#Gurubashi_Arena_Booty_Run', 'http://www.worldofwarcraft.com/community/ingameevents.html#gurubashi'),
  17 => array('http://www.wowwiki.com/The_Scourge_Invasion', 'http://www.worldofwarcraft.com/info/events/scourgeinvasion/'),
  18 => array('', 'http://www.worldofwarcraft.com/community/ingameevents.html#calltoarms'),
  19 => array('', 'http://www.worldofwarcraft.com/community/ingameevents.html#calltoarms'),
  20 => array('', 'http://www.worldofwarcraft.com/community/ingameevents.html#calltoarms'),
  21 => array('', 'http://www.worldofwarcraft.com/community/ingameevents.html#calltoarms'),
  22 => array('http://www.wowwiki.com/Ahn%27Qiraj_War_Effort', 'http://www.worldofwarcraft.com/wareffort/servers.html'),
  23 => array('', ''),
  24 => array('', ''),
  25 => array('', 'http://www.worldofwarcraft.com/community/ingameevents.html#calltoarms'),
  26 => array('http://www.wowwiki.com/Brewfest', 'http://www.worldofwarcraft.com/info/events/brewfest/'),
  27 => array('', ''),
  28 => array('http://www.wowwiki.com/Noblegarden', 'http://www.worldofwarcraft.com/community/ingameevents.html#noblegarden'),
  29 => array('http://www.wowwiki.com/Edge_of_Madness', ''),
  30 => array('http://www.wowwiki.com/Edge_of_Madness', ''),
  31 => array('http://www.wowwiki.com/Edge_of_Madness', ''),
  32 => array('http://www.wowwiki.com/Edge_of_Madness', ''),
  34 => array('http://www.wowwiki.com/L70ETC', ''),
);

function Events()
{
  global $db, $EventsLinks, $Config, $System;
  
  $Events = array('Running' => array(), 'Planned' => array());
  $Realm = new Realm($System, $_COOKIE['RealmIndex']);

  $DbResult = $Realm->MangosDatabase->query('SELECT *, UNIX_TIMESTAMP(start_time) as start FROM game_event WHERE end_time > NOW() AND start_time < NOW()');
  while($Row = $DbResult->fetch_array())
  { 
    if(!array_key_exists($Row['entry'], $EventsLinks)) $EventsLinks[$Row['entry']] = array('', '');
    $Start = (floor((time() - $Row['start']) / ($Row['occurence'] * 60))) * $Row['occurence'] * 60 + $Row['start'];
    $End = $Start + $Row['length'] * 60;
    if((time() > $Start) and (time() < $End)) $EventState = 'Running';
    else 
    {
      $EventState = 'Planned'; 
      $Start += $Row['occurence'] * 60;
      $End += $Row['occurence'] * 60;
    }
    $Events[$EventState][$Row['start']] = array('StartTime' => $Start, 'EndTime' => $End, 'Title' => $Row['description'], 'WoWWiki' => $EventsLinks[$Row['entry']][0], 'Official' => $EventsLinks[$Row['entry']][1]);
  }

  // Add events not started so far
  //$DbResult = $db->select('game_event', '*, UNIX_TIMESTAMP(start_time) as start', 'start_time > NOW()');
  $DbResult = $db->query('SELECT * , UNIX_TIMESTAMP(start_time) as start FROM game_event WHERE start_time > NOW()');
  while($Row = $DbResult->fetch_array())
  { 
    if(!array_key_exists($Row['entry'], $EventsLinks)) $EventsLinks[$Row['entry']] = array('', '');
    $Events['Planned'][$Row['start']] = array('StartTime' => $Row['start'], 'EndTime' => ($Row['start'] + $Row['length'] * 60), 'Title' => $Row['description'], 'WoWWiki' => $EventsLinks[$Row['entry']][0], 'Official' => $EventsLinks[$Row['entry']][1]);
  }

  sort($Events['Running']);
  sort($Events['Planned']);

  return($Events);
}

function ShowEventList($Events)
{
  foreach($Events as $Event)
  {
    echo('<tr><td>'.$Event['Title'].'</td><td>'.date('j.n.Y H:i', $Event['StartTime']).'</td><td>'.date('j.n.Y H:i', $Event['EndTime']).'</td>');
    if($Event['WoWWiki'] != '') echo('<td><a href="'.$Event['WoWWiki'].'">wowwiki</a>'); 
else echo('<td>&nbsp;');
    if($Event['Official'] != '') echo(' <a href="'.$Event['Official'].'">worldofwarcraft</a></td>'); else echo(' &nbsp;</td>');
    echo('</tr>');
  }
}

echo('<h3 class="PageTitle">Události ve hře</h3>');
$Realm = new Realm($System, $_COOKIE['RealmIndex']);
$db = $Realm->MangosDatabase;

$Events = Events();
echo('<table class="BaseTable">');
echo('<tr><th>Název</th><th>Začátek</th><th>Konec</th><th>Informace</th></tr>');
echo('<tr><td colspan="5"><b>Probíhající akce</b></td></tr>');
ShowEventList($Events['Running']);
echo('<tr><td colspan="5"><b>Plánované akce</b></td></tr>');
ShowEventList($Events['Planned']);
echo('</table>');

?>
