<?php

include_once(dirname(__FILE__).'/../../Base/View.php');

class NewsView extends View
{
  var $ItemFormClass = array(
    'Title' => 'Aktualita',
    'Table' => 'News',
    'Items' => array(
      'Title' => array('Type' => 'String', 'Caption' => 'Titulek', 'Default' => ''),
      'Content' => array('Type' => 'Text', 'Caption' => 'Obsah', 'Default' => ''),
    ),
  );
  
  function AddFinish()
  {
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
    {
      $Form = new Form($this->System, $this->ItemFormClass);
      $Form->LoadValuesFromForm();
      $Form->Values['Time'] = 'NOW()';
      $Form->Values['User'] = $this->System->Modules['User']->User['Id'];
      $Form->SaveValuesToDatabase(0);
      $Output = $this->System->SystemMessage('Nová aktualita', 'Přidáno');
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
  
  function Add()
  {
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
    {
      $Form = new Form($this->System, $this->ItemFormClass);
      $Form->OnSubmit = '?Module=News&amp;Action=AddFinish';
      $Output = $Form->ShowEditForm();
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
  
  function View()
  {
    $Output = '<td class="News"><strong>Aktuálně:</strong><br />';
    $DbResult = $this->Database->query('SELECT * FROM News ORDER BY Time DESC LIMIT 10');
    while($DbRow = $DbResult->fetch_assoc())
    {
      $Output .= '<div><strong>'.$DbRow['Title'].'</strong>('.$DbRow['Time'].')<br />'.$DbRow['Content'].'</div>';
    }
    $Output .= '</td>';
    return($Output);
  }
}
