Refactored import a bit by writing a function to insert projects with binds.
authorNik Okuntseff <support@anuko.com>
Sun, 18 Nov 2018 22:14:32 +0000 (22:14 +0000)
committerNik Okuntseff <support@anuko.com>
Sun, 18 Nov 2018 22:14:32 +0000 (22:14 +0000)
WEB-INF/lib/ttOrgImportHelper.class.php
WEB-INF/templates/footer.tpl

index d95aabb..4c57739 100644 (file)
@@ -29,7 +29,6 @@
 import('ttUserHelper');
 import('ttRoleHelper');
 import('ttTaskHelper');
-import('ttProjectHelper');
 import('ttClientHelper');
 import('ttInvoiceHelper');
 import('ttTimeHelper');
@@ -200,12 +199,14 @@ class ttOrgImportHelper {
         // We get here when processing <project> tags for the current group.
 
         // Prepare a list of task ids.
-        $tasks = explode(',', $attrs['TASKS']);
-        foreach ($tasks as $id)
-          $mapped_tasks[] = $this->currentGroupTaskMap[$id];
+        if ($attrs['TASKS']) {
+          $tasks = explode(',', $attrs['TASKS']);
+          foreach ($tasks as $id)
+            $mapped_tasks[] = $this->currentGroupTaskMap[$id];
+        }
 
-        $project_id = ttProjectHelper::insert(array( // TODO: continue refactoring, write a separate, simple function to import projects
-          'group_id' => $this->current_group_id,     // because ttProjectHelper::insert is complicated.
+        $project_id = $this->insertProject(array(
+          'group_id' => $this->current_group_id,
           'org_id' => $this->org_id,
           'name' => $attrs['NAME'],
           'description' => $attrs['DESCRIPTION'],
@@ -705,4 +706,44 @@ class ttOrgImportHelper {
     $affected = $mdb2->exec($sql);
     return (!is_a($affected, 'PEAR_Error'));
   }
+
+  // insertProject - a helper function to insert a project as well as project to task binds.
+  private function insertProject($fields)
+  {
+    $mdb2 = getConnection();
+
+    $group_id = (int) $fields['group_id'];
+    $org_id = (int) $fields['org_id'];
+
+    $name = $fields['name'];
+    $description = $fields['description'];
+    $tasks = $fields['tasks'];
+    $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
+    $status = $fields['status'];
+
+    $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
+      values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error'))
+      return false;
+
+    $last_id = 0;
+    $sql = "select last_insert_id() as last_insert_id";
+    $res = $mdb2->query($sql);
+    $val = $res->fetchRow();
+    $last_id = $val['last_insert_id'];
+
+    // Insert binds into tt_project_task_binds table.
+    if (is_array($tasks)) {
+      foreach ($tasks as $task_id) {
+        $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
+          " values($last_id, $task_id, $group_id, $org_id)";
+        $affected = $mdb2->exec($sql);
+        if (is_a($affected, 'PEAR_Error'))
+          return false;
+      }
+    }
+
+    return $last_id;
+  }
 }
index cf8c742..417439b 100644 (file)
@@ -12,7 +12,7 @@
       <br>
       <table cellspacing="0" cellpadding="4" width="100%" border="0">
         <tr>
-          <td align="center">&nbsp;Anuko Time Tracker 1.18.18.4451 | Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
+          <td align="center">&nbsp;Anuko Time Tracker 1.18.18.4453 | Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
             <a href="https://www.anuko.com/lp/tt_4.htm" target="_blank">{$i18n.footer.credits}</a> |
             <a href="https://www.anuko.com/lp/tt_5.htm" target="_blank">{$i18n.footer.license}</a> |
             <a href="https://www.anuko.com/lp/tt_7.htm" target="_blank">{$i18n.footer.improve}</a>