<?php

class PageChatHistory extends Page
{
  function __construct()
  {
    parent::__construct();
    $this->FullTitle = 'Výpis z historie chatu';
    $this->ShortTitle = 'Historie chatu';
    $this->NavigationPath[] = array('Name' => $this->ShortTitle, 'URL' => 'chat');     
  }
  
  function dechexr($Num)
  {
    $Num = dechex($Num);
    return(substr($Num, 4, 2).substr($Num, 2, 2).substr($Num, 0, 2));
  }

  function Show()
  {
    global $MonthNames;
    
    if(!$this->System->Modules['User']->CheckPermission('Chat', 'Display')) return('Nemáte oprávnění');

    if(array_key_exists('date', $_GET)) $Date = $_GET['date']; 
      else $Date = date('Y-m-d');
    $DateParts = explode('-', $Date);

    $DbResult = $this->Database->select('ChatHistory', 'MAX(Time), MIN(Time)');
    $RowTotal = $DbResult->fetch_array();

    $StartDateTimeParts = explode(' ', $RowTotal['MIN(Time)']);
    $StartDateParts = explode('-', $StartDateTimeParts[0]);
    $EndDateTimeParts = explode(' ', $RowTotal['MAX(Time)']);
    $EndDateParts = explode('-', $EndDateTimeParts[0]);

    if(!array_key_exists('year', $_SESSION)) $_SESSION['year'] = date('Y', time());
    if(array_key_exists('year', $_GET)) $_SESSION['year'] = addslashes($_GET['year']);

    if(!array_key_exists('month', $_SESSION)) $_SESSION['month'] = date('n', time());
    if(array_key_exists('month', $_GET)) $_SESSION['month'] = addslashes($_GET['month']);

    $Output = '<div class="ChatHistory">';
    for($Year = $EndDateParts[0]; $Year >= $StartDateParts[0]; $Year--)
    {
      if($_SESSION['year'] == $Year)
      {
        $Output .= '<div class="Year">'.$Year.'<div class="YearContent">';
        if($Year == $StartDateParts[0]) $StartMonth = ($StartDateParts[1] + 0); else $StartMonth = 1;
        if($Year == $EndDateParts[0]) $EndMonth = ($EndDateParts[1] + 0); else $EndMonth = 12;
        for($Month = $EndMonth; $Month >= $StartMonth; $Month--)
        {
          if($_SESSION['month'] == $Month)
          {
            $Output .= '<div class="Months">'.$MonthNames[$Month].'<span>';
            if(($Year == $StartDateParts[0]) and ($Month == $StartDateParts[1])) $StartDay = ($StartDateParts[2]+0); else $StartDay = 1;
            if(($Year == $EndDateParts[0]) and ($Month == $EndDateParts[1])) $EndDay = ($EndDateParts[2]+0); else $EndDay = date('t',mktime(0,0,0,$Month,0,$Year));
            for($Day = $StartDay; $Day <= $EndDay; $Day++) 
            { 
              $Text = '<a href="?date='.$Year.'-'.$Month.'-'.$Day.'">'.$Day.'</a> ';
              if(($DateParts[0] == $Year) and ($DateParts[1] == $Month) and ($DateParts[2] == $Day)) $Text = '<strong>'.$Text.'</strong>';
              $Output .= $Text;
            }
            $Output .= '</span></div>';
          } else $Output .= '<span><a href="?month='.$Month.'">'.$MonthNames[$Month].'</a></span> ';
        }
        $Output .='</div></div>';
      } else $Output .= '<div class="Year"><a href="?year='.$Year.'">'.$Year.'</a></div>';
    }
    $Output .= '</div><div></div>';

    $DbResult = $this->Database->select('ChatHistory', 'Nick, Color, Text, UNIX_TIMESTAMP(Time)', "RoomType = 0 AND Time > '".$Date." 00:00:00' AND Time < '".$Date." 23:59:59' ORDER BY Time DESC");
    $Output .= '<div class="ChatHistoryText">';
    if($DbResult->num_rows > 0)
    while($Row = $DbResult->fetch_array())
    {
      $Text = $Row['Text'];;
      // StrTr($Row['text'], "\x8A\x8D\x8E\x9A\x9D\x9E", "\xA9\xAB\xAE\xB9\xBB\xBE"); 
      $Output .= '['.date('d.m.Y H:i:s',$Row['UNIX_TIMESTAMP(Time)']).'] <span style="color: #'.$this->dechexr($Row['Color']).'"><strong>&lt;'.$Row['Nick'].'&gt;</strong> '.(htmlspecialchars($Text)).'</span><br>';
    }
    else $Output .= 'V daném dni nebyly zaznamenány žádné zprávy.';
    $Output .= '</div>';
    return($Output);
  }
}

class ModuleChat extends AppModule
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Name = 'Chat';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPL';
    $this->Description = 'Show history of IRC chat and previous SunriseChat';
    $this->Dependencies = array();
  }
  
  function Start()
  {
    parent::Start();
    $this->System->Pages['chat'] = 'PageChatHistory';
  }  
}

?>
