<?php
namespace admin\factory;

class Update
{
  public static function update()
  {
    global $mdb, $settings;

    \S::delete_session( 'new-version' );

    $versions = file_get_contents( 'http://www.shoppro.project-dc.pl/updates/versions.php?key=' . $settings['update_key'] );
    $versions = explode( PHP_EOL, $versions );

    foreach ( $versions as $ver )
    {
      $ver = trim( $ver );
      if ( (float)$ver > (float)\S::get_version() )
      {
        if ( strlen( $ver ) == 5 )
          $dir = substr( $ver, 0, strlen( $ver ) - 2 ) . 0;
        else
          $dir = substr( $ver, 0, strlen( $ver ) - 1 ) . 0;

        $file = file_get_contents( 'http://www.shoppro.project-dc.pl/updates/' . $dir . '/ver_' . $ver . '.zip' );

        $dlHandler = fopen( 'update.zip' , 'w' );
        if ( !fwrite( $dlHandler, $file ) )
          return false;
        fclose( $dlHandler );

        if ( !file_exists( 'update.zip' ) )
          return false;
        else
        {
          /* aktualizacja bazy danych */
          $sql = file_get_contents( 'http://www.shoppro.project-dc.pl/updates/' . $dir . '/ver_' . $ver . '_sql.txt' );
          $sql = explode( PHP_EOL, $sql );
          if ( is_array( $sql ) and !empty( $sql ) ) foreach ( $sql as $query )
          {
            if ( $sql  )
              $result = $mdb -> query( $query );
          }

          /* usuwanie zbędnych plików */
          $lines = file_get_contents( 'http://www.shoppro.project-dc.pl/updates/' . $dir . '/ver_' . $ver . '_files.txt' );
          $lines = explode( PHP_EOL, $lines );
          if ( is_array( $lines ) ) foreach ( $lines as $line )
          {
            if ( strpos( $line, 'F: ' ) !== false )
            {
              $file = substr( $line, 3, strlen( $line ) );
              if ( file_exists( $file ) )
                unlink( $file );
            }

            if ( strpos( $line, 'D: ' ) !== false )
            {
              $dir = substr( $line, 3, strlen( $line ) );
              if ( is_dir( $dir ) )
                \S::delete_dir( $dir );
            }
          }

          /* wgrywanie nowych plików */
          $file_name = 'update.zip';

          $path = pathinfo( realpath( $file_name ), PATHINFO_DIRNAME );
          $path = substr( $path, 0, strlen( $path ) - 5 );
          $zip = new \ZipArchive;
          $res = $zip -> open( $file_name );
          if ( $res === TRUE )
          {
            $zip -> extractTo( $path );
            $zip -> close();
            unlink( $file_name );
          }

          $updateThis = fopen( '../libraries/version.ini', 'w' );
          fwrite( $updateThis, $ver );
          fclose( $updateThis );

          return true;
        }
      }
    }
  }

  public static function update0197()
  {
    global $mdb;

    $rows = $mdb -> select( 'pp_shop_order_products', [ 'id', 'product_id' ], [ 'parent_product_id' => null ] );
    foreach ( $rows as $row )
    {
      $parent_id = $mdb -> get( 'pp_shop_products', 'parent_id', [ 'id' => $row['product_id'] ] );
      if ( $parent_id )
        $mdb -> update( 'pp_shop_order_products', [ 'parent_product_id' => $parent_id ], [ 'id' => $row['id'] ] );
      else
        $mdb -> update( 'pp_shop_order_products', [ 'parent_product_id' => $row['product_id'] ], [ 'id' => $row['id'] ] );
    }

    $mdb -> update( 'pp_updates', [ 'done' => 1 ], [ 'name' => 'update0197' ] );
  }
}
