<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.8.0
 *	LISENSE: FL4HLD2A8696 - https://www.flynax.com/flynax-software-eula.html
 *	PRODUCT: Real Estate Script
 *	DOMAIN : immofil.com
 *	FILE   : IMPORT.PHP
 *
 *	This script is a commercial software and any kind of using it must be
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2020
 *	https://www.flynax.com
 *
 ******************************************************************************/

session_start();

require_once('../../../includes/config.inc.php');
require_once(RL_CLASSES .'rlDb.class.php' );
require_once(RL_CLASSES .'reefless.class.php' );

$autoload = RL_ROOT . 'vendor/autoload.php';
if (file_exists($autoload)) {
    require_once($autoload);
}


$rlDb = new rlDb();
$reefless = new reefless();

$reefless -> connect(RL_DBHOST, RL_DBPORT, RL_DBUSER, RL_DBPASS, RL_DBNAME);
$reefless -> loadClass('Debug');
$reefless -> loadClass('Valid');
$reefless -> loadClass('Categories');
$reefless -> loadClass('ImportExportCategories', null, 'importExportCategories');

// utf8 library functions
function loadUTF8functions() {
    $names = func_get_args();

    if (empty($names)) {
        return false;
    }

    foreach ($names as $name) {
        if (file_exists(RL_LIBS . 'utf8' . RL_DS . 'utils' . RL_DS . $name . '.php')) {
            require_once RL_LIBS . 'utf8' . RL_DS . 'utils' . RL_DS . $name . '.php';
        }
    }
}

if ( file_exists($rlImportExportCategories -> tmp_file) )
{
	if ( $_GET['start'] == 0 )
	{
		unset($_SESSION['imex_plugin']['ic_count']);
	}

	$start = (int)$_GET['start'];
	$limit = 1000;
	$stop = $start + $limit;
	$next_iteration = false;

	$languages = $rlDb -> fetch(array('Code'), array('Status' => 'active'), null, null, 'languages');
	$GLOBALS['languages'] = &$languages;

	$listing_type = $_SESSION['imex_plugin']['listing_type'];
	$categories = $rlImportExportCategories -> getCategories($start, $stop, true);

	$c_count = count($categories);
	if ( $c_count == $limit )
	{
		array_splice($categories, -1, 1); // remove last element
		$next_iteration = true;
	}

	$status = $rlImportExportCategories -> import($categories, $listing_type);

	$response = array(
		'ic_count' => $rlImportExportCategories -> last_import_count,
		'next' => $next_iteration,
		'start' => $stop - 2,
		'status' => $status
	);
	echo json_encode($response);
}
