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 'name' => $attrs['NAME'],
106 'description' => $attrs['DESCRIPTION'],
107 'currency' => $attrs['CURRENCY'],
108 'decimal_mark' => $attrs['DECIMAL_MARK'],
109 'lang' => $attrs['LANG'],
110 'date_format' => $attrs['DATE_FORMAT'],
111 'time_format' => $attrs['TIME_FORMAT'],
112 'week_start' => $attrs['WEEK_START'],
113 'tracking_mode' => $attrs['TRACKING_MODE'],
114 'project_required' => $attrs['PROJECT_REQUIRED'],
115 'task_required' => $attrs['TASK_REQUIRED'],
116 'record_type' => $attrs['RECORD_TYPE'],
117 'bcc_email' => $attrs['BCC_EMAIL'],
118 'allow_ip' => $attrs['ALLOW_IP'],
119 'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
120 'plugins' => $attrs['PLUGINS'],
121 'lock_spec' => $attrs['LOCK_SPEC'],
122 'workday_minutes' => $attrs['WORKDAY_MINUTES'],
123 'custom_logo' => $attrs['CUSTOM_LOGO'],
124 'config' => $attrs['CONFIG']));
126 // Special handling for top group.
127 if (!$this->org_id && $this->current_group_id) {
128 $this->org_id = $this->current_group_id;
129 $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id";
130 $affected = $mdb2->exec($sql);
132 // Add self to parent stack.
133 array_push($this->parents, $this->current_group_id);
135 // Recycle all maps as we are starting to work on new group.
136 // Note that for this to work properly all nested groups must be last entries in xml for each group.
137 unset($this->currentGroupRoleMap); $this->currentGroupRoleMap = array();
138 unset($this->currentGroupTaskMap); $this->currentGroupTaskMap = array();
139 unset($this->currentGroupProjectMap); $this->currentGroupProjectMap = array();
140 unset($this->currentGroupClientMap); $this->currentGroupClientMap = array();
141 unset($this->currentGroupUserMap); $this->currentGroupUserMap = array();
142 unset($this->currentGroupTimesheetMap); $this->currentGroupTimesheetMap = array();
143 unset($this->currentGroupInvoiceMap); $this->currentGroupInvoiceMap = array();
144 unset($this->currentGroupLogMap); $this->currentGroupLogMap = array();
145 unset($this->currentGroupCustomFieldMap); $this->currentGroupCustomFieldMap = array();
146 unset($this->currentGroupCustomFieldOptionMap); $this->currentGroupCustomFieldOptionMap = array();
147 unset($this->currentGroupFavReportMap); $this->currentGroupCustomFavReportMap = array();
151 if ($name == 'ROLE') {
152 // We get here when processing <role> tags for the current group.
153 $role_id = $this->insertRole(array(
154 'group_id' => $this->current_group_id,
155 'org_id' => $this->org_id,
156 'name' => $attrs['NAME'],
157 'description' => $attrs['DESCRIPTION'],
158 'rank' => $attrs['RANK'],
159 'rights' => $attrs['RIGHTS'],
160 'status' => $attrs['STATUS']));
163 $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
165 $this->errors->add($i18n->get('error.db'));
170 if ($name == 'TASK') {
171 // We get here when processing <task> tags for the current group.
172 $task_id = $this->insertTask(array(
173 'group_id' => $this->current_group_id,
174 'org_id' => $this->org_id,
175 'name' => $attrs['NAME'],
176 'description' => $attrs['DESCRIPTION'],
177 'status' => $attrs['STATUS']));
180 $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
182 $this->errors->add($i18n->get('error.db'));
187 if ($name == 'PROJECT') {
188 // We get here when processing <project> tags for the current group.
190 // Prepare a list of task ids.
191 if ($attrs['TASKS']) {
192 $tasks = explode(',', $attrs['TASKS']);
193 foreach ($tasks as $id)
194 $mapped_tasks[] = $this->currentGroupTaskMap[$id];
197 $project_id = $this->insertProject(array(
198 'group_id' => $this->current_group_id,
199 'org_id' => $this->org_id,
200 'name' => $attrs['NAME'],
201 'description' => $attrs['DESCRIPTION'],
202 'tasks' => $mapped_tasks,
203 'status' => $attrs['STATUS']));
206 $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
208 $this->errors->add($i18n->get('error.db'));
213 if ($name == 'CLIENT') {
214 // We get here when processing <client> tags for the current group.
216 // Prepare a list of project ids.
217 if ($attrs['PROJECTS']) {
218 $projects = explode(',', $attrs['PROJECTS']);
219 foreach ($projects as $id)
220 $mapped_projects[] = $this->currentGroupProjectMap[$id];
223 $client_id = $this->insertClient(array(
224 'group_id' => $this->current_group_id,
225 'org_id' => $this->org_id,
226 'name' => $attrs['NAME'],
227 'address' => $attrs['ADDRESS'],
228 'tax' => $attrs['TAX'],
229 'projects' => $mapped_projects,
230 'status' => $attrs['STATUS']));
233 $this->currentGroupClientMap[$attrs['ID']] = $client_id;
235 $this->errors->add($i18n->get('error.db'));
240 if ($name == 'USER') {
241 // We get here when processing <user> tags for the current group.
243 $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id : $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
245 $user_id = $this->insertUser(array(
246 'group_id' => $this->current_group_id,
247 'org_id' => $this->org_id,
248 'role_id' => $role_id,
249 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
250 'name' => $attrs['NAME'],
251 'login' => $attrs['LOGIN'],
252 'password' => $attrs['PASSWORD'],
253 'rate' => $attrs['RATE'],
254 'quota_percent' => $attrs['QUOTA_PERCENT'],
255 'email' => $attrs['EMAIL'],
256 'status' => $attrs['STATUS']), false);
259 $this->currentGroupUserMap[$attrs['ID']] = $user_id;
261 $this->errors->add($i18n->get('error.db'));
266 if ($name == 'USER_PROJECT_BIND') {
267 if (!$this->insertUserProjectBind(array(
268 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
269 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
270 'group_id' => $this->current_group_id,
271 'org_id' => $this->org_id,
272 'rate' => $attrs['RATE'],
273 'status' => $attrs['STATUS']))) {
274 $this->errors->add($i18n->get('error.db'));
279 if ($name == 'TIMESHEET') {
280 // We get here when processing <timesheet> tags for the current group.
281 $timesheet_id = $this->insertTimesheet(array(
282 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
283 'group_id' => $this->current_group_id,
284 'org_id' => $this->org_id,
285 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
286 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
287 'name' => $attrs['NAME'],
288 'comment' => $attrs['COMMENT'],
289 'start_date' => $attrs['START_DATE'],
290 'end_date' => $attrs['END_DATE'],
291 'submit_status' => $attrs['SUBMIT_STATUS'],
292 'approve_status' => $attrs['APPROVE_STATUS'],
293 'approve_comment' => $attrs['APPROVE_COMMENT'],
294 'status' => $attrs['STATUS']));
297 $this->currentGroupTimesheetMap[$attrs['ID']] = $timesheet_id;
299 $this->errors->add($i18n->get('error.db'));
304 if ($name == 'INVOICE') {
305 // We get here when processing <invoice> tags for the current group.
306 $invoice_id = $this->insertInvoice(array(
307 'group_id' => $this->current_group_id,
308 'org_id' => $this->org_id,
309 'name' => $attrs['NAME'],
310 'date' => $attrs['DATE'],
311 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
312 'status' => $attrs['STATUS']));
315 $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
317 $this->errors->add($i18n->get('error.db'));
322 if ($name == 'LOG_ITEM') {
323 // We get here when processing <log_item> tags for the current group.
324 $log_item_id = $this->insertLogEntry(array(
325 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
326 'group_id' => $this->current_group_id,
327 'org_id' => $this->org_id,
328 'date' => $attrs['DATE'],
329 'start' => $attrs['START'],
330 'finish' => $attrs['FINISH'],
331 'duration' => $attrs['DURATION'],
332 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
333 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
334 'task_id' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
335 'timesheet_id' => $this->currentGroupTimesheetMap[$attrs['TIMESHEET_ID']],
336 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
337 'comment' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
338 'billable' => $attrs['BILLABLE'],
339 'approved' => $attrs['APPROVED'],
340 'paid' => $attrs['PAID'],
341 'status' => $attrs['STATUS']));
344 $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
345 } else $this->errors->add($i18n->get('error.db'));
349 if ($name == 'CUSTOM_FIELD') {
350 // We get here when processing <custom_field> tags for the current group.
351 $custom_field_id = $this->insertCustomField(array(
352 'group_id' => $this->current_group_id,
353 'org_id' => $this->org_id,
354 'type' => $attrs['TYPE'],
355 'label' => $attrs['LABEL'],
356 'required' => $attrs['REQUIRED'],
357 'status' => $attrs['STATUS']));
358 if ($custom_field_id) {
360 $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
361 } else $this->errors->add($i18n->get('error.db'));
365 if ($name == 'CUSTOM_FIELD_OPTION') {
366 // We get here when processing <custom_field_option> tags for the current group.
367 $custom_field_option_id = $this->insertCustomFieldOption(array(
368 'group_id' => $this->current_group_id,
369 'org_id' => $this->org_id,
370 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
371 'value' => $attrs['VALUE']));
372 if ($custom_field_option_id) {
374 $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
375 } else $this->errors->add($i18n->get('error.db'));
379 if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
380 // We get here when processing <custom_field_log_entry> tags for the current group.
381 if (!$this->insertCustomFieldLogEntry(array(
382 'group_id' => $this->current_group_id,
383 'org_id' => $this->org_id,
384 'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
385 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
386 'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
387 'value' => $attrs['VALUE'],
388 'status' => $attrs['STATUS']))) {
389 $this->errors->add($i18n->get('error.db'));
394 if ($name == 'EXPENSE_ITEM') {
395 // We get here when processing <expense_item> tags for the current group.
396 $expense_item_id = $this->insertExpense(array(
397 'date' => $attrs['DATE'],
398 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
399 'group_id' => $this->current_group_id,
400 'org_id' => $this->org_id,
401 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
402 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
403 'timesheet_id' => $this->currentGroupTimesheetMap[$attrs['TIMESHEET_ID']],
404 'name' => $attrs['NAME'],
405 'cost' => $attrs['COST'],
406 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
407 'approved' => $attrs['APPROVED'],
408 'paid' => $attrs['PAID'],
409 'status' => $attrs['STATUS']));
410 if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
414 if ($name == 'PREDEFINED_EXPENSE') {
415 if (!$this->insertPredefinedExpense(array(
416 'group_id' => $this->current_group_id,
417 'org_id' => $this->org_id,
418 'name' => $attrs['NAME'],
419 'cost' => $attrs['COST']))) {
420 $this->errors->add($i18n->get('error.db'));
425 if ($name == 'MONTHLY_QUOTA') {
426 if (!$this->insertMonthlyQuota(array(
427 'group_id' => $this->current_group_id,
428 'org_id' => $this->org_id,
429 'year' => $attrs['YEAR'],
430 'month' => $attrs['MONTH'],
431 'minutes' => $attrs['MINUTES']))) {
432 $this->errors->add($i18n->get('error.db'));
437 if ($name == 'FAV_REPORT') {
439 if (strlen($attrs['USERS']) > 0) {
440 $arr = explode(',', $attrs['USERS']);
442 $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
444 $fav_report_id = $this->insertFavReport(array(
445 'name' => $attrs['NAME'],
446 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
447 'group_id' => $this->current_group_id,
448 'org_id' => $this->org_id,
449 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
450 'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
451 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
452 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
453 'billable' => $attrs['BILLABLE'],
454 'approved' => $attrs['APPROVED'],
455 'invoice' => $attrs['INVOICE'],
456 'timesheet' => $attrs['TIMESHEET'],
457 'paid_status' => $attrs['PAID_STATUS'],
458 'users' => $user_list,
459 'period' => $attrs['PERIOD'],
460 'from' => $attrs['PERIOD_START'],
461 'to' => $attrs['PERIOD_END'],
462 'chclient' => (int) $attrs['SHOW_CLIENT'],
463 'chinvoice' => (int) $attrs['SHOW_INVOICE'],
464 'chpaid' => (int) $attrs['SHOW_PAID'],
465 'chip' => (int) $attrs['SHOW_IP'],
466 'chproject' => (int) $attrs['SHOW_PROJECT'],
467 'chtimesheet' => (int) $attrs['SHOW_TIMESHEET'],
468 'chstart' => (int) $attrs['SHOW_START'],
469 'chduration' => (int) $attrs['SHOW_DURATION'],
470 'chcost' => (int) $attrs['SHOW_COST'],
471 'chtask' => (int) $attrs['SHOW_TASK'],
472 'chfinish' => (int) $attrs['SHOW_END'],
473 'chnote' => (int) $attrs['SHOW_NOTE'],
474 'chapproved' => (int) $attrs['SHOW_APPROVED'],
475 'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
476 'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
477 'group_by1' => $attrs['GROUP_BY1'],
478 'group_by2' => $attrs['GROUP_BY2'],
479 'group_by3' => $attrs['GROUP_BY3'],
480 'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
481 if ($fav_report_id) {
483 $this->currentGroupFavReportMap[$attrs['ID']] = $fav_report_id;
484 } else $this->errors->add($i18n->get('error.db'));
488 if ($name == 'NOTIFICATION') {
489 if (!$this->insertNotification(array(
490 'group_id' => $this->current_group_id,
491 'org_id' => $this->org_id,
492 'cron_spec' => $attrs['CRON_SPEC'],
493 'last' => $attrs['LAST'],
494 'next' => $attrs['NEXT'],
495 'report_id' => $this->currentGroupFavReportMap[$attrs['REPORT_ID']],
496 'email' => $attrs['EMAIL'],
497 'cc' => $attrs['CC'],
498 'subject' => $attrs['SUBJECT'],
499 'report_condition' => $attrs['REPORT_CONDITION'],
500 'status' => $attrs['STATUS']))) {
501 $this->errors->add($i18n->get('error.db'));
506 if ($name == 'USER_PARAM') {
507 if (!$this->insertUserParam(array(
508 'group_id' => $this->current_group_id,
509 'org_id' => $this->org_id,
510 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
511 'param_name' => $attrs['PARAM_NAME'],
512 'param_value' => $attrs['PARAM_VALUE']))) {
513 $this->errors->add($i18n->get('error.db'));
520 // endElement - callback handler for ending tags in XML.
521 // We use this only for process </group> element endings and
522 // set current_group_id to an immediate parent.
523 // This is required to import group hierarchy correctly.
524 function endElement($parser, $name) {
525 // No need to care about first or second pass, as this is used only in second pass.
526 // See 2nd xml_set_element_handler, where this handler is set.
527 if ($name == 'GROUP') {
528 // Remove self from the parent stack.
529 $self = array_pop($this->parents);
530 // Set current group id to an immediate parent.
531 $len = count($this->parents);
532 $this->current_group_id = $len ? $this->parents[$len-1] : null;
536 // importXml - uncompresses the file, reads and parses its content.
537 // It goes through the file 2 times.
539 // During 1st pass, it determines whether we can import data.
540 // In 1st pass, startElement function is called as many times as necessary.
542 // Actual import occurs during 2nd pass.
543 // In 2nd pass, startElement and endElement are called many times.
544 // We only use endElement to finish current group processing.
546 // The above allows us to export/import complex orgs with nested groups,
547 // while by design all data are in attributes of the elements (no CDATA).
549 // There is currently at least one problem with keeping all data in attributes:
550 // a vertical tab character 0xB anywhere breaks parsing, making import impossible.
551 // See https://github.com/sparklemotion/nokogiri/issues/1581 - looks like
552 // an XML standard thing. Apparently, other invalid characters break parsing too.
553 // This problem needs to be addressed at some point but how exactly without
554 // complicating export-import too much with CDATA and dataElement processing?
555 function importXml() {
558 if (!$_FILES['xmlfile']['name']) {
559 $this->errors->add($i18n->get('error.upload'));
560 return; // There is nothing to do if we don't have a file.
563 // Do we have a compressed file?
565 $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
566 if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
570 // Create a temporary file.
571 $dirName = dirname(TEMPLATE_DIR . '_c/.');
572 $filename = tempnam($dirName, 'import_');
574 // If the file is compressed - uncompress it.
576 if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
577 $this->errors->add($i18n->get('error.sys'));
580 unlink($_FILES['xmlfile']['tmp_name']);
582 if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
583 $this->errors->add($i18n->get('error.upload'));
588 // Initialize XML parser.
589 $parser = xml_parser_create();
590 xml_set_object($parser, $this);
591 xml_set_element_handler($parser, 'startElement', false); // No need to process end tags in 1st pass.
593 // We need to parse the file 2 times:
594 // 1) First pass: determine if import is possible.
595 // 2) Second pass: import data, one tag at a time.
597 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
598 $file = fopen($filename, 'r');
599 while (($data = fread($file, 4096)) && $this->errors->no()) {
600 if (!xml_parse($parser, $data, feof($file))) {
601 $this->errors->add(sprintf($i18n->get('error.xml'),
602 xml_get_current_line_number($parser),
603 xml_error_string(xml_get_error_code($parser))));
606 if ($this->conflicting_logins) {
607 $this->canImport = false;
608 $this->errors->add($i18n->get('error.user_exists'));
609 $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins));
611 if (!ttUserHelper::canAdd($this->num_users)) {
612 $this->canImport = false;
613 $this->errors->add($i18n->get('error.user_count'));
616 $this->firstPass = false; // We are done with 1st pass.
617 xml_parser_free($parser);
618 if ($file) fclose($file);
619 if ($this->errors->yes()) {
620 // Remove the file and exit if we have errors.
625 // Now we can do a second pass, where real work is done.
626 $parser = xml_parser_create();
627 xml_set_object($parser, $this);
628 xml_set_element_handler($parser, 'startElement', 'endElement'); // Need to process ending tags too.
630 // Read and parse the content of the file. During parsing, startElement and endElement are called back for each tag.
631 $file = fopen($filename, 'r');
632 while (($data = fread($file, 4096)) && $this->errors->no()) {
633 if (!xml_parse($parser, $data, feof($file))) {
634 $this->errors->add(sprintf($i18n->get('error.xml'),
635 xml_get_current_line_number($parser),
636 xml_error_string(xml_get_error_code($parser))));
639 xml_parser_free($parser);
640 if ($file) fclose($file);
644 // uncompress - uncompresses the content of the $in file into the $out file.
645 function uncompress($in, $out) {
646 // Do we have the uncompress function?
647 if (!function_exists('bzopen'))
650 // Initial checks of file names and permissions.
651 if (!file_exists($in) || !is_readable ($in))
653 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
656 if (!$out_file = fopen($out, 'wb'))
658 if (!$in_file = bzopen ($in, 'r'))
661 while (!feof($in_file)) {
662 $buffer = bzread($in_file, 4096);
663 fwrite($out_file, $buffer, 4096);
670 // createGroup function creates a new group.
671 private function createGroup($fields) {
674 $mdb2 = getConnection();
676 $columns = '(parent_id, org_id, name, description, currency, decimal_mark, lang, date_format, time_format'.
677 ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
678 ', allow_ip, password_complexity, plugins, lock_spec'.
679 ', workday_minutes, config, created, created_ip, created_by)';
681 $values = ' values (';
682 $values .= $mdb2->quote($fields['parent_id']);
683 $values .= ', '.$mdb2->quote($fields['org_id']);
684 $values .= ', '.$mdb2->quote(trim($fields['name']));
685 $values .= ', '.$mdb2->quote(trim($fields['description']));
686 $values .= ', '.$mdb2->quote(trim($fields['currency']));
687 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
688 $values .= ', '.$mdb2->quote($fields['lang']);
689 $values .= ', '.$mdb2->quote($fields['date_format']);
690 $values .= ', '.$mdb2->quote($fields['time_format']);
691 $values .= ', '.(int)$fields['week_start'];
692 $values .= ', '.(int)$fields['tracking_mode'];
693 $values .= ', '.(int)$fields['project_required'];
694 $values .= ', '.(int)$fields['task_required'];
695 $values .= ', '.(int)$fields['record_type'];
696 $values .= ', '.$mdb2->quote($fields['bcc_email']);
697 $values .= ', '.$mdb2->quote($fields['allow_ip']);
698 $values .= ', '.$mdb2->quote($fields['password_complexity']);
699 $values .= ', '.$mdb2->quote($fields['plugins']);
700 $values .= ', '.$mdb2->quote($fields['lock_spec']);
701 $values .= ', '.(int)$fields['workday_minutes'];
702 $values .= ', '.$mdb2->quote($fields['config']);
703 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
706 $sql = 'insert into tt_groups '.$columns.$values;
707 $affected = $mdb2->exec($sql);
708 if (is_a($affected, 'PEAR_Error')) {
709 $this->errors->add($i18n->get('error.db'));
713 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
717 // insertMonthlyQuota - a helper function to insert a monthly quota.
718 private function insertMonthlyQuota($fields) {
719 $mdb2 = getConnection();
721 $group_id = (int) $fields['group_id'];
722 $org_id = (int) $fields['org_id'];
723 $year = (int) $fields['year'];
724 $month = (int) $fields['month'];
725 $minutes = (int) $fields['minutes'];
727 $sql = "INSERT INTO tt_monthly_quotas (group_id, org_id, year, month, minutes)".
728 " values ($group_id, $org_id, $year, $month, $minutes)";
729 $affected = $mdb2->exec($sql);
730 return (!is_a($affected, 'PEAR_Error'));
733 // insertPredefinedExpense - a helper function to insert a predefined expense.
734 private function insertPredefinedExpense($fields) {
735 $mdb2 = getConnection();
737 $group_id = (int) $fields['group_id'];
738 $org_id = (int) $fields['org_id'];
739 $name = $mdb2->quote($fields['name']);
740 $cost = $mdb2->quote($fields['cost']);
742 $sql = "INSERT INTO tt_predefined_expenses (group_id, org_id, name, cost)".
743 " values ($group_id, $org_id, $name, $cost)";
744 $affected = $mdb2->exec($sql);
745 return (!is_a($affected, 'PEAR_Error'));
748 // insertExpense - a helper function to insert an expense item.
749 private function insertExpense($fields) {
751 $mdb2 = getConnection();
753 $group_id = (int) $fields['group_id'];
754 $org_id = (int) $fields['org_id'];
755 $date = $fields['date'];
756 $user_id = (int) $fields['user_id'];
757 $client_id = $fields['client_id'];
758 $project_id = $fields['project_id'];
759 $timesheet_id = $fields['timesheet_id'];
760 $name = $fields['name'];
761 $cost = str_replace(',', '.', $fields['cost']);
762 $invoice_id = $fields['invoice_id'];
763 $status = $fields['status'];
764 $approved = (int) $fields['approved'];
765 $paid = (int) $fields['paid'];
766 $created = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
768 $sql = "insert into tt_expense_items".
769 " (date, user_id, group_id, org_id, client_id, project_id, timesheet_id, name,".
770 " cost, invoice_id, approved, paid, created, created_ip, created_by, status)".
771 " values (".$mdb2->quote($date).", $user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
772 ", ".$mdb2->quote($timesheet_id).", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).
773 ", $approved, $paid $created, ".$mdb2->quote($status).")";
774 $affected = $mdb2->exec($sql);
775 return (!is_a($affected, 'PEAR_Error'));
778 // insertTask function inserts a new task into database.
779 private function insertTask($fields)
781 $mdb2 = getConnection();
783 $group_id = (int) $fields['group_id'];
784 $org_id = (int) $fields['org_id'];
785 $name = $fields['name'];
786 $description = $fields['description'];
787 $projects = $fields['projects'];
788 $status = $fields['status'];
790 $sql = "insert into tt_tasks (group_id, org_id, name, description, status)
791 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($status).")";
792 $affected = $mdb2->exec($sql);
794 if (is_a($affected, 'PEAR_Error'))
797 $last_id = $mdb2->lastInsertID('tt_tasks', 'id');
801 // insertUserProjectBind - inserts a user to project bind into tt_user_project_binds table.
802 private function insertUserProjectBind($fields) {
803 $mdb2 = getConnection();
805 $group_id = (int) $fields['group_id'];
806 $org_id = (int) $fields['org_id'];
807 $user_id = (int) $fields['user_id'];
808 $project_id = (int) $fields['project_id'];
809 $rate = $mdb2->quote($fields['rate']);
810 $status = $mdb2->quote($fields['status']);
812 $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
813 " values($user_id, $project_id, $group_id, $org_id, $rate, $status)";
814 $affected = $mdb2->exec($sql);
815 return (!is_a($affected, 'PEAR_Error'));
818 // insertUser - inserts a user into database.
819 private function insertUser($fields) {
821 $mdb2 = getConnection();
823 $group_id = (int) $fields['group_id'];
824 $org_id = (int) $fields['org_id'];
826 $columns = '(login, password, name, group_id, org_id, role_id, client_id, rate, quota_percent, email, created, created_ip, created_by, status)';
828 $values = 'values (';
829 $values .= $mdb2->quote($fields['login']);
830 $values .= ', '.$mdb2->quote($fields['password']);
831 $values .= ', '.$mdb2->quote($fields['name']);
832 $values .= ', '.$group_id;
833 $values .= ', '.$org_id;
834 $values .= ', '.(int)$fields['role_id'];
835 $values .= ', '.$mdb2->quote($fields['client_id']);
836 $values .= ', '.$mdb2->quote($fields['rate']);
837 $values .= ', '.$mdb2->quote($fields['quota_percent']);
838 $values .= ', '.$mdb2->quote($fields['email']);
839 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
840 $values .= ', '.$mdb2->quote($fields['status']);
843 $sql = "insert into tt_users $columns $values";
844 $affected = $mdb2->exec($sql);
845 if (is_a($affected, 'PEAR_Error')) return false;
847 $last_id = $mdb2->lastInsertID('tt_users', 'id');
851 // insertProject - a helper function to insert a project as well as project to task binds.
852 private function insertProject($fields)
854 $mdb2 = getConnection();
856 $group_id = (int) $fields['group_id'];
857 $org_id = (int) $fields['org_id'];
858 $name = $fields['name'];
859 $description = $fields['description'];
860 $tasks = $fields['tasks'];
861 $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
862 $status = $fields['status'];
864 $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
865 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
866 $affected = $mdb2->exec($sql);
867 if (is_a($affected, 'PEAR_Error'))
870 $last_id = $mdb2->lastInsertID('tt_projects', 'id');
872 // Insert binds into tt_project_task_binds table.
873 if (is_array($tasks)) {
874 foreach ($tasks as $task_id) {
875 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
876 " values($last_id, $task_id, $group_id, $org_id)";
877 $affected = $mdb2->exec($sql);
878 if (is_a($affected, 'PEAR_Error'))
886 // insertRole - inserts a role into tt_roles table.
887 private function insertRole($fields)
889 $mdb2 = getConnection();
891 $group_id = (int) $fields['group_id'];
892 $org_id = (int) $fields['org_id'];
893 $name = $fields['name'];
894 $rank = (int) $fields['rank'];
895 $description = $fields['description'];
896 $rights = $fields['rights'];
897 $status = $fields['status'];
899 $sql = "insert into tt_roles (group_id, org_id, name, rank, description, rights, status)
900 values ($group_id, $org_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
901 $affected = $mdb2->exec($sql);
902 if (is_a($affected, 'PEAR_Error'))
905 $last_id = $mdb2->lastInsertID('tt_roles', 'id');
909 // insertTimesheet - inserts a timesheet in database.
910 private function insertTimesheet($fields)
912 $mdb2 = getConnection();
914 $user_id = (int) $fields['user_id'];
915 $group_id = (int) $fields['group_id'];
916 $org_id = (int) $fields['org_id'];
917 $client_id = $fields['client_id'];
918 $project_id = $fields['project_id'];
919 $name = $fields['name'];
920 $comment = $fields['comment'];
921 $start_date = $fields['start_date'];
922 $end_date = $fields['end_date'];
923 $submit_status = $fields['submit_status'];
924 $approve_status = $fields['approve_status'];
925 $approve_comment = $fields['approve_comment'];
926 $status = $fields['status'];
928 // Insert a new timesheet record.
929 $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name,".
930 " comment, start_date, end_date, submit_status, approve_status, approve_comment, status)".
931 " values($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).", ".
932 $mdb2->quote($comment).", ".$mdb2->quote($start_date).", ".$mdb2->quote($end_date).", ".
933 $mdb2->quote($submit_status).", ".$mdb2->quote($approve_status).", ".
934 $mdb2->quote($approve_comment).", ".$mdb2->quote($status).")";
935 $affected = $mdb2->exec($sql);
936 if (is_a($affected, 'PEAR_Error')) return false;
938 $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
942 // insertInvoice - inserts an invoice in database.
943 private function insertInvoice($fields)
945 $mdb2 = getConnection();
947 $group_id = (int) $fields['group_id'];
948 $org_id = (int) $fields['org_id'];
949 $name = $fields['name'];
950 $client_id = (int) $fields['client_id'];
951 $date = $fields['date'];
952 $status = $fields['status'];
954 // Insert a new invoice record.
955 $sql = "insert into tt_invoices (group_id, org_id, name, date, client_id, status)".
956 " values($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($date).", $client_id, ".$mdb2->quote($fields['status']).")";
957 $affected = $mdb2->exec($sql);
958 if (is_a($affected, 'PEAR_Error')) return false;
960 $last_id = $mdb2->lastInsertID('tt_invoices', 'id');
964 // The insertClient function inserts a new client as well as client to project binds.
965 private function insertClient($fields)
967 $mdb2 = getConnection();
969 $group_id = (int) $fields['group_id'];
970 $org_id = (int) $fields['org_id'];
971 $name = $fields['name'];
972 $address = $fields['address'];
973 $tax = $fields['tax'];
974 $projects = $fields['projects'];
976 $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
977 $status = $fields['status'];
979 $tax = str_replace(',', '.', $tax);
980 if ($tax == '') $tax = 0;
982 $sql = "insert into tt_clients (group_id, org_id, name, address, tax, projects, status)".
983 " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
985 $affected = $mdb2->exec($sql);
986 if (is_a($affected, 'PEAR_Error'))
989 $last_id = $mdb2->lastInsertID('tt_clients', 'id');
991 if (count($projects) > 0)
992 foreach ($projects as $p_id) {
993 $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)";
994 $affected = $mdb2->exec($sql);
995 if (is_a($affected, 'PEAR_Error'))
1002 // insertFavReport - inserts a favorite report in database.
1003 private function insertFavReport($fields) {
1004 $mdb2 = getConnection();
1006 $group_id = (int) $fields['group_id'];
1007 $org_id = (int) $fields['org_id'];
1009 $sql = "insert into tt_fav_reports".
1010 " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
1011 " billable, approved, invoice, timesheet, paid_status, users, period, period_start, period_end,".
1012 " show_client, show_invoice, show_paid, show_ip,".
1013 " show_project, show_timesheet, show_start, show_duration, show_cost,".
1014 " show_task, show_end, show_note, show_approved, show_custom_field_1, show_work_units,".
1015 " group_by1, group_by2, group_by3, show_totals_only)".
1017 $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
1018 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
1019 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
1020 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['approved']).", ".
1021 $mdb2->quote($fields['invoice']).", ".$mdb2->quote($fields['timesheet']).", ".
1022 $mdb2->quote($fields['paid_status']).", ".
1023 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
1024 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
1025 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
1026 $fields['chproject'].", ".$fields['chtimesheet'].", ".$fields['chstart'].", ".$fields['chduration'].", ".
1027 $fields['chcost'].", ".$fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".
1028 $fields['chapproved'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
1029 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
1030 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
1031 $affected = $mdb2->exec($sql);
1032 if (is_a($affected, 'PEAR_Error'))
1035 $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
1039 // insertNotification function inserts a new notification into database.
1040 private function insertNotification($fields)
1042 $mdb2 = getConnection();
1044 $group_id = (int) $fields['group_id'];
1045 $org_id = (int) $fields['org_id'];
1046 $cron_spec = $fields['cron_spec'];
1047 $last = (int) $fields['last'];
1048 $next = (int) $fields['next'];
1049 $report_id = (int) $fields['report_id'];
1050 $email = $fields['email'];
1051 $cc = $fields['cc'];
1052 $subject = $fields['subject'];
1053 $report_condition = $fields['report_condition'];
1054 $status = $fields['status'];
1056 $sql = "insert into tt_cron".
1057 " (group_id, org_id, cron_spec, last, next, report_id, email, cc, subject, report_condition, status)".
1058 " 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).")";
1059 $affected = $mdb2->exec($sql);
1060 return (!is_a($affected, 'PEAR_Error'));
1063 // insertUserParam - a helper function to insert a user parameter.
1064 private function insertUserParam($fields) {
1065 $mdb2 = getConnection();
1067 $group_id = (int) $fields['group_id'];
1068 $org_id = (int) $fields['org_id'];
1069 $user_id = (int) $fields['user_id'];
1070 $param_name = $fields['param_name'];
1071 $param_value = $fields['param_value'];
1073 $sql = "insert into tt_config".
1074 " (user_id, group_id, org_id, param_name, param_value)".
1075 " values ($user_id, $group_id, $org_id, ".$mdb2->quote($param_name).", ".$mdb2->quote($param_value).")";
1076 $affected = $mdb2->exec($sql);
1077 return (!is_a($affected, 'PEAR_Error'));
1080 // insertCustomField - a helper function to insert a custom field.
1081 private function insertCustomField($fields) {
1082 $mdb2 = getConnection();
1084 $group_id = (int) $fields['group_id'];
1085 $org_id = (int) $fields['org_id'];
1086 $type = (int) $fields['type'];
1087 $label = $fields['label'];
1088 $required = (int) $fields['required'];
1089 $status = $fields['status'];
1091 $sql = "insert into tt_custom_fields".
1092 " (group_id, org_id, type, label, required, status)".
1093 " values($group_id, $org_id, $type, ".$mdb2->quote($label).", $required, ".$mdb2->quote($status).")";
1094 $affected = $mdb2->exec($sql);
1095 if (is_a($affected, 'PEAR_Error'))
1098 $last_id = $mdb2->lastInsertID('tt_custom_fields', 'id');
1102 // insertCustomFieldOption - a helper function to insert a custom field option.
1103 private function insertCustomFieldOption($fields) {
1104 $mdb2 = getConnection();
1106 $group_id = (int) $fields['group_id'];
1107 $org_id = (int) $fields['org_id'];
1108 $field_id = (int) $fields['field_id'];
1109 $value = $fields['value'];
1111 $sql = "insert into tt_custom_field_options (group_id, org_id, field_id, value)".
1112 " values ($group_id, $org_id, $field_id, ".$mdb2->quote($value).")";
1113 $affected = $mdb2->exec($sql);
1114 if (is_a($affected, 'PEAR_Error'))
1117 $last_id = $mdb2->lastInsertID('tt_custom_field_options', 'id');
1121 // insertLogEntry - a helper function to insert a time log entry.
1122 private function insertLogEntry($fields) {
1124 $mdb2 = getConnection();
1126 $group_id = (int) $fields['group_id'];
1127 $org_id = (int) $fields['org_id'];
1128 $user_id = (int) $fields['user_id'];
1129 $date = $fields['date'];
1130 $start = $fields['start'];
1131 $duration = $fields['duration'];
1132 $client_id = $fields['client_id'];
1133 $project_id = $fields['project_id'];
1134 $task_id = $fields['task_id'];
1135 $timesheet_id = $fields['timesheet_id'];
1136 $invoice_id = $fields['invoice_id'];
1137 $comment = $fields['comment'];
1138 $billable = (int) $fields['billable'];
1139 $approved = (int) $fields['approved'];
1140 $paid = (int) $fields['paid'];
1141 $status = $fields['status'];
1143 $sql = "insert into tt_log".
1144 " (user_id, group_id, org_id, date, start, duration, client_id, project_id, task_id, timesheet_id, invoice_id, comment".
1145 ", billable, approved, paid, created, created_ip, created_by, status)".
1146 " values ($user_id, $group_id, $org_id".
1147 ", ".$mdb2->quote($date).
1148 ", ".$mdb2->quote($start).
1149 ", ".$mdb2->quote($duration).
1150 ", ".$mdb2->quote($client_id).
1151 ", ".$mdb2->quote($project_id).
1152 ", ".$mdb2->quote($task_id).
1153 ", ".$mdb2->quote($timesheet_id).
1154 ", ".$mdb2->quote($invoice_id).
1155 ", ".$mdb2->quote($comment).
1156 ", $billable, $approved, $paid".
1157 ", now(), ".$mdb2->quote($_SERVER['REMOTE_ADDR']).", ".$user->id.
1158 ", ". $mdb2->quote($status).")";
1159 $affected = $mdb2->exec($sql);
1160 if (is_a($affected, 'PEAR_Error')) {
1161 $this->errors->add($i18n->get('error.db')); // TODO: review whether or not to add error here in all insert calls.
1165 $log_id = $mdb2->lastInsertID('tt_log', 'id');
1169 // insertCustomFieldLogEntry - a helper function to insert a custom field log entry.
1170 private function insertCustomFieldLogEntry($fields) {
1171 $mdb2 = getConnection();
1173 $group_id = (int) $fields['group_id'];
1174 $org_id = (int) $fields['org_id'];
1175 $log_id = (int) $fields['log_id'];
1176 $field_id = (int) $fields['field_id'];
1177 $option_id = $fields['option_id'];
1178 $value = $fields['value'];
1179 $status = $fields['status'];
1181 $sql = "insert into tt_custom_field_log (group_id, org_id, log_id, field_id, option_id, value, status)".
1182 " values ($group_id, $org_id, $log_id, $field_id, ".$mdb2->quote($option_id).", ".$mdb2->quote($value).", ".$mdb2->quote($status).")";
1183 $affected = $mdb2->exec($sql);
1184 return (!is_a($affected, 'PEAR_Error'));
1187 // getTopRole returns top role id.
1188 private function getTopRole() {
1189 $mdb2 = getConnection();
1191 $sql = "select id from tt_roles where group_id = 0 and rank = ".MAX_RANK." and status = 1";
1192 $res = $mdb2->query($sql);
1194 if (!is_a($res, 'PEAR_Error')) {
1195 $val = $res->fetchRow();
1202 // The loginExists function detrmines if a login already exists.
1203 private function loginExists($login) {
1204 $mdb2 = getConnection();
1206 $sql = "select id from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)";
1207 $res = $mdb2->query($sql);
1208 if (!is_a($res, 'PEAR_Error')) {
1209 if ($val = $res->fetchRow()) {