Skip to content

Commit

Permalink
fix: main logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MIU-cz committed Sep 18, 2023
1 parent 3ffdb3a commit 039e6c5
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 134 deletions.
139 changes: 6 additions & 133 deletions Kalendar/index.php
Original file line number Diff line number Diff line change
@@ -1,64 +1,6 @@
<?php
require('src/components/const.php');

$cur_day = date('j');
$cur_month = date('n');
$cur_year = date('Y');
// $cur_day_week = date('w');
// $cur_month_str = date('F');

if (isset($_GET['month'])) {
$new_month = $_GET['month'];
if ($new_month > 12) {
$new_month = 1;
} elseif ($new_month <= 0) {
$new_month = 12;
}
} else {
$new_month = $cur_month;
}

if (isset($_GET['day'])) {
$select_day = $_GET['day'];
} else {
$select_day = $cur_day;
}

$cur_month_str = $months[$new_month - 1];
$col_days = cal_days_in_month(CAL_GREGORIAN, $new_month, $cur_year);
$cur_firstday_month = date('w', mktime(0, 0, 0, $new_month, 1, $cur_year));

if ($cur_firstday_month == 0) {
$cur_firstday_month = 7;
}

// $select_date = date('d.m.Y', mktime(0, 0, 0, $select_day, $new_month, $cur_year));
// echo $select_date . '<br>';
// echo $select_day;
// echo $new_month;

$db_link = new mysqli($db['host'], $db['name'], $db['pass'], $db['base']);
$tasks = $db_link->query("SHOW TABLES LIKE 'tasks'");
if ($tasks->num_rows > 0) {
$query = "SELECT * FROM `tasks` WHERE `datum`='$cur_year.$new_month.$select_day'";
} else {
$query = "CREATE TABLE `tasks` (
id INT(10) NOT NULL AUTO_INCREMENT,
txt VARCHAR(64) NOT NULL,
datum DATE,
PRIMARY KEY (id)
)";
}

if (isset($_POST['ukol'])) {
$query_add_task = 'INSERT INTO tasks (txt, datum) VALUES ("' . $_POST['ukol'] . '", "' . $_POST['date'] . '")';
$db_link->query($query_add_task);
}

if (isset($_GET['cur-task']) && $_GET['cur-task'] == 'del') {
$query_del_task = 'DELETE FROM `tasks` WHERE `tasks`.`id` = ' . $_GET['task-id'] . '';
$db_link->query($query_del_task);
}
require('src/components/cal_var.php');

?>

Expand All @@ -73,80 +15,11 @@
</head>

<body>
<main class="container">
<div class="kal_container">
<table>
<caption>
<?php
echo '<a class="btn_cal" href="?month=' . -1 + $new_month . '">' . $kal_btn['prev'] . '</a>';
echo '<a class="btn_cal" href="?month=' . $cur_month . '">' . $kal_btn['cur_month'] . '</a>';
echo '<a class="btn_cal" href="?month=' . 1 + $new_month . '">' . $kal_btn['next'] . '</a>';
echo $cur_month_str;
?>
</caption>
<thead>
<tr>
<?php
foreach ($weekday as $day) {
echo '
<th>' . $day . '</th>
';
}
?>
</tr>
</thead>

<tbody>
<?php
$d = 1;
while ($d <= $col_days) {
echo '<tr>';
for ($w = 1; $w <= 7; $w++) {
if ($d == 1 && $w < $cur_firstday_month || $d > $col_days) {
echo '<td></td>';
} else {
if ($d == $cur_day && $new_month == $cur_month) {
echo '<td class="cur_day">';
} else {
echo '<td>';
}
echo '<a href="?month=' . $new_month . '&day=' . $d . '">' . $d . '</a>
</td>
';
$d++;
}
}
echo '</tr>';
}
?>
</tbody>
</table>
</div>

