Compare commits

..

4 Commits

4 changed files with 142 additions and 192 deletions

View File

@ -16,7 +16,7 @@ if ( isset($_REQUEST['complete']) && ($_REQUEST['complete'] == "0") ) {
$task->setCompleteTime(Task::NOW); $task->setCompleteTime(Task::NOW);
} }
$saved = $task->save(); $saved = $task->save();
if ( $task->isComplete() ) { if ( $task->getCompleted() ) {
$data["message"] = "Completed \"{$task->getTitle(HTMLSAFE)}\" on {$task->getCompleteTime(SHORTDATE)}"; $data["message"] = "Completed \"{$task->getTitle(HTMLSAFE)}\" on {$task->getCompleteTime(SHORTDATE)}";
} else { } else {
$data["message"] = "Task \"{$task->getTitle(HTMLSAFE)}\" set to incomplete"; $data["message"] = "Task \"{$task->getTitle(HTMLSAFE)}\" set to incomplete";

View File

@ -10,47 +10,31 @@ class Task implements JsonSerializable {
private $completetime = null; private $completetime = null;
const INCOMPLETE = 1000301; const INCOMPLETE = 1000301;
const COMPLETE = 1000302;
const ALLTASKS = 1000303; const ALLTASKS = 1000303;
const OVERDUE = 1000304; const OVERDUE = 1000304;
const NOW = 1000305; 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() { public function getID() {
return intval($this->id); return intval($this->id);
} }
public function getTitle($flag = 0) { public function getTitle($flag = 0) {
switch ($flag) { return self::formatText($this->title, $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;
}
} }
public function getDescription($flag = 0) { public function getDescription($flag = 0) {
switch ($flag) { return self::formatText($this->description, $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;
}
} }
public function getPriority() { public function getPriority() {
@ -62,62 +46,35 @@ class Task implements JsonSerializable {
switch ($flag) { switch ($flag) {
case AppDB::BOOLEANDB: case AppDB::BOOLEANDB:
return ($completed) ? "true" : "false"; return ($completed) ? "true" : "false";
break;
default: default:
return $completed; 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) { public function getCreateTime($flag = 0) {
switch ($flag) { return self::formatDate($this->createtime, $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;
}
} }
public function getDueTime($flag = 0) { public function getDueTime($flag = 0) {
switch ($flag) { return self::formatDate($this->duetime, $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;
}
} }
public function getCompleteTime($flag = 0) { public function getCompleteTime($flag = 0) {
switch ($flag) { return self::formatDate($this->completetime, $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;
}
} }
public function setID($id = null) { public function setID($id = null) {
@ -129,14 +86,20 @@ class Task implements JsonSerializable {
} }
public function setTitle($title = null) { 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 ( strlen($title) > 255 ) $title = substr($title, 0, 255);
if ( $title == "" ) return false;
settype($title, "string"); settype($title, "string");
$this->title = $title; $this->title = $title;
return true; return true;
} }
public function setDescription($description = null) { 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"); settype($description, "string");
$this->description = $description; $this->description = $description;
return true; return true;
@ -144,15 +107,19 @@ class Task implements JsonSerializable {
public function setPriority($p = null) { public function setPriority($p = null) {
if (is_null($p)) return false; if (is_null($p)) return false;
$p = abs(intval($p)); $p = intval($p);
if ($p == 0) return false; if ( ($p <= 1) || ($p > 10) ) return false;
$this->priority = $p; $this->priority = $p;
return true; return true;
} }
public function setDueTime($time = null) { public function setDueTime($time = null) {
if ( is_null($time) ) return false; if ( is_null($time) ) return false;
try {
$duedate = new DateTime($time); $duedate = new DateTime($time);
} catch ( Exception $e ) {
return false;
}
if ( $time !== $duedate->format("Y-m-d H:i:s") ) return false; if ( $time !== $duedate->format("Y-m-d H:i:s") ) return false;
$this->duetime = $time; $this->duetime = $time;
return true; return true;
@ -167,27 +134,19 @@ class Task implements JsonSerializable {
$this->completetime = null; $this->completetime = null;
return true; return true;
} else { } else {
try {
$completetime = new DateTime($time); $completetime = new DateTime($time);
} catch ( Exception $e ) {
return false;
}
if ( $time !== $completetime->format("Y-m-d H:i:s") ) return false; if ( $time !== $completetime->format("Y-m-d H:i:s") ) return false;
$this->completetime = $time; $this->completetime = $time;
return true; 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() { public function isOverdue() {
if ( is_null($this->duetime) ) return false;
$duedate = new DateTime($this->getDueTime()); $duedate = new DateTime($this->getDueTime());
$now = new DateTime(); $now = new DateTime();
if ( $now >= $duedate ) { if ( $now >= $duedate ) {
@ -214,105 +173,81 @@ class Task implements JsonSerializable {
'description' => $this->getDescription(), 'description' => $this->getDescription(),
'description_safe' => $this->getDescription(HTMLSAFE), 'description_safe' => $this->getDescription(HTMLSAFE),
'priority' => $this->getPriority(), 'priority' => $this->getPriority(),
'completed' => $this->isComplete(), 'completed' => $this->getCompleted(),
'overdue' => $this->isOverdue(), 'overdue' => $this->isOverdue(),
'daysleft' => $this->getDaysLeft(), 'daysleft' => $this->getDaysLeft(),
'createtime' => $this->getCreateTime(), 'createtime' => $this->getCreateTime(),
'createdate' => $this->getCreateTime(SHORTDATE), 'createdate' => $this->getCreateTime(SHORTDATE),
'duetime' => $this->getDueTime(), 'duetime' => $this->getDueTime(),
'duedate' => $this->getDueTime(SHORTDATE), 'duedate' => $this->getDueTime(SHORTDATE),
'completed' => $this->getCompleted(),
'completetime' => $this->getCompleteTime(), 'completetime' => $this->getCompleteTime(),
'completedate' => $this->getCompleteTime(SHORTDATE) 'completedate' => $this->getCompleteTime(SHORTDATE)
]; ];
} }
public static function getStats() { private static function countByWeek(string $column, string $since, array $index): array {
$time = new DateTime(); $counts = array_fill(0, 6, 0);
$index = 5; $sth = AppDB::getSTH("SELECT YEARWEEK($column, 3) AS w, COUNT(*) AS c FROM "
$curweeks = array("", "", "", "", "", ""); . AppDB::TABLE_TASKS . " WHERE $column >= :since GROUP BY w");
while ( $index >= 0 ) { if ($sth === false) return $counts;
$curweeks[$index] = $time->format("YW"); $sth->execute([':since' => $since]);
$time->modify("-7 day");
$index--;
}
$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)) { while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$index = 5; $i = $index[(string) $row['w']] ?? null;
while ( $index >= 0 ) { if ($i !== null) $counts[$i] = (int) $row['c'];
if ( $row['w'] == $curweeks[$index] ) $data["completedtasks"][$index] = intval($row["count"]);
$index--;
} }
return $counts;
} }
$query = "SELECT COUNT(id) AS count, YEARWEEK(createtime) AS w, ";
$query .= "YEARWEEK(NOW()) AS cur FROM " . AppDB::TABLE_TASKS . " "; public static function getStats(): array {
$query .= "WHERE createtime IS NOT NULL and YEARWEEK(createtime) IN ("; $week = new DateTime('monday this week');
$query .= "YEARWEEK(NOW()), "; $week->setTime(0, 0)->modify('-5 weeks');
$query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)), "; $since = $week->format('Y-m-d H:i:s');
$query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 2 WEEK)), "; $weeks = [];
$query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 3 WEEK)), "; for ($i = 0; $i < 6; $i++) {
$query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 4 WEEK)), "; $weeks[] = $week->format('oW'); // ISO year + ISO week, matches YEARWEEK(x, 3)
$query .= "YEARWEEK(DATE_SUB(NOW(), INTERVAL 5 WEEK))"; $week->modify('+1 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--;
} }
} $index = array_flip($weeks); // index 0 = oldest, 5 = current (same as before)
return $data; return [
'weeks' => $weeks,
'createdtasks' => self::countByWeek('createtime', $since, $index),
'completedtasks' => self::countByWeek('completetime', $since, $index),
];
} }
public static function findTasks($search = "") { public static function findTasks($search = "") {
$thelist = []; if ( strlen($search) < 3 ) return [];
if ( strlen($search) < 3 ) return $thelist; $search = '%' . addcslashes($search, '%_\\') . '%';
$query = "SELECT id FROM " . AppDB::TABLE_TASKS . " "; $query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " " .
$query .= "WHERE LOWER(title) LIKE CONCAT('%',:search1,'%') "; "WHERE title LIKE :search1 OR description LIKE :search2 " .
$query .= "OR LOWER(description) LIKE CONCAT('%',:search2,'%') "; "ORDER BY title";
$query .= "ORDER BY title";
$sth = AppDB::getSTH($query); $sth = AppDB::getSTH($query);
if ( $sth === false ) return $thelist; if ( $sth === false ) return [];
$sth->bindValue(":search1", $search, PDO::PARAM_STR); $sth->bindValue(":search1", $search, PDO::PARAM_STR);
$sth->bindValue(":search2", $search, PDO::PARAM_STR); $sth->bindValue(":search2", $search, PDO::PARAM_STR);
$sth->execute(); $sth->execute();
$thelist = [];
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$thelist[] = new Task($row[':id']); $task = new self();
$task->hydrate($row);
$thelist[] = $task;
} }
return $thelist; return $thelist;
} }
public static function getList($flag = Task::INCOMPLETE) { public static function getList($flag = self::INCOMPLETE): array {
$query = "SELECT id FROM " . AppDB::TABLE_TASKS . " "; $query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " ";
if ( $flag == Task::INCOMPLETE ) { $query .= ($flag === self::INCOMPLETE)
$query .= "WHERE completetime IS NULL ORDER BY priority ASC, duetime ASC"; ? "WHERE completetime IS NULL ORDER BY priority ASC, duetime ASC"
} else { : "ORDER BY createtime ASC";
$query .= "ORDER BY createtime ASC";
}
$sth = AppDB::getSTH($query); $sth = AppDB::getSTH($query);
if ( $sth === false ) return $thelist; if ( $sth === false ) return [];
$sth->execute(); $sth->execute();
$thelist = []; $thelist = [];
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$thelist[] = new Task($row['id']); $task = new self();
$task->hydrate($row);
$thelist[] = $task;
} }
return $thelist; return $thelist;
} }
@ -321,10 +256,11 @@ class Task implements JsonSerializable {
$query = "DELETE FROM " . AppDB::TABLE_TASKS . " WHERE id=:id"; $query = "DELETE FROM " . AppDB::TABLE_TASKS . " WHERE id=:id";
try { try {
$sth = AppDB::getSTH($query); $sth = AppDB::getSTH($query);
$sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT);
if ( $sth === false ) return false; if ( $sth === false ) return false;
$sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT);
$sth->execute(); $sth->execute();
} catch(PDOException $e) { } catch(PDOException $e) {
error_log((string) $e);
return false; return false;
} }
return true; return true;
@ -333,46 +269,60 @@ class Task implements JsonSerializable {
public function save() { public function save() {
if ( $this->getTitle() == "" ) return false; if ( $this->getTitle() == "" ) return false;
if ( is_null($this->getDueTime()) ) return false; if ( is_null($this->getDueTime()) ) return false;
$query = "INSERT INTO " . AppDB::TABLE_TASKS . " "; if ($this->id === 0) {
$query .= "(id, title, description, priority, createtime, duetime, completetime) "; $newcreatetime = date('Y-m-d H:i:s');
$query .= "VALUES(:id, :title, :description, :priority, NOW(), :duetime, :completetime) "; $query = "INSERT INTO " . AppDB::TABLE_TASKS . " (title, description, priority, createtime, duetime, completetime) " .
$query .= "ON DUPLICATE KEY UPDATE "; "VALUES (:title, :description, :priority, :createtime, :duetime, :completetime)";
$query .= "title=:title, description=:description, priority=:priority, duetime=:duetime, completetime=:completetime"; } else {
$query = "UPDATE " . AppDB::TABLE_TASKS . " SET title=:title, description=:description, priority=:priority, ".
"duetime=:duetime, completetime=:completetime WHERE id=:id";
}
try { try {
$sth = AppDB::getSTH($query); $sth = AppDB::getSTH($query);
if ( $sth === false ) return $thelist; 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(":id", (int) $this->getID(), PDO::PARAM_INT);
}
$sth->bindValue(":title", $this->getTitle(), PDO::PARAM_STR); $sth->bindValue(":title", $this->getTitle(), PDO::PARAM_STR);
$sth->bindValue(":description", $this->getDescription(), PDO::PARAM_STR); $sth->bindValue(":description", $this->getDescription(), PDO::PARAM_STR);
$sth->bindValue(":priority", (int) $this->getPriority(), PDO::PARAM_INT); $sth->bindValue(":priority", (int) $this->getPriority(), PDO::PARAM_INT);
$sth->bindValue(":duetime", $this->getDueTime(), PDO::PARAM_STR); $sth->bindValue(":duetime", $this->getDueTime(), PDO::PARAM_STR);
$sth->bindValue(":completetime", $this->getCompleteTime(), PDO::PARAM_STR); $sth->bindValue(":completetime", $this->getCompleteTime(), PDO::PARAM_STR);
$sth->execute(); $executed = $sth->execute();
if ( !$executed ) return false;
} catch(PDOException $e) { } catch(PDOException $e) {
error_log((string) $e);
return false; return false;
} }
if ( $this->getID() == 0 ) $this->setID(AppDB::getLastInsertId()); if ( $this->getID() == 0 ) {
$this->setCreateTime($newcreatetime);
$this->setID(AppDB::getLastInsertId());
}
return true; return true;
} }
function __construct($reqid = 0) { public function __construct($reqid = 0) {
$reqid = intval($reqid); $reqid = intval($reqid);
$query = "SELECT id, title, description, priority, createtime, duetime, completetime "; if ( $reqid <= 0 ) return;
$query .= "FROM " . AppDB::TABLE_TASKS . " WHERE id=:id"; $query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " WHERE id=:id";
$sth = AppDB::getSTH($query); $sth = AppDB::getSTH($query);
if ( $sth === false ) return $thelist; if ( $sth === false ) return;
$sth->bindValue(":id", $reqid, PDO::PARAM_INT); $sth->bindValue(":id", $reqid, PDO::PARAM_INT);
$sth->execute(); $sth->execute();
if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) $this->hydrate($row);
$this->setID($row['id']); }
$this->setTitle($row['title']);
$this->setDescription($row['description']); private function hydrate(array $row): void {
$this->setPriority($row['priority']); $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->createtime = $row['createtime'];
$this->setDueTime($row['duetime']); $this->duetime = $row['duetime'];
$this->completetime = $row['completetime']; $this->completetime = $row['completetime'];
} }
} }
}
// vim: sw=4 ts=4 ai: // vim: et sw=4 ts=4 ai:

View File

@ -23,15 +23,15 @@ DROP TABLE IF EXISTS `tasks`;
/*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tasks` ( CREATE TABLE `tasks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `id` int unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '', `title` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL DEFAULT '', `description` text NOT NULL DEFAULT (''),
`priority` tinyint(3) unsigned NOT NULL DEFAULT 5, `priority` tinyint unsigned NOT NULL DEFAULT 5,
`createtime` datetime NOT NULL, `createtime` datetime NOT NULL,
`duetime` datetime NOT NULL, `duetime` datetime NOT NULL,
`completetime` datetime DEFAULT NULL, `completetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
-- --

View File

@ -122,7 +122,7 @@ function setTaskCompleteState(complete = TASKCOMPLETE) {
return; return;
} }
if ( !data.task ) { if ( !data.task ) {
toastr.error("No task returned", "API Error); toastr.error("No task returned", "API Error");
return; return;
} }
toastr.success(data.message, (data.task.completed) ? "Task Complete" : "Task Incomplete"); toastr.success(data.message, (data.task.completed) ? "Task Complete" : "Task Incomplete");