<?php
// po poprawkach
namespace admin\factory;

class Articles {
  
  public static function cloneArticle( $id )
  {  
    global $db;
    if ( $art_id = \System::duplicateMysqlRow( 'pp_articles', 'id', $id, array( 'date_add' ) ) )
    {
      $query = $db -> prepare( 'SELECT id FROM pp_articles_langs WHERE article_id = :article_id' );
      $query -> bindValue( ':article_id', $id, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        if ( $lang_id = \System::duplicateMysqlRow( 'pp_articles_langs', 'id', $row['id'], array( 'seo_link', 'date_modify' ) ) )
        {
          $query2 = $db -> prepare( 'UPDATE pp_articles_langs SET article_id = :article_id WHERE id = :id' );
          $query2 -> bindValue( ':article_id',  $art_id,  \PDO::PARAM_INT );
          $query2 -> bindValue( ':id',          $lang_id, \PDO::PARAM_INT );
          $query2 -> execute();
          $query2 -> closeCursor();
          
          $query2 = $db -> prepare( 'UPDATE pp_articles SET date_modify = :date_modify WHERE id = :id' );
          $query2 -> bindValue( ':id',          $art_id,            \PDO::PARAM_INT );
          $query2 -> bindValue( ':date_modify', \System::getDate(), \PDO::PARAM_INT );
          $query2 -> execute();
          $query2 -> closeCursor();
          
          $query2 = $db -> prepare( 'UPDATE pp_articles SET thumbnail = NULL WHERE id = :id' );
          $query2 -> bindValue( ':id', $art_id, \PDO::PARAM_INT );
          $query2 -> execute();
          $query2 -> closeCursor();
        }
      }
      $query -> closeCursor();
      return true;
    }
    return false;
  }
  
  public static function changeArticleEnabled( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT enabled FROM pp_articles WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $enabled = $row['enabled'];
    $query -> closeCursor();
    
    $enabled ? $enabled_new = 0 : $enabled_new = 1;
    
    $query = $db -> prepare( 'UPDATE pp_articles SET enabled = :enabled WHERE id = :id' );
    $query -> bindValue( ':id',       $id,          \PDO::PARAM_INT );
    $query -> bindValue( ':enabled',  $enabled_new, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() )
      return true;
    $query -> closeCursor();
    return false;
  }
  
  public static function getPagesWithArticles()
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT id FROM pp_pages ORDER BY id_menu ASC, o ASC' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      unset( $articles );
      
      $row['title'] = \admin\factory\Pages::getPageTitle( $row['id'] );
      
      $query2 = $db -> query( 'SELECT article_id AS id FROM pp_articles_pages AS pap, pp_articles AS pa WHERE pa.id = pap.article_id AND page_id = ' . $row['id'] . ' AND archive = 0 ORDER BY o ASC' );
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
      {
        $row2['title']  = \System::getArticleTitle( $row2['id'] );
        $articles[]     = $row2;
      }
      $query2 -> closeCursor();
      
