[insert_php]
global $schemaKey, $schemaFKeys;
$title = $year = $freelink = $otherlink = $ttlErr = $authErr = $topicErr = $linkErr1 = $linkErr2 = “”;
$authors = $topics = array(“”,””);
$nofreelink = false;
$ready = false;
include(“/home/appliedct/appliedcategorytheory.org/wp-content/themes/twentyfifteen_mod/utils.php”);
include(“/home/appliedct/appliedcategorytheory.org/wp-content/themes/twentyfifteen_mod/utils.js”);
[/insert_php]
var counters = {author:[insert_php] echo count($authors); [/insert_php], topic: [insert_php] echo count($topics); [/insert_php]};
function resetForm(formID) {
var form = document.getElementById(formID);
var elts = form.elements;
for (i=0; i
[insert_php]
// This section processes the inputs of the form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$ready = true;
if (empty($_POST["ztitle"])) {
$ttlErr = "* Title is required.";
$ready = false;
} else {
$title = capitalize(testInput($_POST["ztitle"]));
$ttlErr = "";
}
if ($_POST["authors"][0]=="") {
$authErr = "* At least one author is required.";
$ready = false;
} else {
$authErr = "";
for ($i=0; $i
(* = required field)
[insert_php]
// This section adds the inputs to the database
if ($_SERVER["REQUEST_METHOD"] == "POST" and $ready) {
global $schemaKey, $schemaFKeys;
$servername = "mysql.appliedcategorytheory.org";
$dbname = "appliedcategorytheory_or";
$username = "appliedcategoryt";
$password = "Z65iDPVZ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$paperData = array('title' => $title);
if ($freelink) {
$paperData['free_link'] = $freelink;
}
if ($year != "") {
$paperData['year'] = $year;
}
if ($otherlink != "") {
$paperData['other_access'] = $otherlink;
}
$paperID = addRow($conn,'Paper',$paperData);
$tables = array('Person','Area');
$rels = array('Author','Topic','Interest');
$newData = array('Person' => array_filter($authors), 'Area' => array_filter($topics));
$oldData = array();
foreach ($tables as $table) {
$otherKey = $schemaKey[$table];
$entries = array();
foreach ($newData[$table] as $entry) {
$entries[] = "$otherKey='$entry'";
}
$tableQuery = "SELECT $otherKey FROM $table WHERE (" . join(" OR ",$entries) . ")";
$tableRes = $conn->query($tableQuery);
$oldData[$table] = array();
while ($row = $tableRes->fetch_assoc()) {
$oldData[$table][] = $row[$otherKey];
}
$diff = array_diff($newData[$table],$oldData[$table]);
if (count($diff)>0) {
$insertQuery = "INSERT INTO $table ($otherKey) VALUES ('" . join("'), ('", $diff) . "')";
if ($conn->query($insertQuery) === TRUE) {
echo "$table records inserted successfully.
";
} else {
echo "Error: " . $insertQuery . "
" . $conn->error;
}
}
}
$relQuery = buildInsertCheck('Paper',$paperID, array('Person' => $authors,'Area' => $topics), array('Author','Topic','Interest'));
$relRes = $conn->query($relQuery);
$newRels = array();
foreach ($rels as $rel) {
$newRels[$rel] = array();
}
while ($row = $relRes->fetch_assoc()) {
foreach ($rels as $rel) {
$relTables = array_keys($schemaFKeys[$rel]);
if (empty($row[$rel . "ID"])) {
$newRels[$rel][] = "('" . $row[$relTables[0] . "ID"] . "', '" . $row[$relTables[1] . "ID"] . "')";
}
}
}
foreach ($rels as $rel) {
$fKeys = array_values($schemaFKeys[$rel]);
if (count($newRels[$rel])>0) {
$insertQuery = "INSERT INTO $rel ($fKeys[0],$fKeys[1]) VALUES " . join(", ",array_unique($newRels[$rel]));
if ($conn->query($insertQuery) === TRUE) {
echo "$rel records inserted successfully.
";
} else {
echo "Error: " . $insertQuery . "
" . $conn->error . "
";
}
}
}
$displayData = array('year' => $year, 'free_link' => $freelink, 'other_access' => $otherlink);
$displayData['authors'] = array_filter($authors);
$displayData['topics'] = array_filter($topics);
echo "\n";
}
[/insert_php]