-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_site.php
163 lines (146 loc) · 4.4 KB
/
simple_site.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
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use KSQLite\KSQLite;
use KSQLite\KSQLiteQueryContext;
use KSQLite\KSQLiteParamsBinder;
// To run with PHP:
// $ php -d opcache.preload=preload.php -S localhost:8888
//
// To run with KPHP:
// $ kphp --composer-root $(pwd) ./examples/simple_site.php
// $ ./kphp_out/server --http-port 8888
//
// Then visit http://localhost:8888/examples/simple_site.php in your browser.
if (KPHP_COMPILER_VERSION) { KSQLite::loadFFI(); }
try {
$err = main();
if ($err) {
die('internal server error: ' . $err);
}
} catch (\Throwable $e) {
die('internal server error: ' . $e->getMessage());
}
function main(): string {
$db = new KSQLite();
if (!$db->open('testdb')) {
return $db->getLastError();
}
$err = create_tables($db);
if ($err) {
return $err;
}
if (array_key_exists('delete_all', $_POST)) {
$err = delete_all_tasks($db);
if ($err) {
return $err;
}
} else if (array_key_exists('task_name', $_POST)) {
$new_task = [
'task_creation_time' => time(),
'task_priority' => (float)$_POST['task_priority'],
'task_name' => (string)$_POST['task_name'],
'task_description' => (string)$_POST['task_description'],
];
$err = insert_task($db, $new_task);
if ($err) {
return $err;
}
}
$select_tasks = 'SELECT *
FROM app_tasks
ORDER BY task_creation_time DESC
LIMIT 100';
[$tasks, $ok] = $db->fetch($select_tasks);
if (!$ok) {
return $db->getLastError();
}
echo build_page_html($tasks);
return '';
}
function create_tables(KSQLite $db): string {
$create_tasks_table = 'CREATE TABLE IF NOT EXISTS app_tasks(
task_id INTEGER PRIMARY KEY,
task_creation_time INTEGER NOT NULL,
task_name TEXT NOT NULL,
task_priority REAL NOT NULL,
task_description TEXT NOT NULL
);';
if (!$db->exec($create_tasks_table)) {
return $db->getLastError();
}
return '';
}
function delete_all_tasks(KSQLite $db): string {
if (!$db->exec('DELETE FROM app_tasks')) {
return $db->getLastError();
}
return '';
}
function insert_task(KSQLite $db, $task): string {
$query = 'INSERT INTO
app_tasks(task_creation_time, task_priority, task_name, task_description)
VALUES (?1, ?2, ?3, ?4)';
$params = [
1 => $task['task_creation_time'],
2 => $task['task_priority'],
3 => $task['task_name'],
4 => $task['task_description'],
];
if (!$db->exec($query, $params)) {
return $db->getLastError();
}
return '';
}
function build_page_html(array $tasks) {
$rows_html = '';
foreach ($tasks as $i => $task) {
$num = $i + 1;
$date = date(DATE_ATOM, $task['task_creation_time']);
$rows_html .= '<tr>';
$rows_html .= "<td>$num</td>";
$rows_html .= "<td>$date</td>";
$rows_html .= "<td>$task[task_priority]</td>";
$rows_html .= "<td>$task[task_name]</td>";
$rows_html .= "<td>$task[task_description]</td>";
$rows_html .= '</tr>';
}
$css = get_site_styles();
$html = "
<!DOCTYPE HTML>
<head>
$css
</head>
<body><div class='container'>
<fieldset>
<legend>Manage tasks</legend>
<form action='simple_site.php' method='post'>
<p><label><input type='text' value='5.0' name='task_priority'/> Task priority</label></p>
<p><label><input type='text' name='task_name'/> Task name</label></p>
<p><label><input type='text' name='task_description'/> Task description</label></p>
<p><input style='width: 16em' class='btn btn-success' type='submit' value='Create task'/></p>
</form>
<form action='simple_site.php' method='post'>
<p><input type='text' value='1' name='delete_all' hidden=true/></p>
<p><input style='width: 16em' class='btn btn-danger' type='submit' value='Delete all tasks'/></p>
</form>
</fieldset>
Created tasks:<br>
<table class='table'>
<tr>
<th>Number</th>
<th>Created at</th>
<th>Priority</th>
<th>Name</th>
<th>Description</th>
</tr>
$rows_html
</table>
</div></body>
";
return $html;
}
function get_site_styles(): string {
return '
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
';
}