<?php
namespace guestbook;

class FGuestBook {

  const iLimit = 10;

  public function addEntry( $signature , $text )
  {
    global $db , $sys;

    $query = $db -> prepare( 'INSERT INTO pcms_guest_book ( signature , text , date) VALUES ( :signature , :text , :date )' );
    $query -> bindValue( ':signature'	, $signature        , \PDO::PARAM_STR );
    $query -> bindValue( ':text'      , $text             , \PDO::PARAM_STR );
    $query -> bindValue( ':date'      , $sys -> getDate()	, \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();
  }

  public function getLimit()
  {
    return self::iLimit;
  }

  public function getEntries( $from , $to )
  {
    global $db , $cache , $config;

    $sKey = "FGuestBook:getEntries:$from:$to";

    if ( !$aEntries = $cache -> fetch ( $sKey ) )
    {
			$query = $db -> prepare( 'SELECT signature, text, date FROM pcms_guest_book ORDER BY date DESC LIMIT ' . $from . ',' . $to );
			$query -> execute();
			if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
			{
				$entry['signature']	= $row['signature'];
				$entry['text']      = nl2br($row['text']);
				$entry['date']      = date("d/m/Y H:i",strtotime($row['date']));
				$aEntries[]         = $entry;
			}
      $cache -> store( $sKey , $aEntries , $config['cache_expire_short'] );
    }
    return $aEntries;
  }

  public function getCountEntries()
  {
    global $db , $cache , $config;

    $sKey = 'FGuestBook:getCountEntries';

    if ( !$iCount_gb = $cache -> fetch( $sKey ) )
    {
      $query = $db->prepare( 'SELECT COUNT(1) FROM pcms_guest_book' );
      $query->execute();
      if ( $query->rowCount() ) while ( $row = $query->fetch() )
        $iCount_gb 	= $row[0];
      $query->closeCursor();
      $cache -> store( $sKey , $iCount_gb , $config['cache_expire_short'] );
    }
    return $iCount_gb;
  }
}
?>
