<?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 ( floatval( $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 */
          $url = 'http://www.shoppro.project-dc.pl/updates/' . $dir . '/ver_' . $ver . '_sql.txt';
          $ch = curl_init( $url );
          curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
          curl_setopt( $ch, CURLOPT_HEADER, true );
          $response = curl_exec( $ch );
          $content_type = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );

          if ( $response and strpos( $content_type, 'text/plain' ) !== false )
            $sql = explode( PHP_EOL, $response );

          if ( is_array( $sql ) and !empty( $sql ) ) foreach ( $sql as $query )
          {
            if ( $sql  )
            {
              $result = $mdb -> query( $query );
            }
          }

          /* usuwanie zbędnych plików */
          $url = 'http://www.shoppro.project-dc.pl/updates/' . $dir . '/ver_' . $ver . '_files.txt';
          $ch = curl_init( $url );
          curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
          curl_setopt( $ch, CURLOPT_HEADER, true );
          $response = curl_exec( $ch );
          $content_type = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );

          if ( $response and strpos( $content_type, 'text/plain' ) !== false )
            $files = explode( PHP_EOL, $response );

          if ( is_array( $files ) and !empty( $files ) ) foreach ( $files as $file )
          {
            if ( strpos( $file, 'F: ' ) !== false )
            {
              $file = substr( $file, 3, strlen( $file ) );
              if ( file_exists( $file ) )
                unlink( $file );
            }

            if ( strpos( $file, 'D: ' ) !== false )
            {
              $dir = substr( $file, 3, strlen( $file ) );
              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' ] );
  }
}
