<?
namespace admin\factory;

class BannerManager
{
  public function getBanners( $from , $lpk )
  {
    global $lang , $db , $sys;

    $lp       = 1;
    $tab      = $sys -> getComboYesNo();
    $banners  = '';

    $query = $db -> prepare( 'SELECT title , link , enabled , id , o FROM pcms_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 pcms_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 pcms_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('" . $lang -> getTrans( 'T_POTWIERDZ_USUNIECIE' ) . "',{ callback: mycallbackform, buttons: { " . $lang -> getTrans( 'T_TAK' ) . ": true, " . $lang -> getTrans( 'T_NIE' ) . ": false }, focus: 1 })";
      $akcja = 'onClick="'.$akcja.'"';
      $ban['action']    = $akcja;
      $banners[]      = $ban;
      $lp++;
    }
    $query -> closeCursor();

    return $banners;
  }

  public function getMaxOrder()
  {
    global $db;

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

  public function down( $id )
  {
    global $db , $lang , $sys;

    $query  = $db -> prepare( 'SELECT o FROM pcms_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 pcms_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 pcms_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 pcms_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();

    \System::setAlert( $lang -> getTrans( 'T_PRZESUNIETO_BANER' ) );
    \System::deleteCacheAdmin();
    \System::deleteCache();
  }

  public function up( $id )
  {
    global $db , $lang , $sys;

    $query  = $db -> prepare( 'SELECT o FROM pcms_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 pcms_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 pcms_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 pcms_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();

    \System::setAlert( $lang -> getTrans( 'T_PRZESUNIETO_BANER' ) );
    \System::deleteCacheAdmin();
    \System::deleteCache();
  }

  public function delete( $id )
  {
    global $lang , $db , $sys;

    $query  = $db -> prepare( 'SELECT image FROM pcms_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 pcms_banner WHERE id = :id' );
      $query2 -> bindValue( ':id' , $id , \PDO::PARAM_INT );
      $query2 -> execute();
      $query2 -> closeCursor();
    }
    $query -> closeCursor();

    \System::setAlert( $lang -> getTrans( 'T_USUNIETO_BANER' ) );
    \System::deleteCacheAdmin();
    \System::deleteCache();
  }
}
?>