X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/9a23a8c0a51b7ec38a96f525484134f3cb85dc7e..f4916707c8af136a72a12933d01a5fea1a4322ab:/WEB-INF/lib/ttProjectHelper.class.php diff --git a/WEB-INF/lib/ttProjectHelper.class.php b/WEB-INF/lib/ttProjectHelper.class.php index 1ec21fe1..455076fa 100644 --- a/WEB-INF/lib/ttProjectHelper.class.php +++ b/WEB-INF/lib/ttProjectHelper.class.php @@ -28,22 +28,24 @@ import('ttTeamHelper'); import('ttUserHelper'); +import('ttGroupHelper'); // Class ttProjectHelper is used to help with project related tasks. class ttProjectHelper { // getAssignedProjects - returns an array of assigned projects. - static function getAssignedProjects($user_id) - { - global $user; - - $result = array(); + static function getAssignedProjects($user_id) { + global $user; $mdb2 = getConnection(); - + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $result = array(); // Do a query with inner join to get assigned projects. - $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p - inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1) - where p.team_id = $user->team_id and p.status = 1 order by p.name"; + $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p". + " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)". + " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by p.name"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { while ($val = $res->fetchRow()) { @@ -54,36 +56,38 @@ class ttProjectHelper { } // getRates - returns an array of project rates for user, including deassigned and deactivated projects. - static function getRates($user_id) - { + static function getRates($user_id) { global $user; - - $result = array(); $mdb2 = getConnection(); - - $sql = "select p.id, upb.rate from tt_projects p - inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id) - where team_id = $user->team_id"; + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $result = array(); + $sql = "select p.id, upb.rate from tt_projects p". + " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id)". + " where p.group_id = $group_id and p.org_id = $org_id"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { while ($val = $res->fetchRow()) { + $val['rate'] = str_replace('.', $user->getDecimalMark(), $val['rate']); $result[] = $val; } } return $result; } - // getProjects - returns an array of active and inactive projects in a team. - static function getProjects() - { - global $user; - - $result = array(); + // getProjects - returns an array of active and inactive projects in group. + static function getProjects() { + global $user; $mdb2 = getConnection(); - - $sql = "select id, name, tasks from tt_projects - where team_id = $user->team_id and (status = 0 or status = 1) order by name"; - + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $result = array(); + $sql = "select id, name, tasks from tt_projects". + " where group_id = $group_id and org_id = $org_id and (status = 0 or status = 1) order by name"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { while ($val = $res->fetchRow()) { @@ -93,19 +97,20 @@ class ttProjectHelper { return $result; } - // getProjectsForClient - returns an array of active and inactive projects in a team for a client. - static function getProjectsForClient() - { - global $user; - - $result = array(); + // getProjectsForClient - returns an array of active and inactive projects in a group for a client. + static function getProjectsForClient() { + global $user; $mdb2 = getConnection(); - - $sql = "select p.id, p.name, p.tasks from tt_projects p - inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id) - where p.team_id = $user->team_id and (p.status = 0 or p.status = 1) - order by p.name"; - + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $result = array(); + $sql = "select p.id, p.name, p.tasks from tt_projects p". + " inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id)". + " where p.group_id = $group_id and p.org_id = $org_id and (p.status = 0 or p.status = 1)". + " order by p.name"; + $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { while ($val = $res->fetchRow()) { @@ -114,37 +119,41 @@ class ttProjectHelper { } return $result; } - - + // get - gets details of the project identified by its id. - static function get($id) - { + static function get($id) { global $user; - $mdb2 = getConnection(); - $sql = "select id, name, description, status, tasks from tt_projects where id = $id and team_id = $user->team_id and (status = 0 or status = 1)"; + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $sql = "select id, name, description, status, tasks from tt_projects". + " where id = $id and group_id = $group_id and org_id = $org_id and (status = 0 or status = 1)"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); - if ($val && $val['id']) + if ($val && $val['id']) return $val; } return false; } - + // The getProjectByName looks up a project by name. static function getProjectByName($name) { - - $mdb2 = getConnection(); global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; - $sql = "select id from tt_projects where team_id = $user->team_id and name = ". - $mdb2->quote($name)." and (status = 1 or status = 0)"; - $res = $mdb2->query($sql); - if (!is_a($res, 'PEAR_Error')) { + $sql = "select id from tt_projects". + " where group_id = $group_id and org_id = $org_id and name = ".$mdb2->quote($name). + " and (status = 1 or status = 0)"; + $res = $mdb2->query($sql); + if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); - if ($val && $val['id']) + if ($val && $val['id']) return $val; } return false; @@ -153,41 +162,50 @@ class ttProjectHelper { // delete - deletes things associated with a project and marks the project as deleted. static function delete($id) { + global $user; $mdb2 = getConnection(); - - // Delete user binds to this project. - $sql = "delete from tt_user_project_binds where project_id = $id"; + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + // Start with project itself. Reason: if the passed in project_id is bogus, + // we'll fail right here and don't damage any other data. + + // Mark project as deleted and remove associated tasks. + $sql = "update tt_projects set status = NULL, tasks = NULL where id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); - if (is_a($affected, 'PEAR_Error')) - return false; - - // Delete task binds to this project. - $sql = "delete from tt_project_task_binds where project_id = $id"; + if (is_a($affected, 'PEAR_Error') || 0 == $affected) + return false; // An error ocurred, or 0 rows updated. + + // Delete user binds to this project. + $sql = "delete from tt_user_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; - - // Remove associated tasks. - $sql = "update tt_projects set tasks = NULL where id = $id"; + + // Delete task binds to this project. + $sql = "delete from tt_project_task_binds where project_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; - // Mark project as deleted. - $sql = "update tt_projects set status = NULL where id = $id"; + // Delete client binds to this project. + $sql = "delete from tt_client_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; - return true; + return true; } // insert function inserts a new project into database. static function insert($fields) { + global $user; $mdb2 = getConnection(); - $team_id = (int) $fields['team_id']; + $group_id = $user->getGroup(); + $org_id = $user->org_id; $name = $fields['name']; $description = $fields['description']; @@ -196,24 +214,20 @@ class ttProjectHelper { $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids. $status = $fields['status']; - $sql = "insert into tt_projects (team_id, name, description, tasks, status) - values ($team_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")"; + $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)". + " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; - - $last_id = 0; - $sql = "select last_insert_id() as last_insert_id"; - $res = $mdb2->query($sql); - $val = $res->fetchRow(); - $last_id = $val['last_insert_id']; + + $last_id = $mdb2->lastInsertID('tt_projects', 'id'); // Bind the project to users. - $active_users = ttTeamHelper::getActiveUsers(array('getAllFields'=>true)); + $active_users = ttGroupHelper::getActiveUsers(array('getAllFields'=>true)); foreach ($active_users as $u) { if(in_array($u['id'], $users)) { - $sql = "insert into tt_user_project_binds (project_id, user_id, status, rate) values( - $last_id, ".$u['id'].", 1, ".$u['rate'].")"; + $sql = "insert into tt_user_project_binds (project_id, user_id, group_id, org_id, status, rate) values( + $last_id, ".$u['id'].", $group_id, $org_id, 1, ".$u['rate'].")"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -221,10 +235,11 @@ class ttProjectHelper { } // Bind the project to tasks in tt_project_task_binds table. - $all_tasks = ttTeamHelper::getAllTasks($team_id); + $all_tasks = ttTeamHelper::getAllTasks($group_id); foreach ($all_tasks as $task) { if(in_array($task['id'], $tasks)) { - $sql = "insert into tt_project_task_binds (project_id, task_id) values($last_id, ".$task['id'].")"; + $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)". + " values($last_id, ".$task['id'].", $group_id, $org_id)"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -235,10 +250,13 @@ class ttProjectHelper { } // update function - updates the project in database. - static function update($fields) - { + static function update($fields) { + global $user; $mdb2 = getConnection(); - + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + $project_id = $fields['id']; // Project we are updating. $name = $fields['name']; // Project name. $description = $fields['description']; // Project description. @@ -255,9 +273,9 @@ class ttProjectHelper { if(!in_array($row['user_id'], $users_to_bind)) { // Delete tt_user_project_binds record (safely). ttUserHelper::deleteBind($row['user_id'], $project_id); - } else if (!$row['status']) { + } elseif (!$row['status']) { // If we are here, status of the bind is not active. Memorize such users to update their bind status. - $users_to_update[] = $row['user_id']; // Users we need to update in tt_user_project_binds. + $users_to_update[] = $row['user_id']; // Users we need to update in tt_user_project_binds. } $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project. } @@ -270,7 +288,8 @@ class ttProjectHelper { $user_rate[$row['id']] = $row['rate']; } foreach ($users_to_add as $id) { - $sql = "insert into tt_user_project_binds (user_id, project_id, rate, status) values($id, $project_id, ".$user_rate[$id].", 1)"; + $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)". + " values($id, $project_id, $group_id, $org_id, ".$user_rate[$id].", 1)"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -303,7 +322,8 @@ class ttProjectHelper { return false; } foreach ($task_binds_to_add as $task_id) { - $sql = "insert into tt_project_task_binds (project_id, task_id) values($project_id, $task_id)"; + $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)". + " values($project_id, $task_id, $group_id, $org_id)"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -312,7 +332,8 @@ class ttProjectHelper { // Update project name, description, tasks and status in tt_projects table. $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids. - $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id"; + $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description). + ", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id and group_id = ".$user->getGroup(); $affected = $mdb2->exec($sql); return (!is_a($affected, 'PEAR_Error')); } @@ -338,4 +359,3 @@ class ttProjectHelper { return $result; }*/ } -?> \ No newline at end of file