<?php

include_once(dirname(__FILE__).'/../../Base/View.php');

class UserView extends View
{
  var $OptionsFormClass = array(
    'Title' => 'Základní nastavení',
    'Table' => 'User',
    'SubmitText' => 'Uložit',
    'Items' => array(
      'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
      'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
      'Name' => array('Type' => 'String', 'Caption' => 'Zobrazované jméno', 'Default' => ''),
      'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
      //'PhoneNumber' => array('Type' => 'String', 'Caption' => 'Telefón', 'Default' => ''),
      //'ICQ' => array('Type' => 'String', 'Caption' => 'ICQ', 'Default' => ''),
    ),
  );
  var $RegisterFormClass = array(
    'Title' => 'Registrace uživatele',
    'SubmitText' => 'Registrovat',
    'Table' => 'User',
    'Items' => array(
      'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
      'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
      'Password2' => array('Type' => 'Password', 'Caption' => 'Potvrzení hesla', 'Default' => ''),
      'Name' => array('Type' => 'String', 'Caption' => 'Zobrazované jméno', 'Default' => ''),
      'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
      //'PhoneNumber' => array('Type' => 'String', 'Caption' => 'Telefón', 'Default' => ''),
      //'ICQ' => array('Type' => 'String', 'Caption' => 'ICQ', 'Default' => ''),
    ),
  );
  var $PasswordRecoveryFormClass = array(
    'Title' => 'Obnova hesla',
    'SubmitText' => 'Obnovit',
    'Table' => '',
    'Items' => array(
      'Name' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
      'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
    ),
  );  
  var $LoginFormClass = array(
    'Title' => 'Přihlášení uživatele',
    'SubmitText' => 'Přihlásit',
    'Table' => '',
    'Items' => array(
      'Username' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
      'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
    ),
  );
  
  function Login()
  {
    $Form = new Form($this->System, $this->LoginFormClass);
    $Form->OnSubmit = '?Module=User&amp;Action=LoginFinish';
    $Output = $Form->ShowEditForm();
    $Output .= '<div class="Centred">';
    if($this->Config['Web']['UserRegistrationEnabled']) 
      $Output .= '<a href="?Module=User&amp;Action=Register">Registrovat se</a> ';
    $Output .= '<a href="?Module=User&amp;Action=PasswordRecovery">Obnova zapomenutého hesla</a></div>';
    return($Output);
  }
  
  function LoginFinish()
  {
    $Form = new Form($this->System, $this->LoginFormClass);
    $Form->OnSubmit = '?Module=User&amp;Action=LoginFinish';
    $Form->LoadValuesFromForm();
    $Result = $this->System->Modules['User']->Login($Form->Values['Username'], $Form->Values['Password']);
    $Page = new PageView($this->System);
    $Output = $Page->SystemMessage('Přihlášení', $Result);
    if($Result <> $this->System->Translate('UserLoggedIn'))
    {
      $Form->Values['Password'] = '';
      $Output .= $Form->ShowEditForm();
      $Output .= '<div class="Centred"><a href="?Module=User&amp;Action=UserRegister">Registrovat se</a> '.
        '<a href="?Module=User&amp;Action=PasswordRecovery">Obnova zapomenutého hesla</a></div>';
    }
    return($Output);
  }
  
  function RegisterSave()
  {
    if($this->Config['Web']['UserRegistrationEnabled']) 
    {
      $Form = new Form($this->System, $this->RegisterFormClass, array());
      $Form->LoadValuesFromForm();
      $Result = $this->System->Modules['User']->Register($Form->Values['Login'], $Form->Values['Password'], $Form->Values['Password2'], $Form->Values['Email'], $Form->Values['Name']);
      $Page = new PageView($this->System);
      $Output = $Page->SystemMessage('Registrace nového účtu', $Result);
      if($Result <> $this->System->Translate('UserRegistrated'))
      {
        $Form->OnSubmit = '?Module=User&amp;Action=RegisterSave';
        $Output = $Form->ShowEditForm();
      }
    } else $Output = 'Registrace nových účtů nejsou povoleny.';
    return($Output);
  }
  
  function PasswordRecoveryConfirm()
  {
    $Page = new PageView($this->System);
    $Output = $Page->SystemMessage('Obnova hesla', $this->System->Modules['User']->PasswordRecoveryConfirm($_GET['User'], $_GET['H'], $_GET['P']));
    $Output .= $this->LoginForm();
    return($Output);
  }
  
  function PasswordRecoveryFinish()
  {
    $Form = new Form($this->System, $this->PasswordRecoveryFormClass);
    $Form->LoadValuesFromForm();
    $Result = $this->System->Modules['User']->PasswordRecoveryRequest($Form->Values['Name'], $Form->Values['Email']);
    $Page = new PageView($this->System);
    $Output = $Page->SystemMessage('Obnova hesla', $Result);
    if($Result <> $this->System->Translate('UserPasswordRecoverySuccess'))
    {
      $Output .= $Form->ShowEditForm();
    }        
    return($Output);
  }
  
  function PasswordRecovery()
  {
    $Form = new Form($this->System, $this->PasswordRecoveryFormClass);
    $Form->OnSubmit = '?Module=User&amp;Action=PasswordRecovery2';
    $Output = $Form->ShowEditForm();
    return($Output);
  }
  
  function Logout()
  {
    $Page = new PageView($this->System);
    $Output = $Page->SystemMessage('Odhlášení', $this->System->Modules['User']->Logout());
    $Output .= $this->Login();
    return($Output);
  }
  
  function Options()
  {
    $UserOptions = new Form($this->System, $this->OptionsFormClass);
    $UserOptions->LoadValuesFromDatabase($this->System->Modules['User']->Data['Id']);
    $UserOptions->OnSubmit = '?Module=User&amp;Action=OptionsSave';
    $Output = $UserOptions->ShowEditForm();
    return($Output);
  }
  
  function OptionsSave()
  {
    $UserOptions = new Form($this->System, $this->OptionsFormClass, array());
    $UserOptions->LoadValuesFromForm();
    $UserOptions->SaveValuesToDatabase($this->System->Modules['User']->Data['Id']);
    $Page = new PageView($this->System);
    $Output = $Page->SystemMessage('Nastavení', 'Nastavení uloženo.');
    $this->System->Modules['Log']->NewRecord('User', 'Nastavení uživatele změněno', $UserOptions->Values['Name']);
    $UserOptions->LoadValuesFromDatabase($this->System->Modules['User']->Data['Id']);
    $UserOptions->OnSubmit = '?Module=User&amp;Action=UserOptionsSave';
    $Output .= $UserOptions->ShowEditForm();
    return($Output);
  }
  
  function Register()
  {
    if($this->Config['Web']['UserRegistrationEnabled']) 
    {
      $Form = new Form($this->System, $this->RegisterFormClass);
      $Form->LoadValuesFromForm();
      $Form->OnSubmit = '?Module=User&amp;Action=RegisterSave';
      $Output = 'Vyplňte správně požadované údaje. Na zadaný email vám bude zaslán aktivační email.';
      $Output .= $Form->ShowEditForm();
    } else $Output = 'Registrace nových účtů nejsou povoleny.';
    return($Output);
  }
  
  function RegisterConfirm()
  {
    if($this->Config['Web']['UserRegistrationEnabled']) 
    {
      $Page = new PageView($this->System);
      $Output = $Page->SystemMessage('Potvrzení registrace', $this->System->Modules['User']->RegisterConfirm($_GET['User'], $_GET['H']));
      $Output .= $this->ShowLoginForm();
    } else $Output = 'Registrace nových účtů nejsou povoleny.';
    return($Output);
  }
}