<?
namespace admin\factory;

class Banners
{
  public static function getBannersAll()
  {
    global $db, $cache, $config;

    $key = 'getBannersAll';
    
    if ( !$banners = $cache -> fetch( $key ) )
    {      
      $query = $db -> prepare('SELECT title, link, image FROM pp_banner WHERE enabled = :enabled ORDER BY o');
      $query -> bindValue(':enabled', 1, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $banners[] = $row;
      $query -> closeCursor();
      $cache -> store( $key, $banners, $config['cache_expire'] );
    }
    
    return $banners;
  }
  
  public static function getCountBanners()
  {
    global $db;

    $query = $db -> prepare( 'SELECT count(id) FROM pp_banner' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row[0];
  }
  
  public static function getBanners( $from, $lpk )
  {
    global $db;

    $lp       = 1;
    $tab      = \System::getComboYesNo();
    $banners  = '';

    $query = $db -> prepare( 'SELECT title, link, enabled, id, o FROM pp_banner ORDER BY o ASC LIMIT ' . $from . ',' . $lpk );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $ban['title']    = $row['title'];
      $ban['link']     = $row['link'];
      $ban['lp']       = $lp;
      $ban['enabled']  = $tab[$row['enabled']];
      $ban['id']       = $row['id'];

      $query2 = $db -> prepare( 'SELECT id FROM pp_banner WHERE o > :o ORDER BY o ASC LIMIT 1' );
      $query2 -> bindValue( ':o', $row['o'], \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        $ban['down'] = true;
      else
        $ban['down'] = false;
      $query2 -> closeCursor();

      $query2 = $db -> prepare( 'SELECT id FROM pp_banner WHERE o < :o ORDER BY o ASC LIMIT 1' );
      $query2 -> bindValue( ':o', $row['o'], \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        $ban['up'] = true;
      else
        $ban['up'] = false;
      $query2 -> closeCursor();

      $akcja = "function mycallbackform(v,m,f){
                  if( v == true )
                    document.location.href='index.php?rw=del&amp;id=" . $row['id'] . "';
                }";
      $akcja .= "$.prompt( 'Na pewno chcesz usunąć wybrany baner?',{ callback: mycallbackform, buttons: { tak: true, nie: false }, focus: 1 })";
      $akcja = 'onClick="'.$akcja.'"';
      $ban['action']  = $akcja;
      $banners[]      = $ban;
      $lp++;
    }
    $query -> closeCursor();

    return $banners;
  }

  public static function getMaxOrder()
  {
    global $db;

    $query = $db -> prepare( 'SELECT MAX(o) FROM pp_banner' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row[0]+1;
  }

  public static function moveBannerDown( $id )
  {
    global $db;

    try
    {
      $db -> beginTransaction();
      
      $query  = $db -> prepare( 'SELECT o FROM pp_banner WHERE id = :id' );
      $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $o1 = $row['o'];
      $query -> closeCursor();

      $query = $db -> prepare( 'SELECT id, o FROM pp_banner WHERE o > :order ORDER BY o ASC LIMIT 1' );
      $query -> bindValue( ':order', $o1, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $query2 = $db -> prepare( 'UPDATE pp_banner SET o = :order WHERE id = :id' );
        $query2 -> bindValue( ':order', $row['o'],  \PDO::PARAM_INT );
        $query2 -> bindValue( ':id',    $id,        \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();

        $query2 = $db -> prepare( 'UPDATE pp_banner SET o = :order WHERE id = :id' );
        $query2 -> bindValue( ':order', $o1,        \PDO::PARAM_INT );
        $query2 -> bindValue( ':id',    $row['id'], \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();
      }
      $query -> closeCursor();      
      
      $db -> commit();
      
      \System::deleteCacheAdmin();
      \System::deleteCache();
      return true;      
    } 
    catch ( \PDOException $ex ) 
    {
      $db -> rollBack();
      \System::setAlert( 'Błąd: ' . $ex -> getMessage() );
    }
    return false;
  }

  public static function moveBannerUp( $id )
  {
    global $db;

    try 
    {
      $db -> beginTransaction();
      
      $query  = $db -> prepare( 'SELECT o FROM pp_banner WHERE id = :id' );
      $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $o1 = $row['o'];
      $query -> closeCursor();

      $query = $db -> prepare( 'SELECT id, o FROM pp_banner WHERE o < :order ORDER BY o DESC LIMIT 1' );
      $query -> bindValue( ':order', $o1, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $query2 = $db -> prepare( 'UPDATE pp_banner SET o = :order WHERE id = :id' );
        $query2 -> bindValue( ':order', $row['o'],  \PDO::PARAM_INT );
        $query2 -> bindValue( ':id',    $id,        \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();

        $query2 = $db -> prepare( 'UPDATE pp_banner SET o = :order WHERE id = :id' );
        $query2 -> bindValue( ':order', $o1,        \PDO::PARAM_INT );
        $query2 -> bindValue( ':id',    $row['id'], \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();
      }
      $query -> closeCursor();
      
      $db -> commit();
      
      \System::deleteCacheAdmin();
      \System::deleteCache();
      return true;      
    } 
    catch ( \PDOException $ex ) 
    {
      $db -> rollBack();
      \System::setAlert( 'Błąd: ' . $ex -> getMessage() );
    }
    return false;
  }

  public static function deleteBanner( $id )
  {
    global $db;

    try
    {
      $db -> beginTransaction();
      
      $query  = $db -> prepare( 'SELECT image FROM pp_banner WHERE id = :id' );
      $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        if ( file_exists( "../" . $row['image'] ) && $row['image'] )
          unlink("../" . $row['image']);

        $query2 = $db -> prepare( 'DELETE FROM pp_banner WHERE id = :id' );
        $query2 -> bindValue( ':id', $id, \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();
      }
      $query -> closeCursor();

      $db -> commit();
      
      \System::deleteCacheAdmin();
      \System::deleteCache();      
      return true;      
    } 
    catch ( \PDOException $ex ) 
    {
      $db -> rollBack();
      \System::setAlert( 'Błąd: ' . $ex -> getMessage() );
    }
    return false;
  }
}
?>