-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixsave.php
137 lines (108 loc) · 5.67 KB
/
matrixsave.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
require_once('headerDB.inc.php');
$db = new PDO('mysql:host=' . $config["host"] . ';dbname=' . $config["db"], $config["user"], $config["pass"]);
//table,updCol,pcol1,pid1,col2,id2,col3,id3,col4,id4,col5,id5,updVal
$updVal = htmlspecialchars_decode($_POST["updVal"]);
$updVal = str_replace(array(' .CL', ' .LIST'), '', $updVal);
if (substr($updVal, -4) == '<br>') $updVal = substr_replace($updVal,'',-4);
$updVal = str_replace(array('<br>', '<br />'), PHP_EOL, $updVal);
// quick workaround for error when extra carriage return being included when only one blank line expected
#file_put_contents ('a.txt', '---' . PHP_EOL . $updVal . PHP_EOL . '---' . PHP_EOL, FILE_APPEND);
$updVal = str_replace('
', '
', $updVal);
#file_put_contents ('a.txt', '---' . PHP_EOL . $updVal . PHP_EOL . '---' . PHP_EOL, FILE_APPEND);
if ($_POST["table"] !== 'lookupqualities') {
// if using visId for other purpose, do not save
if ($_POST["col2"] == 'visId') $_POST["id2"] = '';
if ($_POST["col3"] == 'visId') $_POST["id3"] = '';
if ($_POST["col4"] == 'visId') $_POST["id4"] = '';
if ($_POST["col5"] == 'visId') $_POST["id5"] = '';
}
if ($_POST["updVal"] == '' && in_array($_POST["table"],['itemstatus','itemattributes']) && in_array($_POST["updCol"],['dateCompleted','dateModified','dateCreated','deadline'])) $_POST["updVal"] = 'NULL';
if ($_POST["table"] == 'nextactions') {
// if no longer NA its straightforward
if ($updVal == 'n') {
$query = "DELETE FROM nextactions WHERE `nextaction` = '" . $_POST["pid1"] . "'";
$db->query($query);
#file_put_contents ('a.txt', PHP_EOL . $query . PHP_EOL . '--', FILE_APPEND);
}
// otherwise iterate
if ($updVal == 'y') {
// get parent ids
$query = "SELECT parentId FROM lookup WHERE `itemId` = '" . $_POST["pid1"] . "'";
$result = $db->query($query);
$array = $result->fetchAll();
// foreach ($array as $row)
// foreach ($row as $key => $val)
// file_put_contents ('a.txt', PHP_EOL . $key . ' x ' . $val . PHP_EOL . '--', FILE_APPEND);
// update nextactions table
foreach ($array as $row) {
$query = "INSERT INTO nextactions (`parentId`,`nextaction`) VALUES ('" . $row['parentId'] . "','" . $_POST["pid1"] . "')";
$result = $db->query($query);
// file_put_contents ('a.txt', PHP_EOL . $query . PHP_EOL . '--', FILE_APPEND);
}
}
$db = NULL; // destroy connection
die;
}
/*
if ($_POST['updCol'] == 'title' && in_array($_POST['table'], array('list','checklist'))) {
$updVal = preg_replace('/[0-9]+/', '', $updVal);
$updVal = trim($updVal);
}
*/
$updVal = mysqli_real_escape_string($config["conn"], $updVal);
// file_put_contents ('a.txt',$_POST['updCol'] . 'has' . $_POST['table'], FILE_APPEND);die;
// logic seems klunky but do not change without thorough testing
if ($_POST["pid1"] == '' || $_POST["pid1"] == 'undefined') $_POST["pid1"] = false;
if ($_POST["id2"] == '') $_POST["id2"] = 'undefined';
if ($_POST["id3"] == '') $_POST["id3"] = 'undefined';
if ($_POST["id4"] == '') $_POST["id4"] = 'undefined';
if ($_POST["id5"] == '') $_POST["id5"] = 'undefined';
$query = "SELECT " . $_POST["updCol"] . " FROM " . $_POST["table"] . " WHERE 1 = 1";
if ($_POST["pid1"]) $query .= " AND `" . $_POST["pcol1"] . "` = '" . $_POST["pid1"] . "'";
if ($_POST["id2"] !== 'undefined') $query .= " AND `" . $_POST["col2"] . "` = '" . $_POST["id2"] . "'";
if ($_POST["id3"] !== 'undefined') $query .= " AND `" . $_POST["col3"] . "` = '" . $_POST["id3"] . "'";
if ($_POST["id4"] !== 'undefined') $query .= " AND `" . $_POST["col4"] . "` = '" . $_POST["id4"] . "'";
if ($_POST["id5"] !== 'undefined') $query .= " AND `" . $_POST["col5"] . "` = '" . $_POST["id5"] . "'";
// file_put_contents ('a.txt',PHP_EOL . $query, FILE_APPEND);
$result = $db->query($query);
if ($result->rowCount() > 0) {
// file_put_contents ('a.txt',PHP_EOL . 'has', FILE_APPEND);
$i = true;
} else {
// file_put_contents ('a.txt',PHP_EOL . 'has not', FILE_APPEND);
$i = false;
}
if ($i) {
$query = "UPDATE " . $_POST["table"] . " SET `" . $_POST["updCol"] . "` = " . ($_POST["updVal"] == 'NULL' ? "NULL" : "'" . $updVal . "'") . " WHERE 1 = 1 ";
if ($_POST["pid1"]) $query .= " AND `" . $_POST["pcol1"] . "` = '" . $_POST["pid1"] . "'";
if ($_POST["id2"] !== 'undefined') $query .= " AND `" . $_POST["col2"] . "` = '" . $_POST["id2"] . "'";
if ($_POST["id3"] !== 'undefined') $query .= " AND `" . $_POST["col3"] . "` = '" . $_POST["id3"] . "'";
if ($_POST["id4"] !== 'undefined') $query .= " AND `" . $_POST["col4"] . "` = '" . $_POST["id4"] . "'";
if ($_POST["id5"] !== 'undefined') $query .= " AND `" . $_POST["col5"] . "` = '" . $_POST["id5"] . "'";
} else {
$query = "INSERT INTO " . $_POST["table"] . " (`" .
$_POST["pcol1"] .
"`,`" . $_POST["updCol"];
if ($_POST["col2"] !== 'undefined') $query .= "`, `" . $_POST["col2"];
if ($_POST["col3"] !== 'undefined') $query .= "`, `" . $_POST["col3"];
if ($_POST["col4"] !== 'undefined') $query .= "`, `" . $_POST["col4"];
if ($_POST["col5"] !== 'undefined') $query .= "`, `" . $_POST["col5"];
$query .= "`)
VALUES ('" .
"','" . $updVal;
if ($_POST["col2"]) $query .= "', '" . $_POST["id2"];
if ($_POST["col3"]) $query .= "', '" . $_POST["id3"];
if ($_POST["col4"]) $query .= "', '" . $_POST["id4"];
if ($_POST["col5"]) $query .= "', '" . $_POST["id5"];
$query .= "')";
}
// file_put_contents ('a.txt',PHP_EOL . $query, FILE_APPEND);
// this $result is not used, only for debugging, but the query is needed
$result = $db->query($query);
//file_put_contents ('a.txt',PHP_EOL . $i . ' x ' . $query, FILE_APPEND);
//file_put_contents ('a.txt',PHP_EOL . $result, FILE_APPEND);
$db = NULL; // destroy connection
?>