From b97f55c1e563089ed454744a9bf8850c31ec3416 Mon Sep 17 00:00:00 2001
From: Josh Leder <josh@ha.cr>
Date: Thu, 11 Jul 2013 20:04:20 -0600
Subject: [PATCH] parse task list in createTasksFromText

---
 nag/lib/Nag.php |   87 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 78 insertions(+), 9 deletions(-)

diff --git a/nag/lib/Nag.php b/nag/lib/Nag.php
index 28a8ee8..078d8ca 100644
--- a/nag/lib/Nag.php
+++ b/nag/lib/Nag.php
@@ -329,6 +329,8 @@ class Nag
      *                          imported.  If 'null', the user's default
      *                          tasklist will be used.
      *
+     * @throws Horde_Exception_PermissionDenied
+     * @throws Nag_Exception
      * @return array  The UIDs of all tasks that were added.
      */
     static public function createTasksFromText($text, $tasklist = null)
@@ -339,17 +341,25 @@ class Nag
             throw new Horde_Exception_PermissionDenied();
         }
 
-        $storage = $GLOBALS['injector']
+        $defaultStorage = $GLOBALS['injector']
             ->getInstance('Nag_Factory_Driver')
             ->create($tasklist);
+        $listStorage = array($tasklist => $defaultStorage);
+
+        $listNames = self::listTasklists(false, Horde_Perms::EDIT);
+        array_walk($listNames, function(&$share) {
+            $share = $share->get("name");
+        });
+
         $dateParser = Horde_Date_Parser::factory(
             array('locale' => $GLOBALS['prefs']->getValue('language')) );
 
         $quickParser = new Nag_QuickParser();
-        $tasks = $quickParser->parse($text);
+        $parsedTasks = $quickParser->parse($text);
 
-        $uids = array();
-        foreach ($tasks as &$task) {
+        // prepare task data
+        $addTasks = array();
+        foreach ($parsedTasks as &$task) {
             if (!is_array($task)) {
                 $name = $task;
                 $task = array($name);
@@ -375,13 +385,72 @@ class Nag
                 $tags = '';
             }
 
-            if (isset($task['parent'])) {
-                $newTask = $storage->add(array('name' => $name, 'due' => $due, 'parent' => $tasks[$task['parent']]['id'], 'tags' => $tags));
-            } else {
-                $newTask = $storage->add(array('name' => $name, 'due' => $due, 'tags' => $tags));
+            // Look for an explicit task list prefix
+            $list = $tasklist;
+            if (preg_match('/^(.+?)\s*:\s*/', $name, $prefix)) {
+                $strip = '/' . preg_quote($prefix[0], '/') . '/';
+                $name = preg_replace($strip, '', $name, 1);
+
+                $possibleLists = array();
+                foreach ($listNames as $id => $listName) {
+                    if (stristr($listName, trim($prefix[1])) !== false)
+                        $possibleLists[] = $id;
+                }
+
+                if (empty($possibleLists)) {
+                    throw new Nag_Exception(sprintf(
+                        _("No list named like '%s'"), $prefix[1]));
+                }
+                elseif (count($possibleLists) > 1) {
+                    Horde::log('Matched ' . count($possibleLists) .
+                        " task lists for '$prefix[1]'", Horde_Log::WARN);
+                }
+
+                $list = array_shift($possibleLists);
+
+                // cache storage object
+                if (!isset($listStorage[$list])) {
+                    $listStorage[$list] = $GLOBALS['injector']
+                            ->getInstance('Nag_Factory_Driver')
+                            ->create($list);
+                }
+            }
+
+            $task['tasklist'] = $list;
+            $addTasks[] = array(
+                'name'  => $name,
+                'due'   => $due,
+                'tags'  => $tags,
+                'parent'=> isset($task['parent']) ? $task['parent'] : null
+            );
+        }
+
+        // do the actual adding
+        $uids = array();
+        foreach ($addTasks as $index => &$taskData) {
+            /** @var Nag_Driver $storage */
+            // the storage driver for each tasklist has already been loaded
+            $storage = $listStorage[$parsedTasks[$index]['tasklist']];
+
+            // find parent task id
+            if (!is_null($taskData['parent'])) {
+                $parentIndex = $taskData['parent'];
+                $taskData['parent'] = $taskData[$parentIndex]['id'];
+                // children have the same task list as their parent
+                $storage = $listStorage[$parsedTasks[$parentIndex]['tasklist']];
+            }
+            else {
+                unset($taskData['parent']);
+            }
+
+            if (!isset($storage)) {
+                throw new Nag_Exception(_("Invalid task list"));
             }
+
+            $newTask = $storage->add($taskData);
+
             $uids[] = $newTask[1];
-            $task['id'] = $newTask[0];
+            $taskData['id'] = $newTask[0];
         }
 
         return $uids;
-- 
1.7.7.5 (Apple Git-26)