-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.php
180 lines (152 loc) · 5.31 KB
/
controller.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
include_once 'person.php';
include_once 'reference.php';
$error = '';
$qs = false;
function desribed_by_source(){
if (isset($_COOKIE['described_by_source']) and count($_POST) == 0){
return $_COOKIE['described_by_source'];
}
elseif (isset($_POST['described_by_source'])){
return "checked";
}
return "unchecked";
}
$described_by_source = desribed_by_source();
setcookie("described_by_source", $described_by_source , time() + (86400 * 30 * 100));
function get_notice_closed() {
if (isset($_COOKIE['notice_closed'])) {
return $_COOKIE['notice_closed'] === "true";
}
return false; // Default value if cookie is not set
}
$notice_closed = get_notice_closed();
setcookie("notice_closed", $notice_closed, time() + (86400 * 30 * 100));
// Function to handle form submission
function handle_form_submission() {
$qs = ''; // Initialize $qs within the function scope
global $qs;
$reference1 = new Reference(
$_POST['ref_url'],
$_POST['ref_lang'],
$_POST['ref_authors'],
$_POST['ref_title'],
$_POST['ref_pubdate']);
$loopDate = new DateTime();
$today = new DateTime();
if ($reference1->getPubDate() != null){
//shift "today" to pubDate
$today = clone $reference1->getPubDate();
}
//handle person
$person1 = new Person();
$person1->setQID($_POST['person_QID']);
$person1->setName($_POST['fullname']);
$person1->setAge($_POST['age']);
$person1->setDescription($_POST['description']);
//set Date of Death
$dod = trim(strip_tags($_POST['dod']));
if ($dod != " "){
// makes in possible to input "last friday -1 weeks"
if (strripos($dod, "last ") !== false) {
for ($days = 7; $days--;) {
$dayOfWeek = $loopDate->modify('+1 days')->format('l');
if (strripos($dod, $dayOfWeek) !== false) {
if ($dayOfWeek == $today->format('l')) {
$person1->setDOD($today->format("Y-m-d"));
} else {
$person1->setDOD('last ' . $dayOfWeek . " " . $today->format("Y-m-d"));
}
}
}
// Month handling
for ($months = 0; $months < 11; $months++) {
$month = $loopDate->modify('-1 months')->format('F');
if (strripos($dod, $month) !== false) {
$date = new DateTime($month . " " . $today->format("Y"));
if ($date > $today) {
$date->modify('-1 years');
}
$person1->setDOD($date->format("Y-m-d"), 10);
}
}
} else {
// Year handling
if (preg_match('/^[21]\d{3}$/', $dod)) {
$person1->setDOD($dod . "-01-01", 9);
} elseif (preg_match('/\d/', $dod) && strtotime($dod) !== false) {
$person1->setDOD($dod);
}
// Month and year without day handling
for ($months = 0; $months < 11; $months++) {
$month = $loopDate->modify('-1 months')->format('F');
if (preg_match('/^' . $month . '\s*[12]\d{3}$/i', $dod) || preg_match('/^[12]\d{3}\s*' . $month . '$/i', $dod) ){
$person1->setDOD($dod, 10);
}
}
}
}
//end DOD
//DOB
$dob = trim(strip_tags($_POST['dob']));
if (preg_match('/^[12]\d{3}$/', $dob)){
$person1->setDOB($dob."-01-01", 9);
}
elseif(strtotime($dob) != false){
$person1->setDOB($dob);
}
//reduce accuracy if only month + year:
for ($months=0; $months < 11; $months++) {
$month = $loopDate->modify( '-1 months' )->format( 'F' );
if (preg_match('/^'.$month.'\s*[12]\d{3}$/i' , $dob) || preg_match('/^[12]\d{3}\s*'.$month.'$/i' , $dob) ){
$person1->setDOB($dob, 10);
}
}
//if age + referenced source
if ($person1->getDOD() == null and $person1->getAge() != null and $reference1->getPubDate() != null){
$pubdate = clone $reference1->getPubDate();
$person1->setDOB($pubdate->modify( "-".$person1->getAge()." years")->format("Y-m-d"), "APROX");
}
//end DOB
//construct QuickStatement
if (!$person1->getQID()){
$qs .=
"CREATE
LAST|Len|\"".$person1->getName()."\"
LAST|Lde|\"".$person1->getName()."\"
LAST|Lfr|\"".$person1->getName()."\"
LAST|Lnl|\"".$person1->getName()."\"
LAST|Den|\"".$person1->getDescription()."\"
LAST|P31|Q5";
}
// Append properties to QuickStatements
$qs .= appendProp($person1->getQID(), $person1->getName('qs'));
$qs .= appendProp($person1->getQID(), $person1->getDOB('qs'), $reference1->getQS());
$qs .= appendProp($person1->getQID(), $person1->getDOD('qs'), $reference1->getQS());
// Custom QuickStatement
$customQS = isset($_POST['qs']) ? trim(strip_tags($_POST['qs'])) : "";
$qs .= appendProp($person1->getQID(), $customQS, $reference1->getQS());
// If described_by_source is checked, append reference as "described by source" statement
global $described_by_source;
if($described_by_source == "checked"){
$qs .= appendProp($person1->getQID(), $reference1->getDescribedAtUrlQS());
}
}
function appendProp($qid = null, $prop = null, $ref = null){
$qs = '';
if ($qid == null){
$qid = "LAST";
}
$propLines = explode("\n", $prop);
foreach ($propLines as $propLine) {
if (trim($propLine) != ''){
$qs .= "\n".$qid."|".$propLine.$ref;
}
}
return $qs;
}
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
handle_form_submission();
}
?>