<?php

class TypeDateTime extends TypeBase
{
  var $DatabaseCompareOperators = array('Rovno' => '=', 'Nerovno' => '!=', 'Menší' => '<', 'Větší' => '>');

  function OnView($Item)
  {
    global $MonthNames;

    $ValueParts = explode(' ', $Item['Value']);
    $DateParts = explode('-', $ValueParts[0]);
    $TimeParts = explode(':', $ValueParts[1]);

    $Output = $TimeParts[0].':'.$TimeParts[1].':'.$TimeParts[2].' '.($DateParts[2] * 1).'. '.$MonthNames[$DateParts[1] * 1].' '.$DateParts[0];
    return($Output);
  }

  function OnEdit($Item)
  {
    global $MonthNames;

    $ValueParts = explode(' ', $Item['Value']);
    $DateParts = explode('-', $ValueParts[0]);
    $TimeParts = explode(':', $ValueParts[1]);

    // Hour
    $Output = '<select name="'.$Item['Name'].'-hour">';
    for($I = 1; $I <= 24; $I++)
    {
      if($TimeParts[2] == $I) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    }
    $Output .= '</select>';
    // Minute
    $Output .= '<select name="'.$Item['Name'].'-minute">';
    for($I = 1; $I <= 60; $I++)
    {
      if($TimeParts[1] == $I) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    }
    $Output .= '</select>';
    // Second
    $Output .= '<select name="'.$Item['Name'].'-second">';
    for($I = 1; $I <= 60; $I++)
    {
      if($TimeParts[0] == $I) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    }
    $Output .= '</select>';
    // Day
    $Output .= ' <select name="'.$Item['Name'].'-day">';
    for($I = 1; $I <= 31; $I++)
    {
      if($DateParts[2] == $I) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    }
    $Output .= '</select>';
    // Month
    $Output .= '<select name="'.$Item['Name'].'-month">';
    for($I = 1; $I <= 12; $I++)
    {
      if($DateParts[1] == $I) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$I.'"'.$Selected.'>'.$MonthNames[$I].'</option>';
    }
    $Output .= '</select>';
    // Year
    $Output .= '<select name="'.$Item['Name'].'-year">';
    for($I = 1900; $I < 2100; $I++)
    {
      if($DateParts[0] == $I) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    }
    $Output .= '</select>';
    return($Output);
  }

  function OnLoad($Item)
  {
    return($_POST[$Item['Name'].'-year'].'-'.$_POST[$Item['Name'].'-month'].'-'.$_POST[$Item['Name'].'-day'].' '.
      $_POST[$Item['Name'].'-hour'].':'.$_POST[$Item['Name'].'-minute'].':'.$_POST[$Item['Name'].'-second']);
  }

  function DatabaseEscape($Value)
  {
    return('"'.addslashes($Value).'"');
  }
}

?>