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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 // Class ttOrgHelper contains helper functions that operate with organizations.
 
  30 // Organizations are collections of nested groups of users.
 
  33   // The getOrgs function returns an array of all active organizations on the server.
 
  34   static function getOrgs() {
 
  36     $mdb2 = getConnection();
 
  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);
 
  42     if (!is_a($res, 'PEAR_Error')) {
 
  43       while ($val = $res->fetchRow()) {
 
  44         $val['date'] = substr($val['created'], 0, 10); // Strip the time.
 
  52   // The getName function returns organization name (which is a name of its top group).
 
  53   static function getName($org_id) {
 
  54     $mdb2 = getConnection();
 
  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);
 
  59     if (!is_a($res, 'PEAR_Error')) {
 
  60       $val = $res->fetchRow();
 
  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();
 
  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'];
 
  81       return $inactive_orgs;
 
  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.
 
  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;
 
  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;
 
 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;
 
 108     return true; // Work in progress, currently not fully implemented.