Running in Drupal 7.
[mkdru-moved-to-drupal.org.git] / mkdru.module
index a68fc93..6acbcd9 100644 (file)
@@ -5,13 +5,13 @@
 
 // Module metainfo
 /**
-* Implementation of hook_node_info()
+* Implements hook_node_info()
 */
 function mkdru_node_info() {
   return array(
     'mkdru' => array(
       'name' => t("Pazpar2 metasearch interface"),
-      'module' => 'mkdru',
+      'base' => 'mkdru',
       'description' => t("Metasearch interface for Z39.50/SRU and other targets via a Pazpar2/Service Proxy backend"),
     )
   );
@@ -20,12 +20,14 @@ function mkdru_node_info() {
 function mkdru_ting_search_show($params) {
    $path = drupal_get_path('module', 'mkdru');
   // Include client library.
-  drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') 
-    . '/pz2.js', 'module', 'footer');
-  drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer');
-  drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer');
-  drupal_add_js($path . '/mkdru.client.js', 'module', 'footer');
-  $html = theme('mkdru_results'); 
+  drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js',
+    array('type' => 'file', 'scope' => 'footer'));
+  drupal_add_library('overlay', 'jquery-bbq');
+  drupal_add_js($path . '/mkdru.theme.js',
+    array('type' => 'file', 'scope' => 'footer'));
+  drupal_add_js($path . '/mkdru.client.js',
+    array('type' => 'file', 'scope' => 'footer'));
+  $html = theme('mkdru_results');
   drupal_add_js(array('mkdru' => 
     array('use_sessions' => '1', 'query' => $params['keys']
     )), 'setting');
@@ -33,17 +35,29 @@ function mkdru_ting_search_show($params) {
 }
 
 /**
-* Implementation of hook_perm()
+* Implements hook_permission()
 */
-function mkdru_perm() {
-  return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
+function mkdru_permission() {
+  return array(
+    'administer metasearch interfaces' => array(
+      'title' => t('Administer Pazpar2 metasearch integration'),
+    ),
+    'create metasearch interface' => array(
+      'title' => t('Create metasearch interface'),
+    ),
+    'edit any metasearch interface' => array(
+      'title' => t('Edit any metasearch interface'),
+    ),
+    'edit own metasearch interface' => array(
+      'title' => t('Edit own metasearch interface'),
+    ),
+  );
 }
 
 /**
-* Implementation of hook_access()
+* Implements hook_node_access()
 */
-function mkdru_access($op, $node, $account) {
-
+function mkdru_node_access($node, $op, $account) {
   if ($op == 'create') {
     // Only users with permission to do so may create this node type.
     return user_access('create metasearch interface', $account);
@@ -62,7 +76,7 @@ function mkdru_access($op, $node, $account) {
 }
 
 /**
-* Implementation of hook_menu()
+* Implements hook_menu()
 */
 function mkdru_menu() {
   $items['admin/settings/mkdru'] = array(
@@ -91,10 +105,10 @@ function mkdru_init() {
 
 // Node config
 /**
-* Implementation of hook_form()
+* Implements hook_form()
 */
 function mkdru_form(&$node, $form_state) {
-  $type = node_get_types('type', $node);
+  $type = node_type_get_type($node);
 
   $form['title'] = array(
     '#type' => 'textfield',
@@ -182,7 +196,7 @@ function mkdru_form(&$node, $form_state) {
 
 
 /**
-* Implementation of hook_validate()
+* Implements hook_validate()
 */
 function mkdru_validate($node) {
   if (!is_numeric($node->source_max)) {
@@ -197,14 +211,14 @@ function mkdru_validate($node) {
 }
 
 /**
-* Implementation of hook_insert().
+* Implements hook_insert().
 */
 function mkdru_insert($node) {
   drupal_write_record('mkdru', $node);
 }
 
 /**
-* Implementation of hook_update().
+* Implements hook_update().
 */
 function mkdru_update($node) {
   if ($node->revision) {
@@ -217,62 +231,60 @@ function mkdru_update($node) {
 }
 
 /**
- * Implementation of hook_nodeapi().
- *
- * When a node revision is deleted, we need to remove the corresponding record
- * from our table. The only way to handle revision deletion is by implementing
- * hook_nodeapi().
+ * Implements hook_node_revision_delete()
  */
-function mkdru_nodeapi(&$node, $op, $teaser, $page) {
-  switch ($op) {
-    case 'delete revision':
-      db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
-      break;
-  }
+function mkdru_node_revision_delete($node) {
+  db_delete('mkdru')
+    ->condition('vid', $node->vid)
+    ->execute();
 }
 
 /**
- * Implementation of hook_delete().
+ * Implements hook_delete()
  */
 function mkdru_delete($node) {
   // Deleting by nid covers all revisions.
-  db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
+  db_delete('mkdru')
+    ->condition('nid', $nid->nid)
+    ->execute();
 }
 
 
 
 // Node rendering
 /**
-* Implementation of hook_load()
+* Implements hook_load()
 */
-function mkdru_load($node) {
-  return array('mkdru' => db_fetch_object(db_query(
-    'SELECT * FROM {mkdru} WHERE vid = %d', $node->vid)));
+function mkdru_load($nodes) {
+  $result = db_query('SELECT * FROM {mkdru} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
+  foreach ($result as $record) {
+    $nodes[$record->nid]->mkdru = $record;
+  }
 }
 
 /**
-* Implementation of hook_theme().
+* Implements hook_theme().
 */
 function mkdru_theme() {
   return array(
     'mkdru_form' => array(
       'template' => 'mkdru-form',
-      'arguments' => array(),
+      'variables' => array(),
     ),
     'mkdru_results' => array(
       'template' => 'mkdru-results',
-      'arguments' => array(),
+      'variables' => array(),
     ),
     'mkdru_js' => array(
-      'arguments' => array('node' => NULL),
+      'variables' => array('node' => NULL),
     ),
     'mkdru_block_search' => array(
       'template' => 'mkdru-block-search',
-      'arguments' => array('nid' => NULL, 'path' => NULL),
+      'variables' => array('nid' => NULL, 'path' => NULL),
     ),
     'mkdru_block_facet' => array(
       'template' => 'mkdru-block-facet',
-      'arguments' => array('class' => NULL)
+      'variables' => array('class' => NULL)
     )
   );
 }
@@ -280,93 +292,88 @@ function mkdru_theme() {
 /**
 * Theme function to include Javascript search client and deps
 */
-function theme_mkdru_js($node) {
+function theme_mkdru_js(&$variables) {
   $path = drupal_get_path('module', 'mkdru');
-  // Pazpar2 client library.
-  drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
+  // Pazpar2 client library
+  drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', array(
+    'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
   // jQuery plugin for query string/history manipulation.
-  drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
-  drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
-  drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
-  drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
+  drupal_add_library('overlay', 'jquery-bbq');
+  drupal_add_js($path . '/mkdru.theme.js', array(
+    'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
+  drupal_add_js($path . '/mkdru.client.js', array(
+    'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
+  drupal_add_js(array('mkdru' => $variables['node']->mkdru), 'setting');
 }
 
 /** 
-* Implementation of hook_view()
+* Implements hook_view()
 */
-function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
-  $node->content['mkdru_js'] = array(
-    '#value' => theme('mkdru_js', $node), 
-    '#weight' => 0,
-  );
-  $node->content['mkdru_form'] = array(
-    '#value' => theme('mkdru_form'), 
-    '#weight' => 1,
-  );
-  $node->content['mkdru_results'] = array(
-    '#value' => theme('mkdru_results'), 
-    '#weight' => 2,
-  );
+function mkdru_view($node, $view_mode) {
+  if ($view_mode == 'full') {
+    $node->content['mkdru_js'] = array(
+      '#markup' => theme('mkdru_js', array('node' => $node)), 
+      '#weight' => 0,
+    );
+    $node->content['mkdru_form'] = array(
+      '#markup' => theme('mkdru_form'), 
+      '#weight' => 1,
+    );
+    $node->content['mkdru_results'] = array(
+      '#markup' => theme('mkdru_results'), 
+      '#weight' => 2,
+    );
+  }
   return $node;
 }
 
+
+
+// Blocks
 /** 
-* Implementation of hook_block()
+* Implements hook_block_info()
 */
-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';");
-      while ($node = db_fetch_object($result)) {
-        $blocks['mkdru_search_' . $node->nid]['info'] = 
-           t('mkdru - search box for "' . $node->title . '"');
-        $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
-      };
-      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);
+function mkdru_block_info() {
+  // NB: block caching is redundant for static content
+  $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
+  $blocks['mkdru_sources']['cache'] = DRUPAL_NO_CACHE;
+  $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
+  $blocks['mkdru_subjects']['cache'] = DRUPAL_NO_CACHE;
+  $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
+  $blocks['mkdru_authors']['cache'] = DRUPAL_NO_CACHE;
+
+  // search blocks
+  $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
+  foreach($result as $node) {
+    $blocks['mkdru_search_' . $node->nid]['info'] = 
+        t('mkdru - search box for "' . $node->title . '"');
+    $blocks['mkdru_sources']['cache'] = DRUPAL_NO_CACHE;
+  };
+  return $blocks;
+}
+
+/** 
+* Implements hook_block_view()
+*/
+function mkdru_block_view($delta) {
+  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',
+        array('nid' => $nid, 'path' => '/node/' . $nid));
+    return $block;
   }
 }