+ new notifications for tasks (edited, completed, deleted) and lists (deleted, deleted complted tasks)

This commit is contained in:
maxpozdeev 2023-08-19 18:24:40 +03:00
parent a6bde46cc3
commit aeb732b446
4 changed files with 80 additions and 1 deletions

View file

@ -118,6 +118,10 @@ class ListsController extends ApiController {
$t = array();
$t['total'] = 0;
$id = (int)$id;
$list = null;
if (MTTNotificationCenter::hasObserversForNotification(MTTNotification::didDeleteList)) {
$list = $this->getListRowById($id);
}
$db->ex("BEGIN");
$db->ex("DELETE FROM {$db->prefix}lists WHERE id=$id");
$t['total'] = $db->affected();
@ -126,6 +130,9 @@ class ListsController extends ApiController {
$db->ex("DELETE FROM {$db->prefix}todolist WHERE list_id=$id");
}
$db->ex("COMMIT");
if ($t['total'] && MTTNotificationCenter::hasObserversForNotification(MTTNotification::didDeleteList)) {
MTTNotificationCenter::postNotification(MTTNotification::didDeleteList, $list);
}
$this->response->data = $t;
}
@ -182,6 +189,15 @@ class ListsController extends ApiController {
);
}
private function getListRowById(int $id)
{
$r = DBCore::default()->getListById($id);
if (!$r) {
throw new Exception("Failed to fetch list data");
}
return $this->prepareList($r, true);
}
private function prepareList($row, bool $haveWriteAccess): array
{
$taskview = (int)$row['taskview'];
@ -331,6 +347,13 @@ class ListsController extends ApiController {
$db->ex("DELETE FROM {$db->prefix}todolist WHERE list_id=$listId and compl=1");
$t['total'] = $db->affected();
$db->ex("COMMIT");
if (MTTNotificationCenter::hasObserversForNotification(MTTNotification::didDeleteCompletedInList)) {
$list = $this->getListRowById($listId);
MTTNotificationCenter::postNotification(MTTNotification::didDeleteCompletedInList, [
'total' => $t['total'],
'list' => $list
]);
}
return $t;
}

View file

@ -175,6 +175,10 @@ class TasksController extends ApiController {
{
checkWriteAccess();
$id = (int)$id;
$task = null;
if (MTTNotificationCenter::hasObserversForNotification(MTTNotification::didDeleteTask)) {
$task = $this->getTaskRowById($id);
}
$db = DBConnection::instance();
$db->ex("BEGIN");
$db->ex("DELETE FROM {$db->prefix}tag2task WHERE task_id=$id");
@ -182,6 +186,9 @@ class TasksController extends ApiController {
$db->dq("DELETE FROM {$db->prefix}todolist WHERE id=$id");
$deleted = $db->affected();
$db->ex("COMMIT");
if ($deleted && MTTNotificationCenter::hasObserversForNotification(MTTNotification::didDeleteTask)) {
MTTNotificationCenter::postNotification(MTTNotification::didDeleteTask, $task);
}
$t = array();
$t['total'] = $deleted;
$t['list'][] = array('id' => $id);
@ -349,6 +356,7 @@ class TasksController extends ApiController {
array($title, $note, $prio, $duedate, time()) );
$db->ex("COMMIT");
$task = $this->getTaskRowById($id);
MTTNotificationCenter::postNotification(MTTNotification::didEditTask, ['task' => $task]);
$t['list'][] = $task;
$t['total'] = 1;
return $t;
@ -359,6 +367,13 @@ class TasksController extends ApiController {
$fromId = (int)($this->req->jsonBody['from'] ?? 0);
$toId = (int)($this->req->jsonBody['to'] ?? 0);
$result = $this->doMoveTask($id, $toId);
if ($result && MTTNotificationCenter::hasObserversForNotification(MTTNotification::didEditTask)) {
$task = $this->getTaskRowById($id);
MTTNotificationCenter::postNotification(MTTNotification::didEditTask, [
'property' => 'list',
'task' => $task
]);
}
$t = array('total' => $result ? 1 : 0);
if ($fromId == -1 && $result) {
$t['list'][] = $this->getTaskRowById($id);
@ -398,9 +413,11 @@ class TasksController extends ApiController {
$dateCompleted = $compl ? $date : 0;
$db->dq("UPDATE {$db->prefix}todolist SET compl=$compl,ow=$ow,d_completed=?,d_edited=? WHERE id=$id",
array($dateCompleted, $date) );
$task = $this->getTaskRowById($id);
MTTNotificationCenter::postNotification(MTTNotification::didCompleteTask, $task);
$t = array();
$t['total'] = 1;
$t['list'][] = $this->getTaskRowById($id);
$t['list'][] = $task;
return $t;
}
@ -410,6 +427,13 @@ class TasksController extends ApiController {
$note = $this->req->jsonBody['note'] ?? '';
$note = str_replace("\r\n", "\n", $note);
$db->dq("UPDATE {$db->prefix}todolist SET note=?,d_edited=? WHERE id=$id", array($note, time()) );
if (MTTNotificationCenter::hasObserversForNotification(MTTNotification::didEditTask)) {
$task = $this->getTaskRowById($id);
MTTNotificationCenter::postNotification(MTTNotification::didEditTask, [
'property' => 'note',
'task' => $task
]);
}
$t = array();
$t['total'] = 1;
$t['list'][] = array('id'=>$id, 'note'=> noteMarkup($note), 'noteText'=>(string)$note);
@ -423,6 +447,13 @@ class TasksController extends ApiController {
if ($prio < -1) $prio = -1;
elseif ($prio > 2) $prio = 2;
$db->ex("UPDATE {$db->prefix}todolist SET prio=$prio,d_edited=? WHERE id=$id", array(time()) );
if (MTTNotificationCenter::hasObserversForNotification(MTTNotification::didEditTask)) {
$task = $this->getTaskRowById($id);
MTTNotificationCenter::postNotification(MTTNotification::didEditTask, [
'property' => 'priority',
'task' => $task
]);
}
$t = array();
$t['total'] = 1;
$t['list'][] = array('id'=>$id, 'prio'=>$prio);

View file

@ -75,6 +75,14 @@ class DBCore
}
public function getListById(int $id): ?array
{
$db = $this->db;
$r = $db->sqa("SELECT * FROM {$db->prefix}lists WHERE id=?", [$id]);
return $r;
}
public function taskExists(int $id): bool
{
$db = $this->db;

View file

@ -55,6 +55,18 @@ class MTTNotificationCenter
self::$observers[$notification][] = $callback;
}
/**
*
* @param string $notification
* @return bool
*/
public static function hasObserversForNotification(string $notification): bool
{
if (isset(self::$observers[$notification]) && count(self::$observers[$notification]) > 0) {
return true;
}
return false;
}
public static function postNotification(string $notification, $object)
{
@ -100,7 +112,12 @@ abstract class MTTNotification
{
const didFinishRequest = 'didFinishRequest';
const didCreateTask = 'didCreateTask';
const didEditTask = 'didEditTask';
const didDeleteTask = 'didDeleteTask';
const didCompleteTask = 'didCompleteTask';
const didCreateList = 'didCreateList';
const didDeleteList = 'didDeleteList';
const didDeleteCompletedInList = 'didDeleteCompletedInList';
}
function add_action(string $notification, callable $callback)