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

class Articles
{
  public static function drawPagesArticles()
  {
    $tpl = new \Savant3();
    $tpl -> _pages = \admin\factory\Articles::getPagesWithArticles();
    return $tpl -> fetch( 'articles/article-sort' );
  }
  
  public static function showArticlesArchive()
  {    
    $delete_articles = "var articles = '';  $('input:checkbox:checked').each( function() { if ( articles != '' ) articles = articles + ':'; articles = articles + $(this).val(); } ); $.prompt( 'Trwa usuwanie...' ); xajax_deleteArticles( articles );";

    $tab_comment[0] = 'nie';
    $tab_comment[1] = 'tak';
    $tab_comment[2] = 'tylko dla zarejestrowanych';

    $tab = \System::getArticlesTitle();

    $dbrowse = new \DataBrowse( 'pp_articles', 'archive = 1' );
    $dbrowse -> addPosition( 'id',          'Tytuł',        '', $tab, '' );
    $dbrowse -> addPosition( 'enabled',     'Opublikowany', '', \System::getComboYesNo(), 'text-align: center; width: 100px;' );
    $dbrowse -> addPosition( 'date_modify', 'Data modyfikacji', '', '', 'width: 150px; text-align: center;', true );
    $dbrowse -> addPositionSimple( 'przywróc',  '', './?rw=restore' );
		$dbrowse -> addPositionSimple( 'usuń',      '', '', \System::deleteAction() );
    $dbrowse -> setParam( 'id' );
    $dbrowse -> addSort( 'date_add DESC' );
    $dbrowse -> addLp();
    $dbrowse -> addCheckbox();
    $dbrowse -> addFiltr( 'id{trans}',  'Tytuł',        $tab );
    $dbrowse -> addFiltr( 'print',      'Drukuj',       \System::getComboYesNo() );
    $dbrowse -> addFiltr( 'enabled',    'Opublikowany', \System::getComboYesNo() );
    $dbrowse -> addFiltr( 'show_title', 'Pokaż tytuł',  \System::getComboYesNo() );
    $dbrowse -> addMenu( \admin\view\PagePanel::show( false, false, false, 'Archiwum', 'formularz', false, false, false, $delete_articles ) );
    $out .= $dbrowse -> draw();

    return $out;
  }
  
  public static function drawArticle( $version_id )
  {
    $article_id = \admin\factory\Articles::getArticleIdByVersionId( $version_id );
    $version    = \admin\factory\Articles::getArticleVersionByVersionId( $version_id );
    $language   = \admin\factory\Articles::getArticleParamLanguage( $article_id , $version );

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

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

    return $out;
  }

  public static function addArticle()
  {
    \System::deleteSessionVar( 'id_menu' );

    $out      = \admin\view\PagePanel::show( false, 'Dodaj', true, 'Artykuły', 'formularz', false, false, 'saveArticle()' );

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

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

    return $out;
  }
  
  public static function drawArticleEditPages( $pages, $step = 0 )
  {
    $tpl = new \Savant3;
    $tpl -> _pages  = $pages;
    $tpl -> _step   = $step;
    return $tpl -> fetch( 'articles/article-edit-pages' );
  }

  public static function editArticle( $id )
  {
    \System::deleteSessionVar( 'id_menu' );
    
    $out      = \admin\view\PagePanel::show( false, 'Zapisz', true, 'Artykuły', 'formularz', false, false, 'saveArticle()' );

    $article  = \admin\factory\Articles::getArticleParam( $id );
    $pages    = \admin\factory\Articles::getPagesAssign( $id );
    $language = \admin\factory\Articles::getArticleParamLanguage( $id );

    $tpl = new \Savant3();
    $tpl -> _article      = $article;
    $tpl -> _language     = $language;
    $tpl -> _pages        = $pages;
    $tpl -> _layouts      = \admin\factory\Layouts::getLayouts();
    $tpl -> _version      = \admin\factory\Articles::getMaxArticleVersion( $id ) + 1;
    $tpl -> _images       = \admin\factory\Articles::getArticleImages( $id );
    $tpl -> _files        = \admin\factory\Articles::getArticleFiles( $id );
    $out .= $tpl -> fetch( 'articles/article-edit' );

    return $out;
  }

  public static function showVersions( $id )
  {
    global $bs;

    $out = \admin\view\PagePanel::show( false, false, false, 'Artykuł: ' . \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   = './';

    $count_page = \admin\factory\Articles::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\Articles::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( 'articles/article-versions' );

    return $out;
  }

  public static function showArticles()
  {    
    $delete = "var articles = ''; $('input:checkbox:checked').each( function() { if ( articles != '' ) articles = articles + ':'; articles = articles + $(this).val(); } ); $.prompt( 'Trwa przenoszenie do archiwum...' ); xajax_moveArticlesToArchive( articles );";
        
    $change = "$.prompt( 'Na pewno chcesz wykonać wybraną czynność?',{ submit: function(e,v,m,f) { if ( v == true ) document.location.href='./?rw=change-enabled&id=[param]'; }, buttons: { tak: true, nie: false }, focus: 1 })";
    $change = 'onClick="' . $change . '"';
    
    
    $clone = "$.prompt( 'Na pewno chcesz wykonać wybraną czynność?',{ submit: function(e,v,m,f) { if ( v == true ) xajax_cloneArticle( [param] ); }, buttons: { 'tak': true, 'nie': false }, focus: 1 })";
    $clone = 'onClick="' . $clone . '"';

    $tab_comment[0] = 'nie';
    $tab_comment[1] = 'tak';
    $tab_comment[2] = 'tylko dla zarejestrowanych';

    $tab = \System::getArticlesTitle();

    $dbrowse = new \DataBrowse( 'pp_articles', 'archive = 0' );
    $dbrowse -> addPosition( 'id',          'Tytuł',            '', $tab );
    $dbrowse -> addPosition( 'enabled',     'Opublikowany',     '', \System::getComboYesNo(), 'cursor: pointer; width: 100px; text-align: center;', false, false, $change );
    $dbrowse -> addPosition( 'date_modify', 'Data modyfikacji', '', '', 'width: 150px; text-align: center;', 'date_modify' );
    $dbrowse -> addPositionSimple( 'duplikuj',  '', '', $clone );
    $dbrowse -> addPositionSimple( 'pokaż',     '', './?act=show_versions' );
    $dbrowse -> addPositionSimple( 'edytuj',    '', './?rw=edit' );
    $dbrowse -> addPositionSimple( 'usuń',      '', '', \System::deleteAction() );
    $dbrowse -> setParam( 'id' );
    $dbrowse -> addSort( 'date_add DESC' );
    $dbrowse -> addLp();
    $dbrowse -> addCheckbox();
    $dbrowse -> addFiltr( 'id{trans}',  'Tytuł',        $tab );
    $dbrowse -> addFiltr( 'enabled',    'Opublikowany', \System::getComboYesNo() );
    $dbrowse -> addFiltr( 'show_title', 'Pokaż tytuł',  \System::getComboYesNo() );
    $dbrowse -> addMenu( \admin\view\PagePanel::show( true, false, false, 'Artykuły', 'formularz', false, false, false, $delete ) );
    $out .= $dbrowse -> draw();

    return $out;
  }
}
?>