From fff7786bd4be1a6de9b200a8b546493b9bb739e5 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Tue, 5 Mar 2019 21:24:56 +0000 Subject: [PATCH] Added code to handle projects field in tt_clients on project deletion. --- WEB-INF/lib/ttClientHelper.class.php | 47 +++++++++++++++++++++++++++ WEB-INF/lib/ttProjectHelper.class.php | 5 ++- WEB-INF/templates/footer.tpl | 2 +- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/WEB-INF/lib/ttClientHelper.class.php b/WEB-INF/lib/ttClientHelper.class.php index 64eedb0a..1974401c 100644 --- a/WEB-INF/lib/ttClientHelper.class.php +++ b/WEB-INF/lib/ttClientHelper.class.php @@ -316,4 +316,51 @@ class ttClientHelper { } return $result; } + + // deleteProject - deletes a project from the projects field it tt_clients table + // for all clients in a group. + static function deleteProject($project_id) { + global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $sql = "select id from tt_clients". + " where projects like '%$project_id%'". + " and group_id = $group_id and org_id = $org_id"; + $res = $mdb2->query($sql); + if (!is_a($res, 'PEAR_Error')) { + while ($val = $res->fetchRow()) { + if (!ttClientHelper::deleteProjectFromClient($project_id, $val['id'])) + return false; + } + } + return true; + } + + // deleteProject - deletes a project from the projects field in tt_clients table + // for a single client in a group. + static function deleteProjectFromClient($project_id, $client_id) { + global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $sql = "select projects from tt_clients". + " where id = $client_id and group_id = $group_id and org_id = $org_id"; + $res = $mdb2->query($sql); + if (is_a($res, 'PEAR_Error')) return false; + $val = $res->fetchRow(); + $projects = explode(',', $val['projects']); + if (($key = array_search($project_id, $projects)) !== false) { + unset($projects[$key]); + } + $comma_separated = implode(',', $projects); + $sql = "update tt_clients set projects = ".$mdb2->quote($comma_separated). + " where id = $client_id and group_id = $group_id and org_id = $org_id"; + $affected = $mdb2->exec($sql); + return (!is_a($affected, 'PEAR_Error')); + } } diff --git a/WEB-INF/lib/ttProjectHelper.class.php b/WEB-INF/lib/ttProjectHelper.class.php index 31a88ccc..1bd363f9 100644 --- a/WEB-INF/lib/ttProjectHelper.class.php +++ b/WEB-INF/lib/ttProjectHelper.class.php @@ -28,6 +28,7 @@ import('ttUserHelper'); import('ttGroupHelper'); +import('ttClientHelper'); // Class ttProjectHelper is used to help with project related tasks. class ttProjectHelper { @@ -194,7 +195,9 @@ class ttProjectHelper { if (is_a($affected, 'PEAR_Error')) return false; - return true; + // Finally, delete the project from the projects field in tt_clients table. + $result = ttClientHelper::deleteProject($id); + return $result; } // insert function inserts a new project into database. diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index a83f58e3..4dd6c172 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.53.4827 | Copyright © Anuko | +  Anuko Time Tracker 1.18.53.4828 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} -- 2.20.1