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 // ttOrgImportHelper class is used to import organization data from an XML file
30 // prepared by ttOrgExportHelper and consisting of nested groups with their info.
31 class ttOrgImportHelper {
32 var $errors = null; // Errors go here. Set in constructor by reference.
33 var $schema_version = null; // Database schema version from XML file we import from.
34 var $num_users = 0; // A number of active and inactive users we are importing.
35 var $conflicting_logins = null; // A comma-separated list of logins we cannot import.
36 var $canImport = true; // False if we cannot import data due to a conflict such as login collision.
37 var $firstPass = true; // True during first pass through the file.
38 var $org_id = null; // Organization id (same as top group_id).
39 var $current_group_id = null; // Current group id during parsing.
40 var $parents = array(); // A stack of parent group ids for current group all the way to the root including self.
41 var $top_role_id = 0; // Top role id.
43 // Entity maps for current group. They map XML ids with database ids.
44 var $currentGroupRoleMap = array();
45 var $currentGroupTaskMap = array();
46 var $currentGroupProjectMap = array();
47 var $currentGroupClientMap = array();
48 var $currentGroupUserMap = array();
49 var $currentGroupTimesheetMap = array();
50 var $currentGroupInvoiceMap = array();
51 var $currentGroupLogMap = array();
52 var $currentGroupCustomFieldMap = array();
53 var $currentGroupCustomFieldOptionMap = array();
54 var $currentGroupFavReportMap = array();
57 function __construct(&$errors) {
58 $this->errors = &$errors;
59 $this->top_role_id = $this->getTopRole();
62 // startElement - callback handler for opening tags in XML.
63 function startElement($parser, $name, $attrs) {
66 // First pass through the file determines if we can import data.
67 // We require 2 things:
68 // 1) Database schema version must be set. This ensures we have a compatible file.
69 // 2) No login coillisions are allowed.
70 if ($this->firstPass) {
71 if ($name == 'ORG' && $this->canImport) {
72 if ($attrs['SCHEMA'] == null) {
73 // We need (database) schema attribute to be available for import to work.
74 // Old Time Tracker export files don't have this.
75 // Current import code does not work with old format because we had to
76 // restructure data in export files for subgroup support.
77 $this->canImport = false;
78 $this->errors->add($i18n->get('error.format'));
83 // In first pass we check user logins for potential collisions with existing.
84 if ($name == 'USER' && $this->canImport) {
85 $login = $attrs['LOGIN'];
86 if ('' != $attrs['STATUS']) $this->num_users++;
87 if ('' != $attrs['STATUS'] && $this->loginExists($login)) {
88 // We have a login collision. Append colliding login to a list of things we cannot import.
89 $this->conflicting_logins .= ($this->conflicting_logins ? ", $login" : $login);
90 // The above is printed in error message with all found colliding logins.
95 // Second pass processing. We import data here, one tag at a time.
96 if (!$this->firstPass && $this->canImport && $this->errors->no()) {
97 $mdb2 = getConnection();
99 // We are in second pass and can import data.
100 if ($name == 'GROUP') {
101 // Create a new group.
102 $this->current_group_id = $this->createGroup(array(
103 'parent_id' => $this->current_group_id, // Note: after insert current_group_id changes.
104 'org_id' => $this->org_id,
105 'group_key' => $attrs['GROUP_KEY'],
106 'name' => $attrs['NAME'],
107 'description' => $attrs['DESCRIPTION'],
108 'currency' => $attrs['CURRENCY'],
109 'decimal_mark' => $attrs['DECIMAL_MARK'],
110 'lang' => $attrs['LANG'],
111 'date_format' => $attrs['DATE_FORMAT'],
112 'time_format' => $attrs['TIME_FORMAT'],
113 'week_start' => $attrs['WEEK_START'],
114 'tracking_mode' => $attrs['TRACKING_MODE'],
115 'project_required' => $attrs['PROJECT_REQUIRED'],
116 'task_required' => $attrs['TASK_REQUIRED'],
117 'record_type' => $attrs['RECORD_TYPE'],
118 'bcc_email' => $attrs['BCC_EMAIL'],
119 'allow_ip' => $attrs['ALLOW_IP'],
120 'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
121 'plugins' => $attrs['PLUGINS'],
122 'lock_spec' => $attrs['LOCK_SPEC'],
123 'workday_minutes' => $attrs['WORKDAY_MINUTES'],
124 'custom_logo' => $attrs['CUSTOM_LOGO'],
125 'config' => $attrs['CONFIG']));
127 // Special handling for top group.
128 if (!$this->org_id && $this->current_group_id) {
129 $this->org_id = $this->current_group_id;
130 $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id";
131 $affected = $mdb2->exec($sql);
133 // Add self to parent stack.
134 array_push($this->parents, $this->current_group_id);
136 // Recycle all maps as we are starting to work on new group.
137 // Note that for this to work properly all nested groups must be last entries in xml for each group.
138 unset($this->currentGroupRoleMap); $this->currentGroupRoleMap = array();
139 unset($this->currentGroupTaskMap); $this->currentGroupTaskMap = array();
140 unset($this->currentGroupProjectMap); $this->currentGroupProjectMap = array();
141 unset($this->currentGroupClientMap); $this->currentGroupClientMap = array();
142 unset($this->currentGroupUserMap); $this->currentGroupUserMap = array();
143 unset($this->currentGroupTimesheetMap); $this->currentGroupTimesheetMap = array();
144 unset($this->currentGroupInvoiceMap); $this->currentGroupInvoiceMap = array();
145 unset($this->currentGroupLogMap); $this->currentGroupLogMap = array();
146 unset($this->currentGroupCustomFieldMap); $this->currentGroupCustomFieldMap = array();
147 unset($this->currentGroupCustomFieldOptionMap); $this->currentGroupCustomFieldOptionMap = array();
148 unset($this->currentGroupFavReportMap); $this->currentGroupCustomFavReportMap = array();
152 if ($name == 'ROLE') {
153 // We get here when processing <role> tags for the current group.
154 $role_id = $this->insertRole(array(
155 'group_id' => $this->current_group_id,
156 'org_id' => $this->org_id,
157 'name' => $attrs['NAME'],
158 'description' => $attrs['DESCRIPTION'],
159 'rank' => $attrs['RANK'],
160 'rights' => $attrs['RIGHTS'],
161 'status' => $attrs['STATUS']));
164 $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
166 $this->errors->add($i18n->get('error.db'));
171 if ($name == 'TASK') {
172 // We get here when processing <task> tags for the current group.
173 $task_id = $this->insertTask(array(
174 'group_id' => $this->current_group_id,
175 'org_id' => $this->org_id,
176 'name' => $attrs['NAME'],
177 'description' => $attrs['DESCRIPTION'],
178 'status' => $attrs['STATUS']));
181 $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
183 $this->errors->add($i18n->get('error.db'));
188 if ($name == 'PROJECT') {
189 // We get here when processing <project> tags for the current group.
191 // Prepare a list of task ids.
192 if ($attrs['TASKS']) {
193 $tasks = explode(',', $attrs['TASKS']);
194 foreach ($tasks as $id)
195 $mapped_tasks[] = $this->currentGroupTaskMap[$id];
198 $project_id = $this->insertProject(array(
199 'group_id' => $this->current_group_id,
200 'org_id' => $this->org_id,
201 'name' => $attrs['NAME'],
202 'description' => $attrs['DESCRIPTION'],
203 'tasks' => $mapped_tasks,
204 'status' => $attrs['STATUS']));
207 $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
209 $this->errors->add($i18n->get('error.db'));
214 if ($name == 'CLIENT') {
215 // We get here when processing <client> tags for the current group.
217 // Prepare a list of project ids.
218 if ($attrs['PROJECTS']) {
219 $projects = explode(',', $attrs['PROJECTS']);
220 foreach ($projects as $id)
221 $mapped_projects[] = $this->currentGroupProjectMap[$id];
224 $client_id = $this->insertClient(array(
225 'group_id' => $this->current_group_id,
226 'org_id' => $this->org_id,
227 'name' => $attrs['NAME'],
228 'address' => $attrs['ADDRESS'],
229 'tax' => $attrs['TAX'],
230 'projects' => $mapped_projects,
231 'status' => $attrs['STATUS']));
234 $this->currentGroupClientMap[$attrs['ID']] = $client_id;
236 $this->errors->add($i18n->get('error.db'));
241 if ($name == 'USER') {
242 // We get here when processing <user> tags for the current group.
244 $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id : $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
246 $user_id = $this->insertUser(array(
247 'group_id' => $this->current_group_id,
248 'org_id' => $this->org_id,
249 'role_id' => $role_id,
250 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
251 'name' => $attrs['NAME'],
252 'login' => $attrs['LOGIN'],
253 'password' => $attrs['PASSWORD'],
254 'rate' => $attrs['RATE'],
255 'quota_percent' => $attrs['QUOTA_PERCENT'],
256 'email' => $attrs['EMAIL'],
257 'status' => $attrs['STATUS']), false);
260 $this->currentGroupUserMap[$attrs['ID']] = $user_id;
262 $this->errors->add($i18n->get('error.db'));
267 if ($name == 'USER_PROJECT_BIND') {
268 if (!$this->insertUserProjectBind(array(
269 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
270 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
271 'group_id' => $this->current_group_id,
272 'org_id' => $this->org_id,
273 'rate' => $attrs['RATE'],
274 'status' => $attrs['STATUS']))) {
275 $this->errors->add($i18n->get('error.db'));
280 if ($name == 'TIMESHEET') {
281 // We get here when processing <timesheet> tags for the current group.
282 $timesheet_id = $this->insertTimesheet(array(
283 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
284 'group_id' => $this->current_group_id,
285 'org_id' => $this->org_id,
286 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
287 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
288 'name' => $attrs['NAME'],
289 'comment' => $attrs['COMMENT'],
290 'start_date' => $attrs['START_DATE'],
291 'end_date' => $attrs['END_DATE'],
292 'submit_status' => $attrs['SUBMIT_STATUS'],
293 'approve_status' => $attrs['APPROVE_STATUS'],
294 'approve_comment' => $attrs['APPROVE_COMMENT'],
295 'status' => $attrs['STATUS']));
298 $this->currentGroupTimesheetMap[$attrs['ID']] = $timesheet_id;
300 $this->errors->add($i18n->get('error.db'));
305 if ($name == 'INVOICE') {
306 // We get here when processing <invoice> tags for the current group.
307 $invoice_id = $this->insertInvoice(array(
308 'group_id' => $this->current_group_id,
309 'org_id' => $this->org_id,
310 'name' => $attrs['NAME'],
311 'date' => $attrs['DATE'],
312 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
313 'status' => $attrs['STATUS']));
316 $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
318 $this->errors->add($i18n->get('error.db'));
323 if ($name == 'LOG_ITEM') {
324 // We get here when processing <log_item> tags for the current group.
325 $log_item_id = $this->insertLogEntry(array(
326 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
327 'group_id' => $this->current_group_id,
328 'org_id' => $this->org_id,
329 'date' => $attrs['DATE'],
330 'start' => $attrs['START'],
331 'finish' => $attrs['FINISH'],
332 'duration' => $attrs['DURATION'],
333 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
334 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
335 'task_id' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
336 'timesheet_id' => $this->currentGroupTimesheetMap[$attrs['TIMESHEET_ID']],
337 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
338 'comment' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
339 'billable' => $attrs['BILLABLE'],
340 'approved' => $attrs['APPROVED'],
341 'paid' => $attrs['PAID'],
342 'status' => $attrs['STATUS']));
345 $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
346 } else $this->errors->add($i18n->get('error.db'));
350 if ($name == 'CUSTOM_FIELD') {
351 // We get here when processing <custom_field> tags for the current group.
352 $custom_field_id = $this->insertCustomField(array(
353 'group_id' => $this->current_group_id,
354 'org_id' => $this->org_id,
355 'entity_type' => $attrs['ENTITY_TYPE'],
356 'type' => $attrs['TYPE'],
357 'label' => $attrs['LABEL'],
358 'required' => $attrs['REQUIRED'],
359 'status' => $attrs['STATUS']));
360 if ($custom_field_id) {
362 $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
363 } else $this->errors->add($i18n->get('error.db'));
367 if ($name == 'CUSTOM_FIELD_OPTION') {
368 // We get here when processing <custom_field_option> tags for the current group.
369 $custom_field_option_id = $this->insertCustomFieldOption(array(
370 'group_id' => $this->current_group_id,
371 'org_id' => $this->org_id,
372 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
373 'value' => $attrs['VALUE']));
374 if ($custom_field_option_id) {
376 $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
377 } else $this->errors->add($i18n->get('error.db'));
381 if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
382 // We get here when processing <custom_field_log_entry> tags for the current group.
383 if (!$this->insertCustomFieldLogEntry(array(
384 'group_id' => $this->current_group_id,
385 'org_id' => $this->org_id,
386 'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
387 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
388 'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
389 'value' => $attrs['VALUE'],
390 'status' => $attrs['STATUS']))) {
391 $this->errors->add($i18n->get('error.db'));
396 if ($name == 'EXPENSE_ITEM') {
397 // We get here when processing <expense_item> tags for the current group.
398 $expense_item_id = $this->insertExpense(array(
399 'date' => $attrs['DATE'],
400 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
401 'group_id' => $this->current_group_id,
402 'org_id' => $this->org_id,
403 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
404 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
405 'timesheet_id' => $this->currentGroupTimesheetMap[$attrs['TIMESHEET_ID']],
406 'name' => $attrs['NAME'],
407 'cost' => $attrs['COST'],
408 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
409 'approved' => $attrs['APPROVED'],
410 'paid' => $attrs['PAID'],
411 'status' => $attrs['STATUS']));
412 if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
416 if ($name == 'PREDEFINED_EXPENSE') {
417 if (!$this->insertPredefinedExpense(array(
418 'group_id' => $this->current_group_id,
419 'org_id' => $this->org_id,
420 'name' => $attrs['NAME'],
421 'cost' => $attrs['COST']))) {
422 $this->errors->add($i18n->get('error.db'));
427 if ($name == 'TEMPLATE') {
428 if (!$this->insertTemplate(array(
429 'group_id' => $this->current_group_id,
430 'org_id' => $this->org_id,
431 'name' => $attrs['NAME'],
432 'description' => $attrs['DESCRIPTION'],
433 'content' => $attrs['CONTENT'],
434 'status' => $attrs['STATUS']))) {
435 $this->errors->add($i18n->get('error.db'));
440 if ($name == 'MONTHLY_QUOTA') {
441 if (!$this->insertMonthlyQuota(array(
442 'group_id' => $this->current_group_id,
443 'org_id' => $this->org_id,
444 'year' => $attrs['YEAR'],
445 'month' => $attrs['MONTH'],
446 'minutes' => $attrs['MINUTES']))) {
447 $this->errors->add($i18n->get('error.db'));
452 if ($name == 'FAV_REPORT') {
454 if (strlen($attrs['USERS']) > 0) {
455 $arr = explode(',', $attrs['USERS']);
457 $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
459 $fav_report_id = $this->insertFavReport(array(
460 'name' => $attrs['NAME'],
461 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
462 'group_id' => $this->current_group_id,
463 'org_id' => $this->org_id,
464 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
465 'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
466 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
467 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
468 'billable' => $attrs['BILLABLE'],
469 'approved' => $attrs['APPROVED'],
470 'invoice' => $attrs['INVOICE'],
471 'timesheet' => $attrs['TIMESHEET'],
472 'paid_status' => $attrs['PAID_STATUS'],
473 'users' => $user_list,
474 'period' => $attrs['PERIOD'],
475 'from' => $attrs['PERIOD_START'],
476 'to' => $attrs['PERIOD_END'],
477 'chclient' => (int) $attrs['SHOW_CLIENT'],
478 'chinvoice' => (int) $attrs['SHOW_INVOICE'],
479 'chpaid' => (int) $attrs['SHOW_PAID'],
480 'chip' => (int) $attrs['SHOW_IP'],
481 'chproject' => (int) $attrs['SHOW_PROJECT'],
482 'chtimesheet' => (int) $attrs['SHOW_TIMESHEET'],
483 'chstart' => (int) $attrs['SHOW_START'],
484 'chduration' => (int) $attrs['SHOW_DURATION'],
485 'chcost' => (int) $attrs['SHOW_COST'],
486 'chtask' => (int) $attrs['SHOW_TASK'],
487 'chfinish' => (int) $attrs['SHOW_END'],
488 'chnote' => (int) $attrs['SHOW_NOTE'],
489 'chapproved' => (int) $attrs['SHOW_APPROVED'],
490 'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
491 'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
492 'group_by1' => $attrs['GROUP_BY1'],
493 'group_by2' => $attrs['GROUP_BY2'],
494 'group_by3' => $attrs['GROUP_BY3'],
495 'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
496 if ($fav_report_id) {
498 $this->currentGroupFavReportMap[$attrs['ID']] = $fav_report_id;
499 } else $this->errors->add($i18n->get('error.db'));
503 if ($name == 'NOTIFICATION') {
504 if (!$this->insertNotification(array(
505 'group_id' => $this->current_group_id,
506 'org_id' => $this->org_id,
507 'cron_spec' => $attrs['CRON_SPEC'],
508 'last' => $attrs['LAST'],
509 'next' => $attrs['NEXT'],
510 'report_id' => $this->currentGroupFavReportMap[$attrs['REPORT_ID']],
511 'email' => $attrs['EMAIL'],
512 'cc' => $attrs['CC'],
513 'subject' => $attrs['SUBJECT'],
514 'report_condition' => $attrs['REPORT_CONDITION'],
515 'status' => $attrs['STATUS']))) {
516 $this->errors->add($i18n->get('error.db'));
521 if ($name == 'USER_PARAM') {
522 if (!$this->insertUserParam(array(
523 'group_id' => $this->current_group_id,
524 'org_id' => $this->org_id,
525 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
526 'param_name' => $attrs['PARAM_NAME'],
527 'param_value' => $attrs['PARAM_VALUE']))) {
528 $this->errors->add($i18n->get('error.db'));
535 // endElement - callback handler for ending tags in XML.
536 // We use this only for process </group> element endings and
537 // set current_group_id to an immediate parent.
538 // This is required to import group hierarchy correctly.
539 function endElement($parser, $name) {
540 // No need to care about first or second pass, as this is used only in second pass.
541 // See 2nd xml_set_element_handler, where this handler is set.
542 if ($name == 'GROUP') {
543 // Remove self from the parent stack.
544 $self = array_pop($this->parents);
545 // Set current group id to an immediate parent.
546 $len = count($this->parents);
547 $this->current_group_id = $len ? $this->parents[$len-1] : null;
551 // importXml - uncompresses the file, reads and parses its content.
552 // It goes through the file 2 times.
554 // During 1st pass, it determines whether we can import data.
555 // In 1st pass, startElement function is called as many times as necessary.
557 // Actual import occurs during 2nd pass.
558 // In 2nd pass, startElement and endElement are called many times.
559 // We only use endElement to finish current group processing.
561 // The above allows us to export/import complex orgs with nested groups,
562 // while by design all data are in attributes of the elements (no CDATA).
564 // There is currently at least one problem with keeping all data in attributes:
565 // a vertical tab character 0xB anywhere breaks parsing, making import impossible.
566 // See https://github.com/sparklemotion/nokogiri/issues/1581 - looks like
567 // an XML standard thing. Apparently, other invalid characters break parsing too.
568 // This problem needs to be addressed at some point but how exactly without
569 // complicating export-import too much with CDATA and dataElement processing?
570 function importXml() {
573 if (!$_FILES['xmlfile']['name']) {
574 $this->errors->add($i18n->get('error.upload'));
575 return; // There is nothing to do if we don't have a file.
578 // Do we have a compressed file?
580 $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
581 if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
585 // Create a temporary file.
586 $dirName = dirname(TEMPLATE_DIR . '_c/.');
587 $filename = tempnam($dirName, 'import_');
589 // If the file is compressed - uncompress it.
591 if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
592 $this->errors->add($i18n->get('error.sys'));
595 unlink($_FILES['xmlfile']['tmp_name']);
597 if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
598 $this->errors->add($i18n->get('error.upload'));
603 // Initialize XML parser.
604 $parser = xml_parser_create();
605 xml_set_object($parser, $this);
606 xml_set_element_handler($parser, 'startElement', false); // No need to process end tags in 1st pass.
608 // We need to parse the file 2 times:
609 // 1) First pass: determine if import is possible.
610 // 2) Second pass: import data, one tag at a time.
612 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
613 $file = fopen($filename, 'r');
614 while (($data = fread($file, 4096)) && $this->errors->no()) {
615 if (!xml_parse($parser, $data, feof($file))) {
616 $this->errors->add(sprintf($i18n->get('error.xml'),
617 xml_get_current_line_number($parser),
618 xml_error_string(xml_get_error_code($parser))));
621 if ($this->conflicting_logins) {
622 $this->canImport = false;
623 $this->errors->add($i18n->get('error.user_exists'));
624 $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins));
626 if (!ttUserHelper::canAdd($this->num_users)) {
627 $this->canImport = false;
628 $this->errors->add($i18n->get('error.user_count'));
631 $this->firstPass = false; // We are done with 1st pass.
632 xml_parser_free($parser);
633 if ($file) fclose($file);
634 if ($this->errors->yes()) {
635 // Remove the file and exit if we have errors.
640 // Now we can do a second pass, where real work is done.
641 $parser = xml_parser_create();
642 xml_set_object($parser, $this);
643 xml_set_element_handler($parser, 'startElement', 'endElement'); // Need to process ending tags too.
645 // Read and parse the content of the file. During parsing, startElement and endElement are called back for each tag.
646 $file = fopen($filename, 'r');
647 while (($data = fread($file, 4096)) && $this->errors->no()) {
648 if (!xml_parse($parser, $data, feof($file))) {
649 $this->errors->add(sprintf($i18n->get('error.xml'),
650 xml_get_current_line_number($parser),
651 xml_error_string(xml_get_error_code($parser))));
654 xml_parser_free($parser);
655 if ($file) fclose($file);
659 // uncompress - uncompresses the content of the $in file into the $out file.
660 function uncompress($in, $out) {
661 // Do we have the uncompress function?
662 if (!function_exists('bzopen'))
665 // Initial checks of file names and permissions.
666 if (!file_exists($in) || !is_readable ($in))
668 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
671 if (!$out_file = fopen($out, 'wb'))
673 if (!$in_file = bzopen ($in, 'r'))
676 while (!feof($in_file)) {
677 $buffer = bzread($in_file, 4096);
678 fwrite($out_file, $buffer, 4096);
685 // createGroup function creates a new group.
686 private function createGroup($fields) {
689 $mdb2 = getConnection();
691 $columns = '(parent_id, org_id, group_key, name, description, currency, decimal_mark, lang, date_format, time_format,'.
692 ' week_start, tracking_mode, project_required, task_required, record_type, bcc_email,'.
693 ' allow_ip, password_complexity, plugins, lock_spec,'.
694 ' workday_minutes, config, created, created_ip, created_by)';
696 $values = ' values (';
697 $values .= $mdb2->quote($fields['parent_id']);
698 $values .= ', '.$mdb2->quote($fields['org_id']);
699 $values .= ', '.$mdb2->quote(trim($fields['group_key']));
700 $values .= ', '.$mdb2->quote(trim($fields['name']));
701 $values .= ', '.$mdb2->quote(trim($fields['description']));
702 $values .= ', '.$mdb2->quote(trim($fields['currency']));
703 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
704 $values .= ', '.$mdb2->quote($fields['lang']);
705 $values .= ', '.$mdb2->quote($fields['date_format']);
706 $values .= ', '.$mdb2->quote($fields['time_format']);
707 $values .= ', '.(int)$fields['week_start'];
708 $values .= ', '.(int)$fields['tracking_mode'];
709 $values .= ', '.(int)$fields['project_required'];
710 $values .= ', '.(int)$fields['task_required'];
711 $values .= ', '.(int)$fields['record_type'];
712 $values .= ', '.$mdb2->quote($fields['bcc_email']);
713 $values .= ', '.$mdb2->quote($fields['allow_ip']);
714 $values .= ', '.$mdb2->quote($fields['password_complexity']);
715 $values .= ', '.$mdb2->quote($fields['plugins']);
716 $values .= ', '.$mdb2->quote($fields['lock_spec']);
717 $values .= ', '.(int)$fields['workday_minutes'];
718 $values .= ', '.$mdb2->quote($fields['config']);
719 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
722 $sql = 'insert into tt_groups '.$columns.$values;
723 $affected = $mdb2->exec($sql);
724 if (is_a($affected, 'PEAR_Error')) {
725 $this->errors->add($i18n->get('error.db'));
729 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
733 // insertMonthlyQuota - a helper function to insert a monthly quota.
734 private function insertMonthlyQuota($fields) {
735 $mdb2 = getConnection();
737 $group_id = (int) $fields['group_id'];
738 $org_id = (int) $fields['org_id'];
739 $year = (int) $fields['year'];
740 $month = (int) $fields['month'];
741 $minutes = (int) $fields['minutes'];
743 $sql = "INSERT INTO tt_monthly_quotas (group_id, org_id, year, month, minutes)".
744 " values ($group_id, $org_id, $year, $month, $minutes)";
745 $affected = $mdb2->exec($sql);
746 return (!is_a($affected, 'PEAR_Error'));
749 // insertPredefinedExpense - a helper function to insert a predefined expense.
750 private function insertPredefinedExpense($fields) {
751 $mdb2 = getConnection();
753 $group_id = (int) $fields['group_id'];
754 $org_id = (int) $fields['org_id'];
755 $name = $mdb2->quote($fields['name']);
756 $cost = $mdb2->quote($fields['cost']);
758 $sql = "INSERT INTO tt_predefined_expenses (group_id, org_id, name, cost)".
759 " values ($group_id, $org_id, $name, $cost)";
760 $affected = $mdb2->exec($sql);
761 return (!is_a($affected, 'PEAR_Error'));
764 // insertTemplate - a helper function to insert a template.
765 private function insertTemplate($fields) {
766 $mdb2 = getConnection();
768 $group_id = (int) $fields['group_id'];
769 $org_id = (int) $fields['org_id'];
770 $name = $mdb2->quote($fields['name']);
771 $description = $mdb2->quote($fields['description']);
772 $content = $mdb2->quote($fields['content']);
773 $status = $mdb2->quote($fields['status']);
775 $sql = "INSERT INTO tt_templates (group_id, org_id, name, description, content, status)".
776 " values ($group_id, $org_id, $name, $description, $content, $status)";
777 $affected = $mdb2->exec($sql);
778 return (!is_a($affected, 'PEAR_Error'));
781 // insertExpense - a helper function to insert an expense item.
782 private function insertExpense($fields) {
784 $mdb2 = getConnection();
786 $group_id = (int) $fields['group_id'];
787 $org_id = (int) $fields['org_id'];
788 $date = $fields['date'];
789 $user_id = (int) $fields['user_id'];
790 $client_id = $fields['client_id'];
791 $project_id = $fields['project_id'];
792 $name = $fields['name'];
793 $cost = str_replace(',', '.', $fields['cost']);
794 $invoice_id = $fields['invoice_id'];
795 $status = $fields['status'];
796 $approved = (int) $fields['approved'];
797 $paid = (int) $fields['paid'];
798 $created = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
800 $sql = "insert into tt_expense_items".
801 " (date, user_id, group_id, org_id, client_id, project_id, name,".
802 " cost, invoice_id, approved, paid, created, created_ip, created_by, status)".
803 " values (".$mdb2->quote($date).", $user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
804 ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).
805 ", $approved, $paid $created, ".$mdb2->quote($status).")";
806 $affected = $mdb2->exec($sql);
807 return (!is_a($affected, 'PEAR_Error'));
810 // insertTask function inserts a new task into database.
811 private function insertTask($fields)
813 $mdb2 = getConnection();
815 $group_id = (int) $fields['group_id'];
816 $org_id = (int) $fields['org_id'];
817 $name = $fields['name'];
818 $description = $fields['description'];
819 $projects = $fields['projects'];
820 $status = $fields['status'];
822 $sql = "insert into tt_tasks (group_id, org_id, name, description, status)
823 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($status).")";
824 $affected = $mdb2->exec($sql);
826 if (is_a($affected, 'PEAR_Error'))
829 $last_id = $mdb2->lastInsertID('tt_tasks', 'id');
833 // insertUserProjectBind - inserts a user to project bind into tt_user_project_binds table.
834 private function insertUserProjectBind($fields) {
835 $mdb2 = getConnection();
837 $group_id = (int) $fields['group_id'];
838 $org_id = (int) $fields['org_id'];
839 $user_id = (int) $fields['user_id'];
840 $project_id = (int) $fields['project_id'];
841 $rate = $mdb2->quote($fields['rate']);
842 $status = $mdb2->quote($fields['status']);
844 $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
845 " values($user_id, $project_id, $group_id, $org_id, $rate, $status)";
846 $affected = $mdb2->exec($sql);
847 return (!is_a($affected, 'PEAR_Error'));
850 // insertUser - inserts a user into database.
851 private function insertUser($fields) {
853 $mdb2 = getConnection();
855 $group_id = (int) $fields['group_id'];
856 $org_id = (int) $fields['org_id'];
858 $columns = '(login, password, name, group_id, org_id, role_id, client_id, rate, quota_percent, email, created, created_ip, created_by, status)';
860 $values = 'values (';
861 $values .= $mdb2->quote($fields['login']);
862 $values .= ', '.$mdb2->quote($fields['password']);
863 $values .= ', '.$mdb2->quote($fields['name']);
864 $values .= ', '.$group_id;
865 $values .= ', '.$org_id;
866 $values .= ', '.(int)$fields['role_id'];
867 $values .= ', '.$mdb2->quote($fields['client_id']);
868 $values .= ', '.$mdb2->quote($fields['rate']);
869 $values .= ', '.$mdb2->quote($fields['quota_percent']);
870 $values .= ', '.$mdb2->quote($fields['email']);
871 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
872 $values .= ', '.$mdb2->quote($fields['status']);
875 $sql = "insert into tt_users $columns $values";
876 $affected = $mdb2->exec($sql);
877 if (is_a($affected, 'PEAR_Error')) return false;
879 $last_id = $mdb2->lastInsertID('tt_users', 'id');
883 // insertProject - a helper function to insert a project as well as project to task binds.
884 private function insertProject($fields)
886 $mdb2 = getConnection();
888 $group_id = (int) $fields['group_id'];
889 $org_id = (int) $fields['org_id'];
890 $name = $fields['name'];
891 $description = $fields['description'];
892 $tasks = $fields['tasks'];
893 $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
894 $status = $fields['status'];
896 $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
897 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
898 $affected = $mdb2->exec($sql);
899 if (is_a($affected, 'PEAR_Error'))
902 $last_id = $mdb2->lastInsertID('tt_projects', 'id');
904 // Insert binds into tt_project_task_binds table.
905 if (is_array($tasks)) {
906 foreach ($tasks as $task_id) {
907 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
908 " values($last_id, $task_id, $group_id, $org_id)";
909 $affected = $mdb2->exec($sql);
910 if (is_a($affected, 'PEAR_Error'))
918 // insertRole - inserts a role into tt_roles table.
919 private function insertRole($fields)
921 $mdb2 = getConnection();
923 $group_id = (int) $fields['group_id'];
924 $org_id = (int) $fields['org_id'];
925 $name = $fields['name'];
926 $rank = (int) $fields['rank'];
927 $description = $fields['description'];
928 $rights = $fields['rights'];
929 $status = $fields['status'];
931 $sql = "insert into tt_roles (group_id, org_id, name, rank, description, rights, status)
932 values ($group_id, $org_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
933 $affected = $mdb2->exec($sql);
934 if (is_a($affected, 'PEAR_Error'))
937 $last_id = $mdb2->lastInsertID('tt_roles', 'id');
941 // insertTimesheet - inserts a timesheet in database.
942 private function insertTimesheet($fields)
944 $mdb2 = getConnection();
946 $user_id = (int) $fields['user_id'];
947 $group_id = (int) $fields['group_id'];
948 $org_id = (int) $fields['org_id'];
949 $client_id = $fields['client_id'];
950 $project_id = $fields['project_id'];
951 $name = $fields['name'];
952 $comment = $fields['comment'];
953 $start_date = $fields['start_date'];
954 $end_date = $fields['end_date'];
955 $submit_status = $fields['submit_status'];
956 $approve_status = $fields['approve_status'];
957 $approve_comment = $fields['approve_comment'];
958 $status = $fields['status'];
960 // Insert a new timesheet record.
961 $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name,".
962 " comment, start_date, end_date, submit_status, approve_status, approve_comment, status)".
963 " values($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).", ".
964 $mdb2->quote($comment).", ".$mdb2->quote($start_date).", ".$mdb2->quote($end_date).", ".
965 $mdb2->quote($submit_status).", ".$mdb2->quote($approve_status).", ".
966 $mdb2->quote($approve_comment).", ".$mdb2->quote($status).")";
967 $affected = $mdb2->exec($sql);
968 if (is_a($affected, 'PEAR_Error')) return false;
970 $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
974 // insertInvoice - inserts an invoice in database.
975 private function insertInvoice($fields)
977 $mdb2 = getConnection();
979 $group_id = (int) $fields['group_id'];
980 $org_id = (int) $fields['org_id'];
981 $name = $fields['name'];
982 $client_id = (int) $fields['client_id'];
983 $date = $fields['date'];
984 $status = $fields['status'];
986 // Insert a new invoice record.
987 $sql = "insert into tt_invoices (group_id, org_id, name, date, client_id, status)".
988 " values($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($date).", $client_id, ".$mdb2->quote($fields['status']).")";
989 $affected = $mdb2->exec($sql);
990 if (is_a($affected, 'PEAR_Error')) return false;
992 $last_id = $mdb2->lastInsertID('tt_invoices', 'id');
996 // The insertClient function inserts a new client as well as client to project binds.
997 private function insertClient($fields)
999 $mdb2 = getConnection();
1001 $group_id = (int) $fields['group_id'];
1002 $org_id = (int) $fields['org_id'];
1003 $name = $fields['name'];
1004 $address = $fields['address'];
1005 $tax = $fields['tax'];
1006 $projects = $fields['projects'];
1008 $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
1009 $status = $fields['status'];
1011 $tax = str_replace(',', '.', $tax);
1012 if ($tax == '') $tax = 0;
1014 $sql = "insert into tt_clients (group_id, org_id, name, address, tax, projects, status)".
1015 " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
1017 $affected = $mdb2->exec($sql);
1018 if (is_a($affected, 'PEAR_Error'))
1021 $last_id = $mdb2->lastInsertID('tt_clients', 'id');
1023 if (count($projects) > 0)
1024 foreach ($projects as $p_id) {
1025 $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)";
1026 $affected = $mdb2->exec($sql);
1027 if (is_a($affected, 'PEAR_Error'))
1034 // insertFavReport - inserts a favorite report in database.
1035 private function insertFavReport($fields) {
1036 $mdb2 = getConnection();
1038 $group_id = (int) $fields['group_id'];
1039 $org_id = (int) $fields['org_id'];
1041 $sql = "insert into tt_fav_reports".
1042 " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
1043 " billable, approved, invoice, timesheet, paid_status, users, period, period_start, period_end,".
1044 " show_client, show_invoice, show_paid, show_ip,".
1045 " show_project, show_timesheet, show_start, show_duration, show_cost,".
1046 " show_task, show_end, show_note, show_approved, show_custom_field_1, show_work_units,".
1047 " group_by1, group_by2, group_by3, show_totals_only)".
1049 $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
1050 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
1051 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
1052 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['approved']).", ".
1053 $mdb2->quote($fields['invoice']).", ".$mdb2->quote($fields['timesheet']).", ".
1054 $mdb2->quote($fields['paid_status']).", ".
1055 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
1056 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
1057 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
1058 $fields['chproject'].", ".$fields['chtimesheet'].", ".$fields['chstart'].", ".$fields['chduration'].", ".
1059 $fields['chcost'].", ".$fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".
1060 $fields['chapproved'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
1061 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
1062 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
1063 $affected = $mdb2->exec($sql);
1064 if (is_a($affected, 'PEAR_Error'))
1067 $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
1071 // insertNotification function inserts a new notification into database.
1072 private function insertNotification($fields)
1074 $mdb2 = getConnection();
1076 $group_id = (int) $fields['group_id'];
1077 $org_id = (int) $fields['org_id'];
1078 $cron_spec = $fields['cron_spec'];
1079 $last = (int) $fields['last'];
1080 $next = (int) $fields['next'];
1081 $report_id = (int) $fields['report_id'];
1082 $email = $fields['email'];
1083 $cc = $fields['cc'];
1084 $subject = $fields['subject'];
1085 $report_condition = $fields['report_condition'];
1086 $status = $fields['status'];
1088 $sql = "insert into tt_cron".
1089 " (group_id, org_id, cron_spec, last, next, report_id, email, cc, subject, report_condition, status)".
1090 " 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).")";
1091 $affected = $mdb2->exec($sql);
1092 return (!is_a($affected, 'PEAR_Error'));
1095 // insertUserParam - a helper function to insert a user parameter.
1096 private function insertUserParam($fields) {
1097 $mdb2 = getConnection();
1099 $group_id = (int) $fields['group_id'];
1100 $org_id = (int) $fields['org_id'];
1101 $user_id = (int) $fields['user_id'];
1102 $param_name = $fields['param_name'];
1103 $param_value = $fields['param_value'];
1105 $sql = "insert into tt_config".
1106 " (user_id, group_id, org_id, param_name, param_value)".
1107 " values ($user_id, $group_id, $org_id, ".$mdb2->quote($param_name).", ".$mdb2->quote($param_value).")";
1108 $affected = $mdb2->exec($sql);
1109 return (!is_a($affected, 'PEAR_Error'));
1112 // insertCustomField - a helper function to insert a custom field.
1113 private function insertCustomField($fields) {
1114 $mdb2 = getConnection();
1116 $group_id = (int) $fields['group_id'];
1117 $org_id = (int) $fields['org_id'];
1118 $entity_type = (int) $fields['entity_type'];
1119 $type = (int) $fields['type'];
1120 $label = $fields['label'];
1121 $required = (int) $fields['required'];
1122 $status = $fields['status'];
1124 $sql = "insert into tt_custom_fields".
1125 " (group_id, org_id, entity_type, type, label, required, status)".
1126 " values($group_id, $org_id, $entity_type, $type, ".$mdb2->quote($label).", $required, ".$mdb2->quote($status).")";
1127 $affected = $mdb2->exec($sql);
1128 if (is_a($affected, 'PEAR_Error'))
1131 $last_id = $mdb2->lastInsertID('tt_custom_fields', 'id');
1135 // insertCustomFieldOption - a helper function to insert a custom field option.
1136 private function insertCustomFieldOption($fields) {
1137 $mdb2 = getConnection();
1139 $group_id = (int) $fields['group_id'];
1140 $org_id = (int) $fields['org_id'];
1141 $field_id = (int) $fields['field_id'];
1142 $value = $fields['value'];
1144 $sql = "insert into tt_custom_field_options (group_id, org_id, field_id, value)".
1145 " values ($group_id, $org_id, $field_id, ".$mdb2->quote($value).")";
1146 $affected = $mdb2->exec($sql);
1147 if (is_a($affected, 'PEAR_Error'))
1150 $last_id = $mdb2->lastInsertID('tt_custom_field_options', 'id');
1154 // insertLogEntry - a helper function to insert a time log entry.
1155 private function insertLogEntry($fields) {
1157 $mdb2 = getConnection();
1159 $group_id = (int) $fields['group_id'];
1160 $org_id = (int) $fields['org_id'];
1161 $user_id = (int) $fields['user_id'];
1162 $date = $fields['date'];
1163 $start = $fields['start'];
1164 $duration = $fields['duration'];
1165 $client_id = $fields['client_id'];
1166 $project_id = $fields['project_id'];
1167 $task_id = $fields['task_id'];
1168 $timesheet_id = $fields['timesheet_id'];
1169 $invoice_id = $fields['invoice_id'];
1170 $comment = $fields['comment'];
1171 $billable = (int) $fields['billable'];
1172 $approved = (int) $fields['approved'];
1173 $paid = (int) $fields['paid'];
1174 $status = $fields['status'];
1176 $sql = "insert into tt_log".
1177 " (user_id, group_id, org_id, date, start, duration, client_id, project_id, task_id, timesheet_id, invoice_id, comment".
1178 ", billable, approved, paid, created, created_ip, created_by, status)".
1179 " values ($user_id, $group_id, $org_id".
1180 ", ".$mdb2->quote($date).
1181 ", ".$mdb2->quote($start).
1182 ", ".$mdb2->quote($duration).
1183 ", ".$mdb2->quote($client_id).
1184 ", ".$mdb2->quote($project_id).
1185 ", ".$mdb2->quote($task_id).
1186 ", ".$mdb2->quote($timesheet_id).
1187 ", ".$mdb2->quote($invoice_id).
1188 ", ".$mdb2->quote($comment).
1189 ", $billable, $approved, $paid".
1190 ", now(), ".$mdb2->quote($_SERVER['REMOTE_ADDR']).", ".$user->id.
1191 ", ". $mdb2->quote($status).")";
1192 $affected = $mdb2->exec($sql);
1193 if (is_a($affected, 'PEAR_Error')) {
1194 $this->errors->add($i18n->get('error.db')); // TODO: review whether or not to add error here in all insert calls.
1198 $log_id = $mdb2->lastInsertID('tt_log', 'id');
1202 // insertCustomFieldLogEntry - a helper function to insert a custom field log entry.
1203 private function insertCustomFieldLogEntry($fields) {
1204 $mdb2 = getConnection();
1206 $group_id = (int) $fields['group_id'];
1207 $org_id = (int) $fields['org_id'];
1208 $log_id = (int) $fields['log_id'];
1209 $field_id = (int) $fields['field_id'];
1210 $option_id = $fields['option_id'];
1211 $value = $fields['value'];
1212 $status = $fields['status'];
1214 $sql = "insert into tt_custom_field_log (group_id, org_id, log_id, field_id, option_id, value, status)".
1215 " values ($group_id, $org_id, $log_id, $field_id, ".$mdb2->quote($option_id).", ".$mdb2->quote($value).", ".$mdb2->quote($status).")";
1216 $affected = $mdb2->exec($sql);
1217 return (!is_a($affected, 'PEAR_Error'));
1220 // getTopRole returns top role id.
1221 private function getTopRole() {
1222 $mdb2 = getConnection();
1224 $sql = "select id from tt_roles where group_id = 0 and rank = ".MAX_RANK." and status = 1";
1225 $res = $mdb2->query($sql);
1227 if (!is_a($res, 'PEAR_Error')) {
1228 $val = $res->fetchRow();
1235 // The loginExists function detrmines if a login already exists.
1236 private function loginExists($login) {
1237 $mdb2 = getConnection();
1239 $sql = "select id from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)";
1240 $res = $mdb2->query($sql);
1241 if (!is_a($res, 'PEAR_Error')) {
1242 if ($val = $res->fetchRow()) {