2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttUserHelper');
30 import('ttRoleHelper');
31 import('ttTaskHelper');
32 import('ttProjectHelper');
33 import('ttClientHelper');
34 import('ttInvoiceHelper');
35 import('ttTimeHelper');
36 import('ttCustomFieldHelper');
37 import('ttExpenseHelper');
38 import('ttFavReportHelper');
40 // ttOrgImportHelper class is used to import organization data from an XML file
41 // prepared by ttOrgExportHelper and consisting of nested groups with their info.
42 class ttOrgImportHelper {
43 var $errors = null; // Errors go here. Set in constructor by reference.
44 var $schema_version = null; // Database schema version from XML file we import from.
45 var $conflicting_logins = null; // A comma-separated list of logins we cannot import.
46 var $canImport = true; // False if we cannot import data due to a conflict such as login collision.
47 var $firstPass = true; // True during first pass through the file.
48 var $org_id = null; // Organization id (same as top group_id).
49 var $current_group_id = null; // Current group id during parsing.
50 var $current_parent_group_id = null; // Current parent group id during parsing.
51 var $top_role_id = 0; // Top role id.
53 // Entity maps for current group. They map XML ids with database ids.
54 var $currentGroupRoleMap = array();
55 var $currentGroupTaskMap = array();
56 var $currentGroupProjectMap = array();
57 var $currentGroupClientMap = array();
58 var $currentGroupUserMap = array();
59 var $currentGroupInvoiceMap = array();
60 var $currentGroupLogMap = array();
61 var $currentGroupCustomFieldMap = array();
62 var $currentGroupCustomFieldOptionMap = array();
65 function __construct(&$errors) {
66 $this->errors = &$errors;
67 $this->top_role_id = ttRoleHelper::getRoleByRank(512, 0);
70 // startElement - callback handler for opening tags in XML.
71 function startElement($parser, $name, $attrs) {
74 // First pass through the file determines if we can import data.
75 // We require 2 things:
76 // 1) Database schema version must be set. This ensures we have a compatible file.
77 // 2) No login coillisions are allowed.
78 if ($this->firstPass) {
79 if ($name == 'ORG' && $this->canImport) {
80 if ($attrs['SCHEMA'] == null) {
81 // We need (database) schema attribute to be available for import to work.
82 // Old Time Tracker export files don't have this.
83 // Current import code does not work with old format because we had to
84 // restructure data in export files for subgroup support.
85 $this->canImport = false;
86 $this->errors->add($i18n->get('error.format'));
91 // In first pass we check user logins for potential collisions with existing.
92 if ($name == 'USER' && $this->canImport) {
93 $login = $attrs['LOGIN'];
94 if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) {
95 // We have a login collision. Append colliding login to a list of things we cannot import.
96 $this->conflicting_logins .= ($this->conflicting_logins ? ", $login" : $login);
97 // The above is printed in error message with all found colliding logins.
102 // Second pass processing. We import data here, one tag at a time.
103 if (!$this->firstPass && $this->canImport && $this->errors->no()) {
104 $mdb2 = getConnection();
106 // We are in second pass and can import data.
107 if ($name == 'GROUP') {
108 // Create a new group.
109 $this->current_group_id = $this->createGroup(array(
110 'parent_id' => $this->current_parent_group_id,
111 'org_id' => $this->org_id,
112 'name' => $attrs['NAME'],
113 'currency' => $attrs['CURRENCY'],
114 'decimal_mark' => $attrs['DECIMAL_MARK'],
115 'lang' => $attrs['LANG'],
116 'date_format' => $attrs['DATE_FORMAT'],
117 'time_format' => $attrs['TIME_FORMAT'],
118 'week_start' => $attrs['WEEK_START'],
119 'tracking_mode' => $attrs['TRACKING_MODE'],
120 'project_required' => $attrs['PROJECT_REQUIRED'],
121 'task_required' => $attrs['TASK_REQUIRED'],
122 'record_type' => $attrs['RECORD_TYPE'],
123 'bcc_email' => $attrs['BCC_EMAIL'],
124 'allow_ip' => $attrs['ALLOW_IP'],
125 'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
126 'plugins' => $attrs['PLUGINS'],
127 'lock_spec' => $attrs['LOCK_SPEC'],
128 'workday_minutes' => $attrs['WORKDAY_MINUTES'],
129 'custom_logo' => $attrs['CUSTOM_LOGO'],
130 'config' => $attrs['CONFIG']));
132 // Special handling for top group.
133 if (!$this->org_id && $this->current_group_id) {
134 $this->org_id = $this->current_group_id;
135 $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id";
136 $affected = $mdb2->exec($sql);
138 // Set parent group to create subgroups with this group as parent at next entry here.
139 $this->current_parent_group_id = $this->current_group_id;
143 if ($name == 'ROLES') {
144 // If we get here, we have to recycle $currentGroupRoleMap.
145 unset($this->currentGroupRoleMap);
146 $this->currentGroupRoleMap = array();
147 // Role map is reconstructed after processing <role> elements in XML. See below.
151 if ($name == 'ROLE') {
152 // We get here when processing <role> tags for the current group.
153 $role_id = ttRoleHelper::insert(array(
154 'group_id' => $this->current_group_id,
155 'org_id' => $this->org_id,
156 'name' => $attrs['NAME'],
157 'description' => $attrs['DESCRIPTION'],
158 'rank' => $attrs['RANK'],
159 'rights' => $attrs['RIGHTS'],
160 'status' => $attrs['STATUS']));
163 $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
164 } else $this->errors->add($i18n->get('error.db'));
168 if ($name == 'TASKS') {
169 // If we get here, we have to recycle $currentGroupTaskMap.
170 unset($this->currentGroupTaskMap);
171 $this->currentGroupTaskMap = array();
172 // Task map is reconstructed after processing <task> elements in XML. See below.
176 if ($name == 'TASK') {
177 // We get here when processing <task> tags for the current group.
178 $task_id = ttTaskHelper::insert(array(
179 'group_id' => $this->current_group_id,
180 'org_id' => $this->org_id,
181 'name' => $attrs['NAME'],
182 'description' => $attrs['DESCRIPTION'],
183 'status' => $attrs['STATUS']));
186 $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
187 } else $this->errors->add($i18n->get('error.db'));
191 if ($name == 'PROJECTS') {
192 // If we get here, we have to recycle $currentGroupProjectMap.
193 unset($this->currentGroupProjectMap);
194 $this->currentGroupProjectMap = array();
195 // Project map is reconstructed after processing <project> elements in XML. See below.
199 if ($name == 'PROJECT') {
200 // We get here when processing <project> tags for the current group.
202 // Prepare a list of task ids.
203 $tasks = explode(',', $attrs['TASKS']);
204 foreach ($tasks as $id)
205 $mapped_tasks[] = $this->currentGroupTaskMap[$id];
207 $project_id = ttProjectHelper::insert(array(
208 'group_id' => $this->current_group_id,
209 'org_id' => $this->org_id,
210 'name' => $attrs['NAME'],
211 'description' => $attrs['DESCRIPTION'],
212 'tasks' => $mapped_tasks,
213 'status' => $attrs['STATUS']));
216 $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
217 } else $this->errors->add($i18n->get('error.db'));
221 if ($name == 'CLIENTS') {
222 // If we get here, we have to recycle $currentGroupClientMap.
223 unset($this->currentGroupClientMap);
224 $this->currentGroupClientMap = array();
225 // Client map is reconstructed after processing <client> elements in XML. See below.
229 if ($name == 'CLIENT') {
230 // We get here when processing <client> tags for the current group.
232 // Prepare a list of project ids.
233 if ($attrs['PROJECTS']) {
234 $projects = explode(',', $attrs['PROJECTS']);
235 foreach ($projects as $id)
236 $mapped_projects[] = $this->currentGroupProjectMap[$id];
239 $client_id = ttClientHelper::insert(array(
240 'group_id' => $this->current_group_id,
241 'org_id' => $this->org_id,
242 'name' => $attrs['NAME'],
243 'address' => $attrs['ADDRESS'],
244 'tax' => $attrs['TAX'],
245 'projects' => $mapped_projects,
246 'status' => $attrs['STATUS']));
249 $this->currentGroupClientMap[$attrs['ID']] = $client_id;
250 } else $this->errors->add($i18n->get('error.db'));
254 if ($name == 'USERS') {
255 // If we get here, we have to recycle $currentGroupUserMap.
256 unset($this->currentGroupUserMap);
257 $this->currentGroupUserMap = array();
258 // User map is reconstructed after processing <user> elements in XML. See below.
262 if ($name == 'USER') {
263 // We get here when processing <user> tags for the current group.
265 $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id : $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
267 $user_id = ttUserHelper::insert(array(
268 'group_id' => $this->current_group_id,
269 'org_id' => $this->org_id,
270 'role_id' => $role_id,
271 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
272 'name' => $attrs['NAME'],
273 'login' => $attrs['LOGIN'],
274 'password' => $attrs['PASSWORD'],
275 'rate' => $attrs['RATE'],
276 'email' => $attrs['EMAIL'],
277 'status' => $attrs['STATUS']), false);
280 $this->currentGroupUserMap[$attrs['ID']] = $user_id;
281 } else $this->errors->add($i18n->get('error.db'));
285 if ($name == 'USER_PROJECT_BIND') {
286 if (!ttUserHelper::insertBind(array(
287 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
288 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
289 'group_id' => $this->current_group_id,
290 'org_id' => $this->org_id,
291 'rate' => $attrs['RATE'],
292 'status' => $attrs['STATUS']))) {
293 $this->errors->add($i18n->get('error.db'));
298 if ($name == 'INVOICES') {
299 // If we get here, we have to recycle $currentGroupInvoiceMap.
300 unset($this->currentGroupInvoiceMap);
301 $this->currentGroupInvoiceMap = array();
302 // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
306 if ($name == 'INVOICE') {
307 // We get here when processing <invoice> tags for the current group.
308 $invoice_id = ttInvoiceHelper::insert(array(
309 'group_id' => $this->current_group_id,
310 'org_id' => $this->org_id,
311 'name' => $attrs['NAME'],
312 'date' => $attrs['DATE'],
313 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
314 'status' => $attrs['STATUS']));
317 $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
318 } else $this->errors->add($i18n->get('error.db'));
322 if ($name == 'LOG') {
323 // If we get here, we have to recycle $currentGroupLogMap.
324 unset($this->currentGroupLogMap);
325 $this->currentGroupLogMap = array();
326 // Log map is reconstructed after processing <log_item> elements in XML. See below.
330 if ($name == 'LOG_ITEM') {
331 // We get here when processing <log_item> tags for the current group.
332 $log_item_id = ttTimeHelper::insert(array(
333 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
334 'group_id' => $this->current_group_id,
335 'org_id' => $this->org_id,
336 'date' => $attrs['DATE'],
337 'start' => $attrs['START'],
338 'finish' => $attrs['FINISH'],
339 'duration' => $attrs['DURATION'],
340 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
341 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
342 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
343 'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
344 'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
345 'billable' => $attrs['BILLABLE'],
346 'paid' => $attrs['PAID'],
347 'status' => $attrs['STATUS']));
350 $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
351 } else $this->errors->add($i18n->get('error.db'));
355 if ($name == 'CUSTOM_FIELDS') {
356 // If we get here, we have to recycle $currentGroupCustomFieldMap.
357 unset($this->currentGroupCustomFieldMap);
358 $this->currentGroupCustomFieldMap = array();
359 // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
363 if ($name == 'CUSTOM_FIELD') {
364 // We get here when processing <custom_field> tags for the current group.
365 $custom_field_id = ttCustomFieldHelper::insertField(array(
366 'group_id' => $this->current_group_id,
367 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
368 'type' => $attrs['TYPE'],
369 'label' => $attrs['LABEL'],
370 'required' => $attrs['REQUIRED'],
371 'status' => $attrs['STATUS']));
372 if ($custom_field_id) {
374 $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
375 } else $this->errors->add($i18n->get('error.db'));
379 if ($name == 'CUSTOM_FIELD_OPTIONS') {
380 // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
381 unset($this->currentGroupCustomFieldOptionMap);
382 $this->currentGroupCustomFieldOptionMap = array();
383 // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
387 if ($name == 'CUSTOM_FIELD_OPTION') {
388 // We get here when processing <custom_field_option> tags for the current group.
389 $custom_field_option_id = ttCustomFieldHelper::insertOption(array(
390 // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
391 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
392 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
393 'value' => $attrs['VALUE']));
394 if ($custom_field_option_id) {
396 $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
397 } else $this->errors->add($i18n->get('error.db'));
401 if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
402 // We get here when processing <custom_field_log_entry> tags for the current group.
403 if (!ttCustomFieldHelper::insertLogEntry(array(
404 // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
405 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
406 'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
407 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
408 'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
409 'value' => $attrs['VALUE'],
410 'status' => $attrs['STATUS']))) {
411 $this->errors->add($i18n->get('error.db'));
416 if ($name == 'EXPENSE_ITEM') {
417 // We get here when processing <expense_item> tags for the current group.
418 $expense_item_id = ttExpenseHelper::insert(array(
419 'date' => $attrs['DATE'],
420 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
421 'group_id' => $this->current_group_id,
422 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
423 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
424 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
425 'name' => $attrs['NAME'],
426 'cost' => $attrs['COST'],
427 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
428 'paid' => $attrs['PAID'],
429 'status' => $attrs['STATUS']));
430 if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
434 if ($name == 'MONTHLY_QUOTA') {
435 if (!$this->insertMonthlyQuota($this->current_group_id,
436 // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
439 $attrs['MINUTES'])) {
440 $this->errors->add($i18n->get('error.db'));
445 if ($name == 'FAV_REPORT') {
447 if (strlen($attrs['USERS']) > 0) {
448 $arr = explode(',', $attrs['USERS']);
450 $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
452 $fav_report_id = ttFavReportHelper::insertReport(array(
453 'name' => $attrs['NAME'],
454 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
455 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
456 'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
457 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
458 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
459 'billable' => $attrs['BILLABLE'],
460 'users' => $user_list,
461 'period' => $attrs['PERIOD'],
462 'from' => $attrs['PERIOD_START'],
463 'to' => $attrs['PERIOD_END'],
464 'chclient' => (int) $attrs['SHOW_CLIENT'],
465 'chinvoice' => (int) $attrs['SHOW_INVOICE'],
466 'chpaid' => (int) $attrs['SHOW_PAID'],
467 'chip' => (int) $attrs['SHOW_IP'],
468 'chproject' => (int) $attrs['SHOW_PROJECT'],
469 'chstart' => (int) $attrs['SHOW_START'],
470 'chduration' => (int) $attrs['SHOW_DURATION'],
471 'chcost' => (int) $attrs['SHOW_COST'],
472 'chtask' => (int) $attrs['SHOW_TASK'],
473 'chfinish' => (int) $attrs['SHOW_END'],
474 'chnote' => (int) $attrs['SHOW_NOTE'],
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) $this->errors->add($i18n->get('error.db'));
487 // importXml - uncompresses the file, reads and parses its content. During parsing,
488 // startElement, endElement, and dataElement functions are called as many times as necessary.
489 // Actual import occurs in the endElement handler.
490 function importXml() {
493 // Do we have a compressed file?
495 $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
496 if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
500 // Create a temporary file.
501 $dirName = dirname(TEMPLATE_DIR . '_c/.');
502 $filename = tempnam($dirName, 'import_');
504 // If the file is compressed - uncompress it.
506 if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
507 $this->errors->add($i18n->get('error.sys'));
510 unlink($_FILES['xmlfile']['tmp_name']);
512 if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
513 $this->errors->add($i18n->get('error.upload'));
518 // Initialize XML parser.
519 $parser = xml_parser_create();
520 xml_set_object($parser, $this);
521 xml_set_element_handler($parser, 'startElement', false);
523 // We need to parse the file 2 times:
524 // 1) First pass: determine if import is possible.
525 // 2) Second pass: import data, one tag at a time.
527 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
528 $file = fopen($filename, 'r');
529 while (($data = fread($file, 4096)) && $this->errors->no()) {
530 if (!xml_parse($parser, $data, feof($file))) {
531 $this->errors->add(sprintf($i18n->get('error.xml'),
532 xml_get_current_line_number($parser),
533 xml_error_string(xml_get_error_code($parser))));
536 if ($this->conflicting_logins) {
537 $this->canImport = false;
538 $this->errors->add($i18n->get('error.user_exists'));
539 $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins));
542 $this->firstPass = false; // We are done with 1st pass.
543 xml_parser_free($parser);
544 if ($file) fclose($file);
545 if (!$this->canImport) {
549 if ($this->errors->yes()) return; // Exit if we have errors.
551 // Now we can do a second pass, where real work is done.
552 $parser = xml_parser_create();
553 xml_set_object($parser, $this);
554 xml_set_element_handler($parser, 'startElement', false);
556 // Read and parse the content of the file. During parsing, startElement is called back for each tag.
557 $file = fopen($filename, 'r');
558 while (($data = fread($file, 4096)) && $this->errors->no()) {
559 if (!xml_parse($parser, $data, feof($file))) {
560 $this->errors->add(sprintf($i18n->get('error.xml'),
561 xml_get_current_line_number($parser),
562 xml_error_string(xml_get_error_code($parser))));
565 xml_parser_free($parser);
566 if ($file) fclose($file);
570 // uncompress - uncompresses the content of the $in file into the $out file.
571 function uncompress($in, $out) {
572 // Do we have the uncompress function?
573 if (!function_exists('bzopen'))
576 // Initial checks of file names and permissions.
577 if (!file_exists($in) || !is_readable ($in))
579 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
582 if (!$out_file = fopen($out, 'wb'))
584 if (!$in_file = bzopen ($in, 'r'))
587 while (!feof($in_file)) {
588 $buffer = bzread($in_file, 4096);
589 fwrite($out_file, $buffer, 4096);
596 // createGroup function creates a new group.
597 private function createGroup($fields) {
600 $mdb2 = getConnection();
602 $columns = '(parent_id, org_id, name, currency, decimal_mark, lang, date_format, time_format'.
603 ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
604 ', allow_ip, password_complexity, plugins, lock_spec'.
605 ', workday_minutes, config, created, created_ip, created_by)';
607 $values = ' values (';
608 $values .= $mdb2->quote($fields['parent_id']);
609 $values .= ', '.$mdb2->quote($fields['org_id']);
610 $values .= ', '.$mdb2->quote(trim($fields['name']));
611 $values .= ', '.$mdb2->quote(trim($fields['currency']));
612 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
613 $values .= ', '.$mdb2->quote($fields['lang']);
614 $values .= ', '.$mdb2->quote($fields['date_format']);
615 $values .= ', '.$mdb2->quote($fields['time_format']);
616 $values .= ', '.(int)$fields['week_start'];
617 $values .= ', '.(int)$fields['tracking_mode'];
618 $values .= ', '.(int)$fields['project_required'];
619 $values .= ', '.(int)$fields['task_required'];
620 $values .= ', '.(int)$fields['record_type'];
621 $values .= ', '.$mdb2->quote($fields['bcc_email']);
622 $values .= ', '.$mdb2->quote($fields['allow_ip']);
623 $values .= ', '.$mdb2->quote($fields['password_complexity']);
624 $values .= ', '.$mdb2->quote($fields['plugins']);
625 $values .= ', '.$mdb2->quote($fields['lock_spec']);
626 $values .= ', '.(int)$fields['workday_minutes'];
627 $values .= ', '.$mdb2->quote($fields['config']);
628 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
631 $sql = 'insert into tt_groups '.$columns.$values;
632 $affected = $mdb2->exec($sql);
633 if (is_a($affected, 'PEAR_Error')) {
634 $this->errors->add($i18n->get('error.db'));
638 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
642 // insertMonthlyQuota - a helper function to insert a monthly quota.
643 private function insertMonthlyQuota($group_id, $year, $month, $minutes) {
644 $mdb2 = getConnection();
645 $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)";
646 $affected = $mdb2->exec($sql);
647 return (!is_a($affected, 'PEAR_Error'));