<?php
namespace article;

class FArticle {
  
  public function getArticleLayoutId( $id )
  {
    global $db, $config, $cache;
    
    $key = 'getArticleLayoutId:' . $id;
    if ( !$layout = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT id_layout FROM pcms_article 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 -> prepare( 'SELECT id FROM pcms_layout WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
      $query -> execute();
      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 pcms_article_translation WHERE ( LOWER( title ) LIKE "%' . \System::saveString( $text ) . '%" OR LOWER( text ) LIKE "%' . \System::saveString( $text ) . '%" ) AND article_id IN ( SELECT id FROM pcms_article 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 pcms_article_translation WHERE ( LOWER( title ) LIKE "%' . \System::saveString( $text ) . '%" OR LOWER( text ) LIKE "%' . \System::saveString( $text ) . '%" ) AND article_id IN ( SELECT id FROM pcms_article 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 , $sys;

    $current_lang = $sys -> getSessionVar( 'current_lang' );

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

    if ( !$sDescription = $cache -> fetch( $sKey ) )
    {
      $query = $db -> prepare( 'SELECT meta_description FROM pcms_article_translation 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 , $sys;

    $current_lang = $sys -> getSessionVar( 'current_lang' );

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

    if ( !$sTitle = $cache -> fetch( $sKey ) )
    {
      $query = $db -> prepare( 'SELECT title FROM pcms_article_translation 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() )
        $sTitle = $row['title'];
      $query -> closeCursor();
      $cache -> store( $sKey , $sTitle , $config['cache_expire_long'] );
    }
    return $sTitle;
  }

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

    $current_lang = $sys -> getSessionVar( 'current_lang' );

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

    if ( !$sKeywords = $cache -> fetch( $sKey ) )
    {
      $query = $db -> prepare( 'SELECT meta_keywords FROM pcms_article_translation 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() )
        $sKeywords = $row['meta_keywords'];
      $query -> closeCursor();
      $cache -> store( $sKey , $sKeywords , $config['cache_expire_long'] );
    }
    return $sKeywords;
  }

  public function loadArticleVersion( $version_id ) 
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT article_id FROM pcms_article_translation 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 pcms_article 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' => 'pcms_article_translation',
          'fields' => '',
          'condition' => '',
          'order' => '',
          'limit' => 1
      );
      $query2 = $db -> prepare( 'SELECT title, text, meta_description, meta_keywords FROM pcms_article_translation 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 function loadArticle( $id )
  {
    global $db;

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

    $article = new \article\Article;
    
    $query = $db -> prepare( 'SELECT * FROM pcms_article WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $article -> set_id( $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'] );
      $article -> set_id_layout( $row['id_layout'] );

      $query2 = $db -> prepare( 'SELECT * FROM pcms_article_translation WHERE article_id = :article_id AND lang_id = :lang_id' );
      $query2 -> bindValue( ':article_id', $id,           \PDO::PARAM_INT );
      $query2 -> bindValue( ':lang_id',    $current_lang, \PDO::PARAM_STR );
      $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'] );
        $article -> set_seo_link( $row2['seo_link'] );
      }
      $query2 -> closeCursor();
      
      $query2 = $db -> prepare( 'SELECT * FROM pcms_article_images WHERE id_article = :id_article' );
      $query2 -> bindValue( ':id_article', $id, \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        $images[] = $row2;
      $query2 -> closeCursor();
      $article -> set_images( $images );
      
      $query2 = $db -> prepare( 'SELECT * FROM pcms_article_files WHERE id_article = :id_article' );
      $query2 -> bindValue( ':id_article', $id, \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        $files[] = $row2;
      $query2 -> closeCursor();
      $article -> set_files( $files );
    }
    
    return $article;
  }

  public function getArticles( $from )
  {
    global $site , $db;

    if ( $site -> get_sort_type() == 1 )
      $order_by = "date_add DESC ";
    else if ( $site -> get_sort_type() == 2 )
      $order_by = "date_modify DESC";
    else if ( $site -> get_sort_type() == 3 )
      $order_by = 'o ASC';

    $sql = 'SELECT
              article_id
            FROM
              pcms_article_page as pap, pcms_article 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 -> get_article_number();
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':page_id' , $site -> get_id() , \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while  ( $row = $query -> fetch() )
    {
      $art = \article\FArticle::loadArticle( $row['article_id'] );
      $articles[] = $art;
    }
    
    return $articles;
  }

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