<?php
namespace controls;

class Ranker {
  public function getReportsContent()
  {
    $rw = \S::get( 'rw' );

    if ( $rw == 'create-report' )
      \factory\Ranker::createPdfReport( \S::get( 'sites' ), \S::get( 'report-form' ), \S::get( 'date-from' ), \S::get( 'date-to' ) );

    return \view\Ranker::drawReportsForm();
  }

  public function getSummaryContent()
  {
    return \view\Ranker::drawSummary( \S::get( 'month' ), \S::get( 'year' ) );
  }

  public function getClientsContent()
  {
    $rw = \S::get( 'rw' );

    if ( $rw == 'add_new' )
    {
      if ( \factory\Ranker::addClient( \S::get( 'login' ), \S::get( 'password' ), \S::get( 'sites' ), \S::get( 'enabled' ), \S::get( 'type' ) ) )
        \S::alert( 'Klient został dodany.' );
      header( 'Location: /?p=ranker_clients' );
      exit;
    }

    if ( $rw == 'save' )
    {
      if ( \factory\Ranker::saveClient( \S::get( 'client_id' ), \S::get( 'login' ), \S::get( 'password' ), \S::get( 'sites' ), \S::get( 'enabled' ), \S::get( 'type' ) ) )
        \S::alert( 'Klient został zmodyfikowany.' );
      header( 'Location: /?p=ranker_clients' );
      exit;
    }

    if ( $rw == 'del' )
    {
      if ( \factory\Ranker::deleteClient( \S::get( 'id' ) ) )
        \S::alert( 'Klient został usunięty.' );
      header( 'Location: /?p=ranker_clients' );
      exit;
    }

    if ( $rw == 'add' )
      return \view\Ranker::drawClientEdit();

    if ( $rw == 'edit' )
      return \view\Ranker::drawClientEdit( \S::get( 'id' ) );

    return \view\Ranker::drawClientList();
  }

  public function getSitesContent()
  {
    $rw = \S::get( 'rw' );

    if ( $rw == 'add' )
      return \view\Ranker::drawSiteEdit();

    return \view\Ranker::drawSitesList( \S::get( 'id' ), \S::get( 'month' ), \S::get( 'year' ) );
  }

  public static function phrase_delete()
  {
    if ( \factory\Ranker::phrase_delete( \S::get( 'phrase-id' ) ) )
        \S::alert( 'Wybrana fraza została usunięta.' );
    header( 'Location: /ranker/main_view/id=' . \S::get( 'site-id' ) );
    exit;
  }

  public static function phrase_costs_save()
  {

    if ( \factory\Ranker::phrase_costs_save(
            \S::get( 'phrase-id' ),
            \S::get( 'from' ),
            \S::get( 'to' ),
            \S::get( 'price' )
          )
        )
        \S::alert( 'Koszty frazy zostały zmodyfikowane.' );
    header( 'Location: /ranker/main_view/id=' . \S::get( 'site-id' ) );
    exit;
  }

  public static function phrase_costs_edit()
  {
    return \view\Ranker::phrase_costs_edit( \S::get( 'site-id' ), \S::get( 'phrase-id' ) );
  }

  public static function phrase_save()
  {
     if ( \factory\Ranker::phrase_save( \S::get( 'phrase_id' ), \S::get( 'phrase' ), \S::get( 'discount' ), \S::get( 'localization' ), \S::get( 'date_start' ),
                                        \S::get( 'date_end' ), \S::get( 'site_id' ), \S::get( 'days_offset' ), \S::get( 'to_all' ) ) )
        \S::alert( 'Fraza została zapisana.' );
      header( 'Location: /ranker/main_view/id=' . \S::get( 'site_id' ) );
      exit;
  }

  public static function phrase_edit()
  {
    return \view\Ranker::phrase_edit( \S::get( 'site-id' ), \S::get( 'phrase-id' ) );
  }

  public static function site_delete()
  {
    $prev_id = \factory\Ranker::prev_site_id( \S::get( 'site-id' ), true );

    if ( \factory\Ranker::site_delete( \S::get( 'site-id' ) ) )
      \S::alert( 'Wybrana strona została usunięta.' );
    header( 'Location: /ranker/main_view/id=' . $prev_id );
    exit;
  }

  public static function main_view()
  {
    return \view\Ranker::main_view(
              \S::get( 'id' ),
              \S::get( 'month' ),
              \S::get( 'year' )
            );
  }

  public static function site_save()
  {
    if ( $id = \factory\Ranker::site_save(
                  \S::get( 'id' ), \S::get( 'name' ), \S::get( 'url' ), \S::get( 'discount' ), \S::get( 'comments' ), \S::get( 'subscription' ),
                  \S::get( 'date_start' ), \S::get( 'date_end' ), \S::get( 'majestic' ), \S::get( 'semstorm' ), \S::get( 'need_confirm' )
                )
          )
        \S::alert( 'Strona została zapisana.' );
    header( 'Location: /ranker/main_view/id=' . $id );
    exit;
  }

  public static function site_edit()
  {
    return \Tpl::view( 'ranker/site-edit', [
              'site' => \factory\Ranker::site_details( \S::get( 'id' ) )
            ] );
  }
}
?>
