<?php
namespace factory;

class RankerClients 
{
  
  public static function site_save( $site_id, $comments )
  {
    global $mdb;
    return $mdb -> update( 'pro_rr_sites', [ 'comments' => $comments ], [ 'id' => $site_id ] );
  }
  
  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['reseller_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['reseller_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, subscription 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'];
        $page['subscription'] = $row2['subscription'];
      }
      $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( 'client/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, $user_login = '' )
  {
    global $db, $user;
      
    $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, $user['reseller_id'], $user_login );
    $query -> closeCursor();
    
    return $sites;
  }
  
  public function getPrevClientSiteId( $id, $client_id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT prs.id FROM pro_rr_sites AS prs, pro_rr_clients_sites AS prcs WHERE prs.id = prcs.site_id AND client_id = :client_id AND name < ( SELECT name FROM pro_rr_sites AS prs1 WHERE prs1.id = :id ) AND archive = 0 ORDER BY name DESC LIMIT 1' );
    $query -> bindValue( ':id',         $id,        \PDO::PARAM_INT );
    $query -> bindValue( ':client_id',  $client_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row['id'];
    else
    {
      $site = self::getClientSite( 0, $client_id, 'DESC' );
      return $site['id'];
    }
    $query -> closeCursor();
    return false;    
  }
  
  public function getNextClientSiteId( $id, $client_id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT prs.id FROM pro_rr_sites AS prs, pro_rr_clients_sites AS prcs WHERE prs.id = prcs.site_id AND client_id = :client_id AND name > ( SELECT name FROM pro_rr_sites AS prs1 WHERE prs1.id = :id ) AND archive = 0 ORDER BY name ASC LIMIT 1' );
    $query -> bindValue( ':id',         $id,      \PDO::PARAM_INT );
    $query -> bindValue( ':client_id',  $client_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row['id'];
    else
    {
      $site = self::getClientSite( false, $client_id );
      return $site['id'];
    }
    $query -> closeCursor();
    return false;
  }
  
  public function getClientSites( $client_id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT prs.id, url, name, discount FROM pro_rr_sites AS prs, pro_rr_clients_sites AS prcs WHERE prs.id = prcs.site_id AND client_id = :client_id ORDER BY name ASC' );
    $query -> bindValue( ':client_id', $client_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $sites[] = $row;
    $query -> closeCursor();
    return $sites;
  }
  
  public function getClientSite( $id, $client_id, $sort = 'ASC' )
  {
    global $db;
    
    if ( $id )
      $sql = 'AND prs.id = :id';
    
    $query = $db -> prepare( 'SELECT '
                                . 'prs.id, url, discount, reseller_id, comments, subscription '
                              . 'FROM '
                                  . 'pro_rr_sites AS prs '
                                    . 'INNER JOIN pro_rr_clients_sites AS prcs ON prs.id = prcs.site_id '
                                    . 'INNER JOIN pro_rr_clients AS prc ON prc.id = prcs.client_id '
                              . 'WHERE '
                                  . 'client_id = :client_id ' . $sql . ' '
                              . 'ORDER BY '
                                  . 'name ' . $sort . ' '
                              . 'LIMIT 1' );
    $query -> bindValue( ':client_id', $client_id, \PDO::PARAM_INT );
    if ( $id )
      $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row;
    $query -> closeCursor();
    return false;
  }
}
?>
