<?php
namespace admin\view;

class ArticleManager
{

  public function drawArticle( $version_id )
  {
    global $lang;

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

    $out = \admin\view\PagePanel::show( false , false , true );

    $language = \admin\factory\ArticleManager::getArticleParamLanguage( $article_id , $version );

    $tpl = new \Savant3();
    $tpl -> _language     = $language;
    $out .= $tpl -> fetch( 'templates/article-draw.php' );

    return $out;
  }

  public function add()
  {
    global $lang;

    $_SESSION['KCFINDER']['disabled'] = false;

    $out      = \admin\view\PagePanel::show( false , true , true , $lang -> getTrans( 'T_ARTYKULY' ) , 'formularz' , false, false, 'saveArticle()' );

    $pages    = \admin\factory\ArticleManager::getPagesAssign();
    $language = \admin\factory\ArticleManager::getArticleParamLanguage();

    $tpl = new \Savant3();
    $tpl -> _language = $language;
    $tpl -> _rw       = 'add';
    $tpl -> _pages    = $pages;
    $tpl -> _id_form  = 'formularz';
    $tpl -> _layouts  = \System::getLayouts();
    $out .= $tpl -> fetch( 'templates/article-edit.php' );

    return $out;
  }

  public function edit( $id ) 
  {
    global $lang;

    $_SESSION['KCFINDER']['disabled'] = false;

    $out      = \admin\view\PagePanel::show( false , true , true , $lang -> getTrans( 'T_ARTYKULY' ) , 'formularz' , false, false, 'saveArticle()' );

    $row      = \admin\factory\ArticleManager::getArticleParam( $id );
    $pages    = \admin\factory\ArticleManager::getPagesAssign( $id );
    $language = \admin\factory\ArticleManager::getArticleParamLanguage( $id );

    $tpl = new \Savant3();
    $tpl -> _id           = $id;
    $tpl -> _edit         = true;
    $tpl -> _enabled      = $row['enabled'];
    $tpl -> _print        = $row['print'];
    $tpl -> _show_title   = $row['show_title'];
    $tpl -> _show_date    = $row['show_date'];
    $tpl -> _show_author  = $row['show_author'];
    $tpl -> _keep_archive = $row['keep_archive'];
    $tpl -> _layout       = $row['id_layout'];
    $tpl -> _language     = $language;
    $tpl -> _rw           = 'edit';
    $tpl -> _pages        = $pages;
    $tpl -> _id_form      = 'formularz';
    $tpl -> _layouts      = \System::getLayouts();
    $tpl -> _version      = \admin\factory\ArticleManager::getMaxArticleVersion( $id ) + 1;
    $tpl -> _images       = \admin\factory\ArticleManager::getArticleImages( $id );
    $tpl -> _files        = \admin\factory\ArticleManager::getArticleFiles( $id );
    $out .= $tpl -> fetch( 'templates/article-edit.php');

    return $out;
  }

  public function showVersions( $id )
  {
    global $lang , $bs;

    $out = \admin\view\PagePanel::show( false , false , false , ucfirst( $lang -> getTrans( 'T_ARTYKUL' ) ) . ': ' . \article\FArticle::getArticleTitle( $id ) , '' , true );

    $lpk = 25;

    if ( \System::formGetInt( 'r' ) )
      \System::setSessionVar( 'pcms_page_limit' , \System::formGetInt( 'r' ) );

    $limit  = \System::getSessionVar( 'pcms_page_limit' );
    $limit  = \System::checkBrowseLimit( $limit );
    $link = 'index.php';

    $count_page = \admin\factory\ArticleManager::getCountVersions( $id );

     $tpl = new \Savant3;

    $ls = ceil( $count_page / $lpk );
    if ( $bs < 1 )
      $bs = 1;
    else if ( $bs > $ls )
      $bs = $ls;

    $a  = \System::getPagingVar( "a" , $bs , $ls );
    $b  = \System::getPagingVar( "b" , $bs , $ls );

    $tpl -> _a    = $bs - $b;
    $tpl -> _b    = $bs + $a;
    $tpl -> _ls   = $ls;
    $tpl -> _link = $link;
    $tpl -> _bs   = $bs;

    $from = $lpk * ( $bs - 1 );
    if ( $from < 0 )
      $from = 0;
    $lp = $from + 1;

    $articles = \admin\factory\ArticleManager::getVersions( $id , $from , $lpk );

    $tpl -> _articles = $articles;
    $tpl -> _bs       = $bs;
    $tpl -> _limit    = $lpk;
    $tpl -> _total    = $count_page;
    $tpl -> _start    = $lpk * ( $bs - 1 ) + 1 < 0 ? 0 : $lpk * ( $bs - 1 ) + 1;
    if ( $count_page == 0 )
      $tpl -> _start = 0;
    $tpl -> _end    = $lpk * $bs > $count_page ? $count_page : $lpk * $bs;
    $out .= $tpl -> fetch( 'templates/article-versions.php' );

    return $out;
  }

