<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
function __autoload_my_classes( $classname )
{
  $q = explode( '\\', $classname );
  $c = array_pop( $q );
  $f = 'autoload/' . implode( '/', $q ) . '/class.' . $c . '.php';

  if ( file_exists( $f ) )
    require_once( $f );
}
spl_autoload_register( '__autoload_my_classes' );
date_default_timezone_set( 'Europe/Warsaw' );

require_once 'config.php';
require_once 'libraries/medoo/medoo.php';

session_start();

$mdb = new medoo( [
        'database_type' => 'mysql',
        'database_name' => $database['name'],
        'server'        => $database['host'],
        'username'      => $database['user'],
        'password'      => $database['password'],
        'charset'       => 'utf8',
        'logging' => true
        ] );

if ( !$lang = \S::get_session( 'lang-pl' ) )
{
  $lang = \front\factory\Languages::lang_translations( 'pl' );
  \S::set_session( 'lang-pl', $lang );
}

$work_types = [
          // 1 => \S::lang( 'praca-stala' ),
          // 2 => \S::lang( 'praca-tymczasowa' ),
          // 3 => \S::lang( 'praca-sezonowa' ),
          // 4 => \S::lang( 'praktyka/staz' ),
          // 5 => \S::lang( 'inna' ),
          6 => 'praca na czas nieokreślony',
          7 => 'praca na czas określony/tymczasowa',
          8 => 'staż/praktyka',
          9 => 'B2B',
          10 => 'umowa agencyjna',
          11 => 'praca zdalna',
  ];

$dom = new DOMDocument();
$dom -> encoding = 'utf-8';
$dom -> xmlVersion = '1.0';
$dom -> formatOutput = true;
$root = $dom -> createElement( 'jobs' );

$rows = $mdb -> select( 'globelus_adverts', '*', [ 'visible' => 1 ] );
if ( is_array( $rows ) ) foreach ( $rows as $row )
{
  $job_offer = $dom -> createElement( 'job' );

  $job_id = $dom -> createElement( 'id' );
  $job_id -> appendChild( $dom -> createCDATASection( $row['id'] ) );

  $job_offer -> appendChild( $job_id );

  $job_title = $dom -> createElement( 'title' );
  $job_title -> appendChild( $dom -> createCDATASection( $row['title'] ) );

  $job_offer -> appendChild( $job_title );

  $job_date_add = $dom -> createElement( 'date' );
  $job_date_add -> appendChild( $dom -> createCDATASection( $row['date_add'] ) );

  $job_offer -> appendChild( $job_date_add );

  $job_email = $dom -> createElement( 'email' );
  $job_email -> appendChild( $dom -> createCDATASection( $row['email'] ) );

  $job_offer -> appendChild( $job_email );

  $job_phone = $dom -> createElement( 'phone' );
  $job_phone -> appendChild( $dom -> createCDATASection( $row['phone'] ) );

  $job_offer -> appendChild( $job_phone );

  $job_contact_name = $dom -> createElement( 'contact_name' );
  $job_contact_name -> appendChild( $dom -> createCDATASection( $row['contact_person'] ) );

  $job_offer -> appendChild( $job_contact_name );

  $job_company = $dom -> createElement( 'company' );
  $job_company -> appendChild( $dom -> createCDATASection( $mdb -> get( 'globelus_firms_data', 'firm_name', [ 'user_id' => $row['user_id' ] ] ) ) );

  $job_offer -> appendChild( $job_company );

  $job_company_website = $dom -> createElement( 'company_website' );
  $job_company_website -> appendChild( $dom -> createCDATASection( 'https://www.globelus.pl/' ) );

  $job_offer -> appendChild( $job_company_website );

  $job_job_url = $dom -> createElement( 'job_url' );
  $job_job_url -> appendChild( $dom -> createCDATASection( 'https://www.globelus.pl/oferta/' . $row['id'] . '/' . \S::seo( $row['title'], true ) ) );

  $job_offer -> appendChild( $job_job_url );

  $job_contract_type = $dom -> createElement( 'contract_type' );
  $job_contract_type -> appendChild( $dom -> createCDATASection( $work_types[ $row['work_type'] ] ) );

  $job_offer -> appendChild( $job_contract_type );

  $job_salary_rate = $dom -> createElement( 'salary_rate' );
  $job_salary_rate -> appendChild( $dom -> createCDATASection( $row['salary'] ) );

  $job_offer -> appendChild( $job_salary_rate );

  $job_country = $dom -> createElement( 'country' );
  $job_country -> appendChild( $dom -> createCDATASection( $mdb -> get( 'globelus_countries', 'name', [ 'id' => $row['country_id' ] ] ) ) );

  $job_offer -> appendChild( $job_country );

  $job_region = $dom -> createElement( 'region' );
  $job_region -> appendChild( $dom -> createCDATASection( '' ) );

  $job_offer -> appendChild( $job_region );

  $job_city = $dom -> createElement( 'region' );
  $job_city -> appendChild( $dom -> createCDATASection( $row['city'] ) );

  $job_offer -> appendChild( $job_city );

  $job_address = $dom -> createElement( 'address' );
  $job_address -> appendChild( $dom -> createCDATASection( '' ) );

  $job_offer -> appendChild( $job_address );

  $job_description = $dom -> createElement( 'description' );
  $job_description -> appendChild( $dom -> createCDATASection( $row['text'] ) );

  $job_offer -> appendChild( $job_description );

  $root -> appendChild( $job_offer );
}

// $root = $dom->createElement('Movies');

// $movie_node = $dom->createElement('movie');

// $attr_movie_id = new DOMAttr('movie_id', '5467');

// $movie_node->setAttributeNode($attr_movie_id);

// $child_node_title = $dom->createElement('Title', 'The Campaign');

// $movie_node->appendChild($child_node_title);

// $child_node_year = $dom->createElement('Year', 2012);

// $movie_node->appendChild($child_node_year);

// $child_node_genre = $dom->createElement('Genre', 'The Campaign');

// $movie_node->appendChild($child_node_genre);

// $child_node_ratings = $dom->createElement('Ratings', 6.2);

// $movie_node->appendChild($child_node_ratings);

// $root->appendChild($movie_node);

// $dom->appendChild($root););


$dom -> appendChild( $root );
$dom->save( 'job-offers.xml' );

echo "XML has been successfully created";
?>