<?php
namespace admin\factory;

class ArticleManager {
  
  public function getArticleFiles( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pcms_article_files WHERE id_article = :id_article' );
    $query -> bindValue( ':id_article', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $files[] = $row;
    $query -> closeCursor();
    
    return $files;
  }
  
  public function getArticleImages( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pcms_article_images WHERE id_article = :id_article' );
    $query -> bindValue( ':id_article', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $images[] = $row;
    $query -> closeCursor();
    
    return $images;
  }
  
  public function deleteNonAssignImages()
  {
    global $db;
    
    $query = $db -> query( 'SELECT * FROM pcms_article_images WHERE id_article = 0' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      if ( file_exists( "../" . $row['src'] ) )
        unlink(  "../" . $row['src'] );
    }
    $query -> closeCursor();
    
    $query = $db -> query( 'DELETE FROM pcms_article_images WHERE id_article = 0' );
    $query -> closeCursor();
    return false;
  }

  public function getArticleVersionByVersionId( $version_id )
  {
    global $db;

    $query = $db -> prepare( 'SELECT version FROM pcms_article_translation WHERE id = :id' );
    $query -> bindValue( ':id' , $version_id , \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row['version'];
    $query -> closeCursor();
  }

  public function getArticleIdByVersionId( $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() )
      return $row['article_id'];
    $query -> closeCursor();
  }

  public function getMaxArticleVersion( $id )
  {
    global $db;

    $query = $db -> prepare( 'SELECT MAX( version ) FROM pcms_article_translation WHERE article_id = :article_id' );
    $query -> bindValue( ':article_id' , $id , \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row[0];
    $query -> closeCursor();
    return 0;
  }

  public function restoreArticleVersion( $version_id )
  {
    global $db , $lang;

    if ( \System::formGetHash( 'time' ) != \System::getSessionVar( 'article_restore' ) )
    {
      $article_id = \admin\factory\ArticleManager::getArticleIdByVersionId( $version_id );
      $version    = \admin\factory\ArticleManager::getArticleVersionByVersionId( $version_id );

      $query = $db -> prepare( 'SELECT * FROM pcms_article_translation WHERE article_id = :article_id AND version = :version' );
      $query -> bindValue( ':article_id'  , $article_id , \PDO::PARAM_INT );
      $query -> bindValue( ':version'     , $version    , \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        if ( !$max_ver )
          $max_ver = self::getMaxArticleVersion( $row['article_id'] ) + 1;

        $query2 = $db -> prepare( 'INSERT INTO pcms_article_translation ( text, meta_description, meta_keywords, title, article_id, lang_id, version ) VALUES ( :text, :meta_description, :meta_keywords, :title, :article_id, :lang_id, :version )' );
        $query2 -> bindValue( ':text'             , $row['text']             , \PDO::PARAM_STR );
        $query2 -> bindValue( ':meta_description' , $row['meta_description'] , \PDO::PARAM_STR );
        $query2 -> bindValue( ':meta_keywords'    , $row['meta_keywords']    , \PDO::PARAM_STR );
        $query2 -> bindValue( ':title'            , $row['title']            , \PDO::PARAM_STR );
        $query2 -> bindValue( ':article_id'       , $row['article_id']       , \PDO::PARAM_INT );
        $query2 -> bindValue( ':lang_id'          , $row['lang_id']          , \PDO::PARAM_STR );
        $query2 -> bindValue( ':version'          , $max_ver                 , \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();
      }
      $query -> closeCursor();
      \System::setSessionVar( 'article_restore' , \System::formGetHash( 'time' ) );
      \System::setAlert( $lang -> getTrans( 'T_ARTYKUL_ZOSTAL_PRZYWROCONY' ) );
    }
  }

  public function getVersions( $id , $from , $lpk )
  {
    global $db;

    $query = $db -> prepare( 'SELECT id , title , version , date_modify FROM pcms_article_translation WHERE article_id = :article_id GROUP BY version ORDER BY version DESC LIMIT ' . $from . ',' . $lpk );
    $query -> bindValue( ':article_id' , $id , \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $articles[] = $row;
    $query -> closeCursor();

		return $articles;
  }

  public function getCountVersions( $id ) {
    global $db;

    $query = $db -> prepare( 'SELECT COUNT( DISTINCT( version ) ) FROM pcms_article_translation WHERE article_id = :article_id' );
    $query -> bindValue( ':article_id' , $id , \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $row[0];
    $query -> closeCursor();
    return 0;
  }

  public function getArticleParam( $id ) 
  {
    global $db;
    
    $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() )
      return $row;
    $query -> closeCursor();
    
    return false;
  }

  public function getPagesAssign( $id = '' ) 
  {
    global $db;

    $query = $db -> query( 'SELECT id FROM pcms_page WHERE id_page_type != 5 AND id_page_type != 6 AND id_page_type != 7 AND id_page_type != 8 AND id_page_type != 9 AND id_page_type != 10 AND id_page_type != 11 ORDER BY o ASC' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $pg['id']     = $row['id'];
      $pg['title']  = \System::getPageTitle( $row['id'] );

      $query2 = $db -> prepare( 'SELECT page_id FROM pcms_article_page WHERE article_id = :article_id AND page_id = :page_id' );
      $query2 -> bindValue( ':article_id', (int)$id,    \PDO::PARAM_INT );
      $query2 -> bindValue( ':page_id',    $row['id'],  \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() )
        $pg['check'] = 1;
      else
        $pg['check'] = 0;
      $query2 -> closeCursor();

      $pages[] = $pg;
    }
    $query -> closeCursor();
    
    return $pages;
  }

  public function getArticleParamLanguage( $id = '' , $version = '' )
  {
    global $db;
    
    $query = $db -> query( 'SELECT id, name FROM pcms_lang WHERE enabled = 1' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      if ( $version )
      {
        $query2 = $db -> prepare( 'SELECT * FROM pcms_article_translation WHERE article_id = :article_id AND lang_id = :lang_id AND version = :version ORDER BY version DESC LIMIT 1' );
        $query2 -> bindValue( ':article_id',  $id,        \PDO::PARAM_INT );
        $query2 -> bindValue( ':lang_id',     $row['id'], \PDO::PARAM_STR );
        $query2 -> bindValue( ':version',     $version,   \PDO::PARAM_INT );
        $query2 -> execute();        
      }
      else
      {
        $query2 = $db -> prepare( 'SELECT * FROM pcms_article_translation WHERE article_id = :article_id AND lang_id = :lang_id ORDER BY version DESC LIMIT 1' );
        $query2 -> bindValue( ':article_id',  $id,        \PDO::PARAM_INT );
        $query2 -> bindValue( ':lang_id',     $row['id'], \PDO::PARAM_STR );
        $query2 -> execute();        
      }
      
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
      {
        $lg['title']            = $row2['title'];
        $lg['meta_description'] = $row2['meta_description'];
        $lg['meta_keywords']    = $row2['meta_keywords'];
        $lg['text']             = $row2['text'];
        $lg['seo_link']         = $row2['seo_link'];
        
      }
      else
      {
        $lg['title']            = '';
        $lg['meta_description'] = '';
        $lg['meta_keywords']    = '';
        $lg['text']             = '';
        $lg['seo_link']         = '';
      }
      $lg['id']         = $row['id'];
      $lg['name']       = $row['name'];
      $language[]       = $lg;
    }
    return $language;
  }

  public function getMaxArticleOrder()
  {
    global $db;

    $query = $db -> query( 'SELECT MAX(o) FROM pcms_article_page' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row[0];
    $query -> closeCursor();
    return 0;
  }

  public function save()
  {
    global $lang, $db;

    $date         = \System::getDate();
    $pages        = \System::formGet( 'pages' );
    $pages        = \System::formGet( 'pages' );
    $print        = \System::formGetInt( 'print' );
    $show_date    = \System::formGetInt( 'show_date' );
    $show_title   = \System::formGetInt( 'show_title' );
    $article_id   = \System::formGetInt( 'id' );
    $enabled      = \System::formGetInt( 'enabled' );
    $show_author  = \System::formGetInt( 'show_author' );
    $keep_archive = \System::formGetInt( 'keep_archive' );
    $layout       = \System::formGetInt( 'layout' );

    $sql = 'UPDATE
              pcms_article
            SET
              show_title = :show_title,
              enabled = :enabled,
              print = :print,
              show_date = :show_date,
              date_modify = :date_modify,
              show_author = :show_author,
              keep_archive = :keep_archive,
              id_layout = :id_layout
            WHERE
              id = :id';
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':show_title'    , $show_title       , \PDO::PARAM_STR );
    $query -> bindValue( ':enabled'       , $enabled          , \PDO::PARAM_STR );
    $query -> bindValue( ':print'         , $print            , \PDO::PARAM_STR );
    $query -> bindValue( ':show_date'     , $show_date        , \PDO::PARAM_STR );
    $query -> bindValue( ':date_modify'   , $date             , \PDO::PARAM_STR );
    $query -> bindValue( ':show_author'   , $show_author      , \PDO::PARAM_STR );
    $query -> bindValue( ':keep_archive'  , $keep_archive     , \PDO::PARAM_STR );
    $query -> bindValue( ':id'            , $article_id       , \PDO::PARAM_INT );
    $query -> bindValue( ':id_layout'     , $layout           , \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

    $not_in = '';
    if ( is_array( $pages ) ) foreach ( $pages as $pag )
    {
      if ( $not_in )
        $not_in .= ',';
      $not_in .= $pag;
    }
    if ( $not_in )
    {
      $query = $db -> prepare( 'DELETE FROM pcms_article_page WHERE article_id = :article_id AND page_id NOT IN (' . $not_in . ')' );
      $query -> bindValue( ':article_id' , $article_id , \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor();
    }

    $query = $db -> prepare( 'SELECT page_id FROM pcms_article_page WHERE article_id = :article_id' );
    $query -> bindValue( ':article_id' , $article_id , \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $tab_tmp[] = $row['page_id'];
    $query -> closeCursor();

    if ( is_array( $pages ) )
    {
      $o = self::getMaxArticleOrder() + 1;
      $query = $db -> prepare( 'INSERT INTO pcms_article_page ( page_id, article_id, o ) VALUES ( :page_id, :article_id, :o )' );
      foreach ( $pages as $pag )
      {
        if ( !is_array( $tab_tmp ) || !in_array( $pag , $tab_tmp ) )
        {
          $query -> bindValue( ':page_id'     , $pag        , \PDO::PARAM_INT );
          $query -> bindValue( ':article_id'  , $article_id , \PDO::PARAM_INT );
          $query -> bindValue( ':o'           , $o          , \PDO::PARAM_INT );
          $query -> execute();
          $o++;
        }
      }
      $query -> closeCursor();
    }

    if ( $article_id )
    {
      $version = self::getMaxArticleVersion( $article_id ) + 1;

      $query = $db -> prepare( 'SELECT id FROM pcms_lang WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $title            = stripslashes( \System::saveString( \System::formGet( 'title_' . $row['id'] ) ) );
        $meta_description = stripslashes( \System::saveString( \System::formGet( 'meta_description_' . $row['id'] ) ) );
        $meta_keywords    = stripslashes( \System::saveString( \System::formGet( 'meta_keywords_' . $row['id'] ) ) );
        $text             = stripslashes( \System::formGet( 'text_' . $row['id'] ) );
        $seo_link         = \System::formGet( 'seo_link_' . $row['id'] );

        $query2 = $db -> prepare( 'SELECT id, version FROM pcms_article_translation WHERE article_id = :article_id AND lang_id = :lang_id ORDER BY version DESC LIMIT 1' );
        $query2 -> bindValue( ':article_id' , $article_id , \PDO::PARAM_INT );
        $query2 -> bindValue( ':lang_id'    , $row['id']  , \PDO::PARAM_STR );
        $query2 -> execute();
        if ( $query2 -> rowCount() && !$keep_archive )
        {
          while ( $row2 = $query2 -> fetch() )
          {
            $query3 = $db -> prepare( 'UPDATE pcms_article_translation SET text = :text, meta_description = :meta_description, meta_keywords = :meta_keywords, title = :title, seo_link = :seo_link WHERE article_id = :article_id AND lang_id = :lang_id AND version = :version' );
            $query3 -> bindValue( ':text'             , $text             , \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_description' , $meta_description , \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_keywords'    , $meta_keywords    , \PDO::PARAM_STR );
            $query3 -> bindValue( ':title'            , $title            , \PDO::PARAM_STR );
            $query3 -> bindValue( ':article_id'       , $article_id       , \PDO::PARAM_INT );
            $query3 -> bindValue( ':lang_id'          , $row['id']        , \PDO::PARAM_STR );
            $query3 -> bindValue( ':version'          , $row2['version']  , \PDO::PARAM_INT );
            $query3 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
            $query3 -> execute();
            $query3 -> closeCursor();
          }
        }
        else
        {
          $query3 = $db -> prepare( 'INSERT INTO pcms_article_translation ( text, meta_description, meta_keywords, title, article_id, lang_id, version, seo_link ) VALUES ( :text, :meta_description, :meta_keywords, :title, :article_id, :lang_id, :version, :seo_link )' );
          $query3 -> bindValue( ':text'             , $text             , \PDO::PARAM_STR );
          $query3 -> bindValue( ':meta_description' , $meta_description , \PDO::PARAM_STR );
          $query3 -> bindValue( ':meta_keywords'    , $meta_keywords    , \PDO::PARAM_STR );
          $query3 -> bindValue( ':title'            , $title            , \PDO::PARAM_STR );
          $query3 -> bindValue( ':article_id'       , $article_id       , \PDO::PARAM_INT );
          $query3 -> bindValue( ':lang_id'          , $row['id']        , \PDO::PARAM_STR );
          $query3 -> bindValue( ':version'          , $version          , \PDO::PARAM_INT );
          $query3 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
          $query3 -> execute();
          $query3 -> closeCursor();
        }
      }
      \System::setAlert( $lang -> getTrans( 'T_ZAPISANO_ARTYKUL' ) );
    }
    \System::rewriteHtacces();
    \System::deleteCache();
    \System::deleteCacheAdmin();
  }

  public function saveNew()
  {
    global $lang , $user , $db;

    $date         = \System::getDate();
    $pages        = \System::formGet( 'pages' );
    $pages        = \System::formGet( 'pages' );
    $enabled      = \System::formGetInt( 'enabled' );
    $print        = \System::formGetInt( 'print' );
    $show_date    = \System::formGetInt( 'show_date' );
    $show_title   = \System::formGetInt( 'show_title' );
    $time         = \System::formGetHash( 'time' );
    $show_author  = \System::formGetInt( 'show_author' );
    $keep_archive = \System::formGetInt( 'keep_archive' );
    $layout       = \System::formGetInt( 'layout' );

    if ( $time != \System::getSessionVar( 'db_edit_add' ) )
    {
      $sql = 'INSERT INTO
                pcms_article
                  ( show_title , enabled , print , show_date , date_add , date_modify , show_author , author , keep_archive, id_layout )
              VALUES
                  ( :show_title , :enabled , :print , :show_date , :date_add , :date_modify , :show_author , :author , :keep_archive, :id_layout )';
      $query = $db -> prepare( $sql );
      $query -> bindValue( ':show_title'    , $show_title       , \PDO::PARAM_STR );
      $query -> bindValue( ':enabled'       , $enabled          , \PDO::PARAM_STR );
      $query -> bindValue( ':print'         , $print            , \PDO::PARAM_STR );
      $query -> bindValue( ':show_date'     , $show_date        , \PDO::PARAM_STR );
      $query -> bindValue( ':date_add'      , $date             , \PDO::PARAM_STR );
      $query -> bindValue( ':date_modify'   , $date             , \PDO::PARAM_STR );
      $query -> bindValue( ':show_author'   , $show_author      , \PDO::PARAM_STR );
      $query -> bindValue( ':author'        , $user -> get_id() , \PDO::PARAM_STR );
      $query -> bindValue( ':keep_archive'  , $keep_archive     , \PDO::PARAM_STR );
      $query -> bindValue( ':id_layout'     , $layout           , \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor();

      $article_id = $db -> lastInsertId();

      if ( $article_id )
      {
        if ( is_array( $pages ) )
        {
          $o = self::getMaxArticleOrder() + 1;
          $query = $db -> prepare( 'INSERT INTO pcms_article_page ( page_id, article_id, o ) VALUES ( :page_id, :article_id, :o )' );
          foreach ( $pages as $page )
          {
            $query -> bindValue( ':page_id'     , $page       , \PDO::PARAM_INT );
            $query -> bindValue( ':article_id'  , $article_id , \PDO::PARAM_INT );
            $query -> bindValue( ':o'           , $o          , \PDO::PARAM_INT );
            $query -> execute();
            $o++;
          }
          $query -> closeCursor();
        }

        $query = $db -> prepare( 'SELECT id FROM pcms_lang WHERE enabled = :enabled' );
        $query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
        $query -> execute();
        if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        {
          $title            = \System::saveString( \System::formGet( 'title_' . $row['id'] ) );
          $meta_description = \System::saveString( \System::formGet( 'meta_description_' . $row['id'] ) );
          $meta_keywords    = \System::saveString( \System::formGet( 'meta_keywords_' . $row['id'] ) );
          $text             = stripslashes( \System::formGet( 'text_' . $row['id'] ) );
          $seo_link         = \System::formGet( 'seo_link_' . $row['id'] );

          $sql = 'INSERT INTO
                    pcms_article_translation
                      ( article_id, lang_id, text, meta_description, meta_keywords, title, version, seo_link )
                  VALUES
                      ( :article_id, :lang_id, :text, :meta_description, :meta_keywords, :title, :version, :seo_link )';
          $query2 = $db -> prepare( $sql );
          $query2 -> bindValue( ':article_id'       , $article_id       , \PDO::PARAM_INT );
          $query2 -> bindValue( ':lang_id'          , $row['id']        , \PDO::PARAM_STR );
          $query2 -> bindValue( ':text'             , $text             , \PDO::PARAM_STR );
          $query2 -> bindValue( ':meta_description' , $meta_description , \PDO::PARAM_STR );
          $query2 -> bindValue( ':meta_keywords'    , $meta_keywords    , \PDO::PARAM_STR );
          $query2 -> bindValue( ':title'            , $title            , \PDO::PARAM_STR );
          $query2 -> bindValue( ':version'          , 0                 , \PDO::PARAM_INT );
          $query2 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
          $query2 -> execute();
          $query2 -> closeCursor();
        }
        \System::setSessionVar( 'db_edit_add' , $time );
        \System::setAlert( $lang -> getTrans( 'T_DODANO_ARTYKUL' ) );
      }

      \System::rewriteHtacces();
      \System::deleteCache();
      \System::deleteCacheAdmin();
    }
  }

  public function setAsArchive( $id )
  {
    global $db , $lang;

    $query = $db -> prepare( 'UPDATE pcms_article SET archive = :archive WHERE id = :id' );
    $query -> bindValue( ':id'      , $id , \PDO::PARAM_INT );
    $query -> bindValue( ':archive' , 1   , \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();

    \System::setAlert( $lang -> getTrans( 'T_PRZENIESIONO_ARTYKUL_DO_ARCHIWUM' ) );
    \System::rewriteHtacces();
    \System::deleteCache();
    \System::deleteCacheAdmin();
  }
}
?>