<?php
namespace admin\factory;

class ArticleArchiveManager {


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

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

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

    $query = $db -> prepare( 'DELETE FROM pcms_article_translation WHERE article_id = :id' );
    $query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    $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() )
    {
      if ( file_exists( "../" . $row['src'] ) )
        unlink(  "../" . $row['src'] );
    }
    $query -> closeCursor();
    
    $query = $db -> prepare( 'DELETE FROM pcms_article_images WHERE id_article = :id_article' );
    $query -> bindValue( ':id_article' , $id , \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

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

  public function restore( $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' , 0   , \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();

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