<?php
namespace view;
class Ranker
{
  public function drawReportsForm()
  {
    $tpl = new \Savant3;
    $tpl -> _sites    = \factory\Ranker::getSites();
    return $tpl -> fetch( 'ranker/reports' );
  }

  public function drawSummary( $month = '', $year = '' )
  {
    if ( $month ) \S::set_session( 'drawSitesList:month', $month );
    if ( $year ) \S::set_session( 'drawSitesList:year', $year );

    $month  = \S::get_session( 'drawSitesList:month' );
    $year   = \S::get_session( 'drawSitesList:year' );

    if ( !$month ) $month = date( 'm' );
    if ( !$year ) $year = date( 'Y' );

    $tpl = new \Savant3;
    $tpl -> _profit   = \factory\Ranker::getMonthProfit( $month, $year );
    $tpl -> _clients  = \factory\Ranker::getClientsSitesSummary( $month, $year );
    $tpl -> _month    = $month;
    $tpl -> _year     = $year;
    return $tpl -> fetch( 'ranker/summary' );
  }

  public function phrase_costs_edit( $site_id, $phrase_id )
  {
    $tpl = new \Savant3;
    $tpl -> site_id  = $site_id;
    $tpl -> phrase   = \factory\Ranker::getPhrase( $phrase_id );
    $tpl -> prices   = \factory\Ranker::getPhrasePrices( $phrase_id );
    return $tpl -> fetch( 'ranker/phrase-costs-edit' );
  }

  public function drawClientEdit( $id = '' )
  {
    $tpl = new \Savant3;
    $tpl -> _client   = \factory\Ranker::getClient( $id );
    $tpl -> _sites    = \factory\Ranker::getSites();
    return $tpl -> fetch( 'ranker/client-edit' );
  }

  public function drawClientList()
  {
    $tpl = new \Tpl;
    return $tpl -> render( 'ranker/client-list' );

    $out  = \view\PagePanel::show( true, false, false, 'Dodaj klienta' );

    $type[0] = 'klient';
    $type[1] = 'reseller';

    $dbrowse = new \DataBrowse( 'pro_rr_clients' );
    $dbrowse -> addPosition( 'login', 'Login', '', '', '', true );
    $dbrowse -> addPosition( 'password', 'Hasło', '', '', '', true );
    $dbrowse -> addPosition( 'enabled', 'Aktywny', '', \S::getComboYesNo(), 'text-align: center;' );
    $dbrowse -> addPosition( 'last_logged', 'Ost. logowanie', '', '', 'text-align: center;', 'last_logged' );
    $dbrowse -> addPosition( 'type', 'Typ', '', $type, 'text-align: center;' );
		$dbrowse -> addPositionSimple( 'edytuj' , 'Edytuj'  , './?rw=edit' );
		$dbrowse -> addPositionSimple( 'usuń'   , 'Usuń'    , '' , \S::deleteAction() );
		$dbrowse -> setParam( 'id' );
		$dbrowse -> addSort( 'last_logged DESC' );
		$dbrowse -> addLp();
    $dbrowse -> addFiltr( 'login', 'Login' );
    $dbrowse -> addFiltr( 'enabled', 'Aktywny', \S::getComboYesNo() );
    $dbrowse -> addFiltr( 'type', 'Typ', $type );
    $dbrowse -> addMenu( $out );
		return $dbrowse -> draw();
  }

  public function phrase_edit( $site_id, $phrase_id = '' )
  {
    $tpl = new \Tpl;
    $tpl -> site_id       = $site_id;
    $tpl -> phrase        = \factory\Ranker::getPhrase( $phrase_id );
    $tpl -> sites         = \factory\Ranker::getSites();
    return $tpl -> render( 'ranker/phrase-edit' );
  }

  public static function main_view( $id = '', $month = '', $year = '' )
  {
    global $user;

    if ( $user['type'] != 'admin' )
      return false;

    if ( $id !== null )
      \S::set_session( 'ranker:main_view:id', $id );

    if ( $month )
      \S::set_session( 'ranker:main_view:month', $month );

    if ( $year )
      \S::set_session( 'ranker:main_view:year', $year );

    if ( !$month and !\S::get_session( 'ranker:main_view:month' ) )
    {
      $month = date( 'm' );
      \S::set_session( 'ranker:main_view:month', $month );
    }

    if ( !$year and !\S::get_session( 'ranker:main_view:year' ) )
    {
      $year = date( 'Y' );
      \S::set_session( 'ranker:main_view:year', $year );
    }

    $month  = \S::get_session( 'ranker:main_view:month' );
    $year   = \S::get_session( 'ranker:main_view:year' );
    $id     = \S::get_session( 'ranker:main_view:id' );

    if ( !$id )
      $id = \factory\Ranker::first_site_id();

    $site = \factory\Ranker::site_details( $id );

    $tpl = new \Tpl;
    $tpl -> day_profit = \factory\Ranker::day_profit();
    $tpl -> month_profit = \factory\Ranker::getMonthProfit( $month, $year );
    $tpl -> sites          = \factory\Ranker::sites_list();
    $tpl -> phrases        = \factory\Ranker::site_phrases_details( $site['id'], $month, $year, '', '', $user['login'] );
    $tpl -> next_site_id   = \factory\Ranker::next_site_id( $site['id'] );
    $tpl -> prev_site_id   = \factory\Ranker::prev_site_id( $site['id'] );
    $tpl -> majestic       = \factory\Ranker::majestic( $site['id'] );
    $tpl -> semstorm = \factory\Ranker::semstorm( $site['id'] );
    $tpl -> comments       = \factory\Ranker::site_comments( $site['id'], $month, $year );
    $tpl -> site           = $site;
    $tpl -> month          = $month;
    $tpl -> year           = $year;
    $tpl -> sites_to_confirm = \factory\Ranker::sites_to_confirm();
    return $tpl -> render( 'ranker/main-view' );
  }
}
?>