  public function show()
  {
    global $lang;
    
    $delete_articles = "var articles = ''; 
                        $('input:checkbox:checked').each(
                          function() {
                            if ( articles != '' )
                              articles = articles + ':';
                            articles = articles + $(this).val();
                          }
                        );
                        $.prompt('" . $lang -> getTrans( 'T_TRWA_PRZENOSZENIE_DO_ARCHIWUM' ) . "');
                        xajax_moveArticlesToArchive( articles );";

    $out = \admin\view\PagePanel::show( true , false , false , $lang -> getTrans( 'T_ARTYKULY'), 'formularz', false, false, false, $delete_articles );

    $tab_comment[0] = $lang -> getTrans( 'T_NIE' );
    $tab_comment[1] = $lang -> getTrans( 'T_TAK' );
    $tab_comment[2] = $lang -> getTrans( 'T_DLA_ZAREJESTROWANYCH' );

    $tab = \System::getArticlesTitle();

    $dbrowse = new \DataBrowse( 'pcms_article' , 'archive = "0"' );
    $dbrowse -> addPosition( 'id'         , $lang -> getTrans( 'T_TYTUL' )            , '' , $tab , '' );
    $dbrowse -> addPosition( 'enabled'    , $lang -> getTrans( 'T_AKTYWNY' )          , '' , \System::getComboYesNo() );
    $dbrowse -> addPosition( 'show_title' , $lang -> getTrans( 'T_POKAZ_TYTUL' )      , '' , \System::getComboYesNo() );
    $dbrowse -> addPosition( 'print'      , $lang -> getTrans( 'T_DRUKUJ' )           , '' , \System::getComboYesNo() );
    $dbrowse -> addPosition( 'date_modify', $lang -> getTrans( 'T_DATA_MODYFIKACJI' ) );
    $dbrowse -> addPositionSimple( $lang -> getTrans( 'T_POKAZ' )   , '' , 'index.php?act=show_versions' );
    $dbrowse -> addPositionSimple( $lang -> getTrans( 'T_EDYTUJ' )  , '' , 'index.php?rw=edit' );
    $dbrowse -> addPositionSimple( $lang -> getTrans( 'T_USUN' )    , '' , '' , \System::deleteAction() );
    $dbrowse -> setParam( 'id' );
    $dbrowse -> addSort( 'date_add DESC' );
    $dbrowse -> addLp();
    $dbrowse -> addCheckbox();
    $dbrowse -> addFiltr( 'id{trans}'   , $lang -> getTrans( 'T_TYTUL' )        , $tab );
    $dbrowse -> addFiltr( 'print'       , $lang -> getTrans( 'T_DRUKUJ' )       , \System::getComboYesNo() );
    $dbrowse -> addFiltr( 'enabled'     , $lang -> getTrans( 'T_AKTYWNY' )      , \System::getComboYesNo() );
    $dbrowse -> addFiltr( 'show_title'  , $lang -> getTrans( 'T_POKAZ_TYTUL' )  , \System::getComboYesNo() );
    $out .= $dbrowse -> draw();

    return $out;
  }
}
?>