Added monthly quotas to new export-import.
[timetracker.git] / WEB-INF / lib / ttOrgImportHelper.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('ttUserHelper');
30 import('ttRoleHelper');
31 import('ttTaskHelper');
32 import('ttProjectHelper');
33 import('ttClientHelper');
34 import('ttInvoiceHelper');
35 import('ttCustomFieldHelper');
36 import('ttExpenseHelper');
37
38 // ttOrgImportHelper - this class is a future replacement for ttImportHelper.
39 // Currently, it is work in progress.
40 // When done, it should handle import of complex groups consisting of other groups.
41 class ttOrgImportHelper {
42   var $errors               = null; // Errors go here. Set in constructor by reference.
43   var $conflicting_entities = null; // A comma-separated list of entity names we cannot import.
44   var $canImport      = true;    // False if we cannot import data due to a conflict such as login collision.
45   var $firstPass      = true;    // True during first pass through the file.
46   var $org_id         = null;    // Organization id (same as top group_id).
47   var $current_group_id        = null; // Current group id during parsing.
48   var $current_parent_group_id = null; // Current parent group id during parsing.
49                                        // Set when we create a new group.
50   var $top_role_id    = 0;       // Top role id.
51
52   // Entity maps for current group. They map XML ids with database ids.
53   var $currentGroupRoleMap    = array();
54   var $currentGroupTaskMap    = array();
55   var $currentGroupProjectMap = array();
56   var $currentGroupClientMap  = array();
57   var $currentGroupUserMap    = array();
58   var $currentGroupInvoiceMap = array();
59   var $currentGroupLogMap     = array();
60   var $currentGroupCustomFieldMap = array();
61   var $currentGroupCustomFieldOptionMap = array();
62
63   // Constructor.
64   function __construct(&$errors) {
65     $this->errors = &$errors;
66     $this->top_role_id = ttRoleHelper::getRoleByRank(512, 0);
67   }
68
69   // startElement - callback handler for opening tag of an XML element in the file.
70   function startElement($parser, $name, $attrs) {
71     global $i18n;
72
73     // First pass. We only check user logins for potential collisions with existing.
74     if ($this->firstPass) {
75       if ($name == 'USER' && $this->canImport) {
76         $login = $attrs['LOGIN'];
77         if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) {
78           // We have a login collision. Append colliding login to a list of things we cannot import.
79           $this->conflicting_entities .= ($this->conflicting_entities ? ", $login" : $login);
80         }
81       }
82     }
83
84     // Second pass processing. We import data here, one tag at a time.
85     if (!$this->firstPass && $this->canImport && $this->errors->no()) {
86       $mdb2 = getConnection();
87
88       // We are in second pass and can import data.
89       if ($name == 'GROUP') {
90         // Create a new group.
91         $this->current_group_id = $this->createGroup(array(
92           'parent_id' => $this->current_parent_group_id,
93           'org_id' => $this->org_id,
94           'name' => $attrs['NAME'],
95           'currency' => $attrs['CURRENCY'],
96           'decimal_mark' => $attrs['DECIMAL_MARK'],
97           'lang' => $attrs['LANG'],
98           'date_format' => $attrs['DATE_FORMAT'],
99           'time_format' => $attrs['TIME_FORMAT'],
100           'week_start' => $attrs['WEEK_START'],
101           'tracking_mode' => $attrs['TRACKING_MODE'],
102           'project_required' => $attrs['PROJECT_REQUIRED'],
103           'task_required' => $attrs['TASK_REQUIRED'],
104           'record_type' => $attrs['RECORD_TYPE'],
105           'bcc_email' => $attrs['BCC_EMAIL'],
106           'allow_ip' => $attrs['ALLOW_IP'],
107           'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
108           'plugins' => $attrs['PLUGINS'],
109           'lock_spec' => $attrs['LOCK_SPEC'],
110           'workday_minutes' => $attrs['WORKDAY_MINUTES'],
111           'custom_logo' => $attrs['CUSTOM_LOGO'],
112           'config' => $attrs['CONFIG']));
113
114         // Special handling for top group.
115         if (!$this->org_id && $this->current_group_id) {
116           $this->org_id = $this->current_group_id;
117           $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id";
118           $affected = $mdb2->exec($sql);
119         }
120         // Set parent group to create subgroups with this group as parent at next entry here.
121         $this->current_parent_group_id = $this->current_group_id;
122       }
123
124       if ($name == 'ROLES') {
125         // If we get here, we have to recycle $currentGroupRoleMap.
126         unset($this->currentGroupRoleMap);
127         $this->currentGroupRoleMap = array();
128         // Role map is reconstructed after processing <role> elements in XML. See below.
129       }
130
131       if ($name == 'ROLE') {
132         // We get here when processing <role> tags for the current group.
133         $role_id = ttRoleHelper::insert(array(
134               'group_id' => $this->current_group_id,
135               'org_id' => $this->org_id,
136               'name' => $attrs['NAME'],
137               'description' => $attrs['DESCRIPTION'],
138               'rank' => $attrs['RANK'],
139               'rights' => $attrs['RIGHTS'],
140               'status' => $attrs['STATUS']));
141         if ($role_id) {
142           // Add a mapping.
143           $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
144         } else $this->errors->add($i18n->get('error.db'));
145       }
146
147       if ($name == 'TASKS') {
148         // If we get here, we have to recycle $currentGroupTaskMap.
149         unset($this->currentGroupTaskMap);
150         $this->currentGroupTaskMap = array();
151         // Task map is reconstructed after processing <task> elements in XML. See below.
152       }
153
154       if ($name == 'TASK') {
155         // We get here when processing <task> tags for the current group.
156         $task_id = ttTaskHelper::insert(array(
157           'group_id' => $this->current_group_id,
158           'org_id' => $this->org_id,
159           'name' => $attrs['NAME'],
160           'description' => $attrs['DESCRIPTION'],
161           'status' => $attrs['STATUS']));
162         if ($task_id) {
163           // Add a mapping.
164           $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
165         } else $this->errors->add($i18n->get('error.db'));
166       }
167
168       if ($name == 'PROJECTS') {
169         // If we get here, we have to recycle $currentGroupProjectMap.
170         unset($this->currentGroupProjectMap);
171         $this->currentGroupProjectMap = array();
172         // Project map is reconstructed after processing <project> elements in XML. See below.
173       }
174
175       if ($name == 'PROJECT') {
176         // We get here when processing <project> tags for the current group.
177
178         // Prepare a list of task ids.
179         $tasks = explode(',', $attrs['TASKS']);
180         foreach ($tasks as $id)
181           $mapped_tasks[] = $this->currentGroupTaskMap[$id];
182
183         $project_id = ttProjectHelper::insert(array(
184           'group_id' => $this->current_group_id,
185           'org_id' => $this->org_id,
186           'name' => $attrs['NAME'],
187           'description' => $attrs['DESCRIPTION'],
188           'tasks' => $mapped_tasks,
189           'status' => $attrs['STATUS']));
190         if ($project_id) {
191           // Add a mapping.
192           $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
193         } else $this->errors->add($i18n->get('error.db'));
194       }
195
196       if ($name == 'CLIENTS') {
197         // If we get here, we have to recycle $currentGroupClientMap.
198         unset($this->currentGroupClientMap);
199         $this->currentGroupClientMap = array();
200         // Client map is reconstructed after processing <client> elements in XML. See below.
201       }
202
203       if ($name == 'CLIENT') {
204         // We get here when processing <client> tags for the current group.
205
206         // Prepare a list of project ids.
207         $projects = explode(',', $attrs['PROJECTS']);
208         foreach ($projects as $id)
209           $mapped_projects[] = $this->currentGroupProjectMap[$id];
210
211         $client_id = ttClientHelper::insert(array(
212           'group_id' => $this->current_group_id,
213           'org_id' => $this->org_id,
214           'name' => $attrs['NAME'],
215           'address' => $attrs['ADDRESS'],
216           'tax' => $attrs['TAX'],
217           'projects' => $mapped_projects,
218           'status' => $attrs['STATUS']));
219         if ($client_id) {
220           // Add a mapping.
221           $this->currentGroupClientMap[$attrs['ID']] = $client_id;
222         } else $this->errors->add($i18n->get('error.db'));
223       }
224
225       if ($name == 'USERS') {
226         // If we get here, we have to recycle $currentGroupUserMap.
227         unset($this->currentGroupUserMap);
228         $this->currentGroupUserMap = array();
229         // User map is reconstructed after processing <user> elements in XML. See below.
230       }
231
232       if ($name == 'USER') {
233         // We get here when processing <user> tags for the current group.
234
235         $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id :  $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
236
237         $user_id = ttUserHelper::insert(array(
238           'group_id' => $this->current_group_id,
239           'org_id' => $this->org_id,
240           'role_id' => $role_id,
241           'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
242           'name' => $attrs['NAME'],
243           'login' => $attrs['LOGIN'],
244           'password' => $attrs['PASSWORD'],
245           'rate' => $attrs['RATE'],
246           'email' => $attrs['EMAIL'],
247           'status' => $attrs['STATUS']), false);
248         if ($user_id) {
249           // Add a mapping.
250           $this->currentGroupUserMap[$attrs['ID']] = $user_id;
251         } else $this->errors->add($i18n->get('error.db'));
252       }
253
254       if ($name == 'USER_PROJECT_BIND') {
255         if (!ttUserHelper::insertBind(array(
256           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
257           'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
258           'group_id' => $this->current_group_id,
259           'org_id' => $this->org_id,
260           'rate' => $attrs['RATE'],
261           'status' => $attrs['STATUS']))) {
262           $this->errors->add($i18n->get('error.db'));
263         }
264       }
265
266       if ($name == 'INVOICES') {
267         // If we get here, we have to recycle $currentGroupInvoiceMap.
268         unset($this->currentGroupInvoiceMap);
269         $this->currentGroupInvoiceMap = array();
270         // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
271       }
272
273       if ($name == 'INVOICE') {
274         // We get here when processing <invoice> tags for the current group.
275         $invoice_id = ttInvoiceHelper::insert(array(
276           'group_id' => $this->current_group_id,
277           'org_id' => $this->org_id,
278           'name' => $attrs['NAME'],
279           'date' => $attrs['DATE'],
280           'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
281           'status' => $attrs['STATUS']));
282         if ($invoice_id) {
283           // Add a mapping.
284           $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
285         } else $this->errors->add($i18n->get('error.db'));
286       }
287
288       if ($name == 'LOG') {
289         // If we get here, we have to recycle $currentGroupLogMap.
290         unset($this->currentGroupLogMap);
291         $this->currentGroupLogMap = array();
292         // Log map is reconstructed after processing <log_item> elements in XML. See below.
293       }
294
295       if ($name == 'LOG_ITEM') {
296         // We get here when processing <log_item> tags for the current group.
297         $log_item_id = ttTimeHelper::insert(array(
298           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
299           'group_id' => $this->current_group_id,
300           'org_id' => $this->org_id,
301           'date' => $attrs['DATE'],
302           'start' => $attrs['START'],
303           'finish' => $attrs['FINISH'],
304           'duration' => $attrs['DURATION'],
305           'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
306           'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
307           'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
308           'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
309           'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
310           'billable' => $attrs['BILLABLE'],
311           'paid' => $attrs['PAID'],
312           'status' => $attrs['STATUS']));
313         if ($log_item_id) {
314           // Add a mapping.
315           $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
316         } else $this->errors->add($i18n->get('error.db'));
317       }
318
319       if ($name == 'CUSTOM_FIELDS') {
320         // If we get here, we have to recycle $currentGroupCustomFieldMap.
321         unset($this->currentGroupCustomFieldMap);
322         $this->currentGroupCustomFieldMap = array();
323         // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
324       }
325
326       if ($name == 'CUSTOM_FIELD') {
327         // We get here when processing <custom_field> tags for the current group.
328         $custom_field_id = ttCustomFieldHelper::insertField(array(
329           'group_id' => $this->current_group_id,
330           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
331           'type' => $attrs['TYPE'],
332           'label' => $attrs['LABEL'],
333           'required' => $attrs['REQUIRED'],
334           'status' => $attrs['STATUS']));
335         if ($custom_field_id) {
336           // Add a mapping.
337           $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
338         } else $this->errors->add($i18n->get('error.db'));
339       }
340
341       if ($name == 'CUSTOM_FIELD_OPTIONS') {
342         // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
343         unset($this->currentGroupCustomFieldOptionMap);
344         $this->currentGroupCustomFieldOptionMap = array();
345         // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
346       }
347
348       if ($name == 'CUSTOM_FIELD_OPTION') {
349         // We get here when processing <custom_field_option> tags for the current group.
350         $custom_field_option_id = ttCustomFieldHelper::insertOption(array(
351           // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
352           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
353           'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
354           'value' => $attrs['VALUE']));
355         if ($custom_field_option_id) {
356           // Add a mapping.
357           $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
358         } else $this->errors->add($i18n->get('error.db'));
359       }
360
361       if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
362         // We get here when processing <custom_field_log_entry> tags for the current group.
363         if (!ttCustomFieldHelper::insertLogEntry(array(
364           // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
365           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
366           'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
367           'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
368           'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
369           'value' => $attrs['VALUE'],
370           'status' => $attrs['STATUS']))) {
371           $this->errors->add($i18n->get('error.db'));
372         }
373       }
374
375       if ($name == 'EXPENSE_ITEM') {
376         // We get here when processing <expense_item> tags for the current group.
377         $expense_item_id = ttExpenseHelper::insert(array(
378           'date' => $attrs['DATE'],
379           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
380           'group_id' => $this->current_group_id,
381           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
382           'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
383           'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
384           'name' => $attrs['NAME'],
385           'cost' => $attrs['COST'],
386           'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
387           'paid' => $attrs['PAID'],
388           'status' => $attrs['STATUS']));
389         if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
390       }
391
392       if ($name == 'MONTHLY_QUOTA') {
393         if (!$this->insertMonthlyQuota($this->current_group_id,
394           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
395           $attrs['YEAR'],
396           $attrs['MONTH'],
397           $attrs['MINUTES'])) {
398           $this->errors->add($i18n->get('error.db'));
399         }
400       }
401     }
402   }
403
404   // importXml - uncompresses the file, reads and parses its content. During parsing,
405   // startElement, endElement, and dataElement functions are called as many times as necessary.
406   // Actual import occurs in the endElement handler.
407   function importXml() {
408     global $i18n;
409
410     // Do we have a compressed file?
411     $compressed = false;
412     $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
413     if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
414       $compressed = true;
415     }
416
417     // Create a temporary file.
418     $dirName = dirname(TEMPLATE_DIR . '_c/.');
419     $filename = tempnam($dirName, 'import_');
420
421     // If the file is compressed - uncompress it.
422     if ($compressed) {
423       if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
424         $this->errors->add($i18n->get('error.sys'));
425         return;
426       }
427       unlink($_FILES['xmlfile']['tmp_name']);
428     } else {
429       if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
430         $this->errors->add($i18n->get('error.upload'));
431         return;
432       }
433     }
434
435     // Initialize XML parser.
436     $parser = xml_parser_create();
437     xml_set_object($parser, $this);
438     xml_set_element_handler($parser, 'startElement', false);
439
440     // We need to parse the file 2 times:
441     //   1) First pass: determine if import is possible - there must be no login collisions.
442     //   2) Second pass: if we can import, then do import in a second pass.
443     // This is different from earlier approach for single group import, where we could
444     // do both things in one pass because user info was in the beginning of XML file.
445     // Now, with subgroups, users can be located anywhere in the file.
446
447     // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called.
448     $file = fopen($filename, 'r');
449     while ($data = fread($file, 4096)) {
450       if (!xml_parse($parser, $data, feof($file))) {
451         $this->errors->add(sprintf($i18n->get('error.xml'),
452           xml_get_current_line_number($parser),
453           xml_error_string(xml_get_error_code($parser))));
454       }
455     }
456     if ($this->conflicting_entities) {
457       $this->canImport = false;
458       $this->errors->add($i18n->get('error.user_exists'));
459       $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_entities));
460     }
461
462     $this->firstPass = false; // We are done with 1st pass.
463     xml_parser_free($parser);
464     if ($file) fclose($file);
465     if (!$this->canImport) {
466       unlink($filename);
467       return;
468     }
469     if ($this->errors->yes()) return; // Exit if we have errors.
470
471     // Now we can do a second pass, where real work is done.
472     $parser = xml_parser_create();
473     xml_set_object($parser, $this);
474     xml_set_element_handler($parser, 'startElement', false);
475
476     // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called.
477     $file = fopen($filename, 'r');
478     while ($data = fread($file, 4096)) {
479       if (!xml_parse($parser, $data, feof($file))) {
480         $this->errors->add(sprintf($i18n->get('error.xml'),
481           xml_get_current_line_number($parser),
482           xml_error_string(xml_get_error_code($parser))));
483       }
484     }
485     xml_parser_free($parser);
486     if ($file) fclose($file);
487     unlink($filename);
488   }
489
490   // uncompress - uncompresses the content of the $in file into the $out file.
491   function uncompress($in, $out) {
492     // Do we have the uncompress function?
493     if (!function_exists('bzopen'))
494       return false;
495
496     // Initial checks of file names and permissions.
497     if (!file_exists($in) || !is_readable ($in))
498       return false;
499     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
500       return false;
501
502     if (!$out_file = fopen($out, 'wb'))
503       return false;
504     if (!$in_file = bzopen ($in, 'r'))
505       return false;
506
507     while (!feof($in_file)) {
508       $buffer = bzread($in_file, 4096);
509       fwrite($out_file, $buffer, 4096);
510     }
511     bzclose($in_file);
512     fclose ($out_file);
513     return true;
514   }
515
516   // createGroup function creates a new group.
517   private function createGroup($fields) {
518     global $user;
519     global $i18n;
520     $mdb2 = getConnection();
521
522     $columns = '(parent_id, org_id, name, currency, decimal_mark, lang, date_format, time_format'.
523       ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
524       ', allow_ip, password_complexity, plugins, lock_spec'.
525       ', workday_minutes, config, created, created_ip, created_by)';
526
527     $values = ' values (';
528     $values .= $mdb2->quote($fields['parent_id']);
529     $values .= ', '.$mdb2->quote($fields['org_id']);
530     $values .= ', '.$mdb2->quote(trim($fields['name']));
531     $values .= ', '.$mdb2->quote(trim($fields['currency']));
532     $values .= ', '.$mdb2->quote($fields['decimal_mark']);
533     $values .= ', '.$mdb2->quote($fields['lang']);
534     $values .= ', '.$mdb2->quote($fields['date_format']);
535     $values .= ', '.$mdb2->quote($fields['time_format']);
536     $values .= ', '.(int)$fields['week_start'];
537     $values .= ', '.(int)$fields['tracking_mode'];
538     $values .= ', '.(int)$fields['project_required'];
539     $values .= ', '.(int)$fields['task_required'];
540     $values .= ', '.(int)$fields['record_type'];
541     $values .= ', '.$mdb2->quote($fields['bcc_email']);
542     $values .= ', '.$mdb2->quote($fields['allow_ip']);
543     $values .= ', '.$mdb2->quote($fields['password_complexity']);
544     $values .= ', '.$mdb2->quote($fields['plugins']);
545     $values .= ', '.$mdb2->quote($fields['lock_spec']);
546     $values .= ', '.(int)$fields['workday_minutes'];
547     $values .= ', '.$mdb2->quote($fields['config']);
548     $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
549     $values .= ')';
550
551     $sql = 'insert into tt_groups '.$columns.$values;
552     $affected = $mdb2->exec($sql);
553     if (is_a($affected, 'PEAR_Error')) {
554       $this->errors->add($i18n->get('error.db'));
555       return false;
556     }
557
558     $group_id = $mdb2->lastInsertID('tt_groups', 'id');
559     return $group_id;
560   }
561
562   // insertMonthlyQuota - a helper function to insert a monthly quota.
563   private function insertMonthlyQuota($group_id, $year, $month, $minutes) {
564     $mdb2 = getConnection();
565     $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)";
566     $affected = $mdb2->exec($sql);
567     return (!is_a($affected, 'PEAR_Error'));
568   }
569 }