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('ttUserHelper');
30 import('ttRoleHelper');
31 import('ttTaskHelper');
32 import('ttClientHelper');
33 import('ttInvoiceHelper');
34 import('ttTimeHelper');
35 import('ttCustomFieldHelper');
36 import('ttExpenseHelper');
37 import('ttFavReportHelper');
39 // ttOrgImportHelper class is used to import organization data from an XML file
40 // prepared by ttOrgExportHelper and consisting of nested groups with their info.
41 class ttOrgImportHelper {
42 var $errors = null; // Errors go here. Set in constructor by reference.
43 var $schema_version = null; // Database schema version from XML file we import from.
44 var $conflicting_logins = null; // A comma-separated list of logins we cannot import.
45 var $canImport = true; // False if we cannot import data due to a conflict such as login collision.
46 var $firstPass = true; // True during first pass through the file.
47 var $org_id = null; // Organization id (same as top group_id).
48 var $current_group_id = null; // Current group id during parsing.
49 var $current_parent_group_id = null; // Current parent group id during parsing.
50 var $top_role_id = 0; // Top role id.
52 // Entity maps for current group. They map XML ids with database ids.
53 var $currentGroupRoleMap = array();
54 var $currentGroupTaskMap = array();
55 var $currentGroupProjectMap = array();
56 var $currentGroupClientMap = array();
57 var $currentGroupUserMap = array();
58 var $currentGroupInvoiceMap = array();
59 var $currentGroupLogMap = array();
60 var $currentGroupCustomFieldMap = array();
61 var $currentGroupCustomFieldOptionMap = array();
62 var $currentGroupFavReportMap = array();
65 function __construct(&$errors) {
66 $this->errors = &$errors;
67 $this->top_role_id = ttRoleHelper::getRoleByRank(512, 0);
70 // startElement - callback handler for opening tags in XML.
71 function startElement($parser, $name, $attrs) {
74 // First pass through the file determines if we can import data.
75 // We require 2 things:
76 // 1) Database schema version must be set. This ensures we have a compatible file.
77 // 2) No login coillisions are allowed.
78 if ($this->firstPass) {
79 if ($name == 'ORG' && $this->canImport) {
80 if ($attrs['SCHEMA'] == null) {
81 // We need (database) schema attribute to be available for import to work.
82 // Old Time Tracker export files don't have this.
83 // Current import code does not work with old format because we had to
84 // restructure data in export files for subgroup support.
85 $this->canImport = false;
86 $this->errors->add($i18n->get('error.format'));
91 // In first pass we check user logins for potential collisions with existing.
92 if ($name == 'USER' && $this->canImport) {
93 $login = $attrs['LOGIN'];
94 if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) {
95 // We have a login collision. Append colliding login to a list of things we cannot import.
96 $this->conflicting_logins .= ($this->conflicting_logins ? ", $login" : $login);
97 // The above is printed in error message with all found colliding logins.
102 // Second pass processing. We import data here, one tag at a time.
103 if (!$this->firstPass && $this->canImport && $this->errors->no()) {
104 $mdb2 = getConnection();
106 // We are in second pass and can import data.
107 if ($name == 'GROUP') {
108 // Create a new group.
109 $this->current_group_id = $this->createGroup(array(
110 'parent_id' => $this->current_parent_group_id,
111 'org_id' => $this->org_id,
112 'name' => $attrs['NAME'],
113 'currency' => $attrs['CURRENCY'],
114 'decimal_mark' => $attrs['DECIMAL_MARK'],
115 'lang' => $attrs['LANG'],
116 'date_format' => $attrs['DATE_FORMAT'],
117 'time_format' => $attrs['TIME_FORMAT'],
118 'week_start' => $attrs['WEEK_START'],
119 'tracking_mode' => $attrs['TRACKING_MODE'],
120 'project_required' => $attrs['PROJECT_REQUIRED'],
121 'task_required' => $attrs['TASK_REQUIRED'],
122 'record_type' => $attrs['RECORD_TYPE'],
123 'bcc_email' => $attrs['BCC_EMAIL'],
124 'allow_ip' => $attrs['ALLOW_IP'],
125 'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
126 'plugins' => $attrs['PLUGINS'],
127 'lock_spec' => $attrs['LOCK_SPEC'],
128 'workday_minutes' => $attrs['WORKDAY_MINUTES'],
129 'custom_logo' => $attrs['CUSTOM_LOGO'],
130 'config' => $attrs['CONFIG']));
132 // Special handling for top group.
133 if (!$this->org_id && $this->current_group_id) {
134 $this->org_id = $this->current_group_id;
135 $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id";
136 $affected = $mdb2->exec($sql);
138 // Set parent group to create subgroups with this group as parent at next entry here.
139 $this->current_parent_group_id = $this->current_group_id;
143 if ($name == 'ROLES') {
144 // If we get here, we have to recycle $currentGroupRoleMap.
145 unset($this->currentGroupRoleMap);
146 $this->currentGroupRoleMap = array();
147 // Role map is reconstructed after processing <role> elements in XML. See below.
151 if ($name == 'ROLE') {
152 // We get here when processing <role> tags for the current group.
153 $role_id = ttRoleHelper::insert(array(
154 'group_id' => $this->current_group_id,
155 'org_id' => $this->org_id,
156 'name' => $attrs['NAME'],
157 'description' => $attrs['DESCRIPTION'],
158 'rank' => $attrs['RANK'],
159 'rights' => $attrs['RIGHTS'],
160 'status' => $attrs['STATUS']));
163 $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
164 } else $this->errors->add($i18n->get('error.db'));
168 if ($name == 'TASKS') {
169 // If we get here, we have to recycle $currentGroupTaskMap.
170 unset($this->currentGroupTaskMap);
171 $this->currentGroupTaskMap = array();
172 // Task map is reconstructed after processing <task> elements in XML. See below.
176 if ($name == 'TASK') {
177 // We get here when processing <task> tags for the current group.
178 $task_id = ttTaskHelper::insert(array(
179 'group_id' => $this->current_group_id,
180 'org_id' => $this->org_id,
181 'name' => $attrs['NAME'],
182 'description' => $attrs['DESCRIPTION'],
183 'status' => $attrs['STATUS']));
186 $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
187 } else $this->errors->add($i18n->get('error.db'));
191 if ($name == 'PROJECTS') {
192 // If we get here, we have to recycle $currentGroupProjectMap.
193 unset($this->currentGroupProjectMap);
194 $this->currentGroupProjectMap = array();
195 // Project map is reconstructed after processing <project> elements in XML. See below.
199 if ($name == 'PROJECT') {
200 // We get here when processing <project> tags for the current group.
202 // Prepare a list of task ids.
203 if ($attrs['TASKS']) {
204 $tasks = explode(',', $attrs['TASKS']);
205 foreach ($tasks as $id)
206 $mapped_tasks[] = $this->currentGroupTaskMap[$id];
209 $project_id = $this->insertProject(array(
210 'group_id' => $this->current_group_id,
211 'org_id' => $this->org_id,
212 'name' => $attrs['NAME'],
213 'description' => $attrs['DESCRIPTION'],
214 'tasks' => $mapped_tasks,
215 'status' => $attrs['STATUS']));
218 $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
219 } else $this->errors->add($i18n->get('error.db'));
223 if ($name == 'CLIENTS') {
224 // If we get here, we have to recycle $currentGroupClientMap.
225 unset($this->currentGroupClientMap);
226 $this->currentGroupClientMap = array();
227 // Client map is reconstructed after processing <client> elements in XML. See below.
231 if ($name == 'CLIENT') {
232 // We get here when processing <client> tags for the current group.
234 // Prepare a list of project ids.
235 if ($attrs['PROJECTS']) {
236 $projects = explode(',', $attrs['PROJECTS']);
237 foreach ($projects as $id)
238 $mapped_projects[] = $this->currentGroupProjectMap[$id];
241 $client_id = ttClientHelper::insert(array(
242 'group_id' => $this->current_group_id,
243 'org_id' => $this->org_id,
244 'name' => $attrs['NAME'],
245 'address' => $attrs['ADDRESS'],
246 'tax' => $attrs['TAX'],
247 'projects' => $mapped_projects,
248 'status' => $attrs['STATUS']));
251 $this->currentGroupClientMap[$attrs['ID']] = $client_id;
252 } else $this->errors->add($i18n->get('error.db'));
256 if ($name == 'USERS') {
257 // If we get here, we have to recycle $currentGroupUserMap.
258 unset($this->currentGroupUserMap);
259 $this->currentGroupUserMap = array();
260 // User map is reconstructed after processing <user> elements in XML. See below.
264 if ($name == 'USER') {
265 // We get here when processing <user> tags for the current group.
267 $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id : $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
269 $user_id = ttUserHelper::insert(array(
270 'group_id' => $this->current_group_id,
271 'org_id' => $this->org_id,
272 'role_id' => $role_id,
273 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
274 'name' => $attrs['NAME'],
275 'login' => $attrs['LOGIN'],
276 'password' => $attrs['PASSWORD'],
277 'rate' => $attrs['RATE'],
278 'email' => $attrs['EMAIL'],
279 'status' => $attrs['STATUS']), false);
282 $this->currentGroupUserMap[$attrs['ID']] = $user_id;
283 } else $this->errors->add($i18n->get('error.db'));
287 if ($name == 'USER_PROJECT_BIND') {
288 if (!ttUserHelper::insertBind(array(
289 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
290 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
291 'group_id' => $this->current_group_id,
292 'org_id' => $this->org_id,
293 'rate' => $attrs['RATE'],
294 'status' => $attrs['STATUS']))) {
295 $this->errors->add($i18n->get('error.db'));
300 if ($name == 'INVOICES') {
301 // If we get here, we have to recycle $currentGroupInvoiceMap.
302 unset($this->currentGroupInvoiceMap);
303 $this->currentGroupInvoiceMap = array();
304 // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
308 if ($name == 'INVOICE') {
309 // We get here when processing <invoice> tags for the current group.
310 $invoice_id = ttInvoiceHelper::insert(array(
311 'group_id' => $this->current_group_id,
312 'org_id' => $this->org_id,
313 'name' => $attrs['NAME'],
314 'date' => $attrs['DATE'],
315 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
316 'status' => $attrs['STATUS']));
319 $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
320 } else $this->errors->add($i18n->get('error.db'));
324 if ($name == 'LOG') {
325 // If we get here, we have to recycle $currentGroupLogMap.
326 unset($this->currentGroupLogMap);
327 $this->currentGroupLogMap = array();
328 // Log map is reconstructed after processing <log_item> elements in XML. See below.
332 if ($name == 'LOG_ITEM') {
333 // We get here when processing <log_item> tags for the current group.
334 $log_item_id = ttTimeHelper::insert(array(
335 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
336 'group_id' => $this->current_group_id,
337 'org_id' => $this->org_id,
338 'date' => $attrs['DATE'],
339 'start' => $attrs['START'],
340 'finish' => $attrs['FINISH'],
341 'duration' => $attrs['DURATION'],
342 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
343 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
344 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
345 'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
346 'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
347 'billable' => $attrs['BILLABLE'],
348 'paid' => $attrs['PAID'],
349 'status' => $attrs['STATUS']));
352 $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
353 } else $this->errors->add($i18n->get('error.db'));
357 if ($name == 'CUSTOM_FIELDS') {
358 // If we get here, we have to recycle $currentGroupCustomFieldMap.
359 unset($this->currentGroupCustomFieldMap);
360 $this->currentGroupCustomFieldMap = array();
361 // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
365 if ($name == 'CUSTOM_FIELD') {
366 // We get here when processing <custom_field> tags for the current group.
367 $custom_field_id = ttCustomFieldHelper::insertField(array(
368 'group_id' => $this->current_group_id,
369 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
370 'type' => $attrs['TYPE'],
371 'label' => $attrs['LABEL'],
372 'required' => $attrs['REQUIRED'],
373 'status' => $attrs['STATUS']));
374 if ($custom_field_id) {
376 $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
377 } else $this->errors->add($i18n->get('error.db'));
381 if ($name == 'CUSTOM_FIELD_OPTIONS') {
382 // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
383 unset($this->currentGroupCustomFieldOptionMap);
384 $this->currentGroupCustomFieldOptionMap = array();
385 // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
389 if ($name == 'CUSTOM_FIELD_OPTION') {
390 // We get here when processing <custom_field_option> tags for the current group.
391 $custom_field_option_id = ttCustomFieldHelper::insertOption(array(
392 // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
393 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
394 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
395 'value' => $attrs['VALUE']));
396 if ($custom_field_option_id) {
398 $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
399 } else $this->errors->add($i18n->get('error.db'));
403 if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
404 // We get here when processing <custom_field_log_entry> tags for the current group.
405 if (!ttCustomFieldHelper::insertLogEntry(array(
406 // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
407 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
408 'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
409 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
410 'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
411 'value' => $attrs['VALUE'],
412 'status' => $attrs['STATUS']))) {
413 $this->errors->add($i18n->get('error.db'));
418 if ($name == 'EXPENSE_ITEM') {
419 // We get here when processing <expense_item> tags for the current group.
420 $expense_item_id = $this->insertExpense(array(
421 'date' => $attrs['DATE'],
422 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
423 'group_id' => $this->current_group_id,
424 'org_id' => $this->org_id,
425 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
426 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
427 'name' => $attrs['NAME'],
428 'cost' => $attrs['COST'],
429 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
430 'paid' => $attrs['PAID'],
431 'status' => $attrs['STATUS']));
432 if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
436 if ($name == 'PREDEFINED_EXPENSE') {
437 if (!$this->insertPredefinedExpense(array(
438 'group_id' => $this->current_group_id,
439 'org_id' => $this->org_id,
440 'name' => $attrs['NAME'],
441 'cost' => $attrs['COST']))) {
442 $this->errors->add($i18n->get('error.db'));
447 if ($name == 'MONTHLY_QUOTA') {
448 if (!$this->insertMonthlyQuota(array(
449 'group_id' => $this->current_group_id,
450 'org_id' => $this->org_id,
451 'year' => $attrs['YEAR'],
452 'month' => $attrs['MONTH'],
453 'minutes' => $attrs['MINUTES']))) {
454 $this->errors->add($i18n->get('error.db'));
459 if ($name == 'FAV_REPORTS') {
460 // If we get here, we have to recycle $currentGroupFavReportMap.
461 unset($this->currentGroupFavReportMap);
462 $this->currentGroupFavReportMap = array();
463 // Favorite report map is reconstructed after processing <fav_report> elements in XML. See below.
467 if ($name == 'FAV_REPORT') {
469 if (strlen($attrs['USERS']) > 0) {
470 $arr = explode(',', $attrs['USERS']);
472 $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
474 $fav_report_id = $this->insertFavReport(array(
475 'name' => $attrs['NAME'],
476 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
477 'group_id' => $this->current_group_id,
478 'org_id' => $this->org_id,
479 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
480 'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
481 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
482 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
483 'billable' => $attrs['BILLABLE'],
484 'users' => $user_list,
485 'period' => $attrs['PERIOD'],
486 'from' => $attrs['PERIOD_START'],
487 'to' => $attrs['PERIOD_END'],
488 'chclient' => (int) $attrs['SHOW_CLIENT'],
489 'chinvoice' => (int) $attrs['SHOW_INVOICE'],
490 'chpaid' => (int) $attrs['SHOW_PAID'],
491 'chip' => (int) $attrs['SHOW_IP'],
492 'chproject' => (int) $attrs['SHOW_PROJECT'],
493 'chstart' => (int) $attrs['SHOW_START'],
494 'chduration' => (int) $attrs['SHOW_DURATION'],
495 'chcost' => (int) $attrs['SHOW_COST'],
496 'chtask' => (int) $attrs['SHOW_TASK'],
497 'chfinish' => (int) $attrs['SHOW_END'],
498 'chnote' => (int) $attrs['SHOW_NOTE'],
499 'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
500 'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
501 'group_by1' => $attrs['GROUP_BY1'],
502 'group_by2' => $attrs['GROUP_BY2'],
503 'group_by3' => $attrs['GROUP_BY3'],
504 'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
505 if ($fav_report_id) {
507 $this->currentGroupFavReportMap[$attrs['ID']] = $fav_report_id;
508 } else $this->errors->add($i18n->get('error.db'));
512 if ($name == 'NOTIFICATION') {
513 if (!$this->insertNotification(array(
514 'group_id' => $this->current_group_id,
515 'org_id' => $this->org_id,
516 'cron_spec' => $attrs['CRON_SPEC'],
517 'last' => $attrs['LAST'],
518 'next' => $attrs['NEXT'],
519 'report_id' => $this->currentGroupFavReportMap[$attrs['REPORT_ID']],
520 'email' => $attrs['EMAIL'],
521 'cc' => $attrs['CC'],
522 'subject' => $attrs['SUBJECT'],
523 'report_condition' => $attrs['REPORT_CONDITION'],
524 'status' => $attrs['STATUS']))) {
525 $this->errors->add($i18n->get('error.db'));
532 // importXml - uncompresses the file, reads and parses its content. During parsing,
533 // startElement, endElement, and dataElement functions are called as many times as necessary.
534 // Actual import occurs in the endElement handler.
535 function importXml() {
538 // Do we have a compressed file?
540 $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
541 if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
545 // Create a temporary file.
546 $dirName = dirname(TEMPLATE_DIR . '_c/.');
547 $filename = tempnam($dirName, 'import_');
549 // If the file is compressed - uncompress it.
551 if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
552 $this->errors->add($i18n->get('error.sys'));
555 unlink($_FILES['xmlfile']['tmp_name']);
557 if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
558 $this->errors->add($i18n->get('error.upload'));
563 // Initialize XML parser.
564 $parser = xml_parser_create();
565 xml_set_object($parser, $this);
566 xml_set_element_handler($parser, 'startElement', false);
568 // We need to parse the file 2 times:
569 // 1) First pass: determine if import is possible.
570 // 2) Second pass: import data, one tag at a time.
572 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
573 $file = fopen($filename, 'r');
574 while (($data = fread($file, 4096)) && $this->errors->no()) {
575 if (!xml_parse($parser, $data, feof($file))) {
576 $this->errors->add(sprintf($i18n->get('error.xml'),
577 xml_get_current_line_number($parser),
578 xml_error_string(xml_get_error_code($parser))));
581 if ($this->conflicting_logins) {
582 $this->canImport = false;
583 $this->errors->add($i18n->get('error.user_exists'));
584 $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins));
587 $this->firstPass = false; // We are done with 1st pass.
588 xml_parser_free($parser);
589 if ($file) fclose($file);
590 if (!$this->canImport) {
594 if ($this->errors->yes()) return; // Exit if we have errors.
596 // Now we can do a second pass, where real work is done.
597 $parser = xml_parser_create();
598 xml_set_object($parser, $this);
599 xml_set_element_handler($parser, 'startElement', false);
601 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
602 $file = fopen($filename, 'r');
603 while (($data = fread($file, 4096)) && $this->errors->no()) {
604 if (!xml_parse($parser, $data, feof($file))) {
605 $this->errors->add(sprintf($i18n->get('error.xml'),
606 xml_get_current_line_number($parser),
607 xml_error_string(xml_get_error_code($parser))));
610 xml_parser_free($parser);
611 if ($file) fclose($file);
615 // uncompress - uncompresses the content of the $in file into the $out file.
616 function uncompress($in, $out) {
617 // Do we have the uncompress function?
618 if (!function_exists('bzopen'))
621 // Initial checks of file names and permissions.
622 if (!file_exists($in) || !is_readable ($in))
624 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
627 if (!$out_file = fopen($out, 'wb'))
629 if (!$in_file = bzopen ($in, 'r'))
632 while (!feof($in_file)) {
633 $buffer = bzread($in_file, 4096);
634 fwrite($out_file, $buffer, 4096);
641 // createGroup function creates a new group.
642 private function createGroup($fields) {
645 $mdb2 = getConnection();
647 $columns = '(parent_id, org_id, name, currency, decimal_mark, lang, date_format, time_format'.
648 ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
649 ', allow_ip, password_complexity, plugins, lock_spec'.
650 ', workday_minutes, config, created, created_ip, created_by)';
652 $values = ' values (';
653 $values .= $mdb2->quote($fields['parent_id']);
654 $values .= ', '.$mdb2->quote($fields['org_id']);
655 $values .= ', '.$mdb2->quote(trim($fields['name']));
656 $values .= ', '.$mdb2->quote(trim($fields['currency']));
657 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
658 $values .= ', '.$mdb2->quote($fields['lang']);
659 $values .= ', '.$mdb2->quote($fields['date_format']);
660 $values .= ', '.$mdb2->quote($fields['time_format']);
661 $values .= ', '.(int)$fields['week_start'];
662 $values .= ', '.(int)$fields['tracking_mode'];
663 $values .= ', '.(int)$fields['project_required'];
664 $values .= ', '.(int)$fields['task_required'];
665 $values .= ', '.(int)$fields['record_type'];
666 $values .= ', '.$mdb2->quote($fields['bcc_email']);
667 $values .= ', '.$mdb2->quote($fields['allow_ip']);
668 $values .= ', '.$mdb2->quote($fields['password_complexity']);
669 $values .= ', '.$mdb2->quote($fields['plugins']);
670 $values .= ', '.$mdb2->quote($fields['lock_spec']);
671 $values .= ', '.(int)$fields['workday_minutes'];
672 $values .= ', '.$mdb2->quote($fields['config']);
673 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
676 $sql = 'insert into tt_groups '.$columns.$values;
677 $affected = $mdb2->exec($sql);
678 if (is_a($affected, 'PEAR_Error')) {
679 $this->errors->add($i18n->get('error.db'));
683 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
687 // insertMonthlyQuota - a helper function to insert a monthly quota.
688 private function insertMonthlyQuota($fields) {
689 $mdb2 = getConnection();
690 $group_id = (int) $fields['group_id'];
691 $org_id = (int) $fields['org_id'];
692 $year = (int) $fields['year'];
693 $month = (int) $fields['month'];
694 $minutes = (int) $fields['minutes'];
696 $sql = "INSERT INTO tt_monthly_quotas (group_id, org_id, year, month, minutes)".
697 " values ($group_id, $org_id, $year, $month, $minutes)";
698 $affected = $mdb2->exec($sql);
699 return (!is_a($affected, 'PEAR_Error'));
702 // insertPredefinedExpense - a helper function to insert a predefined expense.
703 private function insertPredefinedExpense($fields) {
704 $mdb2 = getConnection();
705 $group_id = (int) $fields['group_id'];
706 $org_id = (int) $fields['org_id'];
707 $name = $mdb2->quote($fields['name']);
708 $cost = $mdb2->quote($fields['cost']);
710 $sql = "INSERT INTO tt_predefined_expenses (group_id, org_id, name, cost)".
711 " values ($group_id, $org_id, $name, $cost)";
712 $affected = $mdb2->exec($sql);
713 return (!is_a($affected, 'PEAR_Error'));
716 // insertExpense - a helper function to insert an expense item.
717 private function insertExpense($fields) {
719 $mdb2 = getConnection();
721 $group_id = (int) $fields['group_id'];
722 $org_id = (int) $fields['org_id'];
723 $date = $fields['date'];
724 $user_id = (int) $fields['user_id'];
725 $client_id = $fields['client_id'];
726 $project_id = $fields['project_id'];
727 $name = $fields['name'];
728 $cost = str_replace(',', '.', $fields['cost']);
729 $invoice_id = $fields['invoice_id'];
730 $status = $fields['status'];
731 $paid = (int) $fields['paid'];
732 $created = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
734 $sql = "insert into tt_expense_items".
735 " (date, user_id, group_id, org_id, client_id, project_id, name, cost, invoice_id, paid, created, created_ip, created_by, status)".
736 " values (".$mdb2->quote($date).", $user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
737 ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).", $paid $created, ".$mdb2->quote($status).")";
738 $affected = $mdb2->exec($sql);
739 return (!is_a($affected, 'PEAR_Error'));
742 // insertProject - a helper function to insert a project as well as project to task binds.
743 private function insertProject($fields)
745 $mdb2 = getConnection();
747 $group_id = (int) $fields['group_id'];
748 $org_id = (int) $fields['org_id'];
750 $name = $fields['name'];
751 $description = $fields['description'];
752 $tasks = $fields['tasks'];
753 $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
754 $status = $fields['status'];
756 $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
757 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
758 $affected = $mdb2->exec($sql);
759 if (is_a($affected, 'PEAR_Error'))
763 $sql = "select last_insert_id() as last_insert_id";
764 $res = $mdb2->query($sql);
765 $val = $res->fetchRow();
766 $last_id = $val['last_insert_id'];
768 // Insert binds into tt_project_task_binds table.
769 if (is_array($tasks)) {
770 foreach ($tasks as $task_id) {
771 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
772 " values($last_id, $task_id, $group_id, $org_id)";
773 $affected = $mdb2->exec($sql);
774 if (is_a($affected, 'PEAR_Error'))
782 // insertFavReport - inserts a favorite report in database.
783 private function insertFavReport($fields) {
784 $mdb2 = getConnection();
786 $group_id = (int) $fields['group_id'];
787 $org_id = (int) $fields['org_id'];
789 $sql = "insert into tt_fav_reports".
790 " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
791 " billable, invoice, paid_status, users, period, period_start, period_end,".
792 " show_client, show_invoice, show_paid, show_ip,".
793 " show_project, show_start, show_duration, show_cost,".
794 " show_task, show_end, show_note, show_custom_field_1, show_work_units,".
795 " group_by1, group_by2, group_by3, show_totals_only)".
797 $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
798 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
799 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
800 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['invoice']).", ".
801 $mdb2->quote($fields['paid_status']).", ".
802 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
803 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
804 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
805 $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
806 $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
807 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
808 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
809 $affected = $mdb2->exec($sql);
810 if (is_a($affected, 'PEAR_Error'))
813 $sql = "select last_insert_id() as last_id";
814 $res = $mdb2->query($sql);
815 if (is_a($res, 'PEAR_Error'))
818 $val = $res->fetchRow();
819 return $val['last_id'];
822 // insertNotification function inserts a new notification into database.
823 static function insertNotification($fields)
825 $mdb2 = getConnection();
827 $group_id = (int) $fields['group_id'];
828 $org_id = (int) $fields['org_id'];
829 $cron_spec = $fields['cron_spec'];
830 $last = (int) $fields['last'];
831 $next = (int) $fields['next'];
832 $report_id = (int) $fields['report_id'];
833 $email = $fields['email'];
835 $subject = $fields['subject'];
836 $report_condition = $fields['report_condition'];
837 $status = $fields['status'];
839 $sql = "insert into tt_cron".
840 " (group_id, org_id, cron_spec, last, next, report_id, email, cc, subject, report_condition, status)".
841 " values ($group_id, $org_id, ".$mdb2->quote($cron_spec).", $last, $next, $report_id, ".$mdb2->quote($email).", ".$mdb2->quote($cc).", ".$mdb2->quote($subject).", ".$mdb2->quote($report_condition).", ".$mdb2->quote($status).")";
842 $affected = $mdb2->exec($sql);
843 return (!is_a($affected, 'PEAR_Error'));