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     fwrite($file, "<org>\n");
 
  56     // Use ttGroupExportHelper to export all groups.
 
  57     $groupExportHelper = new ttGroupExportHelper($user->group_id, $file, '  ');  // 2 spaces indentation for home group.
 
  58     $groupExportHelper->writeData();
 
  60     fwrite($file, "</org>\n");
 
  64       $this->fileName = tempnam($dirName, 'tt');
 
  65       $this->compress($tmp_file, $this->fileName);
 
  68       $this->fileName = $tmp_file;
 
  73   // getFileName - returns file name.
 
  74   function getFileName() {
 
  75     return $this->fileName;
 
  78   // compress - compresses the content of the $in file into $out file.
 
  79   function compress($in, $out) {
 
  80     // Initial checks of file names and permissions.
 
  81     if (!file_exists($in) || !is_readable ($in))
 
  83     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
 
  86     $in_file = fopen($in, 'rb');
 
  88     if (function_exists('bzopen')) {
 
  89       if (!$out_file = bzopen($out, 'w'))
 
  92       while (!feof ($in_file)) {
 
  93         $buffer = fread($in_file, 4096);
 
  94         bzwrite($out_file, $buffer, 4096);