<?php
namespace admin\factory;
class Sitemap
{
  public static function sitemap( $sitemap, $available_domains, $domain_prefix, $www, $url )
  {
    global $mdb;

    $site_map_tmp = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
    $site_map_tmp .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
      $site_map_tmp .= '<url>' . PHP_EOL;
        $site_map_tmp .= '<loc>https://www.globelus.pl/</loc>' . PHP_EOL;
          $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' ) . '</lastmod>' . PHP_EOL;
          $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
          $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
      $site_map_tmp .= '</url>' . PHP_EOL;

      $results = $mdb -> select( 'globelus_adverts', [ 'id', 'title', 'last_refresh', 'date_add' ], [ 'AND' => [ 'visible' => 1, 'disabled_by_admin' => 0, 'OR' => [ 'active_to' => null, 'active_to[>=]' => date( 'Y-m-d' ) ] ] ] );
      foreach ( $results as $row )
      {
        $site_map_tmp .= '<url>' . PHP_EOL;
          $site_map_tmp .= '<loc>https://www.globelus.pl/oferta/' . $row['id'] . '/' . \S::seo( $row['title'], true ) . '</loc>' . PHP_EOL;
            $site_map_tmp .= '<lastmod>' . ( $row['last_refresh'] ? date( 'Y-m-d', strtotime( $row['last_refresh'] ) ) : date( 'Y-m-d', strtotime( $row['date_add'] ) ) ) . '</lastmod>' . PHP_EOL;
            $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
            $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
        $site_map_tmp .= '</url>' . PHP_EOL;
      }

    $site_map_tmp .= '</urlset>' . PHP_EOL;

    $fp = fopen( '../sitemap-oferty-pracy.xml', 'w' );
    fwrite( $fp, $site_map_tmp );
    fclose( $fp );

    $site_map_tmp = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
    $site_map_tmp .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
      $site_map_tmp .= '<url>' . PHP_EOL;
        $site_map_tmp .= '<loc>https://www.globelus.pl/</loc>' . PHP_EOL;
          $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' ) . '</lastmod>' . PHP_EOL;
          $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
          $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
      $site_map_tmp .= '</url>' . PHP_EOL;

      $results = $mdb -> select( 'globelus_countries', [ 'id', 'name' ] );
      foreach ( $results as $row )
      {
        $site_map_tmp .= '<url>' . PHP_EOL;
          $site_map_tmp .= '<loc>https://www.globelus.pl/oferty-pracy/' . \S::seo( $row['name'], true ) . '</loc>' . PHP_EOL;
            $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' ) . '</lastmod>' . PHP_EOL;
            $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
            $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
        $site_map_tmp .= '</url>' . PHP_EOL;

        $site_map_tmp .= '<url>' . PHP_EOL;
          $site_map_tmp .= '<loc>https://www.globelus.pl/oferty-pracy/' . \S::seo( $row['name'], true ) . '/bez-jezyka</loc>' . PHP_EOL;
            $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' )  . '</lastmod>' . PHP_EOL;
            $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
            $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
        $site_map_tmp .= '</url>' . PHP_EOL;

        $site_map_tmp .= '<url>' . PHP_EOL;
          $site_map_tmp .= '<loc>https://www.globelus.pl/oferty-pracy/' . \S::seo( $row['name'], true ) . '/bez-doswiadczenia</loc>' . PHP_EOL;
            $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' )  . '</lastmod>' . PHP_EOL;
            $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
            $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
        $site_map_tmp .= '</url>' . PHP_EOL;

        $site_map_tmp .= '<url>' . PHP_EOL;
          $site_map_tmp .= '<loc>https://www.globelus.pl/oferty-pracy/' . \S::seo( $row['name'], true ) . '/dla-par</loc>' . PHP_EOL;
            $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' )  . '</lastmod>' . PHP_EOL;
            $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
            $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
        $site_map_tmp .= '</url>' . PHP_EOL;

        $site_map_tmp .= '<url>' . PHP_EOL;
          $site_map_tmp .= '<loc>https://www.globelus.pl/oferty-pracy/' . \S::seo( $row['name'], true ) . '/od-zaraz</loc>' . PHP_EOL;
            $site_map_tmp .= '<lastmod>' . date( 'Y-m-d' )  . '</lastmod>' . PHP_EOL;
            $site_map_tmp .= '<changefreq>daily</changefreq>' . PHP_EOL;
            $site_map_tmp .= '<priority>1</priority>' . PHP_EOL;
        $site_map_tmp .= '</url>' . PHP_EOL;
      }

    $site_map_tmp .= '</urlset>' . PHP_EOL;

    $fp = fopen( '../sitemap-panstwa.xml', 'w' );
    fwrite( $fp, $site_map_tmp );
    fclose( $fp );

  //   if ( is_array( $available_domains ) and count( $available_domains ) )
  //   {
  //     /* to do */
  //   }
  //   else
  //   {
  //     $sitemap_tmp = $sitemap[ $url ];

  //     try {
  //       $results = $mdb -> query( 'SELECT '
  //                                     . 'name_seo '
  //                                   . 'FROM '
  //                                     . 'globelus_adverts_categories AS gac' ) -> fetchAll( \PDO::FETCH_ASSOC );
  //       if ( is_array( $results ) and count( $results ) ) foreach ( $results as $row )
  //       {
  //         $urls .= '<url>' . PHP_EOL;
  //         $urls .= '<loc>' . $domain_prefix . '://' . $www . $url . '/oferty-pracy/' . $row['name_seo'] . '</loc>' . PHP_EOL;
  //         $urls .= '<changefreq>weekly</changefreq>' . PHP_EOL;
  //         $urls .= '<priority>1</priority>' . PHP_EOL;
  //         $urls .= '</url>' . PHP_EOL;
  //       }
  //     }
  //     catch ( \Throwable $t ) {
  //       \S::pre( $mdb -> error() );
  //     }

  //     try {
  //       $results = $mdb -> query( 'SELECT '
  //                                     . 'name_seo '
  //                                   . 'FROM '
  //                                     . 'globelus_countries AS gc' ) -> fetchAll( \PDO::FETCH_ASSOC );
  //       if ( is_array( $results ) and count( $results ) ) foreach ( $results as $row )
  //       {
  //         $urls .= '<url>' . PHP_EOL;
  //         $urls .= '<loc>' . $domain_prefix . '://' . $www . $url . '/oferty-pracy/' . $row['name_seo'] . '</loc>' . PHP_EOL;
  //         $urls .= '<changefreq>weekly</changefreq>' . PHP_EOL;
  //         $urls .= '<priority>1</priority>' . PHP_EOL;
  //         $urls .= '</url>' . PHP_EOL;
  //       }
  //     }
  //     catch ( \Throwable $t ) {
  //       \S::pre( $mdb -> error() );
  //     }

  //     try {
  //       $results = $mdb -> query( 'SELECT '
  //                                     . 'id, title '
  //                                   . 'FROM '
  //                                     . 'globelus_adverts AS ga '
  //                                   . 'WHERE '
  //                                       . 'visible = 1 '
  //                                     . 'AND '
  //                                       . '( active_to IS NULL OR active_to <= \'' . date( 'Y-m-d' ) . '\' )' ) -> fetchAll( \PDO::FETCH_ASSOC );
  //       if ( is_array( $results ) and count( $results ) ) foreach ( $results as $row )
  //       {
  //         $urls .= '<url>' . PHP_EOL;
  //         $urls .= '<loc>' . $domain_prefix . '://' . $www . $url . '/oferta/' . $row['id'] . '/' . \S::seo( $row['title'], true ) . '</loc>' . PHP_EOL;
  //         $urls .= '<changefreq>weekly</changefreq>' . PHP_EOL;
  //         $urls .= '<priority>1</priority>' . PHP_EOL;
  //         $urls .= '</url>' . PHP_EOL;
  //       }
  //     }
  //     catch ( \Throwable $t ) {
  //       \S::pre( $mdb -> error() );
  //     }

  //     $sitemap_tmp = str_replace( '</urlset>', $urls . '</urlset>', $sitemap_tmp );

  //     $sitemap[ $url ] = $sitemap_tmp;
  //   }
    return $sitemap;
  }
}
