<?
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
function __autoload_my_classes( $classname )
{
	$q = explode( '\\' , $classname );
	$c = array_pop( $q );
	$f = 'autoload/' . implode( '/' , $q ) . '/class.' . $c . '.php';
	if ( $c == 'Savant3' )
	{
		require_once 'autoload/Savant3.php';
		return true;
	}
	if ( file_exists( $f ) )
		require_once( $f );
}

spl_autoload_register( '__autoload_my_classes' );
mb_internal_encoding( "UTF-8" );

define( 'OPD_DIR' , 'autoload/' );
require_once OPD_DIR . 'opd.class.php';
require_once 'config.php';
require_once 'libraries/medoo.php';
require_once 'resources/xajax/xajax_core/xajax.inc.php';
require_once 'libraries/grid/config.php';


date_default_timezone_set('Europe/Warsaw');

function sortByOption( $a, $b ) {
  return strcmp( $a['date'], $b['date']);
}

session_start();

$db = opdClass::create(
                          array(
                              'dsn' => 'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'],
                              'user' => $config['db']['user'] ,
                              'password' => $config['db']['password'] ,
                              'cache' => 'temp/' ,
                              'debugConsole' => $config['db']['debug']
                              )
                        );
$db -> query( 'SET NAMES utf8' );

$mdb = new medoo( [
                    'database_type' => 'mysql',
                    'database_name' => $config['db']['name'],
                    'server'        => $config['db']['host'],
                    'username'      => $config['db']['user'],
                    'password'      => $config['db']['password'],
                    'charset'       => 'utf8'
                ] );

/* usunięcie komentarza do strony */
if ( \S::get( 'a' ) == 'comment_delete' )
{
  $result = \factory\Ranker::delete_site_comment_info( \S::get( 'site_id' ), \S::get( 'date' ) );
  echo json_encode( [ 'status' => 'ok' ] );
  exit;
}

/* zapisanie komentarza z danego dnia */
if ( \S::get( 'a' ) == 'comment_save' )
{
  $result = \factory\Ranker::change_site_comment_info( \S::get( 'site_id' ), \S::get( 'date' ), \S::get( 'comment' ) );
  echo json_encode( [ 'status' => 'ok' ] );
  exit;
}

/* pobranie komentarza z danego dnia */
if ( \S::get( 'a' ) == 'comment_details' )
{
  echo $mdb -> get( 'pro_rr_sites_comments', 'comment', [ 'AND' => [ 'site_id' => \S::get( 'site_id' ), 'date' => \S::get( 'date' ) ] ] );
  exit;
}

/* potiwerdzenie wyników strony */
if ( \S::get( 'a' ) == 'site_confirm' )
{
  $result = \factory\Ranker::site_confirm( \S::get( 'site_id' ) );
  echo json_encode( [ 'status' => 'ok' ] );
  exit;
}

/* odświeżenie pozycji */
if ( \S::get( 'a' ) == 'refresh_phrase_position' )
{
  $mdb -> delete( 'phrase_positions_statistic', [
            'AND' => [
              'phrase_id' => \S::get( 'phrase_id' ),
              'date' => date( 'Y-m-d' )
            ]
          ] );

  $mdb -> delete( 'pro_rr_phrases_positions', [
            'AND' => [
              'phrase_id' => \S::get( 'phrase_id' ),
              'date' => date( 'Y-m-d' )
            ]
          ] );

  $mdb -> update( 'pro_rr_phrases', [
                      'last_checked' => null,
                      'ds_date' => null,
                      'ds_id' => null
                    ], [
                      'id' => \S::get( 'phrase_id' )
                    ] );

  echo json_encode( [ 'status' => 'ok' ] );
  exit;
}

/* zmiana pozycji */
if ( \S::get( 'a' ) == 'change_phrase_position' )
{
  $mdb -> update( 'phrase_positions_statistic', [
                      'position' => \S::get( 'position' )
                    ], [
                      'AND' => [
                        'phrase_id' => \S::get( 'phrase_id'),
                        'date' => \S::get( 'date' )
                      ]
                    ] );

  if ( $mdb -> count( 'pro_rr_phrases_positions', [
                  'AND' => [
                    'phrase_id' => \S::get( 'phrase_id'),
                    'date'      => \S::get( 'date' )
                  ]
                ] ) )
  {
    if ( \S::get( 'position' ) != '' )
      $mdb -> update( 'pro_rr_phrases_positions', [
                          'position' => \S::get( 'position' )
                        ], [
                          'AND' => [
                            'phrase_id' => \S::get( 'phrase_id'),
                            'date'      => \S::get( 'date' )
                          ]
                        ] );
    else
      $mdb -> delete( 'pro_rr_phrases_positions', [
                          'AND' => [
                            'phrase_id' => \S::get( 'phrase_id'),
                            'date'      => \S::get( 'date' )
                          ]
                        ] );
  }
  else
  {
    $mdb -> insert( 'pro_rr_phrases_positions', [
                        'position' => \S::get( 'position' ),
                        'phrase_id' => \S::get( 'phrase_id'),
                        'date'      => \S::get( 'date' )
                      ] );
  }
  echo json_encode( [ 'status' => 'ok' ] );
  exit;
}

/* odświeżenie zarobków */
if ( \S::get( 'a' ) == 'refresh-earnings' )
{
  \S::deleteCache( 'temp/' );
  $month  = \S::get_session( 'ranker:main_view:month' );
  $year   = \S::get_session( 'ranker:main_view:year' );
  echo json_encode( [
          'status'        => 'ok',
          'month_profit'  => \factory\Ranker::getMonthProfit( $month, $year ),
          'day_profit'    => \factory\Ranker::day_profit()
        ] );
  exit;
}