Another small iteration for 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";
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   // getGroupData obtains group attributes for export.
62   function getGroupData() {
63     global $user;
64
65     $mdb2 = getConnection();
66     $sql =  "select name, currency, lang from tt_groups".
67             " where status = 1 and id = $this->group_id and org_id = $user->org_id";
68     $res = $mdb2->query($sql);
69     if (!is_a($res, 'PEAR_Error')) {
70       $val = $res->fetchRow();
71     }
72     return $val;
73   }
74
75   // writeData writes group data into file.
76   function writeData() {
77
78     // Write group info.
79     $group = $this->getGroupData();
80     $group_part = "<group name=\"".htmlentities($group['name'])."\"";
81     $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
82     $group_part .= " lang=\"".$group['lang']."\"";
83     // TODO: add other group attributes here.
84     $group_part .= ">\n";
85
86     // Write group info.
87     fwrite($this->file, $this->indentation.$group_part);
88
89     // Call self recursively for all subgroups.
90     foreach ($this->subgroups as $subgroup) {
91       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
92       $subgroup_helper->writeData();
93     }
94
95     fwrite($this->file, $this->indentation."</group>\n");
96   }
97 }