Work in progress on project attachments.
[timetracker.git] / WEB-INF / lib / ttOrgHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // Class ttOrgHelper contains helper functions that operate with organizations.
30 // Organizations are collections of nested groups of users.
31 class ttOrgHelper {
32
33   // The getOrgs function returns an array of all active organizations on the server.
34   static function getOrgs() {
35     $result = array();
36     $mdb2 = getConnection();
37
38     $sql =  "select id, name, created, lang from tt_groups".
39             " where status = 1 and org_id = id and parent_id is NULL order by id desc";
40     $res = $mdb2->query($sql);
41     $result = array();
42     if (!is_a($res, 'PEAR_Error')) {
43       while ($val = $res->fetchRow()) {
44         $val['date'] = substr($val['created'], 0, 10); // Strip the time.
45         $result[] = $val;
46       }
47       return $result;
48     }
49     return false;
50   }
51
52   // The getName function returns organization name (which is a name of its top group).
53   static function getName($org_id) {
54     $mdb2 = getConnection();
55
56     $sql = "select name from tt_groups where id = $org_id and (status = 1 or status = 0) and parent_id is NULL";
57     $res = $mdb2->query($sql);
58
59     if (!is_a($res, 'PEAR_Error')) {
60       $val = $res->fetchRow();
61       return $val['name'];
62     }
63     return false;
64   }
65
66   // The getInactiveOrgs is a maintenance function that returns an array of inactive organization ids (max 25 for now).
67   static function getInactiveOrgs() {
68     $inactive_orgs = array();
69     $mdb2 = getConnection();
70
71     // Determine inactive organizations by querying the database for max access timestamp for its users.
72     $cutoff_timestamp = $mdb2->quote(date('Y-m-d', strtotime('-1 year')));
73     $sql = "select org_id from".
74       " (select max(accessed) as last_access, org_id from tt_users where org_id > 0 group by org_id order by last_access, org_id) as t".
75       " where last_access is null or last_access < $cutoff_timestamp limit 25"; // Max 25 orgs at a time for now...
76     $res = $mdb2->query($sql);
77     if (!is_a($res, 'PEAR_Error')) {
78       while ($val = $res->fetchRow()) {
79         $inactive_orgs[] = $val['org_id'];
80       }
81       return $inactive_orgs;
82     }
83     return false;
84   }
85
86   // deleteOrg deletes data for the entire organization from database permanently.
87   static function deleteOrg($org_id) {
88     // Go one table at a time and remove all records with matching org_id.
89     // The order is backwards to import (see ttOrgImportHelper). Remove groups last.
90     // This leaves us with something partially working if an error occurs.
91     $mdb2 = getConnection();
92
93     $tables = array(
94       'tt_config',
95       'tt_cron',
96       'tt_fav_reports',
97       'tt_templates',
98       'tt_monthly_quotas',
99       'tt_predefined_expenses',
100       'tt_expense_items',
101       'tt_custom_field_log',
102       'tt_custom_field_options',
103       'tt_custom_fields',
104       'tt_log',
105       'tt_invoices',
106       'tt_timesheets',
107       'tt_user_project_binds',
108       'tt_users',
109       'tt_client_project_binds',
110       'tt_clients',
111       'tt_project_task_binds',
112       'tt_projects',
113       'tt_tasks',
114       'tt_roles',
115       'tt_groups'
116     );
117     foreach($tables as $table) {
118       $sql = "delete from $table where org_id = $org_id";
119       $affected = $mdb2->exec($sql);
120       if (is_a($affected, 'PEAR_Error')) return false;
121     }
122     return true;
123   }
124 }