From fec6ce6d32cc779e350a65c641f1b1eafa096ea8 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Fri, 23 Nov 2018 22:20:01 +0000 Subject: [PATCH] Improved subgroup addition by copying attributes and roles from a parent group. --- WEB-INF/lib/ttGroupHelper.class.php | 69 +++++++++++++++++++++++++---- WEB-INF/lib/ttRoleHelper.class.php | 25 +++++++++++ WEB-INF/templates/footer.tpl | 2 +- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/WEB-INF/lib/ttGroupHelper.class.php b/WEB-INF/lib/ttGroupHelper.class.php index 21e62a60..01429aa6 100644 --- a/WEB-INF/lib/ttGroupHelper.class.php +++ b/WEB-INF/lib/ttGroupHelper.class.php @@ -26,6 +26,8 @@ // | https://www.anuko.com/time_tracker/credits.htm // +----------------------------------------------------------------------+ +import('ttRoleHelper'); + // Class ttGroupHelper - contains helper functions that operate with groups. // This is a planned replacement for ttTeamHelper as we move forward with subgroups. class ttGroupHelper { @@ -86,19 +88,51 @@ class ttGroupHelper { $mdb2 = getConnection(); $parent_id = $user->getActiveGroup(); $org_id = $user->org_id; - - // TODO: inherit all attributes from the parent group, if not supplied. $name = $fields['name']; $description = $fields['description']; - $created = 'now()'; - $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']); - - $sql = "insert into tt_groups (parent_id, org_id, name, description, created, created_ip)". - " values($parent_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", $created, $created_ip)"; + // We need to inherit other attributes from the parent group. + $attrs = ttGroupHelper::getGroupAttrs($parent_id); + + $columns = '(parent_id, org_id, name, description, currency, decimal_mark, lang, date_format, time_format'. + ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'. + ', allow_ip, password_complexity, plugins, lock_spec'. + ', workday_minutes, config, created, created_ip, created_by)'; + + $values = " values ($parent_id, $org_id"; + $values .= ', '.$mdb2->quote($name); + $values .= ', '.$mdb2->quote($description); + $values .= ', '.$mdb2->quote($attrs['currency']); + $values .= ', '.$mdb2->quote($attrs['decimal_mark']); + $values .= ', '.$mdb2->quote($attrs['lang']); + $values .= ', '.$mdb2->quote($attrs['date_format']); + $values .= ', '.$mdb2->quote($attrs['time_format']); + $values .= ', '.(int)$attrs['week_start']; + $values .= ', '.(int)$attrs['tracking_mode']; + $values .= ', '.(int)$attrs['project_required']; + $values .= ', '.(int)$attrs['task_required']; + $values .= ', '.(int)$attrs['record_type']; + $values .= ', '.$mdb2->quote($attrs['bcc_email']); + $values .= ', '.$mdb2->quote($attrs['allow_ip']); + $values .= ', '.$mdb2->quote($attrs['password_complexity']); + $values .= ', '.$mdb2->quote($attrs['plugins']); + $values .= ', '.$mdb2->quote($attrs['lock_spec']); + $values .= ', '.(int)$attrs['workday_minutes']; + $values .= ', '.$mdb2->quote($attrs['config']); + $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id); + $values .= ')'; + + $sql = 'insert into tt_groups '.$columns.$values; $affected = $mdb2->exec($sql); - return (!is_a($affected, 'PEAR_Error')); - // TODO: design subgroup roles carefully. Perhaps roles are not to be touched in subgroups at all. + if (is_a($affected, 'PEAR_Error')) return false; + + $subgroup_id = $mdb2->lastInsertID('tt_groups', 'id'); + + // Copy roles from the parent group to child group. + if (!ttRoleHelper::copyRolesToGroup($subgroup_id)) + return false; + + return $subgroup_id; } // markGroupDeleted marks a group and everything in it as deleted. @@ -219,4 +253,21 @@ class ttGroupHelper { } return $val; } + + // getRoles obtains all active and inactive roles in current group. + static function getRoles() { + global $user; + $mdb2 = getConnection(); + + $group_id = $user->getActiveGroup(); + $org_id = $user->org_id; + $sql = "select * from tt_roles". + " where group_id = $group_id and org_id = $org_id and status is not null"; + $res = $mdb2->query($sql); + if (is_a($res, 'PEAR_Error')) return false; + while ($val = $res->fetchRow()) { + $roles[] = $val; + } + return $roles; + } } diff --git a/WEB-INF/lib/ttRoleHelper.class.php b/WEB-INF/lib/ttRoleHelper.class.php index 2751756b..53fe571a 100644 --- a/WEB-INF/lib/ttRoleHelper.class.php +++ b/WEB-INF/lib/ttRoleHelper.class.php @@ -313,4 +313,29 @@ class ttRoleHelper { } return false; } + + // copyRoles copies roles from one group to another. + static function copyRolesToGroup($group_id) { + global $user; + $mdb2 = getConnection(); + + $org_id = $user->org_id; + $columns = '(group_id, org_id, name, description, rank, rights, status)'; + $roles = ttGroupHelper::getRoles(); // Roles in current on behalf group. + + foreach ($roles as $role) { + $values = "values($group_id, $org_id". + ', '.$mdb2->quote($role['name']). + ', '.$mdb2->quote($role['description']). + ', '.(int)$role['rank']. + ', '.$mdb2->quote($role['rights']). + ', '.$mdb2->quote($role['status']). + ')'; + $sql = "insert into tt_roles $columns $values"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) + return false; + } + return true; + } } diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 425975da..aa0be4dc 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.27.4505 | Copyright © Anuko | +  Anuko Time Tracker 1.18.27.4506 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} -- 2.20.1