<?php
namespace article;

class FArticle {
  
  public function addComment( $article_id, $author, $text, $captcha_code, $check )
  {
    global $db, $securimage, $lang;
    
    if ( !$article_id || !$author || !$text || \System::getSessionVar( 'check' ) == $check || !$securimage -> check( $captcha_code ) )
      return false;
    
    $query = $db -> prepare( 'INSERT INTO pp_articles_comments ( article_id, author, text ) VALUES ( :article_id, :author, :text )' );
    $query -> bindValue( ':article_id', $article_id,    \PDO::PARAM_INT );
    $query -> bindValue( ':author',     '~' . $author,  \PDO::PARAM_STR );
    $query -> bindValue( ':text',       $text,          \PDO::PARAM_STR );
    $query -> execute();
    if ( $query -> rowCount() )
      return \System::setAlert( $lang -> getTrans( 'T_KOMENTARZ_ZOSTAL_DODANY' ) );
    $query -> closeCursor();
    return false;
  }
  
  public function isArticleOnlyForLogged( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT pp.id FROM pp_articles_pages AS pap, pp_pages AS pp WHERE pp.id = pap.page_id AND article_id = :article_id AND only_for_logged = 1' );
    $query -> bindValue( ':article_id', $id, \PDO::PARAM_STR );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return true;
    $query -> closeCursor();
    return false;
  }
  
  public function getArticleLayoutId( $id )
  {
    global $db, $config, $cache;
    
    $key = 'getArticleLayoutId:' . $id;
    if ( !$layout = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT id_layout FROM pp_articles WHERE id = :id' );
      $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $layout = $row[0];
      $query -> closeCursor();
      $cache -> store( $key, $layout, $config['cache_expire_long'] );
    }
    
    if ( !$layout )
    {
      $query = $db -> query( 'SELECT id FROM pp_layouts WHERE enabled = 1' );
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $layout = $row[0];
      $query -> closeCursor();
      $cache -> store( $key, $layout, $config['cache_expire_long'] );
    }
    return $layout;
  }

  public function searchArtileByTextCount( $text )
  {
    global $db;

    $text = \System::saveString( $text, true );
    
    $query = $db -> query( 'SELECT COUNT(DISTINCT(article_id)) FROM pp_articles_langs WHERE ( LOWER( title ) LIKE "%' . \System::saveString( $text ) . '%" OR LOWER( text ) LIKE "%' . \System::saveString( $text ) . '%" ) AND article_id IN ( SELECT id FROM pp_articles WHERE enabled = 1 AND archive = 0 ) GROUP BY article_id' );
    return $query -> rowCount();
  }