      $row['articles'] = $articles;
      $pages[] = $row;
    }
    $query -> closeCursor();

    return $pages;
  }
  
  public static function deleteArticle( $id )
  {
    global $db;
    
    \System::deleteCache( '../upload/files/article_' . $id . '/' );
    \System::deleteCache( '../upload/galeries/article_' . $id . '/' );
    
    $query = $db -> prepare( 'SELECT thumbnail FROM pp_articles WHERE id = :id AND thumbnail IS NOT NULL' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      if ( file_exists( "../" . $row['thumbnail'] ) )
        unlink(  "../" . $row['thumbnail'] );
    }
    $query -> closeCursor();

    $query = $db -> prepare( 'DELETE FROM pp_articles_pages WHERE article_id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

    $query = $db -> prepare( 'DELETE FROM pp_articles_langs WHERE article_id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    $query = $db -> prepare( 'DELETE FROM pp_articles_img WHERE id_article = :id_article' );
    $query -> bindValue( ':id_article', $id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    $query = $db -> prepare( 'DELETE FROM pp_articles_file WHERE id_article = :id_article' );
    $query -> bindValue( ':id_article', $id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

    $query = $db -> prepare( 'DELETE FROM pp_articles WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() )
    {
      \System::rewriteHtacces();
      \System::deleteCache();
      \System::deleteCacheAdmin();
      return true;
    }
    $query -> closeCursor();
    return false;
  }

  public static function restoreArticle( $id )
  {
    global $db;

    $query = $db -> prepare( 'UPDATE pp_articles SET archive = 0 WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() )
    {
      \System::rewriteHtacces();
      \System::deleteCache();
      \System::deleteCacheAdmin();     
      return true;
    }
    $query -> closeCursor();
    return false;
  }
  
  public static function getArticleFiles( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pp_articles_file 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 static function getArticleImages( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pp_articles_img 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 static function deleteNonAssignImages()
  {
    global $db;
    
    $query = $db -> query( 'SELECT * FROM pp_articles_img 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 pp_articles_img WHERE id_article = 0' );
    $query -> closeCursor();
    return false;
  }

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

    $query = $db -> prepare( 'SELECT version FROM pp_articles_langs 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 static function getArticleIdByVersionId( $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() )
      return $row['article_id'];
    $query -> closeCursor();
  }

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

    $query = $db -> prepare( 'SELECT MAX( version ) FROM pp_articles_langs 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 static function restoreArticleVersion( $version_id )
  {
    global $db;

    $article_id = \admin\factory\Articles::getArticleIdByVersionId( $version_id );
    $version    = \admin\factory\Articles::getArticleVersionByVersionId( $version_id );

    $query = $db -> prepare( 'SELECT * FROM pp_articles_langs 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 pp_articles_langs ( 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();
      if ( $query2 -> rowCount() )
        return true;
      $query2 -> closeCursor();
    }
    $query -> closeCursor();
    return false;
  }

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

    $query = $db -> prepare( 'SELECT id , title , version , date_modify FROM pp_articles_langs 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 static function getCountVersions( $id ) 
  {
    global $db;

    $query = $db -> prepare( 'SELECT COUNT( DISTINCT( version ) ) FROM pp_articles_langs 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 static function getArticleParam( $id ) 
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pp_articles 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 static function getPagesAssign( $id = '', $parent_id = 0 ) 
  {
    global $db;

    $query = $db -> prepare( 'SELECT * FROM pp_pages WHERE parent_id = :parent_id ORDER BY id_menu ASC, o ASC' );
    $query -> bindValue( ':parent_id', $parent_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $row['title']  = \admin\factory\Pages::getPageTitle( $row['id'] );

      $query2 = $db -> prepare( 'SELECT page_id FROM pp_articles_pages 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() )
        $row['check'] = 1;
      else
        $row['check'] = 0;
      $query2 -> closeCursor();

      $row['subpages'] = self::getPagesAssign( $id, $row['id'] );
      
      $pages[] = $row;
    }
    $query -> closeCursor();
    
    return $pages;
  }

  public static function getArticleParamLanguage( $id = '', $version = '' )
  {
    global $db;
    
    $query = $db -> query( 'SELECT id, name FROM pp_langs WHERE enabled = 1' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      if ( $version )
      {
        $query2 = $db -> prepare( 'SELECT * FROM pp_articles_langs 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 pp_articles_langs 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 static function getMaxArticleOrder()
  {
    global $db;

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

  public static function saveArticle()
  {
    global $db;
    
    \System::deleteCache();
    \System::deleteCacheAdmin();

    $date             = \System::getDate();
    $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' );
    $replace_menu     = \System::formGetInt( 'replace_menu_title' );
    $comments_enabled = \System::formGetInt( 'comments_enabled' );

    $sql = 'UPDATE
              pp_articles
            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, replace_menu_title = :replace_menu, comments_enabled = :comments_enabled
            WHERE
              id = :id';
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':show_title',         $show_title,        \PDO::PARAM_INT );
    $query -> bindValue( ':enabled',            $enabled,           \PDO::PARAM_INT );
    $query -> bindValue( ':print',              $print,             \PDO::PARAM_INT );
    $query -> bindValue( ':show_date',          $show_date,         \PDO::PARAM_INT );
    $query -> bindValue( ':date_modify',        $date,              \PDO::PARAM_STR );
    $query -> bindValue( ':show_author',        $show_author,       \PDO::PARAM_INT );
    $query -> bindValue( ':keep_archive',       $keep_archive,      \PDO::PARAM_INT );
    $query -> bindValue( ':id',                 $article_id,        \PDO::PARAM_INT );
    $query -> bindValue( ':id_layout',          $layout,            \PDO::PARAM_INT );
    $query -> bindValue( ':replace_menu',       $replace_menu,      \PDO::PARAM_INT );
    $query -> bindValue( ':comments_enabled',   $comments_enabled,  \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

    $not_in = '0';
    if ( is_array( $pages ) ) foreach ( $pages as $pag )
    {
      if ( $not_in )
        $not_in .= ',';
      $not_in .= $pag;
    }
    
    if ( $not_in != '' )
    {
      $query = $db -> prepare( 'DELETE FROM pp_articles_pages 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 pp_articles_pages 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 pp_articles_pages ( 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 pp_langs WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
      $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'] );
          
        if ( $seo_link == 'link seo' )
          $seo_link = '';

        if ( $meta_description == 'meta description' )
          $meta_description = '';

        if ( $meta_keywords == 'meta keywords' )
          $meta_keywords = '';

        $query2 = $db -> prepare( 'SELECT id, version FROM pp_articles_langs 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 pp_articles_langs 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 pp_articles_langs ( 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::rewriteHtacces();
    return true;
  }

  public static function addArticle()
  {
    global $user, $db;
    
    \System::deleteCache();
    \System::deleteCacheAdmin();

    $date             = \System::getDate();
    $pages            = \System::formGet( 'pages' );
    $enabled          = \System::formGetInt( 'enabled' );
    $print            = \System::formGetInt( 'print' );
    $show_date        = \System::formGetInt( 'show_date' );
    $show_title       = \System::formGetInt( 'show_title' );
    $show_author      = \System::formGetInt( 'show_author' );
    $keep_archive     = \System::formGetInt( 'keep_archive' );
    $layout           = \System::formGetInt( 'layout' );
    $replace_menu     = \System::formGetInt( 'replace_menu_title' );
    $comments_enabled = \System::formGetInt( 'comments_enabled' );
    
    if ( \System::getSessionVar( 'check' ) == \System::formGet( 'check' ) )
      return false;
    
    \System::setSessionVar( 'check', \System::formGet( 'check' ) );
    
    $sql = 'INSERT INTO
              pp_articles
                ( show_title , enabled , print , show_date , date_add , date_modify , show_author , author , keep_archive, id_layout, replace_menu_title, comments_enabled )
            VALUES
                ( :show_title , :enabled , :print , :show_date , :date_add , :date_modify , :show_author , :author , :keep_archive, :id_layout, :replace_menu, :comments_enabled )';
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':show_title',         $show_title,            \PDO::PARAM_INT );
    $query -> bindValue( ':enabled',            $enabled,               \PDO::PARAM_INT );
    $query -> bindValue( ':print',              $print,                 \PDO::PARAM_STR );
    $query -> bindValue( ':show_date',          $show_date,             \PDO::PARAM_INT );
    $query -> bindValue( ':date_add',           $date,                  \PDO::PARAM_STR );
    $query -> bindValue( ':date_modify',        $date,                  \PDO::PARAM_STR );
    $query -> bindValue( ':show_author',        $show_author,           \PDO::PARAM_INT );
    $query -> bindValue( ':author',             $user -> _values['id'], \PDO::PARAM_STR );
    $query -> bindValue( ':keep_archive',       $keep_archive,          \PDO::PARAM_INT );
    $query -> bindValue( ':id_layout',          $layout,                \PDO::PARAM_INT );
    $query -> bindValue( ':replace_menu',       $replace_menu,          \PDO::PARAM_INT );
    $query -> bindValue( ':comments_enabled',   $comments_enabled,      \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 pp_articles_pages ( 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 pp_langs WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
      $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'] );

        if ( $seo_link == 'link seo' )
          $seo_link = '';

        if ( $meta_description == 'meta description' )
          $meta_description = '';

        if ( $meta_keywords == 'meta keywords' )
          $meta_keywords = '';

        $sql = 'INSERT INTO
                  pp_articles_langs
                    ( 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::rewriteHtacces();
    }
    return true;
  }

  public static function setAsArchive( $id )
  {
    global $db;

    $query = $db -> prepare( 'UPDATE pp_articles SET archive = :archive WHERE id = :id' );
    $query -> bindValue( ':id'      , $id , \PDO::PARAM_INT );
    $query -> bindValue( ':archive' , 1   , \PDO::PARAM_STR );
    $query -> execute();
    if ( $query -> rowCount() )
    {
      \System::rewriteHtacces();
      \System::deleteCache();
      \System::deleteCacheAdmin();
      return true;
    }
    $query -> closeCursor();
    return false;
  }
}
?>