-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
156 lines (145 loc) · 5.69 KB
/
index.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
<?php
session_start();
ini_set('display_errors', '0');
error_reporting(E_ALL);
include 'helpers.php';
include 'conndb.php';
$errors = [];
$projectId = null;
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8");
function countTasksForCategory($conn, $categoryId)
{
$sql = 'SELECT count(*) as count FROM tasks WHERE project_id =' . $categoryId;
$result = mysqli_query($conn, $sql)->fetch_assoc();
return $result['count'];
}
if (isset($_SESSION['user']) && !empty($_SESSION['user'])) {
$user_id = $_SESSION['user']['id'];
$categories = [];
$tasks = [];
$where = '';
$show_complete_tasks = 0;
if (isset($_GET['filter'])) {
if ($_GET['filter'] == 'tomorrow') {
$date = new DateTime();
$date->modify('+1 day');
$dateTomorrow = $date->format('Y-m-d');
$date_start = $dateTomorrow . ' 00:00:00';
$date_end = $dateTomorrow . ' 23:59:00';
$where = " AND date_term >= '$date_start' AND date_term <= '$date_end'";
}
if ($_GET['filter'] == 'today') {
$date = new DateTime();
$dateToday = $date->format('Y-m-d');
$date_start = $dateToday . ' 00:00:00';
$date_end = $dateToday . ' 23:59:00';
$where = " AND date_term >= '$date_start' AND date_term <= '$date_end'";
}
if ($_GET['filter'] == 'yesterday') {
$date = new DateTime();
$date->modify('-1 day');
$dateYesterday = $date->format('Y-m-d');
$date_end = $dateYesterday . ' 23:59:00';
$where = "AND date_term <= '$date_end' ";
}
}
if (isset($_GET['show_completed'])) {
$show_complete_tasks = intval($_GET['show_completed']);
}
if ($show_complete_tasks == 0) {
$where .= " AND (status is null or status !=1) ";
}
$bodyBackground = true;
if (isset($_GET['check']) and isset($_GET['task_id'])) {
$status = intval($_GET['check']);
$tasks_id = intval($_GET['task_id']);
mysqli_query($conn, "UPDATE `tasks` SET status = $status WHERE id = '$tasks_id'");
}
$sqlProject = "SELECT * FROM `projects` where user_id = '$user_id'";
$result = mysqli_query($conn, $sqlProject);
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($rows as $row) {
$count = countTasksForCategory($conn, $row['id']);
$categories[] = [
'name' => $row['name'],
'project_id' => $row['id'],
'count' => $count,
];
}
if (!empty($_GET['search'])) {
$trim = trim($_GET['search']);
$searchSql = mysqli_query($conn,
"SELECT * FROM `tasks` WHERE MATCH(name) AGAINST('$trim') and user_id = '$user_id' '$where' ");
if (mysqli_num_rows($searchSql) > 0) {
$arSearchRows = mysqli_fetch_all($searchSql, MYSQLI_ASSOC);
foreach ($arSearchRows as $row) {
if (!empty($row['name']) && !empty($row['project_id'])) {
if (!empty($row['file'])) {
$arFile = explode('/', $row['file']);
$fileName = $arFile[count($arFile) - 1];
}
$tasks[] = [
'task' => $row['name'],
'date_of_completion' => $row['date_term'],
'category' => $row['project_id'],
'completed' => $row['status'] == true,
'file' => $row['file'],
'fileName' => $fileName ?? '',
'name' => $row['name'],
];
}
}
} else {
$errors['search'] = "Ничего не найдено по вашему запросу";
}
} else {
$foundMatches = false;
$resultSQL = "SELECT * FROM `tasks` WHERE user_id = '$user_id' $where ";
if (!empty($_GET['project_id'])) {
$projectId = intval($_GET['project_id']);
$resultSQL = $resultSQL . 'and project_id = ' . $projectId;
foreach ($categories as $key => $value) {
if ($projectId === intval($value["project_id"])) {
$foundMatches = true;
}
}
if (!$foundMatches) {
header('Location:404.php');
}
}
$result2 = mysqli_query($conn, $resultSQL);
$rows2 = mysqli_fetch_all($result2, MYSQLI_ASSOC);
foreach ($rows2 as $row) {
if (!empty($row['name']) && !empty($row['project_id'])) {
if (!empty($row['file'])) {
$arFile = explode('/', $row['file']);
$fileName = $arFile[count($arFile) - 1];
}
$tasks[] = [
'task' => $row['name'],
'date_of_completion' => $row['date_term'],
'category' => $row['project_id'],
'completed' => $row['status'] == true,
'file' => $row['file'],
'fileName' => $fileName ?? '',
'name' => $row['name'],
'id' => $row['id'],
];
}
}
}
$mainContent = include_template('main.php', [
'categories' => $categories,
'tasks' => $tasks,
'show_complete_tasks' => $show_complete_tasks,
'projectId' => $projectId,
'errors' => $errors,
]);
} else {
$mainContent = include_template('guest.php', []);
}
echo include_template('layout.php', ['title' => 'Дела в порядке', 'content' => $mainContent]);