<?php
namespace factory;

class Messages {
  
  public function addMessage( $text, $link )
  {
    global $db;
    
    $query = $db -> prepare( 'INSERT INTO pro_messages ( text, link ) VALUES ( :text, :link )' );
    $query -> bindValue( ':text', $text,  \PDO::PARAM_STR );
    $query -> bindValue( ':link', $link,  \PDO::PARAM_STR );
    $query -> execute();
    if ( $query -> rowCount() )
      return true;
    $query -> closeCursor();
    return false;
  }
  
  public function markMessageAsReaded( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'UPDATE pro_messages SET readed = 1 WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return true;
    $query -> closeCursor();
    return false;
  }
  
  public function getMessages()
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pro_messages WHERE readed = 0 ORDER BY date DESC' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $messages[] = $row;
    $query -> closeCursor();
    return $messages;
  }
}
?>
