Added a capability to add attachment on project_add.php.
[timetracker.git] / WEB-INF / lib / ttGroupHelper.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 import('ttRoleHelper');
30
31 // Class ttGroupHelper - contains helper functions that operate with groups.
32 // This is a planned replacement for ttTeamHelper as we move forward with subgroups.
33 class ttGroupHelper {
34
35   // The getGroupName function returns group name.
36   static function getGroupName($group_id) {
37     $mdb2 = getConnection();
38
39     $sql = "select name from tt_groups where id = $group_id and (status = 1 or status = 0)";
40     $res = $mdb2->query($sql);
41
42     if (!is_a($res, 'PEAR_Error')) {
43       $val = $res->fetchRow();
44       return $val['name'];
45     }
46     return false;
47   }
48
49   // The getParentGroup determines a parent group for a given group.
50   static function getParentGroup($group_id) {
51     global $user;
52
53     $mdb2 = getConnection();
54
55     $sql = "select parent_id from tt_groups where id = $group_id and org_id = $user->org_id and status = 1";
56     $res = $mdb2->query($sql);
57
58     if (!is_a($res, 'PEAR_Error')) {
59       $val = $res->fetchRow();
60       return $val['parent_id'];
61     }
62     return false;
63   }
64
65   // The getSubgroupByName obtain an immediate subgroup by name if one exists.
66   static function getSubgroupByName($name) {
67     global $user;
68
69     $mdb2 = getConnection();
70     $parent_id = $user->getGroup();
71     $org_id = $user->org_id;
72
73     $sql = "select id from tt_groups where parent_id = $parent_id and org_id = $org_id".
74       " and name = ".$mdb2->quote($name)." and status is not null";
75     $res = $mdb2->query($sql);
76     if (!is_a($res, 'PEAR_Error')) {
77       $val = $res->fetchRow();
78       if ($val && $val['id'])
79         return $val;
80     }
81     return false;
82   }
83
84   // The insertSubgroup inserts a new subgroup in database.
85   static function insertSubgroup($fields) {
86     global $user;
87
88     $mdb2 = getConnection();
89     $parent_id = $user->getGroup();
90     $org_id = $user->org_id;
91     $group_key = ttRandomString();
92     $name = $fields['name'];
93     $description = $fields['description'];
94
95     // We need to inherit attributes from the parent group.
96     $attrs = ttGroupHelper::getGroupAttrs($parent_id);
97
98     $columns = '(parent_id, org_id, group_key, name, description, currency, decimal_mark, lang, date_format,'.
99       ' time_format, week_start, tracking_mode, project_required, task_required, record_type, bcc_email,'.
100       ' allow_ip, password_complexity, plugins, lock_spec,'.
101       ' workday_minutes, config, created, created_ip, created_by)';
102
103     $values = " values ($parent_id, $org_id";
104     $values .= ', '.$mdb2->quote($group_key);
105     $values .= ', '.$mdb2->quote($name);
106     $values .= ', '.$mdb2->quote($description);
107     $values .= ', '.$mdb2->quote($attrs['currency']);
108     $values .= ', '.$mdb2->quote($attrs['decimal_mark']);
109     $values .= ', '.$mdb2->quote($attrs['lang']);
110     $values .= ', '.$mdb2->quote($attrs['date_format']);
111     $values .= ', '.$mdb2->quote($attrs['time_format']);
112     $values .= ', '.(int)$attrs['week_start'];
113     $values .= ', '.(int)$attrs['tracking_mode'];
114     $values .= ', '.(int)$attrs['project_required'];
115     $values .= ', '.(int)$attrs['task_required'];
116     $values .= ', '.(int)$attrs['record_type'];
117     $values .= ', '.$mdb2->quote($attrs['bcc_email']);
118     $values .= ', '.$mdb2->quote($attrs['allow_ip']);
119     $values .= ', '.$mdb2->quote($attrs['password_complexity']);
120     $values .= ', '.$mdb2->quote($attrs['plugins']);
121     $values .= ', '.$mdb2->quote($attrs['lock_spec']);
122     $values .= ', '.(int)$attrs['workday_minutes'];
123     $values .= ', '.$mdb2->quote($attrs['config']);
124     $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
125     $values .= ')';
126
127     $sql = 'insert into tt_groups '.$columns.$values;
128     $affected = $mdb2->exec($sql);
129     if (is_a($affected, 'PEAR_Error')) return false;
130
131     $subgroup_id = $mdb2->lastInsertID('tt_groups', 'id');
132
133     // Copy roles from the parent group to child group.
134     if (!ttRoleHelper::copyRolesToGroup($subgroup_id))
135       return false;
136     
137     return $subgroup_id;
138   }
139
140   // markGroupDeleted marks a group and everything in it as deleted.
141   // This function is called in context of a logged on user (global $user object).
142   // It uses current user attributes for access checks and in sql queries.
143   // Compare this with admin:
144   //   admin can delete any group.
145   //   user can delete only relevant groups and only if allowed.
146   static function markGroupDeleted($group_id) {
147     global $user;
148
149     $mdb2 = getConnection();
150     $org_id = $user->org_id;
151
152     // Security check.
153     if (!$user->isGroupValid($group_id))
154       return false;
155
156     // Keep the logic simple by returning false on first error.
157
158     // Obtain subgroups and call self recursively on them.
159     $subgroups = $user->getSubgroups($group_id);
160     foreach($subgroups as $subgroup) {
161       if (!ttGroupHelper::markGroupDeleted($subgroup['id']))
162         return false;
163     }
164
165     // Now do actual work with all entities.
166
167     // Delete group files.
168     ttGroupHelper::deleteGroupFiles($group_id);
169
170     // Some things cannot be marked deleted as we don't have the status field for them.
171     // Just delete such things (until we have a better way to deal with them).
172     $tables_to_delete_from = array(
173       'tt_config',
174       'tt_predefined_expenses',
175       'tt_client_project_binds',
176       'tt_project_task_binds'
177     );
178     foreach($tables_to_delete_from as $table) {
179       if (!ttGroupHelper::deleteGroupEntriesFromTable($group_id, $table))
180         return false;
181     }
182
183     // Now mark status deleted where we can.
184     // Note: we don't mark tt_log, tt_custom_field_lod, or tt_expense_items deleted here.
185     // Reasoning is:
186     //
187     // 1) Users may mark some of them deleted during their work.
188     // If we mark all of them deleted here, we can't recover nicely
189     // as we'll lose track of what was accidentally deleted by user.
190     //
191     // 2) DB maintenance script (Clean up DB from inactive groups) should
192     // get rid of these items permanently eventually.
193     $tables_to_mark_deleted_in = array(
194       'tt_cron',
195       'tt_fav_reports',
196       // 'tt_expense_items',
197       // 'tt_custom_field_log',
198       'tt_custom_field_options',
199       'tt_custom_fields',
200       // 'tt_log',
201       'tt_invoices',
202       'tt_user_project_binds',
203       'tt_users',
204       'tt_clients',
205       'tt_projects',
206       'tt_tasks',
207       'tt_roles'
208     );
209     foreach($tables_to_mark_deleted_in as $table) {
210       if (!ttGroupHelper::markGroupDeletedInTable($group_id, $table))
211         return false;
212     }
213
214     // Mark group deleted.
215     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
216     $sql = "update tt_groups set status = null $modified_part where id = $group_id and org_id = $org_id";
217     $affected = $mdb2->exec($sql);
218     if (is_a($affected, 'PEAR_Error')) return false;
219
220     return true;
221   }
222
223   // markGroupDeletedInTable is a generic helper function for markGroupDeleted.
224   // It updates ONE table by setting status to NULL for all records belonging to a group.
225   static function markGroupDeletedInTable($group_id, $table_name) {
226     global $user;
227     $mdb2 = getConnection();
228
229     // Add modified info to sql for some tables, depending on table name.
230     if ($table_name == 'tt_users') {
231       $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
232     }
233
234     $org_id = $user->org_id; // The only security measure we use here for match.
235     $sql = "update $table_name set status = null $modified_part where group_id = $group_id and org_id = $org_id";
236     $affected = $mdb2->exec($sql);
237     return (!is_a($affected, 'PEAR_Error'));
238   }
239
240   // deleteGroupEntriesFromTable is a generic helper function for markGroupDeleted.
241   // It deletes entries in ONE table belonging to a given group.
242   static function deleteGroupEntriesFromTable($group_id, $table_name) {
243     global $user;
244     $mdb2 = getConnection();
245
246     $org_id = $user->org_id; // The only security measure we use here for match.
247     $sql = "delete from $table_name where group_id = $group_id and org_id = $org_id";
248     $affected = $mdb2->exec($sql);
249     return (!is_a($affected, 'PEAR_Error'));
250   }
251
252   // getGroupAttrs obtains all group attributes.
253   static function getGroupAttrs($group_id) {
254     global $user;
255     $mdb2 = getConnection();
256
257     $sql =  "select * from tt_groups".
258             " where status = 1 and id = $group_id and org_id = $user->org_id";
259     $res = $mdb2->query($sql);
260     if (!is_a($res, 'PEAR_Error')) {
261       $val = $res->fetchRow();
262     }
263     return $val;
264   }
265
266   // getRoles obtains all active and inactive roles in current group.
267   static function getRoles() {
268     global $user;
269     $mdb2 = getConnection();
270
271     $group_id = $user->getGroup();
272     $org_id = $user->org_id;
273     $sql =  "select * from tt_roles".
274       " where group_id = $group_id and org_id = $org_id and status is not null";
275     $res = $mdb2->query($sql);
276     if (is_a($res, 'PEAR_Error')) return false;
277     while ($val = $res->fetchRow()) {
278       $roles[] = $val;
279     }
280     return $roles;
281   }
282
283   // The getActiveClients returns an array of active clients for a group.
284   static function getActiveClients($all_fields = false)
285   {
286     global $user;
287     $mdb2 = getConnection();
288
289     $group_id = $user->getGroup();
290     $org_id = $user->org_id;
291     if ($all_fields)
292       $sql = "select * from tt_clients where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
293     else
294       $sql = "select id, name from tt_clients where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
295
296     $res = $mdb2->query($sql);
297     $result = array();
298     if (!is_a($res, 'PEAR_Error')) {
299       while ($val = $res->fetchRow()) {
300         $result[] = $val;
301       }
302     }
303     return $result;
304   }
305
306   // The getInactiveClients returns an array of inactive clients for a group.
307   static function getInactiveClients($all_fields = false)
308   {
309     global $user;
310     $mdb2 = getConnection();
311
312     $group_id = $user->getGroup();
313     $org_id = $user->org_id;
314     if ($all_fields)
315       $sql = "select * from tt_clients where group_id = $group_id and org_id = $org_id and status = 0 order by upper(name)";
316     else
317       $sql = "select id, name from tt_clients where group_id = $group_id and org_id = $org_id and status = 0 order by upper(name)";
318
319     $res = $mdb2->query($sql);
320     $result = array();
321     if (!is_a($res, 'PEAR_Error')) {
322       while ($val = $res->fetchRow()) {
323         $result[] = $val;
324       }
325     }
326     return $result;
327   }
328
329   // getActiveProjects - returns an array of active projects for a group.
330   static function getActiveProjects()
331   {
332     global $user;
333     $mdb2 = getConnection();
334
335     $group_id = $user->getGroup();
336     $org_id = $user->org_id;
337
338     $sql = "select id, name, description, tasks from tt_projects".
339       " where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
340     $res = $mdb2->query($sql);
341     $result = array();
342     if (!is_a($res, 'PEAR_Error')) {
343       while ($val = $res->fetchRow()) {
344         $result[] = $val;
345       }
346     }
347     return $result;
348   }
349
350   // getActiveProjectsWithFiles - returns an array of active projects for a group
351   // with information whether they have attached files (has_files property).
352   // A separate fiunction from getActiveProjects because sql here is more complex.
353   static function getActiveProjectsWithFiles()
354   {
355     global $user;
356     $mdb2 = getConnection();
357
358     $group_id = $user->getGroup();
359     $org_id = $user->org_id;
360
361     $sql = "select p.id, p.name, p.description, if(Sub1.entity_id is null, 0, 1) as has_files from tt_projects p".
362       " left join (select distinct entity_id from tt_files".
363       " where entity_type = 'project' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
364       " on (p.id = Sub1.entity_id)".
365       " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by upper(p.name)";
366     $res = $mdb2->query($sql);
367     $result = array();
368     if (!is_a($res, 'PEAR_Error')) {
369       while ($val = $res->fetchRow()) {
370         $result[] = $val;
371       }
372     }
373     return $result;
374   }
375
376   // getInactiveProjects - returns an array of inactive projects for a group.
377   static function getInactiveProjects()
378   {
379     global $user;
380     $mdb2 = getConnection();
381
382     $group_id = $user->getGroup();
383     $org_id = $user->org_id;
384
385     $sql = "select id, name, description, tasks from tt_projects".
386       "  where group_id = $group_id and org_id = $org_id and status = 0 order by upper(name)";
387     $res = $mdb2->query($sql);
388     $result = array();
389     if (!is_a($res, 'PEAR_Error')) {
390       while ($val = $res->fetchRow()) {
391         $result[] = $val;
392       }
393     }
394     return $result;
395   }
396
397   // getInactiveProjectsWithFiles - returns an array of inactive projects for a group
398   // with information whether they have attached files (has_files property).
399   // A separate fiunction from getInactiveProjects because sql here is more complex.
400   static function getInactiveProjectsWithFiles()
401   {
402     global $user;
403     $mdb2 = getConnection();
404
405     $group_id = $user->getGroup();
406     $org_id = $user->org_id;
407
408     $sql = "select p.id, p.name, if(Sub1.entity_id is null, 0, 1) as has_files from tt_projects p".
409       " left join (select distinct entity_id from tt_files".
410       " where entity_type = 'project' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
411       " on (p.id = Sub1.entity_id)".
412       " where p.group_id = $group_id and p.org_id = $org_id and p.status = 0 order by upper(p.name)";
413     $res = $mdb2->query($sql);
414     $result = array();
415     if (!is_a($res, 'PEAR_Error')) {
416       while ($val = $res->fetchRow()) {
417         $result[] = $val;
418       }
419     }
420     return $result;
421   }
422
423   // getPredefinedExpenses - obtains predefined expenses for a group.
424   static function getPredefinedExpenses() {
425     global $user;
426     $mdb2 = getConnection();
427
428     $group_id = $user->getGroup();
429     $org_id = $user->org_id;
430
431     $result = array();
432     $sql = "select id, name, cost from tt_predefined_expenses".
433       " where group_id = $group_id and org_id = $org_id";
434     $res = $mdb2->query($sql);
435     $result = array();
436     if (!is_a($res, 'PEAR_Error')) {
437       $decimal_mark = $user->getDecimalMark();
438       $replaceDecimalMark = ('.' != $decimal_mark);
439
440       while ($val = $res->fetchRow()) {
441         if ($replaceDecimalMark)
442           $val['cost'] = str_replace('.', $decimal_mark, $val['cost']);
443         $result[] = $val;
444       }
445       return $result;
446     }
447     return false;
448   }
449
450   // The getActiveInvoices returns an array of active invoices for a group.
451   static function getActiveInvoices()
452   {
453     global $user;
454     $mdb2 = getConnection();
455
456     $group_id = $user->getGroup();
457     $org_id = $user->org_id;
458
459     $addPaidStatus = $user->isPluginEnabled('ps');
460     $result = array();
461
462     if ($user->isClient())
463       $client_part = "and i.client_id = $user->client_id";
464
465     $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i".
466       " left join tt_clients c on (c.id = i.client_id)".
467       " where i.status = 1 and i.group_id = $group_id and i.org_id = $org_id $client_part order by i.name";
468     $res = $mdb2->query($sql);
469     $result = array();
470     if (!is_a($res, 'PEAR_Error')) {
471       $dt = new DateAndTime(DB_DATEFORMAT);
472       while ($val = $res->fetchRow()) {
473         // Localize date.
474         $dt->parseVal($val['date']);
475         $val['date'] = $dt->toString($user->getDateFormat());
476         if ($addPaidStatus)
477           $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
478         $result[] = $val;
479       }
480     }
481     return $result;
482   }
483
484   // getNotifications - obtains notification descriptions for a group.
485   static function getNotifications() {
486     global $user;
487     $mdb2 = getConnection();
488
489     $group_id = $user->getGroup();
490     $org_id = $user->org_id;
491
492     $result = array();
493     $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c".
494       " left join tt_fav_reports fr on (fr.id = c.report_id)".
495       " where c.group_id = $group_id and c.org_id = $org_id and c.status = 1 and fr.status = 1";
496     $res = $mdb2->query($sql);
497     $result = array();
498     if (!is_a($res, 'PEAR_Error')) {
499       while ($val = $res->fetchRow()) {
500         $result[] = $val;
501       }
502       return $result;
503     }
504     return false;
505   }
506
507   // The getActiveUsers obtains all active users excluding clients in a given group.
508   static function getActiveUsers($options = null) {
509     global $user;
510     global $i18n;
511     $mdb2 = getConnection();
512
513     $group_id = $user->getGroup();
514     $org_id = $user->org_id;
515
516     $client_part = " and u.client_id is null";
517
518     if (isset($options['getAllFields']))
519       $sql = "select u.*, r.name as role_name, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $group_id and u.org_id = $org_id and u.status = 1 $client_part order by upper(u.name)";
520     else
521       $sql = "select u.id, u.name from tt_users u where u.group_id = $group_id and u.org_id = $org_id and u.status = 1 $client_part order by upper(u.name)";
522     $res = $mdb2->query($sql);
523     $user_list = array();
524     if (is_a($res, 'PEAR_Error'))
525       return false;
526     while ($val = $res->fetchRow()) {
527       // Localize top manager role name, as it is not localized in db.
528       if ($val['rank'] == 512)
529         $val['role_name'] = $i18n->get('role.top_manager.label');
530       $user_list[] = $val;
531     }
532
533     return $user_list;
534   }
535
536   // getActiveTasks - returns an array of active tasks for a group.
537   static function getActiveTasks()
538   {
539     global $user;
540     $mdb2 = getConnection();
541
542     $group_id = $user->getGroup();
543     $org_id = $user->org_id;
544
545     $sql = "select id, name, description from tt_tasks".
546       " where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
547     $res = $mdb2->query($sql);
548     $result = array();
549     if (!is_a($res, 'PEAR_Error')) {
550       while ($val = $res->fetchRow()) {
551         $result[] = $val;
552       }
553     }
554     return $result;
555   }
556
557   // getInactiveTasks - returns an array of inactive tasks for a group.
558   static function getInactiveTasks()
559   {
560     global $user;
561     $mdb2 = getConnection();
562
563     $group_id = $user->getGroup();
564     $org_id = $user->org_id;
565
566     $sql = "select id, name, description from tt_tasks".
567       " where group_id = $group_id and org_id = $org_id and status = 0 order by upper(name)";
568     $res = $mdb2->query($sql);
569     $result = array();
570     if (!is_a($res, 'PEAR_Error')) {
571       while ($val = $res->fetchRow()) {
572         $result[] = $val;
573       }
574     }
575     return $result;
576   }
577
578   // getActiveTemplates - returns an array of active templates for a group.
579   static function getActiveTemplates()
580   {
581     global $user;
582     $mdb2 = getConnection();
583
584     $group_id = $user->getGroup();
585     $org_id = $user->org_id;
586
587     $sql = "select id, name, description, content from tt_templates".
588       " where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
589     $res = $mdb2->query($sql);
590     $result = array();
591     if (!is_a($res, 'PEAR_Error')) {
592       while ($val = $res->fetchRow()) {
593         $result[] = $val;
594       }
595     }
596     return $result;
597   }
598
599   // getInactiveTemplates - returns an array of active templates for a group.
600   static function getInactiveTemplates()
601   {
602     global $user;
603     $mdb2 = getConnection();
604
605     $group_id = $user->getGroup();
606     $org_id = $user->org_id;
607
608     $sql = "select id, name, description from tt_templates".
609       " where group_id = $group_id and org_id = $org_id and status = 0 order by upper(name)";
610     $res = $mdb2->query($sql);
611     $result = array();
612     if (!is_a($res, 'PEAR_Error')) {
613       while ($val = $res->fetchRow()) {
614         $result[] = $val;
615       }
616     }
617     return $result;
618   }
619
620   // validateCheckboxGroupInput - validates user input in a group of checkboxes
621   // in context of a specific database table.
622   //
623   // We need to make sure that input is a set of unique positive integers, and is
624   // "relevant" to the current group (entities exists in table).
625   //
626   // It is a safeguard against manipulation of data in posts.
627   static function validateCheckboxGroupInput($input, $table) {
628     // Empty input is valid.
629     if (!$input) return true;
630
631     // Input containing duplicates is invalid.
632     if (count($input) !== count(array_unique($input))) return false;
633
634     // Input containing anything but positive integers is invalid.
635     foreach ($input as $single_selection) {
636       if (!is_numeric($single_selection) || $single_selection <= 0) return false;
637     }
638
639     global $user;
640     $mdb2 = getConnection();
641
642     $group_id = $user->getGroup();
643     $org_id = $user->org_id;
644
645     // Now check the table. It must contain all entities associated with current group and org.
646     $comma_separated = implode(',', $input);
647     $sql = "select count(*) as item_count from $table".
648       " where id in ($comma_separated) and group_id = $group_id and org_id = $org_id and status = 1";
649     $res = $mdb2->query($sql);
650     if (is_a($res, 'PEAR_Error')) return false;
651     $val = $res->fetchRow();
652     if (count($input) != $val['item_count'])
653       return false; // Number of entities in table is different.
654
655     return true; // All is good.
656   }
657
658   // The getUsers obtains all active and inactive (but not deleted) users in a group.
659   static function getUsers() {
660     global $user;
661     $mdb2 = getConnection();
662
663     $group_id = $user->getGroup();
664     $org_id = $user->org_id;
665
666     $sql = "select id, name from tt_users where group_id = $group_id and org_id = $org_id and (status = 1 or status = 0) order by upper(name)";
667     $res = $mdb2->query($sql);
668     $user_list = array();
669     if (is_a($res, 'PEAR_Error'))
670       return false;
671     while ($val = $res->fetchRow()) {
672       $user_list[] = $val;
673     }
674     return $user_list;
675   }
676
677   // The getUsersForClient obtains all active and inactive users in a group that are relevant to a client.
678   static function getUsersForClient() {
679     global $user;
680     $mdb2 = getConnection();
681
682     $group_id = $user->getGroup();
683     $org_id = $user->org_id;
684
685     $sql = "select u.id, u.name from tt_user_project_binds upb".
686       " inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)".
687       " inner join tt_users u on (u.id = upb.user_id and u.group_id = $group_id and u.org_id = $org_id)".
688       " where (u.status = 1 or u.status = 0)".
689       " group by u.id".
690       " order by upper(u.name)";
691     $res = $mdb2->query($sql);
692     $user_list = array();
693     if (is_a($res, 'PEAR_Error'))
694       return false;
695     while ($val = $res->fetchRow()) {
696       $user_list[] = $val;
697     }
698     return $user_list;
699   }
700
701   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
702   static function getRecentInvoices($client_id) {
703     global $user;
704     $mdb2 = getConnection();
705
706     $group_id = $user->getGroup();
707     $org_id = $user->org_id;
708
709     $sql = "select i.id, i.name from tt_invoices i".
710       " left join tt_clients c on (c.id = i.client_id)".
711       " where i.group_id = $group_id and i.org_id = $org_id and i.status = 1 and c.id = $client_id".
712       " order by i.id desc limit 3";
713     $res = $mdb2->query($sql);
714     $result = array();
715     if (!is_a($res, 'PEAR_Error')) {
716       $dt = new DateAndTime(DB_DATEFORMAT);
717       while ($val = $res->fetchRow()) {
718         $result[] = $val;
719       }
720     }
721     return $result;
722   }
723
724   // deleteGroupFiles deletes files attached to all entities in the entire group.
725   // Note that it is a permanent delete, not "mark deleted" by design.
726   static function deleteGroupFiles($group_id) {
727
728     global $user;
729     $org_id = $user->org_id;
730
731     // Delete all group files from the database.
732     $mdb2 = getConnection();
733     $sql = "delete from tt_files where org_id = $org_id and group_id = $group_id";
734     $affected = $mdb2->exec($sql);
735     if (is_a($affected, 'PEAR_Error'))
736       return false;
737
738     if ($affected == 0) return true; // Do not call file storage utility.
739
740     // Try to make a call to file storage facility.
741     if (!defined('FILE_STORAGE_URI')) return true; // Nothing to do.
742
743     $deletegroupfiles_uri = FILE_STORAGE_URI.'deletegroupfiles';
744
745     // Obtain site id.
746     $sql = "select param_value as site_id from tt_site_config where param_name = 'locker_id'";
747     $res = $mdb2->query($sql);
748     $val = $res->fetchRow();
749     $site_id = $val['site_id'];
750     if (!$site_id) return true; // Nothing to do.
751
752     // Obtain site key.
753     $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
754     $res = $mdb2->query($sql);
755     $val = $res->fetchRow();
756     $site_key = $val['site_key'];
757     if (!$site_key) return true; // Can't continue without site key.
758
759     // Obtain org key.
760     $sql = "select group_key as org_key from tt_groups where id = $org_id";
761     $res = $mdb2->query($sql);
762     $val = $res->fetchRow();
763     $org_key = $val['org_key'];
764     if (!$org_key) return true; // Can't continue without org key.
765
766     // Obtain group key.
767     $sql = "select group_key as group_key from tt_groups where id = $group_id";
768     $res = $mdb2->query($sql);
769     $val = $res->fetchRow();
770     $group_key = $val['group_key'];
771     if (!$group_key) return true; // Can't continue without group key.
772
773     $curl_fields = array('site_id' => $site_id,
774       'site_key' => $site_key,
775       'org_id' => $org_id,
776       'org_key' => $org_key,
777       'group_id' => $group_id,
778       'group_key' => $group_key);
779
780     // url-ify the data for the POST.
781     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
782     $fields_string = rtrim($fields_string, '&');
783
784     // Open connection.
785     $ch = curl_init();
786
787     // Set the url, number of POST vars, POST data.
788     curl_setopt($ch, CURLOPT_URL, $deletegroupfiles_uri);
789     curl_setopt($ch, CURLOPT_POST, true);
790     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
791     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
792
793     // Execute a post request.
794     $result = curl_exec($ch);
795
796     // Close connection.
797     curl_close($ch);
798
799     // Many things can go wrong with a remote call to file storage facility.
800     // By design, we ignore such errors.
801     return true;
802   }
803 }