329 lines
12 KiB
PHP
329 lines
12 KiB
PHP
<?php
|
|
|
|
class Task implements JsonSerializable {
|
|
private $id = 0;
|
|
private $title = "";
|
|
private $description = "";
|
|
private $priority = 5;
|
|
private $createtime = null;
|
|
private $duetime = null;
|
|
private $completetime = null;
|
|
|
|
const INCOMPLETE = 1000301;
|
|
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) {
|
|
return self::formatText($this->title, $flag);
|
|
}
|
|
|
|
public function getDescription($flag = 0) {
|
|
return self::formatText($this->description, $flag);
|
|
}
|
|
|
|
public function getPriority() {
|
|
return intval($this->priority);
|
|
}
|
|
|
|
public function getCompleted($flag = 0) {
|
|
$completed = (is_null($this->getCompleteTime())) ? false : true;
|
|
switch ($flag) {
|
|
case AppDB::BOOLEANDB:
|
|
return ($completed) ? "true" : "false";
|
|
default:
|
|
return $completed;
|
|
}
|
|
}
|
|
|
|
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) {
|
|
return self::formatDate($this->createtime, $flag);
|
|
}
|
|
|
|
public function getDueTime($flag = 0) {
|
|
return self::formatDate($this->duetime, $flag);
|
|
}
|
|
|
|
public function getCompleteTime($flag = 0) {
|
|
return self::formatDate($this->completetime, $flag);
|
|
}
|
|
|
|
public function setID($id = null) {
|
|
if (is_null($id)) return false;
|
|
$id = intval($id);
|
|
if ($id <= 0) return false;
|
|
$this->id = $id;
|
|
return true;
|
|
}
|
|
|
|
public function setTitle($title = null) {
|
|
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");
|
|
$this->title = $title;
|
|
return true;
|
|
}
|
|
|
|
public function setDescription($description = null) {
|
|
if ( is_null($description) ) return false;
|
|
if ( !is_string($description) ) return false;
|
|
$description = trim($description);
|
|
settype($description, "string");
|
|
$this->description = $description;
|
|
return true;
|
|
}
|
|
|
|
public function setPriority($p = null) {
|
|
if (is_null($p)) 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;
|
|
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;
|
|
}
|
|
|
|
public function setCompleteTime($time = null) {
|
|
if ( $time === Task::NOW ) {
|
|
$completetime = new DateTime();
|
|
$this->completetime = $completetime->format("Y-m-d H:i:s");
|
|
return true;
|
|
} elseif ( is_null($time) || ($time === Task::INCOMPLETE) ) {
|
|
$this->completetime = null;
|
|
return true;
|
|
} else {
|
|
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 isOverdue() {
|
|
if ( is_null($this->duetime) ) return false;
|
|
$duedate = new DateTime($this->getDueTime());
|
|
$now = new DateTime();
|
|
if ( $now >= $duedate ) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getDaysLeft() {
|
|
if ( $this->isOverdue() ) {
|
|
return 0;
|
|
} else {
|
|
$now = new DateTime();
|
|
return intval($now->diff(new DateTime($this->getDueTime()))->format('%a')) + 1;
|
|
}
|
|
}
|
|
|
|
public function jsonSerialize():array {
|
|
return [
|
|
'id' => $this->getID(),
|
|
'title' => $this->getTitle(),
|
|
'title_safe' => $this->getTitle(HTMLSAFE),
|
|
'description' => $this->getDescription(),
|
|
'description_safe' => $this->getDescription(HTMLSAFE),
|
|
'priority' => $this->getPriority(),
|
|
'completed' => $this->getCompleted(),
|
|
'overdue' => $this->isOverdue(),
|
|
'daysleft' => $this->getDaysLeft(),
|
|
'createtime' => $this->getCreateTime(),
|
|
'createdate' => $this->getCreateTime(SHORTDATE),
|
|
'duetime' => $this->getDueTime(),
|
|
'duedate' => $this->getDueTime(SHORTDATE),
|
|
'completetime' => $this->getCompleteTime(),
|
|
'completedate' => $this->getCompleteTime(SHORTDATE)
|
|
];
|
|
}
|
|
|
|
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'];
|
|
}
|
|
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');
|
|
}
|
|
$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 = "") {
|
|
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 [];
|
|
$sth->bindValue(":search1", $search, PDO::PARAM_STR);
|
|
$sth->bindValue(":search2", $search, PDO::PARAM_STR);
|
|
$sth->execute();
|
|
$thelist = [];
|
|
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
|
|
$task = new self();
|
|
$task->hydrate($row);
|
|
$thelist[] = $task;
|
|
}
|
|
return $thelist;
|
|
}
|
|
|
|
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 [];
|
|
$sth->execute();
|
|
$thelist = [];
|
|
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
|
|
$task = new self();
|
|
$task->hydrate($row);
|
|
$thelist[] = $task;
|
|
}
|
|
return $thelist;
|
|
}
|
|
|
|
public function delete() {
|
|
$query = "DELETE FROM " . AppDB::TABLE_TASKS . " WHERE id=:id";
|
|
try {
|
|
$sth = AppDB::getSTH($query);
|
|
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;
|
|
}
|
|
|
|
public function save() {
|
|
if ( $this->getTitle() == "" ) return false;
|
|
if ( is_null($this->getDueTime()) ) return false;
|
|
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 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);
|
|
$executed = $sth->execute();
|
|
if ( !$executed ) return false;
|
|
} catch(PDOException $e) {
|
|
error_log((string) $e);
|
|
return false;
|
|
}
|
|
if ( $this->getID() == 0 ) {
|
|
$this->createtime = $newcreatetime;
|
|
$this->setID(AppDB::getLastInsertId());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function __construct($reqid = 0) {
|
|
$reqid = intval($reqid);
|
|
if ( $reqid <= 0 ) return;
|
|
$query = "SELECT " . self::COLUMNS . " FROM " . AppDB::TABLE_TASKS . " WHERE id=:id";
|
|
$sth = AppDB::getSTH($query);
|
|
if ( $sth === false ) return;
|
|
$sth->bindValue(":id", $reqid, PDO::PARAM_INT);
|
|
$sth->execute();
|
|
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: et sw=4 ts=4 ai:
|