A bit more progress on new export.
[timetracker.git] / WEB-INF / lib / ttGroupExportHelper.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 // ttGroupExportHelper - this class is used to write data for a single group
30 // to a file. When group contains other groups, it reuses itself recursively.
31 //
32 // Currently, it is work in progress.
33 // When done, it should handle export of organizations containing multiple groups.
34 class ttGroupExportHelper {
35
36   var $group_id = null;     // Group we are exporting.
37   var $file     = null;     // File to write to.
38   var $indentation = null;  // A string consisting of a number of spaces.
39   var $subgroups = array(); // Immediate subgroups.
40
41   // Constructor.
42   function __construct($group_id, $file, $indentation) {
43     global $user;
44
45     $this->group_id = $group_id;
46     $this->file = $file;
47     $this->indentation = $indentation;
48
49     // Build a list of subgroups.
50     $mdb2 = getConnection();
51     $sql =  "select id from tt_groups".
52             " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id order by id desc";
53     $res = $mdb2->query($sql);
54     if (!is_a($res, 'PEAR_Error')) {
55       while ($val = $res->fetchRow()) {
56         $this->subgroups[] = $val;
57       }
58     }
59   }
60
61   // writeData writes group data into file.
62   function writeData() {
63     // TODO: write code here.
64
65     // Write group info. Something dummy for now to test...
66     fwrite($this->file, $this->indentation."<group>\n");
67
68     // Call itself recursively for all subgroups.
69     foreach ($this->subgroups as $subgroup) {
70       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
71       $subgroup_helper->writeData();
72     }
73
74     fwrite($this->file, $this->indentation."</group>\n");
75   }
76 }