X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2FttClientHelper.class.php;h=7105595d830f8112cbda16dd1a79c82089965097;hb=f787edd7045299de3885c052d243b446f7324ea0;hp=eeb77f77a993fe3246d793460671bad0e953c1bc;hpb=e5c2856600ec9c939f011226c78f39ae9a63d8b0;p=timetracker.git diff --git a/WEB-INF/lib/ttClientHelper.class.php b/WEB-INF/lib/ttClientHelper.class.php index eeb77f77..7105595d 100644 --- a/WEB-INF/lib/ttClientHelper.class.php +++ b/WEB-INF/lib/ttClientHelper.class.php @@ -31,9 +31,11 @@ class ttClientHelper { // The getClient looks up a client by id. static function getClient($client_id, $all_fields = false) { - - $mdb2 = getConnection(); global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; $sql = 'select '; if ($all_fields) @@ -41,7 +43,7 @@ class ttClientHelper { else $sql .= 'name '; - $sql .= "from tt_clients where group_id = ".$user->getActiveGroup(). + $sql .= "from tt_clients where group_id = $group_id and org_id = $org_id". " and id = $client_id and (status = 1 or status = 0)"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { @@ -52,14 +54,16 @@ class ttClientHelper { } // getClients - returns an array of active and inactive clients in a group. - static function getClients() - { + static function getClients() { global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; $result = array(); - $mdb2 = getConnection(); - $sql = "select id, name from tt_clients where group_id = ".$user->getActiveGroup()." and (status = 0 or status = 1) order by upper(name)"; + $sql = "select id, name, projects from tt_clients where group_id = $group_id and org_id = $org_id and (status = 0 or status = 1) order by upper(name)"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { while ($val = $res->fetchRow()) { @@ -71,13 +75,14 @@ class ttClientHelper { // The getClientByName looks up a client by name. static function getClientByName($client_name) { - - $mdb2 = getConnection(); global $user; + $mdb2 = getConnection(); - $sql = "select id from tt_clients where group_id = ".$user->getActiveGroup(). - " and name = ".$mdb2->quote($client_name)." and (status = 1 or status = 0)"; + $group_id = $user->getGroup(); + $org_id = $user->org_id; + $sql = "select id from tt_clients where group_id = $group_id and org_id = $org_id". + " and name = ".$mdb2->quote($client_name)." and (status = 1 or status = 0)"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); @@ -90,11 +95,13 @@ class ttClientHelper { // The getDeletedClient looks up a deleted client by id. static function getDeletedClient($client_id) { - - $mdb2 = getConnection(); global $user; + $mdb2 = getConnection(); - $sql = "select name, address from tt_clients where group_id = ".$user->getActiveGroup(). + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $sql = "select name, address from tt_clients where group_id = $group_id and org_id = $org_id". " and id = $client_id and status is NULL"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { @@ -106,22 +113,26 @@ class ttClientHelper { // The delete function marks client as deleded. static function delete($id, $delete_client_entries) { - - $mdb2 = getConnection(); global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; // Handle custom field log records. if ($delete_client_entries) { - $sql = "update tt_custom_field_log set status = NULL where log_id in (select id from tt_log where client_id = $id and status = 1)"; + $sql = "update tt_custom_field_log set status = null". + " where log_id in (select id from tt_log where client_id = $id and status = 1) and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); - if (is_a($affected, 'PEAR_Error')) - return false; + if (is_a($affected, 'PEAR_Error')) + return false; } // Handle time records. - $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id); + $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id; if ($delete_client_entries) { - $sql = 'update tt_log set status = NULL'.$modified_part." where client_id = $id"; + $sql = 'update tt_log set status = null'.$modified_part. + " where client_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -129,7 +140,8 @@ class ttClientHelper { // Handle expense items. if ($delete_client_entries) { - $sql = 'update tt_expense_items set status = NULL'.$modified_part." where client_id = $id"; + $sql = 'update tt_expense_items set status = null'.$modified_part. + " where client_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -137,26 +149,30 @@ class ttClientHelper { // Handle invoices. if ($delete_client_entries) { - $sql = "update tt_invoices set status = NULL where client_id = $id and group_id = ".$user->getActiveGroup(); + $sql = "update tt_invoices set status = null". + " where client_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 project binds to this client. - $sql = "delete from tt_client_project_binds where client_id = $id"; + $sql = "delete from tt_client_project_binds". + " where client_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; // Handle users for client. - $sql = 'update tt_users set status = NULL'.$modified_part." where client_id = $id and group_id = ".$user->getActiveGroup(); + $sql = 'update tt_users set status = null'.$modified_part. + " where client_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 client deleted. - $sql = "update tt_clients set status = NULL where id = $id and group_id = ".$user->getActiveGroup(); + $sql = "update tt_clients set status = null". + " where id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); return (!is_a($affected, 'PEAR_Error')); } @@ -167,8 +183,9 @@ class ttClientHelper { global $user; $mdb2 = getConnection(); - $group_id = (int) $fields['group_id']; - $org_id = (int) $fields['org_id']; + $group_id = $user->getGroup(); + $org_id = $user->org_id; + $name = $fields['name']; $address = $fields['address']; $tax = $fields['tax']; @@ -187,12 +204,7 @@ class ttClientHelper { 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_clients', 'id'); if (count($projects) > 0) foreach ($projects as $p_id) { $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)"; @@ -207,8 +219,11 @@ class ttClientHelper { // The update function updates a client record in tt_clients table. static function update($fields) { - $mdb2 = getConnection(); global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; $id = $fields['id']; $name = $fields['name']; @@ -221,13 +236,14 @@ class ttClientHelper { if ($tax == '') $tax = 0; // Insert client to project binds into tt_client_project_binds table. - $sql = "delete from tt_client_project_binds where client_id = $id"; + $sql = "delete from tt_client_project_binds". + " where client_id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) die($affected->getMessage()); if (count($projects) > 0) foreach ($projects as $p_id) { - $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)"; + $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($id, $p_id, $group_id, $org_id)"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; @@ -237,23 +253,11 @@ class ttClientHelper { $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids. $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address). ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status". - " where group_id = ".$user->getActiveGroup()." and id = ".$id; + " where id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); return (!is_a($affected, 'PEAR_Error')); } - // The setMappedClient function is used during group import to change client_id value for tt_users to a mapped value. - static function setMappedClient($group_id, $imported_id, $mapped_id) - { - $mdb2 = getConnection(); - $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and group_id = $group_id "; - $affected = $mdb2->exec($sql); - if (is_a($affected, 'PEAR_Error')) - return false; - - return true; - } - // The fillBean function fills the ActionForm object with client data. static function fillBean($client_id, &$bean) { $client = ttClientHelper::getClient($client_id, true); @@ -266,14 +270,17 @@ class ttClientHelper { static function getAssignedProjects($client_id) { global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; $result = array(); - $mdb2 = getConnection(); // Do a query with inner join to get assigned projects. $sql = "select p.id, p.name from tt_projects p". " inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)". - " where p.group_id = ".$user->getActiveGroup()." and p.status = 1 order by p.name"; + " 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()) { @@ -287,15 +294,19 @@ class ttClientHelper { static function getClientsForUser() { global $user; - $user_id = $user->getActiveUser(); + $mdb2 = getConnection(); + + $user_id = $user->getUser(); + $group_id = $user->getGroup(); + $org_id = $user->org_id; $result = array(); - $mdb2 = getConnection(); - $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb - inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id) - inner join tt_clients c on (c.id = cpb.client_id and c.status = 1) - where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)"; + $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb". + " inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)". + " inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)". + " where upb.user_id = $user_id and upb.group_id = $group_id and upb.org_id = $org_id". + " and upb.status = 1 order by upper(c.name)"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { @@ -305,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; + } + + // deleteProjectFromClient - 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')); + } }