Wrote ttGroupHelper::deleteGroupFiles() function.
[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 50 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 50"; // Max 50 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
89     // Delete all org files.
90     ttOrgHelper::deleteOrgFiles($org_id);
91
92     // Go one table at a time and remove all records with matching org_id.
93     // The order is backwards to import (see ttOrgImportHelper). Remove groups last.
94     // This leaves us with something partially working if an error occurs.
95     $mdb2 = getConnection();
96
97     $tables = array(
98       'tt_config',
99       'tt_cron',
100       'tt_fav_reports',
101       'tt_templates',
102       'tt_monthly_quotas',
103       'tt_predefined_expenses',
104       'tt_expense_items',
105       'tt_custom_field_log',
106       'tt_custom_field_options',
107       'tt_custom_fields',
108       'tt_log',
109       'tt_invoices',
110       'tt_timesheets',
111       'tt_user_project_binds',
112       'tt_users',
113       'tt_client_project_binds',
114       'tt_clients',
115       'tt_project_task_binds',
116       'tt_projects',
117       'tt_tasks',
118       'tt_roles',
119       'tt_groups'
120     );
121     foreach($tables as $table) {
122       $sql = "delete from $table where org_id = $org_id";
123       $affected = $mdb2->exec($sql);
124       if (is_a($affected, 'PEAR_Error')) return false;
125     }
126     return true;  }
127
128   // deleteOrgFiles deletes files attached to all entities in the entire organization.
129   static function deleteOrgFiles($org_id) {
130
131     // Delete all org files from the database.
132     $mdb2 = getConnection();
133     $sql = "delete from tt_files where org_id = $org_id";
134     $affected = $mdb2->exec($sql);
135     if (is_a($affected, 'PEAR_Error'))
136       return false;
137
138     if ($affected == 0) return true; // Do not call file storage utility.
139
140     // Try to make a call to file storage facility.
141     if (!defined('FILE_STORAGE_URI')) return true; // Nothing to do.
142
143     $deleteorgfiles_uri = FILE_STORAGE_URI.'deleteorgfiles';
144
145     // Obtain site id.
146     $sql = "select param_value as site_id from tt_site_config where param_name = 'locker_id'";
147     $res = $mdb2->query($sql);
148     $val = $res->fetchRow();
149     $site_id = $val['site_id'];
150     if (!$site_id) return true; // Nothing to do.
151
152     // Obtain site key.
153     $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
154     $res = $mdb2->query($sql);
155     $val = $res->fetchRow();
156     $site_key = $val['site_key'];
157     if (!$site_key) return true; // Can't continue without site key.
158
159     // Obtain org key.
160     $sql = "select group_key as org_key from tt_groups where id = $org_id";
161     $res = $mdb2->query($sql);
162     $val = $res->fetchRow();
163     $org_key = $val['org_key'];
164     if (!$org_key) return true; // Can't continue without org key.
165
166     $curl_fields = array('site_id' => $site_id,
167       'site_key' => $site_key,
168       'org_id' => $org_id,
169       'org_key' => $org_key);
170
171     // url-ify the data for the POST.
172     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
173     $fields_string = rtrim($fields_string, '&');
174
175     // Open connection.
176     $ch = curl_init();
177
178     // Set the url, number of POST vars, POST data.
179     curl_setopt($ch, CURLOPT_URL, $deleteorgfiles_uri);
180     curl_setopt($ch, CURLOPT_POST, true);
181     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
182     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
183
184     // Execute a post request.
185     $result = curl_exec($ch);
186
187     // Close connection.
188     curl_close($ch);
189
190     // Many things can go wrong with a remote call to file storage facility.
191     // By design, we ignore such errors.
192     return true;
193   }
194 }