add(new DateInterval('P1D')); // PnYnMnDTnHnMnS $nextDay = $date->format('Y-m-d'); // ---------------------------------------------------------------- // Calculate the lower limit from which on data should be deleted. // This is either noon (if the date is in the future) or the provided // time (different from 12:00:00) during the night $cut = $day." ".$time; $d = new DateTime($cut); // If the time lays before noon, it belongs to the next day if ($d->format("His")<120000) { $d->add(new DateInterval('P1D')); // PnYnMnDTnHnMnS $cut = $d->format("Y-m-d H:i:s"); } // ================================================================ $db = mysql_connect($dbhost,$dbuser,$dbpass); if (!$db) die(mysql_error()); if (!mysql_select_db($dbname, $db)) die(mysql_error()); $query = "SELECT * FROM MeasurementType"; $sql = mysql_query($query); if (!$sql) die(mysql_error()); $measurements = array(); while($row = mysql_fetch_assoc($sql)) $measurements[$row['fMeasurementTypeKey']] = $row; // ---------------------------------------------------------------- // Now create the queries with the correct dates (date and time) // from the posted data and the times therein $queries = array(); array_push($queries, "LOCK TABLES Schedule WRITE"); array_push($queries, "DELETE FROM Schedule WHERE fStart>'".$cut."' AND DATE(ADDTIME(fStart, '-12:00')) = '".$day."'"); // ---------------------------------------------------------------- $last = $cut; if (count($data)!=1 || !empty($data[0][0])) // empty schedule foreach ($data as $row) { $t = $row[0]; // time // If there is a time set (first task in an observation), // remember the time, if not this is just a measurement // within an observation so duplicate the time if (!isset($t)) { $t = $save; $id++; } else { $save = $t; $id = 0; } // Check if the time is before noon. If it is before noon, // it belongs to the next day $d = date_parse($t); $t = $d['hour']<12 ? $nextDay." ".$t : $day." ".$t; if ($d==FALSE) die("Could not parse time '".$t."' with date_parse"); // Check all but the last task in a measurement whether // the are not unlimited if ($last==$t) { if ($measurements[$m]['fIsUnlimited']==true) die("Unlimited task '".$measurements[$m]['fMeasurementTypeName']."' detected before end of observation\n[".$last."|".($id-1)."]"); } if ($last>$t) die("Times not sequential\n[".$last."|".$t."]"); $last = $t; $m = $row[1]; // measurement $s = $row[2]; // source $v = $row[3]; // value // Check if task need source or must not have a source if ($measurements[$m]['fNeedsSource']==true && $s==0) die("Task '".$measurements[$m]['fMeasurementTypeName']."' needs source.\n[".$t."|".$id."]"); if ($measurements[$m]['fNeedsSource']!=true && $s>0) die("Task '".$measurements[$m]['fMeasurementTypeName']."' must not have source.\n[".$t."|".$id."]"); // Compile query $query = "INSERT INTO Schedule SET"; $query .= " fStart='".$t."'"; $query .= ",fMeasurementID=".$id; $query .= ",fMeasurementTypeKey=".$m; $query .= ",fUser='".$user."'"; if ($s>0) $query .= ",fSourceKey=".$s; // Check if this is a valid JSON object if (!json_decode('{'.$v.'}')) { switch (json_last_error()) { case JSON_ERROR_NONE: break; case JSON_ERROR_DEPTH: $err = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $err = 'Invalid or malformed JSON'; break; case JSON_ERROR_CTRL_CHAR: $err = 'Unexpected control character'; break; case JSON_ERROR_SYNTAX: $err = 'Syntax error'; break; case JSON_ERROR_UTF8: $err = 'Malformed UTF-8 characters'; break; default: $err = 'Unknown error'; break; } if (isset($err)) die($err." at ".$t." [entry #".($id+1)."]:\n".$v); } // PHP >= 5.5.0 // if (!json_decode('{'.$v.'}')) // die("Invalid option at ".$t.": ".$v." [JSON - ".json_last_error_msg()."]"); if ($v) $query .= ",fData='".$v."'"; // add query to the list of queries array_push($queries, $query); } array_push($queries, "UNLOCK TABLES"); // ================================================================ // Database interaction // ================================================================ foreach ($queries as $query) if (!mysql_query($query)) die(mysql_error()); mysql_close($db); ?>