From 114c0af8081fda46d36c3e9e0bea8fea592536ca Mon Sep 17 00:00:00 2001
From: Earl Miles <merlin@logrus.com>
Date: Thu, 29 Jan 2009 22:02:20 +0000
Subject: [PATCH] Implement titles; this caused a retooling of the return value
 of the context task handler rendering so be sure to sync up.

---
 delegator/plugins/tasks/node_edit.inc | 26 ++++----------
 delegator/plugins/tasks/node_view.inc | 27 +++------------
 delegator/plugins/tasks/page.inc      | 21 +++--------
 delegator/plugins/tasks/user_view.inc | 26 ++++----------
 includes/context-task-handler.inc     | 50 +++++++++++++++++++++++++++
 includes/export.inc                   | 19 ----------
 includes/object-cache.inc             | 46 ++++++++++--------------
 7 files changed, 90 insertions(+), 125 deletions(-)

diff --git a/delegator/plugins/tasks/node_edit.inc b/delegator/plugins/tasks/node_edit.inc
index e03c8223..f61077cb 100644
--- a/delegator/plugins/tasks/node_edit.inc
+++ b/delegator/plugins/tasks/node_edit.inc
@@ -74,28 +74,14 @@ function delegator_node_edit($node) {
   ctools_include('context-task-handler');
   $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
 
-  // Load the landlers.
-  $handlers = delegator_load_sorted_handlers($task, '', TRUE);
-
-  // Try each handler.
-  foreach ($handlers as $handler) {
-    if ($function = delegator_get_renderer($handler)) {
-      $output = $function($handler, $contexts);
-      if ($output) {
-        // TRUE is a special return used to let us know that it handled the
-        // task but does not wish us to render anything, as it already did.
-        // This is needed for the 'no blocks' functionality.
-        if ($output === TRUE) {
-          return;
-        }
-        return $output;
-      }
-    }
+  $output = ctools_context_handler_render($task, '', $contexts);
+  if ($output === FALSE) {
+    // Fall back!
+    module_load_include('inc', 'node', 'node.pages');
+    $output = drupal_get_form($node->type . '_node_form', $node);
   }
 
-  // Fall back!
-  module_load_include('inc', 'node', 'node.pages');
-  return drupal_get_form($node->type .'_node_form', $node);
+  return $output;
 }
 
 /**
diff --git a/delegator/plugins/tasks/node_view.inc b/delegator/plugins/tasks/node_view.inc
index a02cb0ef..bd7d0f77 100644
--- a/delegator/plugins/tasks/node_view.inc
+++ b/delegator/plugins/tasks/node_view.inc
@@ -71,30 +71,13 @@ function delegator_node_view($node) {
   ctools_include('context-task-handler');
   $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
 
-  // Load the landlers.
-  $handlers = delegator_load_sorted_handlers($task, '', TRUE);
-
-  // Try each handler.
-  foreach ($handlers as $handler) {
-    if ($function = delegator_get_renderer($handler)) {
-      $output = $function($handler, $contexts);
-      if ($output) {
-        // Since we're not using node_show() we need to emulate what it used to do.
-        // Update the history table, stating that this user viewed this node.
-        node_tag_new($node->nid);
-
-        // TRUE is a special return used to let us know that it handled the
-        // task but does not wish us to render anything, as it already did.
-        // This is needed for the 'no blocks' functionality.
-        if ($output === TRUE) {
-          return;
-        }
-        return $output;
-      }
-    }
+  $output = ctools_context_handler_render($task, '', $contexts);
+  if ($output !== FALSE) {
+    node_tag_new($node->nid);
+    return $output;
   }
 
-  // Fall back!
+  // Otherwise, fall back.
   return node_page_view($node);
 }
 
diff --git a/delegator/plugins/tasks/page.inc b/delegator/plugins/tasks/page.inc
index b14b90ab..290208fb 100644
--- a/delegator/plugins/tasks/page.inc
+++ b/delegator/plugins/tasks/page.inc
@@ -303,26 +303,13 @@ function delegator_page_execute($subtask_id) {
   }
 
   $task = delegator_get_task('page');
-  $handlers = delegator_load_sorted_handlers($task, $subtask_id);
-  $page = delegator_page_load($subtask_id);
 
-  // Try each handler.
-  foreach ($handlers as $handler) {
-    if ($function = delegator_get_renderer($handler)) {
-      $output = $function($handler, $contexts);
-      if ($output) {
-        // TRUE is a special return used to let us know that it handled the
-        // task but does not wish us to render anything, as it already did.
-        // This is needed for the 'no blocks' functionality.
-        if ($output === TRUE) {
-          return;
-        }
-        return $output;
-      }
-    }
+  $output = ctools_context_handler_render($task, $subtask_id, $contexts);
+  if ($output === FALSE) {
+    return drupal_not_found();
   }
 
-  return drupal_access_denied();
+  return $output;
 }
 
 // --------------------------------------------------------------------------
diff --git a/delegator/plugins/tasks/user_view.inc b/delegator/plugins/tasks/user_view.inc
index 384ea055..f9618944 100644
--- a/delegator/plugins/tasks/user_view.inc
+++ b/delegator/plugins/tasks/user_view.inc
@@ -61,28 +61,14 @@ function delegator_user_view($account) {
   ctools_include('context-task-handler');
   $contexts = ctools_context_handler_get_task_contexts($task, '', array($account));
 
-  // Load the landlers.
-  $handlers = delegator_load_sorted_handlers($task, '', TRUE);
-
-  // Try each handler.
-  foreach ($handlers as $handler) {
-    if ($function = delegator_get_renderer($handler)) {
-      $output = $function($handler, $contexts);
-      if ($output) {
-        // TRUE is a special return used to let us know that it handled the
-        // task but does not wish us to render anything, as it already did.
-        // This is needed for the 'no blocks' functionality.
-        if ($output === TRUE) {
-          return;
-        }
-        return $output;
-      }
-    }
+  $output = ctools_context_handler_render($task, '', $contexts);
+  if ($output === FALSE) {
+    // Fall back!
+    module_load_include('inc', 'user', 'user.pages');
+    $output = user_view($account);
   }
 
-  // Fall back!
-  require_once './' . drupal_get_path('module', 'user') . '/user.pages.inc';
-  return user_view($account);
+  return $output;
 }
 
 /**
diff --git a/includes/context-task-handler.inc b/includes/context-task-handler.inc
index a7c6c15a..28d88f39 100644
--- a/includes/context-task-handler.inc
+++ b/includes/context-task-handler.inc
@@ -15,6 +15,56 @@
  * - ...
  */
 
+/**
+ * Render a context type task handler given a list of handlers
+ * attached to a type.
+ *
+ * @param $task
+ *   The $task object in use.
+ * @param $subtask_id
+ *   The id of the subtask in use.
+ * @param $contexts
+ *   The context objects in use.
+ * @param $page
+ *   If TRUE then this renderer owns the page and can use theme('page')
+ *   for no blocks; if false, output is returned regardless of any no
+ *   blocks settings.
+ * @return
+ *   Either the output or NULL if there was output, FALSE if no handler
+ *   accepted the task. If $page is FALSE then the $info block is returned instead.
+ */
+function ctools_context_handler_render($task, $subtask_id, $contexts, $page = TRUE) {
+  // Load the landlers, choosing only enabled handlers.
+  $handlers = delegator_load_sorted_handlers($task, $subtask_id, TRUE);
+
+  // Try each handler.
+  foreach ($handlers as $handler) {
+    if ($function = delegator_get_renderer($handler)) {
+      if ($info = $function($handler, $contexts)) {
+        // If we don't own the page, let the caller deal with rendering.
+        if (!$page) {
+          return $info;
+        }
+
+        if (isset($info['title'])) {
+          drupal_set_title($info['title']);
+        }
+
+        // Only directly output if $page was set to true.
+        if (!empty($info['no_blocks'])) {
+          print theme('page', $info['content'], FALSE);
+          return;
+        }
+        else {
+          return $info['content'];
+        }
+      }
+    }
+  }
+
+  return FALSE;
+}
+
 /**
  * Compare arguments to contexts for selection purposes.
  *
diff --git a/includes/export.inc b/includes/export.inc
index f2f7b22b..8bf11e7b 100644
--- a/includes/export.inc
+++ b/includes/export.inc
@@ -12,25 +12,6 @@
  * more or less sufficient.
  */
 
-/**
-  in schema:
-    'export' = array(
-      'key' => 'name', // unique key to identify records
-      'default hook' => '', // name of hook to get default objects
-      'status' => '', // name of variable to store enabled/disabled status
-      'object' => '', // name of the object to put this data on
-      'sub records' => array(
-        /////// sub records not yet implemented
-        array(
-          'table' => // table subsidiary records are in
-          'parent' => // field in parent that matches
-          'child' => // field in child that matches
-          'array' => // name of array to store record in
-          'key' => // field to use as a key to store sub records.
-        ),
-      ),
-  */
-
 /**
  * A bit flag used to let us know if an object is in the database.
  */
diff --git a/includes/object-cache.inc b/includes/object-cache.inc
index 493567a6..5ca0e716 100644
--- a/includes/object-cache.inc
+++ b/includes/object-cache.inc
@@ -71,6 +71,25 @@ function ctools_object_cache_clear($obj, $name) {
   db_query("DELETE FROM {ctools_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name);
 }
 
+/**
+ * Determine if another user has a given object cached.
+ *
+ * This is very useful for 'locking' objects so that only one user can
+ * modify them.
+ *
+ * @param $obj
+ *   A 32 character or less string to define what kind of object is being
+ *   stored; primarily this is used to prevent collisions.
+ * @param $name
+ *   The name of the object being removed.
+ *
+ * @return
+ *   An object containing the UID and updated date if found; NULL if not.
+ */
+function ctools_object_cache_test($obj, $name) {
+  return db_fetch_object(db_query("SELECT s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions}  s ON c.sid = s.sid WHERE s.sid != '%s' AND c.obj = '%s' AND c.name = '%s' ORDER BY c.updated ASC", session_id(), $obj, $name));
+}
+
 /**
  * Remove an object from the non-volatile ctools cache for all session IDs.
  *
@@ -101,30 +120,3 @@ function ctools_object_cache_clean($age = NULL) {
   db_query("DELETE FROM {ctools_object_cache} WHERE updated < %d", time() - $age);
 }
 
-
-/**
- * Determine if another user has a given object cached.
- *
- * This is very useful for 'locking' objects so that only one user can
- * modify them.
- *
- * @param $obj
- *   A 32 character or less string to define what kind of object is being
- *   stored; primarily this is used to prevent collisions.
- * @param $name
- *   The name of the object being removed.
- *
- * @return
- *   An object containing the UID and updated date if found; NULL if not.
- */
-function ctools_object_cache_test($obj, $name) {
-  return db_fetch_object(db_query("SELECT s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions}  s ON c.sid = s.sid WHERE s.sid != '%s' AND c.obj = '%s' AND c.name = '%s' ORDER BY c.updated ASC", session_id(), $obj, $name));
-}
-
-/**
- * Clean up old cached items on cron.
- */
-function ctools_object_cache_cron() {
-  // delete anything 7 days old or more.
-  db_query("DELETE FROM {ctools_object_cache} WHERE timestamp < %d", time() - (86400 * 7));
-}
-- 
GitLab