Per-node facet blocks.
authorJason Skomorowski <jason@indexdata.com>
Mon, 4 Apr 2011 01:39:43 +0000 (01:39 +0000)
committerJason Skomorowski <jason@indexdata.com>
Mon, 4 Apr 2011 01:39:43 +0000 (01:39 +0000)
mkdru.module

index b363c02..2ee892c 100644 (file)
@@ -493,57 +493,45 @@ function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
 function mkdru_block($op='list', $delta='sources', $edit=array()) {
   switch ($op) {
     case 'list':
-      // facet blocks
-      // D6 has no setting for note type visibility, set
-      // the default to limit facet display to this type
-      $visPHP = '<?php
-  if (arg(0) == "node" && is_numeric(arg(1))) {
-    $node = node_load(array("nid" => arg(1)));
-    return $node->type == "mkdru";
-  }
-?>';
-
-      // NB: block caching is redundant for static content
-      $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
-      $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
-      $blocks['mkdru_sources']['visibility'] = 2;
-      $blocks['mkdru_sources']['pages'] = $visPHP;
-      $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
-      $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE;
-      $blocks['mkdru_subjects']['visibility'] = 2;
-      $blocks['mkdru_subjects']['pages'] = $visPHP;
-      $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
-      $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE;
-      $blocks['mkdru_authors']['visibility'] = 2;
-      $blocks['mkdru_authors']['pages'] = $visPHP;
-      // search blocks
-      $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
+      // maybe if I label the tables or something?
+      $result = db_query("SELECT title, {mkdru}.nid as nid, settings FROM {node},{mkdru} WHERE {mkdru}.nid = {node}.nid;");
       while ($node = db_fetch_object($result)) {
+        // search blocks
         $blocks['mkdru_search_' . $node->nid]['info'] = 
            t('mkdru - search box for "' . $node->title . '"');
-        $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
+        // facet blocks
+        $settings = json_decode($node->settings, TRUE);
+        foreach ($settings['facets'] as $facet_name => $facet) {
+          $key = 'mkdru_facet_' . $facet_name . '_' . $node->nid;
+          $blocks[$key]['info'] = 'mkdru - ' . $facet_name
+            . t(' facet for "') . $node->title . '"';
+          $blocks[$key]['visibility'] = 1;
+          $blocks[$key]['pages'] = 'node/' . $node->nid;
+          $blocks[$key]['cache'] = BLOCK_CACHE_GLOBAL;
+        }
       };
       return $blocks;
 
     case 'view':
-      switch ($delta) {
-        case 'mkdru_sources':
-          $block['subject'] = t('Source');
-          $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-source');
-          return $block;
-        case 'mkdru_subjects':
-          $block['subject'] = t('Subject');
-          $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subject');
-          return $block;
-        case 'mkdru_authors':
-          $block['subject'] = t('Author');
-          $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-author');
-          return $block;
-    }
-    if (substr($delta, 0, 13) == 'mkdru_search_') {
-      $nid = substr($delta, 13);
-      $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
-      return $block;
-    }
+      if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) {
+        $facet = $matches[1];
+        $nid = $matches[2];
+
+        // this is rather spendy just to get the title, should really
+        // be able to set a default title in op=list. will be cached though
+        // so I suppose it's okay?
+        $result = db_query("SELECT settings FROM {mkdru} WHERE nid = %d;", $nid);
+        $settingsjson = db_fetch_object($result)->settings;
+        $settings = json_decode($settingsjson, TRUE);
+        if (isset($settings['facets'][$facet]['displayName'])) {
+          $block['subject'] = $settings['facets'][$facet]['displayName'];
+        }
+        $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-' . $facet);
+      }
+      else if (substr($delta, 0, 13) == 'mkdru_search_') {
+        $nid = substr($delta, 13);
+        $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
+      }
+    return $block;
   }
 }