Driver - sql and category

Johan FISCHER <[email protected]> Sun, 2 Nov 2003 13:54:52 +1100
Newsgroups gmane.comp.horde.nag
Message-ID <[email protected]>

Hi,

I'm trying to modify the source code of the Nag Driver in order to enable both
the category and the sql driver in the HEAD CVS.

I'm basing the modification on the Thor module model and adapting the functions
in the files.

Currently, the new Driver works with the sql driver (the same as before) but is
now using the same model for sql and category, so normally, the category driver
should work too.

The problem is inside the fonctions for the category driver, especially when i
want to modify a task. The function return an error when it try to call the set
function of an CategoryObject return by the function getCategorybyId. I checked
if the object return by the function was a good object type by using the php
function  get_class_methods and the set function actually exist.

So i don't understand Why php is telling me the set function doesn't exist.

If anyone has an idea on this, it would be great.

PS: I join the patch file to test.


-- 
Nag mailing list
Frequently Asked Questions: http://horde.org/faq/
To unsubscribe, mail: [email protected]
patch_lib_Driver.txt (text/plain, 5.1 KB)
--- Driver.php.old	2003-11-02 13:47:05.000000000 +1100
+++ Driver.php	2003-11-02 02:38:00.000000000 +1100
@@ -12,6 +12,20 @@ require_once HORDE_BASE . '/lib/History.
  * @since   Nag 0.1
  * @package Nag
  */
+
+// Internal Status flags (for bitwise operations)
+/** @constant TASK_ANY Task has any flag set. */
+define('TASK_ANY', 0);
+
+/** @constant TASK_ADDED Task has been added. */
+define('TASK_ADDED', 1);
+
+/** @constant TASK_REMOVED Task has been removed */
+define('TASK_DELETED', 2);
+
+/** @constant TASK_MODIFIED Task has been modified */
+define('TASK_MODIFIED', 3);
+
 class Nag_Driver {
 
     /**
@@ -125,9 +139,22 @@ class Nag_Driver {
      *
      * @return array    Returns a list of the requested tasks.
      */
-    function listTasks()
+    function listTasks($criteria = TASK_ANY)
     {
-        return $this->_tasks;
+    	/* Return all of the tasks by default. */
+        if ($criteria == TASK_ANY) {
+	    return $this->_tasks;
+	}
+
+	/* Find those that match the criteria */
+	$return_set = array();
+	foreach ($this->_tasks as $taskID => $task) {
+	    if ($this->_tasks[$taskID]['flags'] & $criteria) {
+	        $return_set[$taskID] = $task;
+	    }
+	}
+
+	return $return_set;
     }
 
     /**
@@ -168,4 +195,127 @@ class Nag_Driver {
         return 'nag:' . $this->_tasklist . ':' . $taskId;
     }
 
+    /**
+     * Modifies an attribute of a task in the $tasks list.
+     * 
+     * @param int	$taskID		The ID of the task to modify
+     * @param string	$attribute	The attribute of the task to modify
+     * @param mixed	$value		The new value for $attribute
+     *
+     * @return boolean	True if the task was successfully marked as modified
+     */
+    function modifyTask ($taskID, $attribute, $value)
+    {
+        if (isset($this->_tasks[$taskID]) && isset($this->_tasks[$taskID][strtolower($attribute)])) {
+	    $this->_tasks[$taskID][strtolower($attribute)] = $value;
+	    $this->_tasks[$taskID]['flags'] |= TASK_MODIFIED;
+	    return ($this->_tasks[$taskID]['flags'] & TASK_MODIFIED);
+	}
+
+	return false;
+    }
+
+    /**
+     * This function create a task given its data
+     * Before calling that function any action/check previous
+     * to the project creation should have been performed.
+     * 
+     * @param string 		$name		The name (short) of the task.
+     * @param string 		$desc		The description (long) of the task.
+     * @param optional integer	$due		The due date of the task.
+     * @param optional integer	$priority	The priority of the task.
+     * @param optional integer	$completed	The completion of the task.
+     * @param optional integer	$category	The category of the task.
+     * @param optional integer	$alarm		The alarm associated to the task.
+     *
+     * @return integer	The numeric ID of the new task.
+     */
+    function addTask($name, $desc, $due = 0, $priority = 0, $completed = 0,
+    		 $category = 0, $alarm = 0)
+    {
+        $task = array();
+	$task['name'] = $name;
+	$task['desc'] = $desc;
+	$task['due'] = $due;
+	$task['priority'] = $priority;
+	$task['completed'] = $completed;
+	$task['category'] = $category;
+	$task['alarm'] = $alarm;
+	$task['flags'] = TASK_ADDED;
+
+	$this->_tasks[] = $task;
+    }
+
+    /**
+     * This function deletes the task
+     * 
+     * @param integer	$taskID	The task identifier
+     * @return boolean	True if it was correctly removed from the backend
+     */
+    function deleteTask ($taskID)
+    {
+        if (array_key_exists($taskID, $this->_tasks)) {
+	    $this->_tasks[$taskID]['flags'] |= TASK_DELETED;
+	    return ($this->_tasks[$taskID]['flags'] & TASK_DELETED);
+	}
+
+        return false;
+    }
+
+    /**
+     * Purges those tasks that have the "removed" flag set
+     */
+    function purgeDeleted()
+    {
+        $deleted_ids = array_keys($this->listTasks(TASK_DELETED));
+	foreach ($deleted_ids as $task_id) {
+	    unset($this->_tasks[$task_id]);
+	}
+    }
+
+    function deleteAll ()
+    {
+    	$deleted_ids = array_keys ($this->listTasks(TASK_ANY));
+	$result = true;
+        foreach ($deleted_ids as $taskID) {
+	    $result &= $this->deleteTask($taskID);
+	}
+	return $result;
+    }
+
+    /**
+     * Sets the requested flag to the given value.
+     *
+     * @param int       $id         The ID of the task to modify.
+     * @param int       $flag       The flag to modify.
+     * @param boolean   $state      The new state of the flag.
+     */
+     function setFlag($id, $flag, $state)
+     {
+         if (isset($this->_tasks[$id])) {
+             if ($state) {
+                 $this->_tasks[$id]['flags'] |= $flag;
+             } else {
+                 $this->_tasks[$id]['flags'] &= ~$flag;   
+	     }
+	 }
+      }
+															          
+    /**
+     * Gets the requested flag's value.
+     *
+     * @param int       $id         The ID of the project to examine.
+     * @param int       $flag       The flag whose value to return.
+     *
+     * @return boolean  The state of the requested flag.
+     */
+    function getFlag($id, $flag)
+    {
+        if (isset($this->_tasks[$id])) {
+            return ($this->_tasks[$id]['flags'] & $flag);
+        }
+        return null;
+    }
+
+										       
 }
