www/upl_brew.php

Wed, 16 Oct 2019 21:05:03 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 16 Oct 2019 21:05:03 +0200
changeset 514
3c680d1dea35
parent 299
047ead629d4a
permissions
-rw-r--r--

Added dutch array strings to a global php script. Changed fermentables, hops, yeast and misc inventory scripts to use these strings between the database scripts and javascript scripts. This makes filtering on types strings useable. Added these changes to the product/recipe forum and print exports too.

<?php
require_once('config.php');

$target_dir = "tmp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
	echo "Fout 1: bestand bestaat al. ";
	$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000 && $uploadOk) {
	echo "Fout 2: het bestand is te groot. ";
	$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "json" && $uploadOk) {
	echo "Fout 3: alleen JSON bestanden toegestaan. ";
	$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
	exit;
}

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
	echo "Verwerken bestand ". basename( $_FILES["fileToUpload"]["name"]). "<br />";
} else {
	echo "Fout 4: er ging iets fout met de upload.";
	exit;
}

$json= file_get_contents($target_file);
$brew = json_decode($json, true);
$records =  count($brew);
if ($records != 1) {
	echo "Fout 5: dit is geen JSON brouw logfile";
	exit;
}

$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $connect) {
	echo "Fout 6: ".mysqli_connect_errno()." ".mysqli_connect_error();
	exit;
}
mysqli_set_charset($connect, "utf8" );
date_default_timezone_set('Europe/Amsterdam');

foreach($brew as $brewdata) {

	foreach($brewdata as $item) {
		$temps  = $item['brewdata'];
		$events = $item['annotations'];
		$date   = date("Y-m-d H:i:s"   , strtotime($item['Date']));
		$code   = strtok($item['Recipe'], " ");
		$name   = strtok('\0');
		$insert = 0;
		$update = 0;
		$delete = 0;

		$sql = "SELECT uuid FROM products WHERE code='".$code."';";
		$result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect));
		if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
			$uuid = $row['uuid'];
		}
		echo "Brouw: ".$code." ".$name."<br />";

		$sql = "DELETE FROM log_brews WHERE code='".$code."';";
		$result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect));
		$delete += mysqli_affected_rows($connect);

		foreach($temps as $temp) {
			// Create full datetime from the Label
			$t = floatval(substr($temp['Label'], 0, 2)) * 60 + floatval(substr($temp['Label'], 3, 2));
			$time = new DateTime($date);
			$time->add(new DateInterval('PT' . $t . 'M'));
			$stamp = $time->format('Y-m-d H:i:s');

			$sql  = "INSERT INTO log_brews SET version='2";
			$sql .= "', datetime='" . $stamp;
			$sql .= "', uuid='" . $uuid;
			$sql .= "', code='" . $code;
			$sql .= "', name='" . mysqli_real_escape_string($connect, $name);
			$sql .= "', pv_mlt='" . $temp['MLT_pv'];
			$sql .= "', sp_mlt='" . $temp['MLT_sp'];
			$sql .= "', pwm_mlt='" . $temp['MLT_pwm'];
			if (isset($temp['HLT_pv']))
				$sql .= "', pv_hlt='" . $temp['HLT_pv'];
			if (isset($temp['HLT_sp']))
				$sql .= "', sp_hlt='" . $temp['HLT_sp'];
			if (isset($temp['HLT_pwm']))
				$sql .= "', pwm_hlt='" . $temp['HLT_pwm'];
			$sql .= "';";
			$result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect));
			$insert += mysqli_affected_rows($connect);
		}

		// The events are annotations in the JSON file.
		foreach($events as $event) {
			$t = floatval(substr($event['value'], 0, 2)) * 60 + floatval(substr($event['value'], 3, 2));
			$time = new DateTime($date);
			$time->add(new DateInterval('PT' . $t . 'M'));
			$stamp = $time->format('Y-m-d H:i:s');

			$sql  = "UPDATE log_brews SET event='".mysqli_real_escape_string($connect, $event['label']['content'])."' WHERE ";
			$sql .= "datetime='".$stamp."' AND uuid='".$uuid."';";
			$result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect));
			$update += mysqli_affected_rows($connect);
		}

		$sql = "UPDATE products SET log_brew='1' WHERE code='".$code."';";
		$result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect));
		$update += mysqli_affected_rows($connect);
		echo "Database delete: ".$delete.", insert: ".$insert.", update: ".$update." records." . PHP_EOL;
	}
}
// Don't clutter the upload directory.
unlink($target_file);

?>

mercurial