Populating tt_user_project_binds.
[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 5 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 group by org_id order by last_access, org_id) as t".
75       " where last_access is null or last_access < $cutoff_timestamp limit 5"; // Max 5 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   // Work in progress, currently not fully implemented.
88   static function deleteOrg($org_id) {
89     // We shall do it in a straightforward way by a delete operation from all tables by org_id.
90     // However, at this time not all tables have org_id.
91     // So, we need to add the field as we write code here.
92
93     // Delete expense items.
94     $sql = "delete from tt_expense_items where org_id = $org_id";
95     $affected = $mdb2->exec($sql);
96     if (is_a($affected, 'PEAR_Error')) return false;
97
98     // Delete predefined expenses.
99     $sql = "delete from tt_predefined_expenses where org_id = $org_id";
100     $affected = $mdb2->exec($sql);
101     if (is_a($affected, 'PEAR_Error')) return false;
102
103     // Delete monthly quotas.
104     $sql = "delete from tt_monthly_quotas where org_id = $org_id";
105     $affected = $mdb2->exec($sql);
106     if (is_a($affected, 'PEAR_Error')) return false;
107
108     return true; // Work in progress, currently not fully implemented.
109   }
110 }