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 import('ttTeamHelper');
30 import('ttTimeHelper');
31 import('ttGroupExportHelper');
33 // ttOrgExportHelper - this class is a future replacement for ttExportHelper.
34 // Currently, it is work in progress.
35 // When done, it should handle export of organizations containing multiple groups.
36 class ttOrgExportHelper {
38 var $fileName = null; // Name of file with data.
40 // createDataFile creates a file with all data for the entire organization.
41 function createDataFile($compress = false) {
44 // Create a temporary file.
45 $dirName = dirname(TEMPLATE_DIR . '_c/.');
46 $tmp_file = tempnam($dirName, 'tt');
48 // Open the file for writing.
49 $file = fopen($tmp_file, 'wb');
50 if (!$file) return false;
52 // Write XML to the file.
53 fwrite($file, "<?xml version=\"1.0\"?>\n");
54 $org_part = "<org schema=\"".$this->getVersion()."\">\n";
55 fwrite($file, $org_part);
57 // Use ttGroupExportHelper to export all groups.
58 $groupExportHelper = new ttGroupExportHelper($user->group_id, $file, ' '); // 2 spaces indentation for home group.
59 $groupExportHelper->writeData();
61 fwrite($file, "</org>\n");
65 $this->fileName = tempnam($dirName, 'tt');
66 $this->compress($tmp_file, $this->fileName);
69 $this->fileName = $tmp_file;
74 // getFileName - returns file name.
75 function getFileName() {
76 return $this->fileName;
79 // compress - compresses the content of the $in file into $out file.
80 function compress($in, $out) {
81 // Initial checks of file names and permissions.
82 if (!file_exists($in) || !is_readable ($in))
84 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
87 $in_file = fopen($in, 'rb');
89 if (function_exists('bzopen')) {
90 if (!$out_file = bzopen($out, 'w'))
93 while (!feof ($in_file)) {
94 $buffer = fread($in_file, 4096);
95 bzwrite($out_file, $buffer, 4096);
103 private function getVersion() {
104 $mdb2 = getConnection();
105 $sql = "select param_value from tt_site_config where param_name = 'version_db'";
106 $res = $mdb2->query($sql);
107 if (!is_a($res, 'PEAR_Error')) {
108 $val = $res->fetchRow();
109 return $val['param_value'];