<?php

class ModuleLog extends AppModule
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Name = 'Log';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Logging user actions';
    $this->Dependencies = array('User');
  }
  
  function Install()
  {
    parent::Install();
  }

  function UnInstall()
  {
    parent::UnInstall();
  }  
  
  function Start()
  {
    parent::Start();
    $this->System->FormManager->RegisterClass('Log', array(
      'Title' => 'Záznamy',
      'Table' => 'Log',
      'DefaultSortColumn' => 'Time',
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => '', 'ReadOnly' => true),
        'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => '', 'Null' => true, 'ReadOnly' => true),
        'Module' => array('Type' => 'String', 'Caption' => 'Modul', 'Default' => '', 'ReadOnly' => true),
        'Operation' => array('Type' => 'String', 'Caption' => 'Operace', 'Default' => '', 'ReadOnly' => true),
        'Value' => array('Type' => 'Text', 'Caption' => 'Hodnota', 'Default' => '', 'ReadOnly' => true),
        'IPAddress' => array('Type' => 'Text', 'Caption' => 'Adresa IP', 'Default' => '', 'ReadOnly' => true),
      ),
    ));
    
  }
  
  function Stop()
  {
  	parent::Stop();
  }
  
  function NewRecord($Module, $Operation, $Value = '')
  {
    if(array_key_exists('User', $this->System->Modules) and 
      array_key_exists('Id', $this->System->User->User))
      $UserId = $this->System->User->User['Id'];
      else $UserId = NULL;
    $this->Database->insert('Log', array('Time' => 'NOW()', 
      'User' => $UserId, 'Module' => $Module, 
      'Operation' => $Operation, 'Value' => $Value, 'IPAddress' => $_SERVER['REMOTE_ADDR']));
  }
}
