<?php

ini_set("memory_limit","2048M");
set_time_limit(6000000);

ini_set('display_errors', 1);
error_reporting(E_ALL);

//$mysqli = new mysqli('localhost', 'root', '1234', 'intelectra');
$mysqli = new mysqli('localhost', 'intelectra_shop_', 'Wwja71!3', 'intelectra_shop__');


$items_table = '`items`';
$attribute_field = '`attribute_18`';
$ignore_fields = array('anr');
$item_ready_ids = array();

// Reset items update status
//$mysqli->query("UPDATE " . $items_table . " SET update_ready = 0");

$starttime = microtime(true);

// Get items
$query = "
	SELECT
		id, number, name
	FROM
		" . $items_table . "
	WHERE
		update_ready = 0
	LIMIT
		60
";
$rsItems = $mysqli->query($query);

$items2go = array();
while ($I = $rsItems->fetch_object()) {
	$items2go[$I->number] = $I;
	$item_ready_ids[] = $I->id;
}

// Get machine associations
$query = "
	SELECT
		anr, bezeichnungoriginal, bezeichnung1, nr
	FROM
		import_geraetezuordnung
	WHERE
		anr IN ('" . implode("','", array_keys($items2go)) . "')
";
$rsAssocs = $mysqli->query($query);

while ($A = $rsAssocs->fetch_object()) {
	if (isset($items2go[$A->anr])) {
		if (!isset($items2go[$A->anr]->attributes) || !is_array($items2go[$A->anr]->attributes)) {
			$items2go[$A->anr]->attributes = array();
		}

		foreach ($A as $fieldName => $fieldValue) {
			if (!in_array($fieldName, $ignore_fields)) {
				$searchWord = preg_replace(
					"@[^aA-zZ0-9]|[_]@sui", '', strtolower($fieldValue)
				);
				
				$items2go[$A->anr]->attributes[$searchWord] = $searchWord;
			}
		}
	}

	// Way too slow; key = value instead
	//$items2go[$A->anr]->attributes = array_keys($items2go[$A->anr]->attributes);
	//$items2go[$A->anr]->attributes = array_unique($items2go[$A->anr]->attributes);
}

// Make inserts for tmp table
$inserts = array();
foreach ($items2go as $itemNo => $itemData) {
	if (isset($itemData->attributes) && is_array($itemData->attributes) && count($itemData->attributes) > 0) {
		$inserts[] = "(" . $itemData->id . ", '" . implode(' ', $itemData->attributes) . "')";
	}
}

// Create temp table for multi updates as a cache
$mysqli->query("
	CREATE TEMPORARY TABLE
		tmp_items_updates (
			id INT, 
			text MEDIUMTEXT
		)
");

// Insert changes into temp table
$insertQuery = "
	INSERT INTO
		tmp_items_updates (id, text)
	VALUES
		" . implode(', ', $inserts) . "
";
$mysqli->query($insertQuery);

// Update items
$updateQuery = "
	UPDATE
		" . $items_table . " i,
		tmp_items_updates i_tmp
	SET
		i." . $attribute_field . " = i_tmp.text
	WHERE
		i.id = i_tmp.id
	AND
		i_tmp.id IS NOT NULL
";
$mysqli->query($updateQuery);

// Mark items as ready
if (count($item_ready_ids) > 0) {
	$readyQuery = "
		UPDATE
			" . $items_table . "
		SET
			update_ready = 1
		WHERE
			id IN (" . implode(',', $item_ready_ids) . ")
	";
	$mysqli->query($readyQuery);
}

// Get items count to go
$queryCount = "
	SELECT
		COUNT(id) AS cnt
	FROM
		" . $items_table . "
	WHERE
		update_ready = 0
";
$rsCount = $mysqli->query($queryCount);
echo 'Items to go: ', $rsCount->fetch_object()->cnt, '<br>';

echo microtime(true) - $starttime;
?>