patch_lib_Driver_category.txt (text/plain, 1.4 KB)
--- category.php.old	2003-11-02 13:47:17.000000000 +1100
+++ category.php	2003-11-02 02:38:00.000000000 +1100
@@ -9,6 +9,10 @@
  * @since   Nag 3.0
  * @package Nag
  */
+
+
+include_once HORDE_BASE . '/lib/Category.php';
+ 
 class Nag_Driver_category extends Nag_Driver {
 
     /**
@@ -33,7 +37,6 @@ class Nag_Driver_category extends Nag_Dr
         $driver = $conf['category']['driver'];
         $params = Horde::getDriverConfig('category', $driver);
         $params = array_merge($params, array( 'group' => 'nag.tasks' ));
-
         $this->_tasks_category = &Category::singleton($driver, $params);
     }
 
@@ -150,8 +153,16 @@ class Nag_Driver_category extends Nag_Dr
         /* Perform any pending modifications. */
         if (count($modified_tasks) > 0) {
             foreach ($modified_tasks as $task_id => $task) {
-                $obj = $this->_tasks_category->getCategoryById($task_id);
-                $obj->set('description', $task['desc']);
+	        $temp = $this->_tasks_category;
+                $obj = $temp->getCategoryById($task_id);
+		$class_methods = get_class_methods(get_class($obj));
+
+//		foreach ($class_methods as $method_name) {
+//		    echo "$method_name\n";
+//		    }
+
+		  
+                $obj.set('description', $task['desc']);
                 $obj->set('due', $task['due']);
                 $obj->set('priority', $task['priority']);
                 $obj->set('completed', $task['completed']);
patch_lib_Driver_sql.txt (text/plain, 2.9 KB)
--- sql.php.old	2003-11-02 13:47:29.000000000 +1100
+++ sql.php	2003-11-02 02:38:00.000000000 +1100
@@ -102,7 +102,7 @@ class Nag_Driver_sql extends Nag_Driver 
      *
      * @return integer  The numeric ID of the new task.
      */
-    function add($name, $desc, $due = 0, $priority = 0, $completed = 0,
+    function _addTask($name, $desc, $due = 0, $priority = 0, $completed = 0,
                  $category = 0, $alarm = 0)
     {
         /* Make sure we have a valid database connection. */
@@ -162,7 +162,7 @@ class Nag_Driver_sql extends Nag_Driver 
      * @param optional integer $category  The category of the task.
      * @param optional integer $completed The alarm associatesd to the task.
      */
-    function modify($taskId, $name, $desc, $due = 0, $priority = 0,
+    function _modifyTask($taskId, $name, $desc, $due = 0, $priority = 0,
                     $completed = 0, $category = 0, $alarm = 0)
     {
         /* Make sure we have a valid database connection. */
@@ -201,7 +201,7 @@ class Nag_Driver_sql extends Nag_Driver 
         return true;
     }
 
-    function deleteTask($taskId)
+    function _deleteTask($taskId)
     {
         $this->_connect();
 
@@ -229,7 +229,7 @@ class Nag_Driver_sql extends Nag_Driver 
         return true;
     }
 
-    function deleteAll()
+    function _deleteAll()
     {
         $this->_connect();
 
@@ -291,6 +291,62 @@ class Nag_Driver_sql extends Nag_Driver 
         return true;
     }
 
+
+    function store ()
+    {
+
+        $added_tasks = $this->listTasks(TASK_ADDED);
+	$modified_tasks = $this->listTasks(TASK_MODIFIED);
+	$deleted_tasks = $this->listTasks(TASK_DELETED);
+
+	if ((count($added_tasks) == 0) && (count($modified_tasks) == 0) &&
+	    (count($deleted_tasks) == 0)) {
+	    return true;
+	}
+
+	if (count ($deleted_tasks) > 0) {
+	    foreach ($deleted_tasks as $taskID => $task) {
+	        $ret = $this->_deleteTask($taskID);
+		if (is_a($ret, 'PEAR_Error')) {
+		    return $ret;
+		}
+            }
+
+	    $this->purgeDeleted();
+	}
+
+	if (count($added_tasks) > 0) {
+	    foreach ($added_tasks as $taskID => $task) {
+	    	$ret = $this->_addTask($task['name'], $task['desc'], $task['due'], 
+		                       $task['priority'], $task['completed'], 
+				       $task['category'],  
+				       $task['alarm']);
+		if (is_a($ret, 'PEAR_Error')) {
+		    return $ret;
+		}
+
+		$this->setFlag($taskID, TASK_ADDED, false);
+	    }
+        }
+
+	if (count($modified_tasks) > 0) {
+	    foreach ($modified_tasks as $taskID => $task) {
+//	    	echo " Task modified : ".$task['name']."<br/>";
+	    	$ret = $this->_modifyTask($taskID, $task['name'], $task['desc'], 
+		                       $task['due'], $task['priority'], 
+				       $task['completed'], $task['category'], 
+				       $task['alarm']);
+		if (is_a($ret, 'PEAR_Error')) {
+		    return $ret;
+		}
+		$this->setFlag($taskID, TASK_MODIFIED, false);
+	    }
+	}
+
+	return true;
+    }
+
+
     /**
      * List all alarms near $date.
      *
patch_lib_Nag.txt (text/plain, 2.5 KB)
--- Nag.php.old	2003-11-02 13:46:53.000000000 +1100
+++ Nag.php	2003-11-02 02:38:00.000000000 +1100
@@ -571,6 +571,82 @@ class Nag {
         return $alarm_text;
     }
 
+    function deleteTask($tasklist, $taskID)
+    {
+        global $conf, $notification;
+
+	if (!isset($taskID)) {
+	    $notification->push(_("Task identifier not set."),'horde.error');
+	    return false;
+	}
+
+	$storage = &Nag_Driver::singleton($tasklist);
+	$storage->retrieve();
+
+	$storage->deleteTask($taskID);
+	/* Store the changes. */
+	$result = $storage->store();
+	if (!is_a($result, 'PEAR_Error')) {
+	    $notification->push(_("The task was saved."), 'horde.success');
+	    return true;
+	} else {
+	    $notification->push(sprintf(_("There was a problem saving the task %s."), $result->getMessage()), 'horde.error');
+	    return false;
+	}
+    }
+
+
+    function addTask ($tasklist, $name, $desc, $due, $priority, $completed, $category, $alarm)
+    {
+        global $conf, $notification;
+	
+	$storage = &Nag_Driver::singleton($tasklist);
+	$storage->retrieve();
+
+	$storage->addTask($name, $desc, $due, $priority, $completed, $alarm);
+
+	/* Store the changes. */
+	$result = $storage->store();
+	if (!is_a($result, 'PEAR_Error')) {
+	    $notification->push(_("The task was saved."), 'horde.success');
+	    return true;
+	} else {
+	    $notification->push(sprintf(_("There was a problem saving the task %s."), $result->getMessage()), 'horde.error');
+	    return false;
+	}
+    }
+    	
+
+    function modifyTask($tasklist, $taskID, $name, $desc, $due, $priority, $completed, $category, $alarm)
+    {
+        global $conf, $notification;
+
+	if (!isset($taskID)) {
+	    $notification->push(_("Task identifier not set."),'horde.error');
+	    return false;
+	}
+
+	$storage = &Nag_Driver::singleton($tasklist);
+	$storage->retrieve();
+
+	$storage->modifyTask($taskID, 'name', $name);
+	$storage->modifyTask($taskID, 'desc', $desc);
+	$storage->modifyTask($taskID, 'due', $due);
+	$storage->modifyTask($taskID, 'priority', $priority);
+	$storage->modifyTask($taskID, 'completed', $completed);
+	$storage->modifyTask($taskID, 'alarm', $alarm);
+
+	/* Store the changes. */
+	$result = $storage->store();
+	if (!is_a($result, 'PEAR_Error')) {
+	    $notification->push(_("The task was saved."), 'horde.success');
+	    return true;
+	} else {
+	    $notification->push(sprintf(_("There was a problem saving the task %s."), $result->getMessage()), 'horde.error');
+	    return false;
+	}
+    }
+
     /**
      * Comparison function for sorting tasks by priority.
      *
patch_task.txt (text/plain, 3.8 KB)
--- task.php.old	2003-11-02 13:46:34.000000000 +1100
+++ task.php	2003-11-02 02:38:00.000000000 +1100
@@ -185,8 +185,7 @@ case NAG_SAVE_TASK:
                 /* Saving to a different task list */
                 $share = $GLOBALS['nag_shares']->getShare($tasklist_original);
                 if (!is_a($share, 'PEAR_Error') && $share->hasPermission(Auth::getAuth(), _PERMS_DELETE)) {
-                    $storage = &Nag_Driver::singleton($tasklist_original);
-                    $result = $storage->deleteTask($task_id);
+                    $result = Nag::deleteTask($tasklist_original, $task_id);
                     if (is_a($result, 'PEAR_Error')) {
                         $notification->push(sprintf(_("There was a problem removing the task from %s."), $share->getShareName()), 'horde.error');
                     }
@@ -194,21 +193,17 @@ case NAG_SAVE_TASK:
                     $notification->push(sprintf(_("Access denied removing task from %s."), $share->getShareName()), 'horde.error');
                 }
 
-                $storage = &Nag_Driver::singleton($tasklist_target);
-                $result = $storage->add($task_name, $task_desc, $task_due,
+                $result = Nag::addTask ($tasklist_target, $task_name, $task_desc, $task_due,
                                         $task_priority, $task_completed,
                                         $task_category, $task_alarm);
             } else {
                 /* Saving to the same task list. */
-                $storage = &Nag_Driver::singleton($tasklist_target);
-                $result = $storage->modify($task_id, $task_name, $task_desc, $task_due, $task_priority,
+                $result = Nag::modifyTask ($tasklist_target, $task_id, $task_name, $task_desc, $task_due, $task_priority,
                                            $task_completed, $task_category, $task_alarm);
             }
         } else {
-            $storage = &Nag_Driver::singleton($tasklist_target);
-            $result = $storage->add($task_name, $task_desc, $task_due,
-                                    $task_priority, $task_completed,
-                                    $task_category, $task_alarm);
+	    $result = Nag::addTask ($tasklist_target, $task_name, $task_desc, $task_due,
+	                            $task_priority, $task_completed, $task_category, $task_alarm);
         }
 
         // Check our results.
@@ -234,8 +229,7 @@ case NAG_DELETE_TASKS:
         if (is_a($share, 'PEAR_Error') || !$share->hasPermission(Auth::getAuth(), _PERMS_DELETE)) {
             $notification->push(sprintf(_("Access denied deleting %s."), $task['name']), 'horde.error');
         } else {
-            $storage = &Nag_Driver::singleton($tasklist_id);
-            $result = $storage->deleteTask($task_id);
+            $result = Nag::deleteTask($tasklist_id, $task_id);
             if (is_a($result, 'PEAR_Error')) {
                 $notification->push(sprintf(_("There was a problem deleting %s: %s"),
                                             $task['name'], $result->getMessage()), 'horde.error');
@@ -260,8 +254,7 @@ case NAG_COMPLETE_TASKS:
         if (is_a($share, 'PEAR_Error') || !$share->hasPermission(Auth::getAuth(), _PERMS_EDIT)) {
             $notification->push(sprintf(_("Access denied completing task %s."), $task['name']), 'horde.error');
         } else {
-            $storage = &Nag_Driver::singleton($tasklist_id);
-            $result = $storage->modify($task_id, $task['name'], $task['desc'], $task['due'],
+            $result = Nag::modifyTask($tasklist_id, $task_id, $task['name'], $task['desc'], $task['due'],
                                        $task['priority'], 1, $task['category'], $task['alarm']);
             if (is_a($result, 'PEAR_Error')) {
                 $notification->push(sprintf(_("There was a problem completing %s: %s"),
(unnamed) (application/pgp-keys, 1018 B) - not displayed