<div class="task_container">
<ul class="task_list">
<?php
$tasks = $db_link->query($query);
if ($tasks->num_rows > 0) {
while ($task = $tasks->fetch_assoc()) {
echo '<li class="task_item">
<div class="item_container">
<p>' . $task['txt'] . '</p>
<span>' . $task['datum'] . '</span>
</div>
<a class="btn_del" href="?cur-task=del&task-id=' . $task['id'] . '">del</a>
</li>';
}
} else {
echo '<p>Pro vybraný termín nejsou žádné úkoly, můžete jít na čaj)</p>';
}
?>
</ul>
<form method="post">
<input type="text" name="ukol" placeholder="zadat úkol" required>
<input type="date" name="date" value="<?php echo date("Y-m-d") ?>" required>
<button type="submit">zadat úkol</button>
</form>
<main class="container" id="mainContainer">
<div class="kal_content">
<?php
include('src/components/Kalendar.php');
?>
</div>
</main>

Expand Down
120 changes: 120 additions & 0 deletions Kalendar/src/components/Kalendar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

$db_link = new mysqli($db['host'], $db['name'], $db['pass'], $db['base']);
$tasks = $db_link->query("SHOW TABLES LIKE 'tasks'");
if ($tasks->num_rows > 0) {
if (isset($_GET['day'])) {
$query = "SELECT * FROM `tasks` WHERE `datum`='$new_year.$new_month.$select_day'";
} elseif (isset($_GET['month'])) {
$query = "SELECT * FROM `tasks` WHERE MONTH(`datum`) = '$new_month' AND YEAR(`datum`) = '$new_year'";
} else {
$query = "SELECT * FROM `tasks` WHERE YEAR(`datum`) = '$new_year'";
}
} else {
$query = "CREATE TABLE `tasks` (
id INT(10) NOT NULL AUTO_INCREMENT,
txt VARCHAR(64) NOT NULL,
datum DATE,
PRIMARY KEY (id)
)";
}

?>
<div class="kal_container">
<table id="kalendar" data="<?php echo $season ?>">
<caption class="kal_title">
<?php
echo '<div class="kal_navi">';
echo '<a class="btn_cal" href="?month=' . $new_month - 1 . '&year=' . $new_year . '">' . $kal_btn['prev'] . '</a>';
echo '<a class="btn_cal" href="?month=' . $cur_month . '">' . $kal_btn['cur_month'] . '</a>';
echo '<a class="btn_cal" href="?month=' . $new_month + 1 . '&year=' . $new_year . '">' . $kal_btn['next'] . '</a>';
echo '</div>';
echo '<span>' . $cur_month_str . '</span><span>' . $new_year . '</span>';
?>
</caption>
<thead>
<tr class="row_header">
<?php
foreach ($weekday as $day) {
echo '
<th>' . $day . '</th>
';
}
?>
</tr>
</thead>

<tbody>
<?php
$d = 1;
while ($d <= $col_days) {
echo '<tr class="row_kal">';
for ($w = 1; $w <= 7; $w++) {
if ($d == 1 && $w < $cur_firstday_month || $d > $col_days) {
echo '<td></td>';
} else {
if ($d == $cur_day && $new_month == $cur_month) {
echo '<td class="cur_day">';
} else {
echo '<td>';
}
echo '<a href="?year=' . $new_year . '&month=' . $new_month . '&day=' . $d . '">' . $d . '</a>
</td>
';
$d++;
}
}
echo '</tr>';
}
?>
</tbody>
</table>
</div>

<div class="task_container">
<ul class="task_list">
<?php
if (isset($_GET['day'])) {
$url = '
&year=' . $new_year . '
&month=' . $new_month . '
&day=' . $select_day;
} else {
$url = '
&year=' . $new_year . '
&month=' . $new_month;
}

print_r($_GET);

$tasks = $db_link->query($query);
if ($tasks->num_rows > 0) {
while ($task = $tasks->fetch_assoc()) {
echo '<li class="task_item">
<div class="item_container">
<p>' . $task['txt'] . '</p>
<span>' . $task['datum'] . '</span>
</div>
<a class="btn_del" href="src/components/engine.php?cur-task=del&task-id=' . $task['id'] . $url . '">'
. $kal_btn['del'] . '</a>
</li>';
}
} else {
echo '<p>Pro vybraný termín nejsou žádné úkoly, můžete jít na čaj 🍵</p>';
}
$db_link->close();

?>
</ul>

