<?php
namespace factory;

class RankerReseller {
  
  public function createPdfReport( $sites, $report_form, $date_from, $date_to )
  {
    global $db, $user;
    
    if ( is_array( $sites ) ) foreach ( $sites as $site )
    {    
      $phrases = array();
      
      $query = $db -> prepare( 'SELECT * FROM pro_rr_phrases WHERE site_id = :site_id AND phrase NOT LIKE \'*%\' ORDER BY phrase ASC' );
      $query -> bindValue( ':site_id', $site, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $positions  = array();
        $costs      = array();
        
        $query2 = $db -> prepare( 'SELECT discount FROM pro_rr_phrases_reseller WHERE reseller_id = :reseller_id AND phrase_id = :phrase_id' );
        $query2 -> bindValue( ':phrase_id',     $row['id'],  \PDO::PARAM_INT );
        $query2 -> bindValue( ':reseller_id',   $user['id'], \PDO::PARAM_INT );
        $query2 -> execute();
        if ( $query2 -> rowCount() ) while  ( $row2 = $query2 -> fetch() )
          $row['discount'] = $row2['discount'];
        $query2 -> closeCursor();

        $query2 = $db -> prepare( 'SELECT * FROM pro_rr_phrases_positions WHERE phrase_id = :phrase_id AND date <= "' . $date_to . '" AND date >= "' . $date_from . '" GROUP BY date ORDER BY date ASC' );
        $query2 -> bindValue( ':phrase_id', $row['id'], \PDO::PARAM_INT );
        $query2 -> execute();
        if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        {
          $positions[$row2['date']]  = $row2['position'];
          $costs[] = $row2['position'];
        }
        $query2 -> closeCursor();

        $prices = \factory\Ranker::getPhrasePrices( $row['id'], $user['id'] );
        for ( $i = 0; $i < count( $prices ); $i++ )
        {
          foreach ( $costs as $cost )
          {
            if ( $cost >= $prices[$i]['start'] && $cost <= $prices[$i]['end'] )
              $prices[$i]['count']++;
          }
        }  

        $row['prices']    = $prices;
        $row['positions'] = $positions;
        $phrases[] = $row;
      }
      $query -> closeCursor();
      
      $query2 = $db -> prepare( 'SELECT url, discount FROM pro_rr_sites WHERE id = :id' );
      $query2 -> bindValue( ':id', $site, \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
      {
        $page['url']      = $row2['url'];
        $page['discount'] = $row2['discount'];
      }
      $query2 -> closeCursor();
      
      $page['id']       = $page_id;
      $page['phrases']  = $phrases;
      $pages[]          = $page;
    }
    
    $tpl = new \Savant3;
    $tpl -> _pages        = $pages;
    $tpl -> _date_from    = $date_from;
    $tpl -> _date_to      = $date_to;
    $tpl -> _report_form  = $report_form;
    $out = $tpl -> fetch( 'ranker/reports-pdf' );
    
    define( "_MPDF_TEMP_PATH", 'temp/' );
    include( "resources/mpdf60/mpdf.php" );

    $link = 'temp_t/raport-pozycji-' . mktime() . '.pdf';
    
    $mpdf = new \mPDF( '', 'A4-L', '', '', 5, 5, 5, 5, 5, 5, 'L' );
    $mpdf -> AddPage('','','','','on');
    $mpdf -> WriteHTML( $out );
    $mpdf -> Output( $link, 'F' );
    \S::alert( 'Link do raportu: <a href="/?rw=download&file=' . $link . '" style="color: #000;">pobierz</a>' );
  }
  
  public function getSites( $user_id )
  {
    global $db;
      
    $query = $db -> prepare( 'SELECT site_id AS id, url, name FROM pro_rr_clients_sites AS prcs, pro_rr_sites AS prs WHERE client_id = :client_id AND prs.id = prcs.site_id ORDER BY url ASC' );
    $query -> bindValue( ':client_id', $user_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $sites[] =  $row;
    $query -> closeCursor();    
    return $sites;
  }
  
  public function getSitesSummary( $month, $year, $user_id, $reseller_id = false )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT site_id FROM pro_rr_clients_sites AS prcs, pro_rr_sites AS prs WHERE client_id = :client_id AND prs.id = prcs.site_id ORDER BY url ASC' );
    $query -> bindValue( ':client_id', $user_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $sites[] =  \factory\Ranker::getSiteSummary( $row['site_id'], $month, $year, $reseller_id );
    $query -> closeCursor();
    
    return $sites;
  }
  
  public function savePhrase( $reseller_id, $phrase_id, $discount )
  {
    global $db;
    
    if ( $discount == '0.00' || !$discount ) $discount = null;
    
    $query = $db -> prepare( 'DELETE FROM pro_rr_phrases_reseller WHERE phrase_id = :phrase_id AND reseller_id = :reseller_id' );
    $query -> bindValue( ':phrase_id',    $phrase_id,   \PDO::PARAM_INT );
    $query -> bindValue( ':reseller_id',  $reseller_id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    $query = $db -> prepare( 'INSERT INTO pro_rr_phrases_reseller ( phrase_id, reseller_id, discount ) VALUES ( :phrase_id, :reseller_id, :discount )' );
    $query -> bindValue( ':phrase_id',    $phrase_id,     \PDO::PARAM_INT );
    $query -> bindValue( ':reseller_id',  $reseller_id,   \PDO::PARAM_INT );
    $query -> bindValue( ':discount',     $discount,      \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();
    
    return true;
  }
  
  public function saveCosts( $reseller_id, $phrase_id, $from, $to, $price )
  {
    global $db;
    
    $query = $db -> prepare( 'DELETE FROM pro_rr_phrases_prices WHERE phrase_id = :phrase_id AND reseller_id = :reseller_id' );
    $query -> bindValue( ':phrase_id',    $phrase_id,   \PDO::PARAM_INT );
    $query -> bindValue( ':reseller_id',  $reseller_id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    for ( $i = 1; $i <= count( $price ); $i++ )
    {
      if ( !empty( $from[$i] ) && !empty( $to[$i] ) && !empty( $price[$i] ) )
      {
        $query = $db -> prepare( 'INSERT INTO pro_rr_phrases_prices ( phrase_id, reseller_id, start, end, price ) VALUES ( :phrase_id, :reseller_id, :start, :end, :price )' );
        $query -> bindValue( ':phrase_id',    $phrase_id,         \PDO::PARAM_INT );
        $query -> bindValue( ':reseller_id',  $reseller_id,       \PDO::PARAM_INT );
        $query -> bindValue( ':start',        trim( $from[$i] ),  \PDO::PARAM_INT );
        $query -> bindValue( ':end',          trim( $to[$i] ),    \PDO::PARAM_INT );
        $query -> bindValue( ':price',        trim( $price[$i] ), \PDO::PARAM_STR );
        $query -> execute();
        $query -> closeCursor();
      }
    }
    return true;
  }
  
  public function deleteClient( $id, $reseller_id )
  {
    global $db;

    $query = $db -> prepare( 'DELETE FROM pro_rr_clients WHERE id = :id AND reseller_id = :reseller_id' );
    $query -> bindValue( ':id',           $id,          \PDO::PARAM_INT );
    $query -> bindValue( ':reseller_id',  $reseller_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() )
    {      
      $query2 = $db -> prepare( 'DELETE FROM pro_rr_clients_sites WHERE client_id = :client_id' );
      $query2 -> bindValue( ':client_id',  $id,  \PDO::PARAM_INT );
      $query2 -> execute();
      $query2 -> closeCursor();
      $query2 -> closeCursor();
      return true;
    }
    $query -> closeCursor();
    return false;
  }
  
  public function saveClient( $id, $login, $password, $sites, $enabled, $reseller_id )
  {
    global $db;
    
    if ( !$id || !$login )
      return false;
    
    $enabled == 'on' ? $enabled = 1 : $enabled = 0;
    
    $query = $db -> prepare( 'UPDATE pro_rr_clients SET login = :login, enabled = :enabled WHERE id = :id AND reseller_id = :reseller_id' );
    $query -> bindValue( ':login',        $login,       \PDO::PARAM_STR );
    $query -> bindValue( ':enabled',      $enabled,     \PDO::PARAM_INT );
    $query -> bindValue( ':id',           $id,          \PDO::PARAM_INT );
    $query -> bindValue( ':reseller_id',  $reseller_id, \PDO::PARAM_INT );
    $query -> execute();
    
    if ( $password )
    {
      $query = $db -> prepare( 'UPDATE pro_rr_clients SET password = :password WHERE id = :id' );
      $query -> bindValue( ':password',   md5( $password ), \PDO::PARAM_STR );
      $query -> bindValue( ':id',         $id,              \PDO::PARAM_INT );
      $query -> execute();
    }
    
    $query = $db -> prepare( 'DELETE FROM pro_rr_clients_sites WHERE client_id = :client_id' );
    $query -> bindValue( ':client_id', $id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    if ( is_array( $sites ) ) 
    {
      $query = $db -> prepare( 'INSERT INTO pro_rr_clients_sites ( client_id, site_id ) VALUES ( :client_id, :site_id )' );
      foreach ( $sites as $site )
      {
        $query -> bindValue( ':client_id',  $id,    \PDO::PARAM_INT );
        $query -> bindValue( ':site_id',    $site,  \PDO::PARAM_INT );
        $query -> execute();
      }
      $query -> closeCursor();
    }
    return true;
  }
    
  public function addClient( $login, $password, $sites, $enabled, $reseller_id )
  {
    global $db;
    
    if ( !$login || !$password )
      return false;
    
    $enabled == 'on' ? $enabled = 1 : $enabled = 0;
    
    $query = $db -> prepare( 'INSERT INTO pro_rr_clients ( login, password, enabled, reseller_id ) VALUES ( :login, :password, :enabled, :reseller_id )' );
    $query -> bindValue( ':password',     md5( $password ),     \PDO::PARAM_STR );
    $query -> bindValue( ':login',        strtolower( $login ), \PDO::PARAM_STR );
    $query -> bindValue( ':enabled',      $enabled,             \PDO::PARAM_INT );
    $query -> bindValue( ':reseller_id',  $reseller_id,         \PDO::PARAM_INT );
    $query -> execute();
    
    $client_id = $db -> lastInsertId();
    
    if ( is_array( $sites ) ) 
    {
      $query = $db -> prepare( 'INSERT INTO pro_rr_clients_sites ( client_id, site_id ) VALUES ( :client_id, :site_id )' );
      foreach ( $sites as $site )
      {
        $query -> bindValue( ':client_id',  $client_id, \PDO::PARAM_INT );
        $query -> bindValue( ':site_id',    $site,      \PDO::PARAM_INT );
        $query -> execute();
      }
      $query -> closeCursor();
    }
    
    return true;
  }
}
?>
