forked from htmlacademy-php1/2074903-doingsdone-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyfunction.php
More file actions
203 lines (192 loc) · 5.29 KB
/
myfunction.php
File metadata and controls
203 lines (192 loc) · 5.29 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Determines whether the task is hot or not
*
* @param string $task The concrete task from our array $tasks
*
* @return bool $is_hot Shows true or false there are 24h or less before the task
*/
function is_hot($task)
{
if (!empty($task['dt_deadline']) and !$task['status']) {
$task_ts = strtotime($task['dt_deadline']);
$ts_diff = $task_ts - time();
$hours = floor($ts_diff / 3600);
if ($hours <= 24) {
return true;
} else {
return false;
}
}
return false;
}
/**
* In a request to the db, creates an array of the response or returns an error
*
* @param resource $result response from our database to our request
*
* @return array|null from db
*/
function array_or_error(object $result)
{
if (!$result) {
exit('Ошибка запроса');
return null;
}
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
/**
* Gives user a message if there aren't results of search
* or $project_id doesn't have request parameter or tasks
*
* @param int $project_id - our request parametr
* @param array $projects_ids - a column with projects id in our projects array
* @param array $tasks our array with tasks for $project_id
* @param string $search Our search request to find tasks
*
* @return string|null about error with code 404
*/
function check_tasks_for_project_or_search(
$project_id, $projects_ids, array $tasks, $search
) {
if (!empty($search) and empty($tasks)) {
return 'Ничего не найдено по вашему запросу';
} elseif (!empty($project_id)) {
if (!in_array($project_id, $projects_ids) or empty($tasks)) {
http_response_code(404);
return 'Ошибка 404. Страница, которую Вы ищете, не может быть найдена';
}
} else {
return null;
}
}
/**
* Check to exist sent project in our form to add tasks
*
* @param int $project_id - our id to check
* @param array $allowed_list - an id column of our exist projects
*
* @return string|null about error
*/
function validate_project($project_id, $allowed_list)
{
if (!in_array($project_id, $allowed_list)) {
return 'Указан несуществующий проект';
}
return null;
}
/**
* Check null of a name in our form
*
* @param string $name - the name of task or user
*
* @return string|null about error
*/
function validate_name($name)
{
if (!$name) {
return 'Название/имя не может быть пустым';
}
return null;
}
/**
* Check date of task deadline to send right format and to be not earlier than today
*
* @param string $date - sent date of deadline
*
* @return string|null about errors
*/
function check_date($date)
{
if (!is_date_valid($date)) {
return 'Указан неверный формат даты';
}
$date_and_time = strtotime($date) + 3600 * 24 - 1;
$deadline = date('Y-m-d H:i:s', $date_and_time);
if (strtotime($deadline) < time()) {
return 'Указана устаревшая дата';
}
return null;
}
/**
* Return sent value from our form to add tasks
*
* @param string $name - name of sent value
*
* @return string|null sent value
*/
function get_post_value($name)
{
if (!empty($name)) {
return filter_input(INPUT_POST, $name);
}
return null;
}
/**
* Check size of upload files
*
* @param string $name - our file
* @param int $max_size_limit our max size for upload files
*
* @return string|null about error
*/
function validate_filesize($name, $max_size_limit)
{
if (filesize($name) > $max_size_limit) {
return 'Размер файла превышает допустимый';
}
return null;
}
/**
* Check to exist sent user email in our form to sign up
*
* @param string $email - our email to check
* @param array $emails - an emails column of our users in database
*
* @return string|null about error
*/
function validate_email($email, $emails)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return 'Введен некорректный e-mail';
}
if (in_array($email, $emails)) {
return 'Указан уже зарегистрированный e-mail';
}
return null;
}
/**
* Check the string to have legth between our bourders
*
* @param string $value our checked string
* @param int $min min number of symbols
* @param int $max max number of symbols
*
* @return string|null about error
*/
function validate_length($value, $min, $max)
{
$length = strlen($value);
if ($length < $min or $length > $max) {
return "Некорректная длина: поле должно содержать $min - $max символов";
}
return null;
}
/**
* Check existing of added project
*
* @param array $projects Projects which current user has
* @param array $project_form New project which current user tries to create
*
* @return bool $same_name exist or not this name of project
*/
function check_name_project(array $projects, array $project_form)
{
$project_name = mb_strtolower($project_form['name']);
foreach ($projects as $project) {
if (mb_strtolower($project['name']) === $project_name) {
return true;
}
}
return false;
}