<form method="post" action="src/components/engine.php">
<?php
$month = date("m", mktime(0, 0, 0, $new_month, 1));
$day = date("d", mktime(0, 0, 0, $month, $select_day));
?>

<input type="text" name="ukol" placeholder="zadat úkol" required>
<input type="date" name="date" value="<?php echo date("$new_year-$month-$day") ?>" required>
<button type="submit">zadat úkol</button>
</form>
</div>
38 changes: 38 additions & 0 deletions Kalendar/src/components/cal_var.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

if (isset($_GET['year'])) {
$new_year = $_GET['year'];
} else {
$new_year = $cur_year;
}

if (isset($_GET['month'])) {
$new_month = $_GET['month'];

if ($new_month > 12) {
$new_month = 1;
$new_year += 1;
} elseif ($new_month <= 0) {
$new_month = 12;
$new_year -= 1;
}
} else {
$new_month = $cur_month;
}

if (isset($_GET['day'])) {
$select_day = $_GET['day'];
} else {
$select_day = $cur_day;
}

$cur_month_str = $months[$new_month - 1];
$col_days = cal_days_in_month(CAL_GREGORIAN, $new_month, $new_year);
$cur_firstday_month = date('w', mktime(0, 0, 0, $new_month, 1, $new_year));

if ($cur_firstday_month == 0) {
$cur_firstday_month = 7;
}

$season_index = (($new_month == 12) ? 3 : ($new_month <= 2)) ? 3 : floor(($new_month - 2) / 3);
$season = $seasons[$season_index];
12 changes: 11 additions & 1 deletion Kalendar/src/components/const.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'base' => 'agenda'
];

$season = [
$seasons = [
'jaro',
'leto',
'podzim',
Expand Down Expand Up @@ -51,5 +51,15 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-calendar-check" viewBox="0 0 16 16">
<path d="M10.854 7.146a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 1 1 .708-.708L7.5 9.793l2.646-2.647a.5.5 0 0 1 .708 0z"/>
<path d="M3.5 0a.5.5 0 0 1 .5.5V1h8V.5a.5.5 0 0 1 1 0V1h1a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2h1V.5a.5.5 0 0 1 .5-.5zM1 4v10a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V4H1z"/>
</svg>',
'del' => '
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x-circle-fill" viewBox="0 0 16 16">
<path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z"/>
</svg>'
];

$cur_day = date('j');
$cur_month = date('n');
$cur_year = date('Y');
// $cur_day_week = date('w');
// $cur_month_str = date('F');
42 changes: 42 additions & 0 deletions Kalendar/src/components/engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
require('const.php');
require('cal_var.php');

$db_link = new mysqli($db['host'], $db['name'], $db['pass'], $db['base']);

if (isset($_POST['ukol'])) {
$ukol = mysqli_real_escape_string($db_link, $_POST['ukol']);
$query_add_task = 'INSERT INTO tasks (txt, datum) VALUES ("' . $ukol . '", "' . $_POST['date'] . '")';
$db_link->query($query_add_task);
unset($_POST['ukol']);

$month = date('n', strtotime($_POST['date']));
$day = date('j', strtotime($_POST['date']));
$year = date('Y', strtotime($_POST['date']));
header('Location: /Kalendar?year=' . $year . '&month=' . $month . '&day=' . $day . '');
exit;
}

if (isset($_GET['cur-task']) && $_GET['cur-task'] == 'del') {
$query_del_task = 'DELETE FROM `tasks` WHERE `tasks`.`id` = ' . $_GET['task-id'] . '';
$db_link->query($query_del_task);
unset($_GET['cur-task']);

if (isset($_GET['day'])) {
$url = 'year=' . $new_year . '&month=' . $new_month . '&day=' . $select_day;
} else {
$url = 'year=' . $new_year . '&month=' . $new_month;
unset($_GET['day']);
}

header('Location: /Kalendar?' . $url . '');
exit;
}

$db_link->close();

// lost variables ???
// echo $select_day . $new_month . '<br>';
// echo $day . $month . '<br>';
// print_r($_GET) . '<br>';
// print_r($_POST) . '<br>';
3 changes: 3 additions & 0 deletions Kalendar/src/img/ico/x-circle-fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 039e6c5

Please sign in to comment.