  public function searchArtileByText( $text, $from, $limit )
  {
    global $db;
    
    $query = $db -> query( 'SELECT article_id FROM pp_articles_langs WHERE ( LOWER( title ) LIKE "%' . \System::saveString( $text ) . '%" OR LOWER( text ) LIKE "%' . \System::saveString( $text ) . '%" ) AND article_id IN ( SELECT id FROM pp_articles WHERE enabled = 1 AND archive = 0 ) GROUP BY article_id ORDER BY version DESC LIMIT ' . $from . ',' . $limit );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $art        = \article\FArticle::loadArticle( $row['article_id'] );
      $articles[] = $art;
    }
    return $articles;
  }

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

    $current_lang = \System::getSessionVar( 'current_lang' );

    $sKey = 'getDescription:' . $id . ':' . $current_lang;

    if ( !$sDescription = $cache -> fetch( $sKey ) )
    {
      $query = $db -> prepare( 'SELECT meta_description FROM pp_articles_langs WHERE article_id = :article_id AND lang_id = :lang_id' );
      $query -> bindValue( ':article_id', $id,            \PDO::PARAM_INT );
      $query -> bindValue( ':lang_id',    $current_lang,  \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $sDescription = $row['meta_description'];
      $query -> closeCursor();
      $cache -> store( $sKey , $sDescription , $config['cache_expire_long'] );
    }
    return $sDescription;
  }

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

    $current_lang = \System::getSessionVar( 'current_lang' );

    $key = 'getArticleTitle:' . $id . ':' . $current_lang;

    if ( !$title = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT title FROM pp_articles_langs WHERE article_id = :article_id AND lang_id = :lang_id' );
      $query -> bindValue( ':article_id', $id,            \PDO::PARAM_INT );
      $query -> bindValue( ':lang_id',    $current_lang,  \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $title = $row['title'];
      $query -> closeCursor();
      $cache -> store( $key, $title, $config['cache_expire_long'] );
    }
    return $title;
  }

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

    $current_lang = \System::getSessionVar( 'current_lang' );

    $key = 'getKeywords:' . $id . ':' . $current_lang;

    if ( !$keywords = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT meta_keywords FROM pp_articles_langs WHERE article_id = :article_id AND lang_id = :lang_id' );
      $query -> bindValue( ':article_id', $id,            \PDO::PARAM_INT );
      $query -> bindValue( ':lang_id',    $current_lang , \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $keywords = $row['meta_keywords'];
      $query -> closeCursor();
      $cache -> store( $key, $keywords, $config['cache_expire_long'] );
    }
    return $keywords;
  }

  public function loadArticleVersion( $version_id ) 
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT article_id FROM pp_articles_langs WHERE id = :id' );
    $query -> bindValue( ':id', $version_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $article_id = $row[0];
    $query -> closeCursor();

    $article = new \article\Article;
    
    $query = $db -> prepare( 'SELECT * FROM pp_articles WHERE id = :id' );
    $query -> bindValue( ':id', $article_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $article -> set_id( $article_id );
      $article -> set_date_add( $row['date_add'] );
      $article -> set_date_modify( $row['date_modify'] );
      $article -> set_print_enabled( $row['print'] );
      $article -> set_show_date( $row['show_date'] );
      $article -> set_show_title( $row['show_title'] );
      $article -> set_show_author( $row['show_author'] );
      $article -> set_author( $row['author'] );
      $article -> set_keep_archive( $row['keep_archive'] );

      $data = array(
          'table' => 'pp_articles_langs',
          'fields' => '',
          'condition' => '',
          'order' => '',
          'limit' => 1
      );
      $query2 = $db -> prepare( 'SELECT title, text, meta_description, meta_keywords FROM pp_articles_langs WHERE id = :id AND lang_id = "pl" ORDER BY version DESC LIMIT 1' );
      $query2 -> bindValue( ':id', $version_id, \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
      {
        $article -> set_title( $row2['title'] );
        $article -> set_text( $row2['text'] );
        $article -> set_meta_description( $row2['meta_description'] );
        $article -> set_meta_keywords( $row2['meta_keywords'] );
      }
      $query2 -> closeCursor();
    }
    $query -> closeCursor();
    
    return $article;
  }

  public static function loadArticle( $id )
  {
    return new \article\Article( $id );
  }

  public static function getArticles( $from, $site_id = null )
  {
    global $site, $db;
    
    if ( $site_id )
    {
      $site_tmp_id = $site -> _values['id'];
      $site = new \site\Site( $site_id );
      $site -> _values['article_number'] = 6;
    }

    if ( $site -> _values['id_sort_type'] == 1 )
      $order_by = "date_add DESC ";
    else if ( $site -> _values['id_sort_type'] == 2 )
      $order_by = "date_modify DESC";
    else
      $order_by = 'o ASC';

    $sql = 'SELECT
              article_id
            FROM
              pp_articles_pages as pap, pp_articles as pa
            WHERE
              page_id = :page_id AND article_id = pa.id AND pa.enabled = 1 AND archive = 0
            GROUP BY
              article_id
            ORDER BY
              ' . $order_by . '
            LIMIT
              ' . $from . ',' . $site -> _values['article_number'];
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':page_id', $site -> _values['id'], \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while  ( $row = $query -> fetch() )
    {
      $art = \article\FArticle::loadArticle( $row['article_id'] );
      $articles[] = $art;
    }
    
    if ( $site_id )
      $site = new \site\Site( $site_tmp_id );
    
    return $articles;
  }

  public static function getCountArticles()
  {
    global $site, $db, $cache, $config;
    
    $key = 'getCountArticles:' . $site -> _values['id'];
    if ( !$articles = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT COUNT( 0 ) FROM pp_articles_pages as pap , pp_articles as pa WHERE page_id = :page_id AND article_id = pa.id AND pa.enabled = 1 AND archive = 0' );
      $query -> bindValue( ':page_id', $site -> _values['id'], \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $articles = $row[0];
      $query -> closeCursor();
      
      $cache -> store( $key, $articles, 'l' );
    }
    return $articles;
  }
}
?>