Skip to content

Instantly share code, notes, and snippets.

@cladjidane
Created November 21, 2023 11:02
Show Gist options
  • Save cladjidane/0d1697449af8dc20510c99269776e040 to your computer and use it in GitHub Desktop.
Save cladjidane/0d1697449af8dc20510c99269776e040 to your computer and use it in GitHub Desktop.
<?php
function checkTaskDeadline($task) {
$currentDate = new DateTime();
$taskDate = DateTime::createFromFormat('Y-m-d', $task['date']);
$dateFormatted = $taskDate->format('d-m-Y');
if ($taskDate < $currentDate) {
$daysPassed = $currentDate->diff($taskDate)->format('%a');
$daysLimits = ['1' => 1, '2' => 3, '3' => 7];
if ($daysPassed > $daysLimits[$task['priority']]) {
return "<span><span class=\"late\"></span>$dateFormatted ($daysPassed jour" . ($daysPassed > 1 ? 's' : '') . ")</span>";
}
}
return "<span><span class=\"on-time\"></span>$dateFormatted</span>";
}
function controllerTask(){
$notice = '';
$request_mode = $_REQUEST['mode'] ?? null;
if($request_mode) {
switch ($request_mode) {
case 'add':
$notice = addTask($_REQUEST);
break;
case 'update':
if($_REQUEST['id']) $notice = updateTask($_REQUEST['id']);
break;
case 'delete':
if($_REQUEST['id']) $notice = deleteTask($_REQUEST['id']);
break;
}
if($notice == '') $notice = 'Un problème est survenu';
}
return $notice;
}
function addTask($data) {
$taskName = !empty($data['field-task']) ? $data['field-task'] : ($data['select-task'] ?? '');
$taskPriority = $data['priority'];
$taskDate = $data['date'];
if (empty($taskName)) {
redirect_to('Veuillez saisir une tâche');
}
$taskId = uniqid();
$_SESSION['tasks'][$taskId] = ['task' => $taskName, 'id' => $taskId, 'status' => 'wip', 'priority' => $taskPriority, 'date' => $taskDate];
redirect_to('La tâche a bien été ajoutée');
}
function deleteTask($taskId) {
unset($_SESSION['tasks'][$taskId]);
redirect_to('La tâche a bien été supprimée');
}
function updateTask($taskId) {
$_SESSION['tasks'][$taskId]['status'] = $_SESSION['tasks'][$taskId]['status'] === 'wip' ? 'finish' : 'wip';
redirect_to('La tâche a bien été modifiée');
}
function redirect_to($message){
header('Location: /?notice='.$message);
exit;
}
@cladjidane
Copy link
Author

cladjidane commented Nov 21, 2023

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();
//$_SESSION['tasks'] = [];
echo '<pre>';var_dump($_SESSION['tasks']);echo '</pre>';
include('functions.php');

$notice = controllerTask();

$tasks = $_SESSION['tasks'];
?>


<!DOCTYPE html>
<html lang="fr">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Todo</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <h1>Todo List</h1>
  <h2>Gestionnaire de tâches</h2>
  <h3><?php echo date("d-m-Y"); ?></h3>

  <form class="form-add" method="post" action="">

    <label for="item">Ajouter une tâche</label>
    <div class="fields">
      <input name="field-task" type="text" id="item" placeholder="Intitulé de la tâche" />
      ou
      <select name="select-task">
        <option value="">Choisir une tâche</option>
        <option value="Passer le balai">Passer le balai</option>
        <option value="Saluer le boss">Saluer le boss</option>
        <option value="Couper l'ordi">Couper l'ordi</option>
      </select>
    </div>
    <div class="fields">
    <select name="priority">
        <option value="1">Urgent</option>
        <option value="2">Moyen</option>
        <option value="3">Mineur</option>
      </select>
      -
      <input name="date" type="date" id="date" value="<?php echo date("Y-m-d"); ?>" placeholder="Date de prise en charge" />
    </div>
    <input type="hidden" name="mode" value="add" />

    <button type="submit">Ajouter</button>

  </form>

  <?php if($notice != null || isset($_GET['notice'])) : ?>
    <div class="notice"><?php echo $notice ? $notice : $_GET['notice']; ?></div>
  <?php endif; ?>

  <ul class="list-todo">
    <?php if($tasks) : ?>
    <?php
      usort($tasks, function($a, $b) {
        return $a['priority'] - $b['priority'];
      });
    ?>
    <?php foreach($tasks as $key => $task) : ?>

    <li class="<?php echo $task['status'] == "finish" ? "ok": ""; ?>">

      <a  href="?mode=update&id=<?php echo $task['id']; ?>">
        <input
          <?php echo $task['status'] == "finish" ? "checked": ""; ?>
          type="checkbox"
          name="status"
        />
      </a>

      <span class="<?php echo $task['status'] == "finish" ? "finished": "wip"; ?>">
        [<?php echo $task['priority']; ?>]
        <?php echo $task['task']; ?>
        <?php echo checkTaskDeadline($task); ?>
      </span>

      <a href="?mode=delete&id=<?php echo $task['id']; ?>" class="btdelete"></a>

    </li>

    <?php endforeach; ?>
    <?php endif; ?>
  </ul>


