From df205e63936c97d8df948944560d09ee031d2d25 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 23 Nov 2026 18:22:01 -0500 Subject: [PATCH] Much bug and efficiency cleanup --- ajax/task-setcomplete.php | 2 +- class_task.php | 321 ++++++++++++++++---------------------- 2 files changed, 136 insertions(+), 187 deletions(-) diff --git a/ajax/task-setcomplete.php b/ajax/task-setcomplete.php index 18d1e12..c1ee199 100644 --- a/ajax/task-setcomplete.php +++ b/ajax/task-setcomplete.php @@ -16,7 +16,7 @@ if ( isset($_REQUEST['complete']) && ($_REQUEST['complete'] == "0") ) { $task->setCompleteTime(Task::NOW); } $saved = $task->save(); -if ( $task->isComplete() ) { +if ( $task->getCompleted() ) { $data["message"] = "Completed \"{$task->getTitle(HTMLSAFE)}\" on {$task->getCompleteTime(SHORTDATE)}"; } else { $data["message"] = "Task \"{$task->getTitle(HTMLSAFE)}\" set to incomplete"; diff --git a/class_task.php b/class_task.php index 8fe7c63..d1577b5 100644 --- a/class_task.php +++ b/class_task.php @@ -10,47 +10,31 @@ class Task implements JsonSerializable { private $completetime = null; const INCOMPLETE = 1000301; - const COMPLETE = 1000302; const ALLTASKS = 1000303; const OVERDUE = 1000304; const NOW = 1000305; + private const COLUMNS = "id, title, description, priority, createtime, duetime, completetime"; + + private static function formatText(string $v, int $flag): string { + return match ($flag) { + HTMLSAFE => htmlspecialchars($v), + HTMLFORMSAFE => htmlspecialchars($v, ENT_QUOTES), + CSVSAFE => str_replace('"', '""', $v), + default => $v, + }; + } + public function getID() { return intval($this->id); } public function getTitle($flag = 0) { - switch ($flag) { - case HTMLSAFE: - return htmlspecialchars($this->title); - break; - case HTMLFORMSAFE: - return htmlspecialchars($this->title, ENT_QUOTES); - break; - case CSVSAFE: - return str_replace('"', '""', $this->title); - break; - default: - return $this->title; - break; - } + return self::formatText($this->title, $flag); } public function getDescription($flag = 0) { - switch ($flag) { - case HTMLSAFE: - return htmlspecialchars($this->description); - break; - case HTMLFORMSAFE: - return htmlspecialchars($this->description, ENT_QUOTES); - break; - case CSVSAFE: - return str_replace('"', '""', $this->description); - break; - default: - return $this->description; - break; - } + return self::formatText($this->description, $flag); } public function getPriority() { @@ -62,62 +46,35 @@ class Task implements JsonSerializable { switch ($flag) { case AppDB::BOOLEANDB: return ($completed) ? "true" : "false"; - break; default: return $completed; - break; + } + } + + private static function formatDate(string|null $v, int $flag): string|int|null { + if ( is_null($v) ) return $v; + switch ($flag) { + case TIMESTAMP: + return strtotime($v); + case PRETTY: + return date("F j Y H:i:s", strtotime($v)); + case SHORTDATE: + return date("m/d/Y", strtotime($v)); + default: + return $v; } } public function getCreateTime($flag = 0) { - switch ($flag) { - case TIMESTAMP: - return strtotime($this->createtime); - break; - case PRETTY: - return date("F j Y H:i:s", strtotime($this->createtime)); - break; - case SHORTDATE: - return date("m/d/Y", strtotime($this->createtime)); - break; - default: - return $this->createtime; - break; - } + return self::formatDate($this->createtime, $flag); } public function getDueTime($flag = 0) { - switch ($flag) { - case TIMESTAMP: - return strtotime($this->duetime); - break; - case PRETTY: - return date("F j Y H:i:s", strtotime($this->duetime)); - break; - case SHORTDATE: - return date("m/d/Y", strtotime($this->duetime)); - break; - default: - return $this->duetime; - break; - } + return self::formatDate($this->duetime, $flag); } public function getCompleteTime($flag = 0) { - switch ($flag) { - case TIMESTAMP: - return strtotime($this->completetime); - break; - case PRETTY: - return date("F j Y H:i:s", strtotime($this->completetime)); - break; - case SHORTDATE: - return ((is_null($this->completetime)) ? null : date("m/d/Y", strtotime($this->completetime))); - break; - default: - return $this->completetime; - break; - } + return self::formatDate($this->completetime, $flag); } public function setID($id = null) { @@ -129,14 +86,19 @@ class Task implements JsonSerializable { } public function setTitle($title = null) { - if (is_null($title) || ($title == "")) return false; + if ( is_null($title) ) return false; + if ( !is_string($title) ) return false; + $title = trim($title); + if ( $title == "" ) return false; settype($title, "string"); $this->title = $title; return true; } public function setDescription($description = null) { - if (is_null($description) || ($description == "")) return false; + if ( is_null($description) ) return false; + if ( !is_string($description) ) return false; + $description = trim($description); settype($description, "string"); $this->description = $description; return true; @@ -144,15 +106,19 @@ class Task implements JsonSerializable { public function setPriority($p = null) { if (is_null($p)) return false; - $p = abs(intval($p)); - if ($p == 0) return false; + $p = intval($p); + if ( ($p <= 1) || ($p > 10) ) return false; $this->priority = $p; return true; } public function setDueTime($time = null) { if ( is_null($time) ) return false; - $duedate = new DateTime($time); + try { + $duedate = new DateTime($time); + } catch ( Exception $e ) { + return false; + } if ( $time !== $duedate->format("Y-m-d H:i:s") ) return false; $this->duetime = $time; return true; @@ -167,27 +133,19 @@ class Task implements JsonSerializable { $this->completetime = null; return true; } else { - $completetime = new DateTime($time); + try { + $completetime = new DateTime($time); + } catch ( Exception $e ) { + return false; + } if ( $time !== $completetime->format("Y-m-d H:i:s") ) return false; $this->completetime = $time; return true; } } - public function setIncomplete() { - $this->completetime = null; - return true; - } - - public function isComplete($flag = 0) { - if ( is_null($this->completetime) ) { - return false; - } else { - return true; - } - } - public function isOverdue() { + if ( is_null($this->duetime) ) return false; $duedate = new DateTime($this->getDueTime()); $now = new DateTime(); if ( $now >= $duedate ) { @@ -214,105 +172,81 @@ class Task implements JsonSerializable { 'description' => $this->getDescription(), 'description_safe' => $this->getDescription(HTMLSAFE), 'priority' => $this->getPriority(), - 'completed' => $this->isComplete(), + 'completed' => $this->getCompleted(), 'overdue' => $this->isOverdue(), 'daysleft' => $this->getDaysLeft(), 'createtime' => $this->getCreateTime(), 'createdate' => $this->getCreateTime(SHORTDATE), 'duetime' => $this->getDueTime(), 'duedate' => $this->getDueTime(SHORTDATE), - 'completed' => $this->getCompleted(), 'completetime' => $this->getCompleteTime(), 'completedate' => $this->getCompleteTime(SHORTDATE) ]; } - public static function getStats() { - $time = new DateTime(); - $index = 5; - $curweeks = array("", "", "", "", "", ""); - while ( $index >= 0 ) { - $curweeks[$index] = $time->format("YW"); - $time->modify("-7 day"); - $index--; + private static function countByWeek(string $column, string $since, array $index): array { + $counts = array_fill(0, 6, 0); + $sth = AppDB::getSTH("SELECT YEARWEEK($column, 3) AS w, COUNT(*) AS c FROM " + . AppDB::TABLE_TASKS . " WHERE $column >= :since GROUP BY w"); + if ($sth === false) return $counts; + $sth->execute([':since' => $since]); + while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { + $i = $index[(string) $row['w']] ?? null; + if ($i !== null) $counts[$i] = (int) $row['c']; } - $data["weeks"] = $curweeks; - $data["createdtasks"] = array(0, 0, 0, 0, 0, 0); - $data["completedtasks"] = array(0, 0, 0, 0, 0, 0); - $query = "SELECT COUNT(id) AS count, YEARWEEK(completetime) AS w, "; - $query .= "YEARWEEK(NOW()) AS cur FROM " . AppDB::TABLE_TASKS . " "; - $query .= "WHERE completetime IS NOT NULL and YEARWEEK(completetime) IN ("; - $query .= "YEARWEEK(NOW()), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 2 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 3 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 4 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 5 WEEK))"; - $query .= ") GROUP BY w ORDER BY w DESC LIMIT 6"; - $sth = AppDB::getSTH($query); - if ( $sth === false ) return $data; - $sth->execute(); - while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - $index = 5; - while ( $index >= 0 ) { - if ( $row['w'] == $curweeks[$index] ) $data["completedtasks"][$index] = intval($row["count"]); - $index--; - } + return $counts; + } + + public static function getStats(): array { + $week = new DateTime('monday this week'); + $week->setTime(0, 0)->modify('-5 weeks'); + $since = $week->format('Y-m-d H:i:s'); + $weeks = []; + for ($i = 0; $i < 6; $i++) { + $weeks[] = $week->format('oW'); // ISO year + ISO week, matches YEARWEEK(x, 3) + $week->modify('+1 week'); } - $query = "SELECT COUNT(id) AS count, YEARWEEK(createtime) AS w, "; - $query .= "YEARWEEK(NOW()) AS cur FROM " . AppDB::TABLE_TASKS . " "; - $query .= "WHERE createtime IS NOT NULL and YEARWEEK(createtime) IN ("; - $query .= "YEARWEEK(NOW()), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 2 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 3 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 4 WEEK)), "; - $query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 5 WEEK))"; - $query .= ") GROUP BY w ORDER BY w DESC LIMIT 6"; - $sth = AppDB::getSTH($query); - if ( $sth === false ) return $data; - $sth->execute(); - while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - $index = 5; - while ( $index >= 0 ) { - if ( $row['w'] == $curweeks[$index] ) $data["createdtasks"][$index] = intval($row["count"]); - $index--; - } - } - return $data; + $index = array_flip($weeks); // index 0 = oldest, 5 = current (same as before) + return [ + 'weeks' => $weeks, + 'createdtasks' => self::countByWeek('createtime', $since, $index), + 'completedtasks' => self::countByWeek('completetime', $since, $index), + ]; } public static function findTasks($search = "") { - $thelist = []; - if ( strlen($search) < 3 ) return $thelist; - $query = "SELECT id FROM " . AppDB::TABLE_TASKS . " "; - $query .= "WHERE LOWER(title) LIKE CONCAT('%',:search1,'%') "; - $query .= "OR LOWER(description) LIKE CONCAT('%',:search2,'%') "; - $query .= "ORDER BY title"; + if ( strlen($search) < 3 ) return []; + $search = '%' . addcslashes($search, '%_\\') . '%'; + $query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " " . + "WHERE title LIKE :search1 OR description LIKE :search2 " . + "ORDER BY title"; $sth = AppDB::getSTH($query); - if ( $sth === false ) return $thelist; + if ( $sth === false ) return []; $sth->bindValue(":search1", $search, PDO::PARAM_STR); $sth->bindValue(":search2", $search, PDO::PARAM_STR); $sth->execute(); + $thelist = []; while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - $thelist[] = new Task($row['id']); + $task = new self(); + $task->hydrate($row); + $thelist[] = $task; } return $thelist; } - public static function getList($flag = Task::INCOMPLETE) { - $query = "SELECT id FROM " . AppDB::TABLE_TASKS . " "; - if ( $flag == Task::INCOMPLETE ) { - $query .= "WHERE completetime IS NULL ORDER BY priority ASC, duetime ASC"; - } else { - $query .= "ORDER BY createtime ASC"; - } + public static function getList($flag = self::INCOMPLETE): array { + $query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " "; + $query .= ($flag === self::INCOMPLETE) + ? "WHERE completetime IS NULL ORDER BY priority ASC, duetime ASC" + : "ORDER BY createtime ASC"; $sth = AppDB::getSTH($query); - if ( $sth === false ) return $thelist; + if ( $sth === false ) return []; $sth->execute(); $thelist = []; while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - $thelist[] = new Task($row['id']); + $task = new self(); + $task->hydrate($row); + $thelist[] = $task; } return $thelist; } @@ -321,10 +255,11 @@ class Task implements JsonSerializable { $query = "DELETE FROM " . AppDB::TABLE_TASKS . " WHERE id=:id"; try { $sth = AppDB::getSTH($query); - $sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT); if ( $sth === false ) return false; + $sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT); $sth->execute(); } catch(PDOException $e) { + error_log((string) $e); return false; } return true; @@ -333,46 +268,60 @@ class Task implements JsonSerializable { public function save() { if ( $this->getTitle() == "" ) return false; if ( is_null($this->getDueTime()) ) return false; - $query = "INSERT INTO " . AppDB::TABLE_TASKS . " "; - $query .= "(id, title, description, priority, createtime, duetime, completetime) "; - $query .= "VALUES(:id, :title, :description, :priority, NOW(), :duetime, :completetime) "; - $query .= "ON DUPLICATE KEY UPDATE "; - $query .= "title=:title, description=:description, priority=:priority, duetime=:duetime, completetime=:completetime"; + if ($this->id === 0) { + $newcreatetime = date('Y-m-d H:i:s'); + $query = "INSERT INTO " . AppDB::TABLE_TASKS . " (title, description, priority, createtime, duetime, completetime) " . + "VALUES (:title, :description, :priority, :createtime, :duetime, :completetime)"; + } else { + $query = "UPDATE " . AppDB::TABLE_TASKS . " SET title=:title, description=:description, priority=:priority, ". + "duetime=:duetime, completetime=:completetime WHERE id=:id"; + } try { $sth = AppDB::getSTH($query); - if ( $sth === false ) return $thelist; - $sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT); + if ( $sth === false ) return false; + if ( $this->id === 0 ) { + $sth->bindValue(":createtime", $newcreatetime, PDO::PARAM_STR); + } else { + $sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT); + } $sth->bindValue(":title", $this->getTitle(), PDO::PARAM_STR); $sth->bindValue(":description", $this->getDescription(), PDO::PARAM_STR); $sth->bindValue(":priority", (int) $this->getPriority(), PDO::PARAM_INT); $sth->bindValue(":duetime", $this->getDueTime(), PDO::PARAM_STR); $sth->bindValue(":completetime", $this->getCompleteTime(), PDO::PARAM_STR); - $sth->execute(); + $executed = $sth->execute(); + if ( !$executed ) return false; } catch(PDOException $e) { + error_log((string) $e); return false; } - if ( $this->getID() == 0 ) $this->setID(AppDB::getLastInsertId()); + if ( $this->getID() == 0 ) { + $this->setCreateTime($newcreatetime); + $this->setID(AppDB::getLastInsertId()); + } return true; } - function __construct($reqid = 0) { + public function __construct($reqid = 0) { $reqid = intval($reqid); - $query = "SELECT id, title, description, priority, createtime, duetime, completetime "; - $query .= "FROM " . AppDB::TABLE_TASKS . " WHERE id=:id"; + if ( $reqid <= 0 ) return; + $query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " WHERE id=:id"; $sth = AppDB::getSTH($query); - if ( $sth === false ) return $thelist; + if ( $sth === false ) return; $sth->bindValue(":id", $reqid, PDO::PARAM_INT); $sth->execute(); - if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - $this->setID($row['id']); - $this->setTitle($row['title']); - $this->setDescription($row['description']); - $this->setPriority($row['priority']); - $this->createtime = $row['createtime']; - $this->setDueTime($row['duetime']); - $this->completetime = $row['completetime']; - } + if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) $this->hydrate($row); + } + + private function hydrate(array $row): void { + $this->id = (int) $row['id']; + $this->title = (string) $row['title']; + $this->description = (string) $row['description']; + $this->priority = (int) $row['priority']; + $this->createtime = $row['createtime']; + $this->duetime = $row['duetime']; + $this->completetime = $row['completetime']; } } -// vim: sw=4 ts=4 ai: +// vim: et sw=4 ts=4 ai: