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 'type' => $attrs['TYPE'],
356 'label' => $attrs['LABEL'],
357 'required' => $attrs['REQUIRED'],
358 'status' => $attrs['STATUS']));
359 if ($custom_field_id) {
361 $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
362 } else $this->errors->add($i18n->get('error.db'));
366 if ($name == 'CUSTOM_FIELD_OPTION') {
367 // We get here when processing <custom_field_option> tags for the current group.
368 $custom_field_option_id = $this->insertCustomFieldOption(array(
369 'group_id' => $this->current_group_id,
370 'org_id' => $this->org_id,
371 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
372 'value' => $attrs['VALUE']));
373 if ($custom_field_option_id) {
375 $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
376 } else $this->errors->add($i18n->get('error.db'));
380 if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
381 // We get here when processing <custom_field_log_entry> tags for the current group.
382 if (!$this->insertCustomFieldLogEntry(array(
383 'group_id' => $this->current_group_id,
384 'org_id' => $this->org_id,
385 'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
386 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
387 'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
388 'value' => $attrs['VALUE'],
389 'status' => $attrs['STATUS']))) {
390 $this->errors->add($i18n->get('error.db'));
395 if ($name == 'EXPENSE_ITEM') {
396 // We get here when processing <expense_item> tags for the current group.
397 $expense_item_id = $this->insertExpense(array(
398 'date' => $attrs['DATE'],
399 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
400 'group_id' => $this->current_group_id,
401 'org_id' => $this->org_id,
402 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
403 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
404 'timesheet_id' => $this->currentGroupTimesheetMap[$attrs['TIMESHEET_ID']],
405 'name' => $attrs['NAME'],
406 'cost' => $attrs['COST'],
407 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
408 'approved' => $attrs['APPROVED'],
409 'paid' => $attrs['PAID'],
410 'status' => $attrs['STATUS']));
411 if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
415 if ($name == 'PREDEFINED_EXPENSE') {
416 if (!$this->insertPredefinedExpense(array(
417 'group_id' => $this->current_group_id,
418 'org_id' => $this->org_id,
419 'name' => $attrs['NAME'],
420 'cost' => $attrs['COST']))) {
421 $this->errors->add($i18n->get('error.db'));
426 if ($name == 'TEMPLATE') {
427 if (!$this->insertTemplate(array(
428 'group_id' => $this->current_group_id,
429 'org_id' => $this->org_id,
430 'name' => $attrs['NAME'],
431 'description' => $attrs['DESCRIPTION'],
432 'content' => $attrs['CONTENT'],
433 'status' => $attrs['STATUS']))) {
434 $this->errors->add($i18n->get('error.db'));
439 if ($name == 'MONTHLY_QUOTA') {
440 if (!$this->insertMonthlyQuota(array(
441 'group_id' => $this->current_group_id,
442 'org_id' => $this->org_id,
443 'year' => $attrs['YEAR'],
444 'month' => $attrs['MONTH'],
445 'minutes' => $attrs['MINUTES']))) {
446 $this->errors->add($i18n->get('error.db'));
451 if ($name == 'FAV_REPORT') {
453 if (strlen($attrs['USERS']) > 0) {
454 $arr = explode(',', $attrs['USERS']);
456 $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
458 $fav_report_id = $this->insertFavReport(array(
459 'name' => $attrs['NAME'],
460 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
461 'group_id' => $this->current_group_id,
462 'org_id' => $this->org_id,
463 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
464 'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
465 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
466 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
467 'billable' => $attrs['BILLABLE'],
468 'approved' => $attrs['APPROVED'],
469 'invoice' => $attrs['INVOICE'],
470 'timesheet' => $attrs['TIMESHEET'],
471 'paid_status' => $attrs['PAID_STATUS'],
472 'users' => $user_list,
473 'period' => $attrs['PERIOD'],
474 'from' => $attrs['PERIOD_START'],
475 'to' => $attrs['PERIOD_END'],
476 'chclient' => (int) $attrs['SHOW_CLIENT'],
477 'chinvoice' => (int) $attrs['SHOW_INVOICE'],
478 'chpaid' => (int) $attrs['SHOW_PAID'],
479 'chip' => (int) $attrs['SHOW_IP'],
480 'chproject' => (int) $attrs['SHOW_PROJECT'],
481 'chtimesheet' => (int) $attrs['SHOW_TIMESHEET'],
482 'chstart' => (int) $attrs['SHOW_START'],
483 'chduration' => (int) $attrs['SHOW_DURATION'],
484 'chcost' => (int) $attrs['SHOW_COST'],
485 'chtask' => (int) $attrs['SHOW_TASK'],
486 'chfinish' => (int) $attrs['SHOW_END'],
487 'chnote' => (int) $attrs['SHOW_NOTE'],
488 'chapproved' => (int) $attrs['SHOW_APPROVED'],
489 'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
490 'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
491 'group_by1' => $attrs['GROUP_BY1'],
492 'group_by2' => $attrs['GROUP_BY2'],
493 'group_by3' => $attrs['GROUP_BY3'],
494 'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
495 if ($fav_report_id) {
497 $this->currentGroupFavReportMap[$attrs['ID']] = $fav_report_id;
498 } else $this->errors->add($i18n->get('error.db'));
502 if ($name == 'NOTIFICATION') {
503 if (!$this->insertNotification(array(
504 'group_id' => $this->current_group_id,
505 'org_id' => $this->org_id,
506 'cron_spec' => $attrs['CRON_SPEC'],
507 'last' => $attrs['LAST'],
508 'next' => $attrs['NEXT'],
509 'report_id' => $this->currentGroupFavReportMap[$attrs['REPORT_ID']],
510 'email' => $attrs['EMAIL'],
511 'cc' => $attrs['CC'],
512 'subject' => $attrs['SUBJECT'],
513 'report_condition' => $attrs['REPORT_CONDITION'],
514 'status' => $attrs['STATUS']))) {
515 $this->errors->add($i18n->get('error.db'));
520 if ($name == 'USER_PARAM') {
521 if (!$this->insertUserParam(array(
522 'group_id' => $this->current_group_id,
523 'org_id' => $this->org_id,
524 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
525 'param_name' => $attrs['PARAM_NAME'],
526 'param_value' => $attrs['PARAM_VALUE']))) {
527 $this->errors->add($i18n->get('error.db'));
534 // endElement - callback handler for ending tags in XML.
535 // We use this only for process </group> element endings and
536 // set current_group_id to an immediate parent.
537 // This is required to import group hierarchy correctly.
538 function endElement($parser, $name) {
539 // No need to care about first or second pass, as this is used only in second pass.
540 // See 2nd xml_set_element_handler, where this handler is set.
541 if ($name == 'GROUP') {
542 // Remove self from the parent stack.
543 $self = array_pop($this->parents);
544 // Set current group id to an immediate parent.
545 $len = count($this->parents);
546 $this->current_group_id = $len ? $this->parents[$len-1] : null;
550 // importXml - uncompresses the file, reads and parses its content.
551 // It goes through the file 2 times.
553 // During 1st pass, it determines whether we can import data.
554 // In 1st pass, startElement function is called as many times as necessary.
556 // Actual import occurs during 2nd pass.
557 // In 2nd pass, startElement and endElement are called many times.
558 // We only use endElement to finish current group processing.
560 // The above allows us to export/import complex orgs with nested groups,
561 // while by design all data are in attributes of the elements (no CDATA).
563 // There is currently at least one problem with keeping all data in attributes:
564 // a vertical tab character 0xB anywhere breaks parsing, making import impossible.
565 // See https://github.com/sparklemotion/nokogiri/issues/1581 - looks like
566 // an XML standard thing. Apparently, other invalid characters break parsing too.
567 // This problem needs to be addressed at some point but how exactly without
568 // complicating export-import too much with CDATA and dataElement processing?
569 function importXml() {
572 if (!$_FILES['xmlfile']['name']) {
573 $this->errors->add($i18n->get('error.upload'));
574 return; // There is nothing to do if we don't have a file.
577 // Do we have a compressed file?
579 $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
580 if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
584 // Create a temporary file.
585 $dirName = dirname(TEMPLATE_DIR . '_c/.');
586 $filename = tempnam($dirName, 'import_');
588 // If the file is compressed - uncompress it.
590 if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
591 $this->errors->add($i18n->get('error.sys'));
594 unlink($_FILES['xmlfile']['tmp_name']);
596 if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
597 $this->errors->add($i18n->get('error.upload'));
602 // Initialize XML parser.
603 $parser = xml_parser_create();
604 xml_set_object($parser, $this);
605 xml_set_element_handler($parser, 'startElement', false); // No need to process end tags in 1st pass.
607 // We need to parse the file 2 times:
608 // 1) First pass: determine if import is possible.
609 // 2) Second pass: import data, one tag at a time.
611 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
612 $file = fopen($filename, 'r');
613 while (($data = fread($file, 4096)) && $this->errors->no()) {
614 if (!xml_parse($parser, $data, feof($file))) {
615 $this->errors->add(sprintf($i18n->get('error.xml'),
616 xml_get_current_line_number($parser),
617 xml_error_string(xml_get_error_code($parser))));
620 if ($this->conflicting_logins) {
621 $this->canImport = false;
622 $this->errors->add($i18n->get('error.user_exists'));
623 $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins));
625 if (!ttUserHelper::canAdd($this->num_users)) {
626 $this->canImport = false;
627 $this->errors->add($i18n->get('error.user_count'));
630 $this->firstPass = false; // We are done with 1st pass.
631 xml_parser_free($parser);
632 if ($file) fclose($file);
633 if ($this->errors->yes()) {
634 // Remove the file and exit if we have errors.
639 // Now we can do a second pass, where real work is done.
640 $parser = xml_parser_create();
641 xml_set_object($parser, $this);
642 xml_set_element_handler($parser, 'startElement', 'endElement'); // Need to process ending tags too.
644 // Read and parse the content of the file. During parsing, startElement and endElement are called back for each tag.
645 $file = fopen($filename, 'r');
646 while (($data = fread($file, 4096)) && $this->errors->no()) {
647 if (!xml_parse($parser, $data, feof($file))) {
648 $this->errors->add(sprintf($i18n->get('error.xml'),
649 xml_get_current_line_number($parser),
650 xml_error_string(xml_get_error_code($parser))));
653 xml_parser_free($parser);
654 if ($file) fclose($file);
658 // uncompress - uncompresses the content of the $in file into the $out file.
659 function uncompress($in, $out) {
660 // Do we have the uncompress function?
661 if (!function_exists('bzopen'))
664 // Initial checks of file names and permissions.
665 if (!file_exists($in) || !is_readable ($in))
667 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
670 if (!$out_file = fopen($out, 'wb'))
672 if (!$in_file = bzopen ($in, 'r'))
675 while (!feof($in_file)) {
676 $buffer = bzread($in_file, 4096);
677 fwrite($out_file, $buffer, 4096);
684 // createGroup function creates a new group.
685 private function createGroup($fields) {
688 $mdb2 = getConnection();
690 $columns = '(parent_id, org_id, group_key, name, description, currency, decimal_mark, lang, date_format, time_format,'.
691 ' week_start, tracking_mode, project_required, task_required, record_type, bcc_email,'.
692 ' allow_ip, password_complexity, plugins, lock_spec,'.
693 ' workday_minutes, config, created, created_ip, created_by)';
695 $values = ' values (';
696 $values .= $mdb2->quote($fields['parent_id']);
697 $values .= ', '.$mdb2->quote($fields['org_id']);
698 $values .= ', '.$mdb2->quote(trim($fields['group_key']));
699 $values .= ', '.$mdb2->quote(trim($fields['name']));
700 $values .= ', '.$mdb2->quote(trim($fields['description']));
701 $values .= ', '.$mdb2->quote(trim($fields['currency']));
702 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
703 $values .= ', '.$mdb2->quote($fields['lang']);
704 $values .= ', '.$mdb2->quote($fields['date_format']);
705 $values .= ', '.$mdb2->quote($fields['time_format']);
706 $values .= ', '.(int)$fields['week_start'];
707 $values .= ', '.(int)$fields['tracking_mode'];
708 $values .= ', '.(int)$fields['project_required'];
709 $values .= ', '.(int)$fields['task_required'];
710 $values .= ', '.(int)$fields['record_type'];
711 $values .= ', '.$mdb2->quote($fields['bcc_email']);
712 $values .= ', '.$mdb2->quote($fields['allow_ip']);
713 $values .= ', '.$mdb2->quote($fields['password_complexity']);
714 $values .= ', '.$mdb2->quote($fields['plugins']);
715 $values .= ', '.$mdb2->quote($fields['lock_spec']);
716 $values .= ', '.(int)$fields['workday_minutes'];
717 $values .= ', '.$mdb2->quote($fields['config']);
718 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
721 $sql = 'insert into tt_groups '.$columns.$values;
722 $affected = $mdb2->exec($sql);
723 if (is_a($affected, 'PEAR_Error')) {
724 $this->errors->add($i18n->get('error.db'));
728 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
732 // insertMonthlyQuota - a helper function to insert a monthly quota.
733 private function insertMonthlyQuota($fields) {
734 $mdb2 = getConnection();
736 $group_id = (int) $fields['group_id'];
737 $org_id = (int) $fields['org_id'];
738 $year = (int) $fields['year'];
739 $month = (int) $fields['month'];
740 $minutes = (int) $fields['minutes'];
742 $sql = "INSERT INTO tt_monthly_quotas (group_id, org_id, year, month, minutes)".
743 " values ($group_id, $org_id, $year, $month, $minutes)";
744 $affected = $mdb2->exec($sql);
745 return (!is_a($affected, 'PEAR_Error'));
748 // insertPredefinedExpense - a helper function to insert a predefined expense.
749 private function insertPredefinedExpense($fields) {
750 $mdb2 = getConnection();
752 $group_id = (int) $fields['group_id'];
753 $org_id = (int) $fields['org_id'];
754 $name = $mdb2->quote($fields['name']);
755 $cost = $mdb2->quote($fields['cost']);
757 $sql = "INSERT INTO tt_predefined_expenses (group_id, org_id, name, cost)".
758 " values ($group_id, $org_id, $name, $cost)";
759 $affected = $mdb2->exec($sql);
760 return (!is_a($affected, 'PEAR_Error'));
763 // insertTemplate - a helper function to insert a template.
764 private function insertTemplate($fields) {
765 $mdb2 = getConnection();
767 $group_id = (int) $fields['group_id'];
768 $org_id = (int) $fields['org_id'];
769 $name = $mdb2->quote($fields['name']);
770 $description = $mdb2->quote($fields['description']);
771 $content = $mdb2->quote($fields['content']);
772 $status = $mdb2->quote($fields['status']);
774 $sql = "INSERT INTO tt_templates (group_id, org_id, name, description, content, status)".
775 " values ($group_id, $org_id, $name, $description, $content, $status)";
776 $affected = $mdb2->exec($sql);
777 return (!is_a($affected, 'PEAR_Error'));
780 // insertExpense - a helper function to insert an expense item.
781 private function insertExpense($fields) {
783 $mdb2 = getConnection();
785 $group_id = (int) $fields['group_id'];
786 $org_id = (int) $fields['org_id'];
787 $date = $fields['date'];
788 $user_id = (int) $fields['user_id'];
789 $client_id = $fields['client_id'];
790 $project_id = $fields['project_id'];
791 $name = $fields['name'];
792 $cost = str_replace(',', '.', $fields['cost']);
793 $invoice_id = $fields['invoice_id'];
794 $status = $fields['status'];
795 $approved = (int) $fields['approved'];
796 $paid = (int) $fields['paid'];
797 $created = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
799 $sql = "insert into tt_expense_items".
800 " (date, user_id, group_id, org_id, client_id, project_id, name,".
801 " cost, invoice_id, approved, paid, created, created_ip, created_by, status)".
802 " values (".$mdb2->quote($date).", $user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
803 ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).
804 ", $approved, $paid $created, ".$mdb2->quote($status).")";
805 $affected = $mdb2->exec($sql);
806 return (!is_a($affected, 'PEAR_Error'));
809 // insertTask function inserts a new task into database.
810 private function insertTask($fields)
812 $mdb2 = getConnection();
814 $group_id = (int) $fields['group_id'];
815 $org_id = (int) $fields['org_id'];
816 $name = $fields['name'];
817 $description = $fields['description'];
818 $projects = $fields['projects'];
819 $status = $fields['status'];
821 $sql = "insert into tt_tasks (group_id, org_id, name, description, status)
822 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($status).")";
823 $affected = $mdb2->exec($sql);
825 if (is_a($affected, 'PEAR_Error'))
828 $last_id = $mdb2->lastInsertID('tt_tasks', 'id');
832 // insertUserProjectBind - inserts a user to project bind into tt_user_project_binds table.
833 private function insertUserProjectBind($fields) {
834 $mdb2 = getConnection();
836 $group_id = (int) $fields['group_id'];
837 $org_id = (int) $fields['org_id'];
838 $user_id = (int) $fields['user_id'];
839 $project_id = (int) $fields['project_id'];
840 $rate = $mdb2->quote($fields['rate']);
841 $status = $mdb2->quote($fields['status']);
843 $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
844 " values($user_id, $project_id, $group_id, $org_id, $rate, $status)";
845 $affected = $mdb2->exec($sql);
846 return (!is_a($affected, 'PEAR_Error'));
849 // insertUser - inserts a user into database.
850 private function insertUser($fields) {
852 $mdb2 = getConnection();
854 $group_id = (int) $fields['group_id'];
855 $org_id = (int) $fields['org_id'];
857 $columns = '(login, password, name, group_id, org_id, role_id, client_id, rate, quota_percent, email, created, created_ip, created_by, status)';
859 $values = 'values (';
860 $values .= $mdb2->quote($fields['login']);
861 $values .= ', '.$mdb2->quote($fields['password']);
862 $values .= ', '.$mdb2->quote($fields['name']);
863 $values .= ', '.$group_id;
864 $values .= ', '.$org_id;
865 $values .= ', '.(int)$fields['role_id'];
866 $values .= ', '.$mdb2->quote($fields['client_id']);
867 $values .= ', '.$mdb2->quote($fields['rate']);
868 $values .= ', '.$mdb2->quote($fields['quota_percent']);
869 $values .= ', '.$mdb2->quote($fields['email']);
870 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
871 $values .= ', '.$mdb2->quote($fields['status']);
874 $sql = "insert into tt_users $columns $values";
875 $affected = $mdb2->exec($sql);
876 if (is_a($affected, 'PEAR_Error')) return false;
878 $last_id = $mdb2->lastInsertID('tt_users', 'id');
882 // insertProject - a helper function to insert a project as well as project to task binds.
883 private function insertProject($fields)
885 $mdb2 = getConnection();
887 $group_id = (int) $fields['group_id'];
888 $org_id = (int) $fields['org_id'];
889 $name = $fields['name'];
890 $description = $fields['description'];
891 $tasks = $fields['tasks'];
892 $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
893 $status = $fields['status'];
895 $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
896 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
897 $affected = $mdb2->exec($sql);
898 if (is_a($affected, 'PEAR_Error'))
901 $last_id = $mdb2->lastInsertID('tt_projects', 'id');
903 // Insert binds into tt_project_task_binds table.
904 if (is_array($tasks)) {
905 foreach ($tasks as $task_id) {
906 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
907 " values($last_id, $task_id, $group_id, $org_id)";
908 $affected = $mdb2->exec($sql);
909 if (is_a($affected, 'PEAR_Error'))
917 // insertRole - inserts a role into tt_roles table.
918 private function insertRole($fields)
920 $mdb2 = getConnection();
922 $group_id = (int) $fields['group_id'];
923 $org_id = (int) $fields['org_id'];
924 $name = $fields['name'];
925 $rank = (int) $fields['rank'];
926 $description = $fields['description'];
927 $rights = $fields['rights'];
928 $status = $fields['status'];
930 $sql = "insert into tt_roles (group_id, org_id, name, rank, description, rights, status)
931 values ($group_id, $org_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
932 $affected = $mdb2->exec($sql);
933 if (is_a($affected, 'PEAR_Error'))
936 $last_id = $mdb2->lastInsertID('tt_roles', 'id');
940 // insertTimesheet - inserts a timesheet in database.
941 private function insertTimesheet($fields)
943 $mdb2 = getConnection();
945 $user_id = (int) $fields['user_id'];
946 $group_id = (int) $fields['group_id'];
947 $org_id = (int) $fields['org_id'];
948 $client_id = $fields['client_id'];
949 $project_id = $fields['project_id'];
950 $name = $fields['name'];
951 $comment = $fields['comment'];
952 $start_date = $fields['start_date'];
953 $end_date = $fields['end_date'];
954 $submit_status = $fields['submit_status'];
955 $approve_status = $fields['approve_status'];
956 $approve_comment = $fields['approve_comment'];
957 $status = $fields['status'];
959 // Insert a new timesheet record.
960 $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name,".
961 " comment, start_date, end_date, submit_status, approve_status, approve_comment, status)".
962 " values($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).", ".
963 $mdb2->quote($comment).", ".$mdb2->quote($start_date).", ".$mdb2->quote($end_date).", ".
964 $mdb2->quote($submit_status).", ".$mdb2->quote($approve_status).", ".
965 $mdb2->quote($approve_comment).", ".$mdb2->quote($status).")";
966 $affected = $mdb2->exec($sql);
967 if (is_a($affected, 'PEAR_Error')) return false;
969 $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
973 // insertInvoice - inserts an invoice in database.
974 private function insertInvoice($fields)
976 $mdb2 = getConnection();
978 $group_id = (int) $fields['group_id'];
979 $org_id = (int) $fields['org_id'];
980 $name = $fields['name'];
981 $client_id = (int) $fields['client_id'];
982 $date = $fields['date'];
983 $status = $fields['status'];
985 // Insert a new invoice record.
986 $sql = "insert into tt_invoices (group_id, org_id, name, date, client_id, status)".
987 " values($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($date).", $client_id, ".$mdb2->quote($fields['status']).")";
988 $affected = $mdb2->exec($sql);
989 if (is_a($affected, 'PEAR_Error')) return false;
991 $last_id = $mdb2->lastInsertID('tt_invoices', 'id');
995 // The insertClient function inserts a new client as well as client to project binds.
996 private function insertClient($fields)
998 $mdb2 = getConnection();
1000 $group_id = (int) $fields['group_id'];
1001 $org_id = (int) $fields['org_id'];
1002 $name = $fields['name'];
1003 $address = $fields['address'];
1004 $tax = $fields['tax'];
1005 $projects = $fields['projects'];
1007 $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
1008 $status = $fields['status'];
1010 $tax = str_replace(',', '.', $tax);
1011 if ($tax == '') $tax = 0;
1013 $sql = "insert into tt_clients (group_id, org_id, name, address, tax, projects, status)".
1014 " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
1016 $affected = $mdb2->exec($sql);
1017 if (is_a($affected, 'PEAR_Error'))
1020 $last_id = $mdb2->lastInsertID('tt_clients', 'id');
1022 if (count($projects) > 0)
1023 foreach ($projects as $p_id) {
1024 $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)";
1025 $affected = $mdb2->exec($sql);
1026 if (is_a($affected, 'PEAR_Error'))
1033 // insertFavReport - inserts a favorite report in database.
1034 private function insertFavReport($fields) {
1035 $mdb2 = getConnection();
1037 $group_id = (int) $fields['group_id'];
1038 $org_id = (int) $fields['org_id'];
1040 $sql = "insert into tt_fav_reports".
1041 " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
1042 " billable, approved, invoice, timesheet, paid_status, users, period, period_start, period_end,".
1043 " show_client, show_invoice, show_paid, show_ip,".
1044 " show_project, show_timesheet, show_start, show_duration, show_cost,".
1045 " show_task, show_end, show_note, show_approved, show_custom_field_1, show_work_units,".
1046 " group_by1, group_by2, group_by3, show_totals_only)".
1048 $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
1049 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
1050 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
1051 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['approved']).", ".
1052 $mdb2->quote($fields['invoice']).", ".$mdb2->quote($fields['timesheet']).", ".
1053 $mdb2->quote($fields['paid_status']).", ".
1054 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
1055 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
1056 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
1057 $fields['chproject'].", ".$fields['chtimesheet'].", ".$fields['chstart'].", ".$fields['chduration'].", ".
1058 $fields['chcost'].", ".$fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".
1059 $fields['chapproved'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
1060 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
1061 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
1062 $affected = $mdb2->exec($sql);
1063 if (is_a($affected, 'PEAR_Error'))
1066 $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
1070 // insertNotification function inserts a new notification into database.
1071 private function insertNotification($fields)
1073 $mdb2 = getConnection();
1075 $group_id = (int) $fields['group_id'];
1076 $org_id = (int) $fields['org_id'];
1077 $cron_spec = $fields['cron_spec'];
1078 $last = (int) $fields['last'];
1079 $next = (int) $fields['next'];
1080 $report_id = (int) $fields['report_id'];
1081 $email = $fields['email'];
1082 $cc = $fields['cc'];
1083 $subject = $fields['subject'];
1084 $report_condition = $fields['report_condition'];
1085 $status = $fields['status'];
1087 $sql = "insert into tt_cron".
1088 " (group_id, org_id, cron_spec, last, next, report_id, email, cc, subject, report_condition, status)".
1089 " 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).")";
1090 $affected = $mdb2->exec($sql);
1091 return (!is_a($affected, 'PEAR_Error'));
1094 // insertUserParam - a helper function to insert a user parameter.
1095 private function insertUserParam($fields) {
1096 $mdb2 = getConnection();
1098 $group_id = (int) $fields['group_id'];
1099 $org_id = (int) $fields['org_id'];
1100 $user_id = (int) $fields['user_id'];
1101 $param_name = $fields['param_name'];
1102 $param_value = $fields['param_value'];
1104 $sql = "insert into tt_config".
1105 " (user_id, group_id, org_id, param_name, param_value)".
1106 " values ($user_id, $group_id, $org_id, ".$mdb2->quote($param_name).", ".$mdb2->quote($param_value).")";
1107 $affected = $mdb2->exec($sql);
1108 return (!is_a($affected, 'PEAR_Error'));
1111 // insertCustomField - a helper function to insert a custom field.
1112 private function insertCustomField($fields) {
1113 $mdb2 = getConnection();
1115 $group_id = (int) $fields['group_id'];
1116 $org_id = (int) $fields['org_id'];
1117 $type = (int) $fields['type'];
1118 $label = $fields['label'];
1119 $required = (int) $fields['required'];
1120 $status = $fields['status'];
1122 $sql = "insert into tt_custom_fields".
1123 " (group_id, org_id, type, label, required, status)".
1124 " values($group_id, $org_id, $type, ".$mdb2->quote($label).", $required, ".$mdb2->quote($status).")";
1125 $affected = $mdb2->exec($sql);
1126 if (is_a($affected, 'PEAR_Error'))
1129 $last_id = $mdb2->lastInsertID('tt_custom_fields', 'id');
1133 // insertCustomFieldOption - a helper function to insert a custom field option.
1134 private function insertCustomFieldOption($fields) {
1135 $mdb2 = getConnection();
1137 $group_id = (int) $fields['group_id'];
1138 $org_id = (int) $fields['org_id'];
1139 $field_id = (int) $fields['field_id'];
1140 $value = $fields['value'];
1142 $sql = "insert into tt_custom_field_options (group_id, org_id, field_id, value)".
1143 " values ($group_id, $org_id, $field_id, ".$mdb2->quote($value).")";
1144 $affected = $mdb2->exec($sql);
1145 if (is_a($affected, 'PEAR_Error'))
1148 $last_id = $mdb2->lastInsertID('tt_custom_field_options', 'id');
1152 // insertLogEntry - a helper function to insert a time log entry.
1153 private function insertLogEntry($fields) {
1155 $mdb2 = getConnection();
1157 $group_id = (int) $fields['group_id'];
1158 $org_id = (int) $fields['org_id'];
1159 $user_id = (int) $fields['user_id'];
1160 $date = $fields['date'];
1161 $start = $fields['start'];
1162 $duration = $fields['duration'];
1163 $client_id = $fields['client_id'];
1164 $project_id = $fields['project_id'];
1165 $task_id = $fields['task_id'];
1166 $timesheet_id = $fields['timesheet_id'];
1167 $invoice_id = $fields['invoice_id'];
1168 $comment = $fields['comment'];
1169 $billable = (int) $fields['billable'];
1170 $approved = (int) $fields['approved'];
1171 $paid = (int) $fields['paid'];
1172 $status = $fields['status'];
1174 $sql = "insert into tt_log".
1175 " (user_id, group_id, org_id, date, start, duration, client_id, project_id, task_id, timesheet_id, invoice_id, comment".
1176 ", billable, approved, paid, created, created_ip, created_by, status)".
1177 " values ($user_id, $group_id, $org_id".
1178 ", ".$mdb2->quote($date).
1179 ", ".$mdb2->quote($start).
1180 ", ".$mdb2->quote($duration).
1181 ", ".$mdb2->quote($client_id).
1182 ", ".$mdb2->quote($project_id).
1183 ", ".$mdb2->quote($task_id).
1184 ", ".$mdb2->quote($timesheet_id).
1185 ", ".$mdb2->quote($invoice_id).
1186 ", ".$mdb2->quote($comment).
1187 ", $billable, $approved, $paid".
1188 ", now(), ".$mdb2->quote($_SERVER['REMOTE_ADDR']).", ".$user->id.
1189 ", ". $mdb2->quote($status).")";
1190 $affected = $mdb2->exec($sql);
1191 if (is_a($affected, 'PEAR_Error')) {
1192 $this->errors->add($i18n->get('error.db')); // TODO: review whether or not to add error here in all insert calls.
1196 $log_id = $mdb2->lastInsertID('tt_log', 'id');
1200 // insertCustomFieldLogEntry - a helper function to insert a custom field log entry.
1201 private function insertCustomFieldLogEntry($fields) {
1202 $mdb2 = getConnection();
1204 $group_id = (int) $fields['group_id'];
1205 $org_id = (int) $fields['org_id'];
1206 $log_id = (int) $fields['log_id'];
1207 $field_id = (int) $fields['field_id'];
1208 $option_id = $fields['option_id'];
1209 $value = $fields['value'];
1210 $status = $fields['status'];
1212 $sql = "insert into tt_custom_field_log (group_id, org_id, log_id, field_id, option_id, value, status)".
1213 " values ($group_id, $org_id, $log_id, $field_id, ".$mdb2->quote($option_id).", ".$mdb2->quote($value).", ".$mdb2->quote($status).")";
1214 $affected = $mdb2->exec($sql);
1215 return (!is_a($affected, 'PEAR_Error'));
1218 // getTopRole returns top role id.
1219 private function getTopRole() {
1220 $mdb2 = getConnection();
1222 $sql = "select id from tt_roles where group_id = 0 and rank = ".MAX_RANK." and status = 1";
1223 $res = $mdb2->query($sql);
1225 if (!is_a($res, 'PEAR_Error')) {
1226 $val = $res->fetchRow();
1233 // The loginExists function detrmines if a login already exists.
1234 private function loginExists($login) {
1235 $mdb2 = getConnection();
1237 $sql = "select id from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)";
1238 $res = $mdb2->query($sql);
1239 if (!is_a($res, 'PEAR_Error')) {
1240 if ($val = $res->fetchRow()) {