</body>

</html>

@cladjidane
Copy link
Author

/*
Organisation
1-Positionnement
2-Dimensions
3-Texte
4-Bordures et fonds
5-Propriétés CSS3 ou spécifiques navigateurs
*/

*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
min-height: 100vh;
font-family: Arial, Helvetica, sans-serif;
background: radial-gradient(rgb(233, 233, 233), rgb(187, 187, 187));
padding-bottom: 100px;
}

h1 {
margin: 30px 0;
text-align: center;
font-size: 30px;
}

h2 {
text-align: center;
font-size: 20px;
}

h3 {
margin-bottom: 50px;
text-align: center;
font-size: 12px;
}

.notice {
padding: 20px;
width: 70%;
max-width: 1200px;
min-width: 320px;
margin: 20px auto 0;
border: 1px solid black;
border-radius: 10px;
background: rgb(178, 233, 177);
}

.list-todo {
width: 70%;
max-width: 1200px;
min-width: 320px;
margin: 20px auto 0;
list-style-type: none;
}

.list-todo li {
display: flex;
align-items: center;
width: 100%;
margin: 5px 0;
padding: 10px;
color: #333;
font-size: 15px;
background: #f1f1f1;
border-radius: 10px;
}

.list-todo li input {
width: 20px;
height: 20px;
margin-left: 10px;
pointer-events: none;
}

.list-todo li .btdelete {
height: 20px;
width: 20px;
margin-left: auto;
padding: 0;
background: transparent;
background-image: url('./ressources/fermer.svg');
border: none;
cursor: pointer;
}

.list-todo li .btdelete:hover {
opacity: .5;
}

.list-todo li > span {
margin-left: 10px;
font-size: 15px;
display: flex;
align-items: center;
gap: 10px;
}

.list-todo li > span > span {
padding: 5px 10px; background: black;
color: white;
font-size: 10px;
border-radius: 10px;
display: flex;
align-items: center;
}

.list-todo li .on-time {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 5px;
pointer-events: none;
background-color: green;
border-radius: 10px;
}

.list-todo li .late {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 5px;
pointer-events: none;
background-color: red;
border-radius: 10px;
}

.list-todo li.ok span {
text-decoration: line-through;
}

.form-add {
display: block;
max-width: 1200px;
min-width: 320px;
width: 70%;
margin: 0 auto;
padding: 20px;
background: white;
border-radius: 10px;
}

.form-add .fields {
display: flex;
align-items: center;
gap: 20px;
}

.form-add label {
display: block;
color: black;
font-size: 15px;
font-weight: bold;
}

.form-add select {
width: 50%;
margin: 10px 0;
padding: 12px 10px;
font-size: 15px;
}

.form-add input {
width: 50%;
margin: 10px 0;
padding: 10px;
font-size: 15px;
}

.form-add button {
padding: 10px;
font-size: 15px;
}

.form-checked {
display: flex;
align-items: center;
gap: 5px;
}

.form-checked button {
border: none;
background: silver;
font-size: 10px;
padding: 4px 3px;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment