Переглянути джерело

Preliminary commit of converted tripal_library module

spficklin 11 роки тому
батько
коміт
53b9506b4e

+ 1 - 1
tripal_feature/includes/tripal_feature.sync_features.inc

@@ -134,7 +134,7 @@ function tripal_feature_set_urls($na = NULL, $job = NULL) {
   
   // get the number of records we need to set URLs for
   $csql = "SELECT count(*) FROM {chado_feature}";
-  $num_nodes = db_result(db_query($csql));
+  $num_nodes = db_query($csql)->fetchField();
     
   // calculate the interval at which we will print an update on the screen
   $num_set = 0;

+ 21 - 9
tripal_feature/tripal_feature.module

@@ -663,7 +663,7 @@ function chado_feature_add_synonyms($synonyms, $feature_id) {
     if (!$synonym) {
       $synonym_isql = "
         INSERT INTO {synonym} (name, synonym_sgml, type_id) 
-      	VALUES (:name, :synonym_sgml, 
+        VALUES (:name, :synonym_sgml, 
           (SELECT cvterm_id 
            FROM {CVTerm} CVT 
              INNER JOIN CV ON CVT.cv_id = CV.cv_id 
@@ -1880,19 +1880,15 @@ function tripal_feature_node_insert($node) {
       break;
   }
 }
-
 /**
  *
  * @ingroup tripal_feature
  */
-function tripal_feature_node_update($node) { 
-  
-  // add items to other nodes, build index and search results
+function tripal_feature_node_view($node, $view_mode, $langcode) {
   switch ($node->type) {
     case 'chado_organism':
-      // Show feature browser
-      $types_to_show = array('chado_organism', 'chado_library');
-      if (in_array($node->type, $types_to_show, TRUE)) {
+      // Show feature browser and counts
+      if ($view_mode == 'full') {
         $node->content['tripal_organism_feature_counts'] = array(
           '#value' => theme('tripal_organism_feature_counts', $node),
         );
@@ -1901,8 +1897,24 @@ function tripal_feature_node_update($node) {
         );
       }
       break;
+    // TODO: handle these node types. Should we also have a feature browser?
+    case 'chado_library':
+      break;
+    case 'chado_stock':
+      break;
+    case 'chado_analysis':
+      break;
+  }
+}
+/**
+ *
+ * @ingroup tripal_feature
+ */
+function tripal_feature_node_update($node) { 
+  
+  // add items to other nodes, build index and search results
+  switch ($node->type) {
     case 'chado_feature':
-      
       // remove any previous alias
       db_query("DELETE FROM {url_alias} WHERE src = :src", array(':src' => "node/$node->nid"));
       

+ 80 - 75
tripal_library/includes/tripal_library.admin.inc

@@ -126,7 +126,7 @@ function get_tripal_library_admin_form_reindex_set(&$form) {
 
   // iterate through all of the libraries
   $lib_boxes = array();
-  while ($library = db_fetch_object($lib_rset)) {
+  while ($library = $lib_rset->fetchObject()) {
     $lib_boxes[$library->library_id] = "$library->name";
   }
   $form['reindex']['description'] = array(
@@ -242,8 +242,8 @@ function tripal_library_admin_validate($form, &$form_state) {
     }
     if ($library_id and preg_match("/^\d+$/i", $library_id)) {
       // get the library info
-      $sql = "SELECT * FROM {Library} WHERE library_id = %d";
-      $library = db_fetch_object(chado_query($sql, $library_id));
+      $sql = "SELECT * FROM {library} WHERE library_id = :library_id";
+      $library = chado_query($sql, array(':library_id' => $library_id))->fetchObject();
       $to_sync[$library_id] = $library->name;
     }
   }
@@ -267,8 +267,8 @@ function tripal_library_admin_validate($form, &$form_state) {
     foreach ($libraries as $library_id) {
       if ($library_id and preg_match("/^\d+$/i", $library_id)) {
         // get the library info
-        $sql = "SELECT * FROM {Library} WHERE library_id = %d";
-        $library = db_fetch_object(chado_query($sql, $library_id));
+        $sql = "SELECT * FROM {Library} WHERE library_id = :library_id";
+        $library = chado_query($sql, array(':library_id' => $library_id))->fetchObject();
         $job_args[0] = $library_id;
         tripal_add_job("Reindex features for library: $library->name", 'tripal_library',
          'tripal_library_reindex_features', $job_args, $user->uid);
@@ -283,8 +283,8 @@ function tripal_library_admin_validate($form, &$form_state) {
     foreach ($libraries as $library_id) {
       if ($library_id and preg_match("/^\d+$/i", $library_id)) {
         // get the library info
-        $sql = "SELECT * FROM {Library} WHERE library_id = %d";
-        $library = db_fetch_object(chado_query($sql, $library_id));
+        $sql = "SELECT * FROM {Library} WHERE library_id = :libary_id";
+        $library = chado_query($sql, array(':library_id' => $library_id))->fetchObject();
         $job_args[0] = $library_id;
         tripal_add_job("Set taxonomy for features in library: $library->name", 'tripal_library',
          'tripal_library_taxonify_features', $job_args, $user->uid);
@@ -307,29 +307,18 @@ function tripal_library_admin_validate($form, &$form_state) {
  */
 function tripal_library_add_taxonomy($node, $library_id) {
 
-    //include the file containing the required functions.  We only have to
-    // do this because Drupal 6 fails to do this globally for us and
-    // the drupal_execute function below won't work
-    module_load_include('inc', 'taxonomy', 'taxonomy.admin');
-
-   /*   // get the vocabulary id
-    $vocabularies = taxonomy_get_vocabularies();
-    $vid = NULL;
-    foreach($vocabularies as $vocab){
-    if($vocab->name == 'DNA Libraries'){
-    $vid = $vocab->vid;
-    }
-    }
-
-    if(!$vid){  */
+  //include the file containing the required functions.  We only have to
+  // do this because Drupal 6 fails to do this globally for us and
+  // the drupal_execute function below won't work
+  module_load_include('inc', 'taxonomy', 'taxonomy.admin');
+    
    // add the vocabulary
   $vocab_form['values']['name'] = 'DNA Libraries';
   $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
   $vocab_form['values']['help'] = '';
   $vocab_form['values']['module'] = 'taxonomy';
-  drupal_execute('taxonomy_form_vocabulary', $vocab_form);
-  return;
-   //   }
+  drupal_form_submit('taxonomy_form_vocabulary', $vocab_form);
+  return;   
 
    // make sure this term doesn't already exist.  If it doesn't then add it
   if ($vid) {
@@ -346,7 +335,7 @@ function tripal_library_add_taxonomy($node, $library_id) {
       $form_state = array();
       $form_state['values']['name'] = $node->title;
       $form_state['values']['description'] = $library_id;
-      drupal_execute('taxonomy_form_term', $form_state, $vid);
+      drupal_form_submit('taxonomy_form_term', $form_state, $vid);
     }
   }
 }
@@ -364,24 +353,24 @@ function tripal_library_sync_libraries($library_id = NULL, $job_id = NULL) {
 
   // get the list of libraries and create new nodes
   if (!$library_id) {
-    $sql = "SELECT * FROM {Library} L";
+    $sql = "SELECT * FROM {library} L";
     $results = chado_query($sql);
   }
   else {
-    $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
-    $results = chado_query($sql, $library_id);
+    $sql = "SELECT * FROM {library} L WHERE library_id = :library_id";
+    $results = chado_query($sql, array(':library_id' => $library_id));
   }
 
   // We'll use the following SQL statement for checking if the library
   // already exists as a drupal node.
   $sql = "SELECT * FROM {chado_library} ".
-        "WHERE library_id = %d";
+        "WHERE library_id = :libary_id";
 
-  while ($library = db_fetch_object($results)) {
+  while ($library = $results->fetchObject()) {
 
     // check if this library already exists in the drupal database. if it
     // does then skip this library and go to the next one.
-    if (!db_fetch_object(db_query($sql, $library->library_id))) {
+    if (!db_query($sql, array(':libary_id' => $library->library_id))->fetchObject()) {
 
     $new_node = new stdClass();
     $new_node->type = 'chado_library';
@@ -438,29 +427,35 @@ function tripal_library_feature_set_taxonomy($library_id = NULL) {
   // We'll use the following SQL statement for getting the node info
   if ($library_id) {
     print "Finding features for library with ID: $library_id\n";
-    $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
-           "FROM {library_feature} LF ".
-           "INNER JOIN Library L ON LF.library_id = L.library_id ".
-           "WHERE L.library_id = $library_id ".
-           "ORDER BY LF.feature_id";
-    $features = chado_query($sql);
+    $sql = "
+      SELECT LF.feature_id, L.library_id, L.name as libname 
+      FROM {library_feature} LF 
+        INNER JOIN {library} L ON LF.library_id = L.library_id 
+      WHERE L.library_id = :library_id 
+      ORDER BY LF.feature_id
+    ";
+    $features = chado_query($sql, array(':library_id' => $library_id));
   }
   else {
     print "Finding features for all libraries\n";
-    $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
-           "FROM {library_feature} LF ".
-           "INNER JOIN Library L ON LF.library_id = L.library_id ".
-           "ORDER BY LF.feature_id";
+    $sql = "
+      SELECT LF.feature_id, L.library_id, L.name as libname 
+      FROM {library_feature} LF 
+        INNER JOIN {library} L ON LF.library_id = L.library_id 
+      ORDER BY LF.feature_id
+    ";
     $features = chado_query($sql);
   }
 
-  $node_sql = "SELECT * FROM {chado_feature} CF ".
-             "  INNER JOIN {node} N ON CF.nid = N.nid ".
-             "WHERE feature_id = %d";
-
+  $node_sql = "
+    SELECT * 
+    FROM {chado_feature} CF 
+      INNER JOIN {node} N ON CF.nid = N.nid 
+    WHERE feature_id = :feature_id
+  ";
   // iterate through the features and add the taxonomy
-  while ($feature = db_fetch_object($features)) {
-    $node = db_fetch_object(db_query($node_sql, $feature->feature_id));
+  while ($feature = $features->fetchObject()) {
+    $node = db_query($node_sql, array(':feature_id' => $feature->feature_id))->fetchObject();
     $tags["$vid"] = $feature->libname;
     $terms['tags'] = $tags;
     taxonomy_node_save($node, $terms);
@@ -478,25 +473,29 @@ function tripal_library_reindex_features($library_id = NULL, $job_id = NULL) {
   // associated with the library. Otherwise get all sequences assoicated
   // with all libraries.
   if ($library_id) {
-    $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
-           " FROM {library_feature} LF ".
-           "  INNER JOIN Library L ON LF.library_id = L.library_id ".
-           "WHERE L.library_id = $library_id ".
-           "ORDER BY LF.feature_id";
-    $results = chado_query($sql);
+    $sql = "
+      SELECT LF.feature_id, L.library_id, L.name as libname 
+      FROM {library_feature} LF 
+        INNER JOIN {library} L ON LF.library_id = L.library_id
+      WHERE L.library_id = :library_id 
+      ORDER BY LF.feature_id
+    ";
+    $results = chado_query($sql, array(':library_id' => $library_id));
   }
   else {
-    $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
-           " FROM {library_feature} LF ".
-           "  INNER JOIN Library L ON LF.library_id = L.library_id ".
-           "ORDER BY LF.feature_id";
+    $sql = "
+      SELECT LF.feature_id, L.library_id, L.name as libname 
+      FROM {library_feature} LF 
+        INNER JOIN {library} L ON LF.library_id = L.library_id 
+      ORDER BY LF.feature_id
+    ";
     $results = chado_query($sql);
   }
 
    // load into ids array
   $count = 0;
   $ids = array();
-  while ($id = db_fetch_object($results)) {
+  while ($id = $results->fetchObject()) {
     $ids[$count] = $id->feature_id;
     $count++;
   }
@@ -521,25 +520,29 @@ function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
   // associated with the library. Otherwise get all sequences assoicated
   // with all libraries.
   if ($library_id) {
-    $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
-           " FROM {library_feature} LF ".
-           "  INNER JOIN Library L ON LF.library_id = L.library_id ".
-           "WHERE L.library_id = $library_id ".
-           "ORDER BY LF.feature_id";
-    $results = chado_query($sql);
+    $sql = "
+      SELECT LF.feature_id, L.library_id, L.name as libname 
+      FROM {library_feature} LF 
+        INNER JOIN {library} L ON LF.library_id = L.library_id 
+      WHERE L.library_id = :library_id 
+      ORDER BY LF.feature_id
+    ";
+    $results = chado_query($sql, array(':library_id' => $library_id));
   }
   else {
-    $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
-           " FROM {library_feature} LF ".
-           "  INNER JOIN Library L ON LF.library_id = L.library_id ".
-           "ORDER BY LF.feature_id";
+    $sql = "
+      SELECT LF.feature_id, L.library_id, L.name as libname 
+      FROM {library_feature} LF 
+        INNER JOIN {library} L ON LF.library_id = L.library_id 
+      ORDER BY LF.feature_id
+    ";
     $results = chado_query($sql);
   }
 
   // load into ids array
   $count = 0;
   $ids = array();
-  while ($id = db_fetch_object($results)) {
+  while ($id = $results->fetchObject()) {
     $ids[$count] = $id->feature_id;
     $count++;
   }
@@ -548,10 +551,12 @@ function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
   tripal_feature_set_vocabulary();
 
   // use this SQL for getting the nodes
-  $nsql =  "SELECT * FROM {chado_feature} CF ".
-          "  INNER JOIN {node} N ON N.nid = CF.nid ".
-          "WHERE feature_id = %d";
-
+  $nsql =  "
+    SELECT * 
+    FROM {chado_feature} CF 
+      INNER JOIN {node} N ON N.nid = CF.nid 
+    WHERE feature_id = :feature_id
+  ";
    // iterate through the features and set the taxonomy
   $interval = intval($count * 0.01);
   foreach ($ids as $feature_id) {
@@ -559,7 +564,7 @@ function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
     if ($job_id and $i % interval == 0) {
       tripal_job_set_progress($job_id, intval(($i/$count)*100));
     }
-      $node = db_fetch_object(db_query($nsql, $feature_id));
+      $node = db_query($nsql, array(':feature_id' => $feature_id))->fetchObject();
       tripal_feature_set_taxonomy($node, $feature_id);
       $i++;
   }

+ 4 - 4
tripal_library/tripal_library.info

@@ -1,12 +1,12 @@
 name = Tripal Library
 description = A module for interfacing the GMOD chado database with Drupal, providing viewing, inserting and editing of lbraries.
-core = 6.x
+core = 7.x
 project = tripal_library
 package = Tripal
-version = 6.x-1.1
-dependencies[] = tripal_core
-dependencies[] = tripal_organism
+version = 7.x-2.0-beta1
 dependencies[] = search
 dependencies[] = path
+dependencies[] = tripal_core
+dependencies[] = tripal_organism
 dependencies[] = tripal_cv
 

+ 90 - 104
tripal_library/tripal_library.install

@@ -4,92 +4,58 @@
  * @todo Add file header description
  */
 
+/**
+ * Implementation of hook_requirements().
+ */
+function tripal_library_requirements($phase) {
+  $requirements = array();
+  if ($phase == 'install') {
+    // make sure chado is installed
+    if (!tripal_core_is_chado_installed()) {
+      $requirements ['tripal_library'] = array(
+        'title' => "tripal_library",
+        'value' => "ERROR: Chado most be installed before this module can be enabled",
+        'severity' => REQUIREMENT_ERROR,
+      );
+    }
+  }
+  return $requirements;
+}
 /**
  * Implementation of hook_install().
  *
  * @ingroup tripal_library
  */
 function tripal_library_install() {
+  
   // create the module's data directory
   tripal_create_moddir('tripal_library');
 
-  // create the tables that correlate drupal nodes with chado
-  // features, librarys, etc....
-  drupal_install_schema('tripal_library');
-
-  // Add the materialized view needed to count the features for the library
-  // Create the MView
-  tripal_add_mview('library_feature_count', 'tripal_library',
-    'library_feature_count',
-    'library_id integer, name character varying(255), '.
-    '  num_features integer, feature_type character varying(255)',
-    'library_id',
-    'SELECT '.
-    '   L.library_id, '.
-    '   L.name, '.
-    '    count(F.feature_id) as num_features, '.
-    '    CVT.name as feature_type '.
-    'FROM Library L '.
-    '    INNER JOIN Library_Feature LF  ON LF.library_id = L.library_id '.
-    '    INNER JOIN Feature F           ON LF.feature_id = F.feature_id '.
-    '    INNER JOIN Cvterm CVT          ON F.type_id = CVT.cvterm_id '.
-    'GROUP BY L.library_id, L.name, CVT.name',
-    ''
-  );
+  // add the materialized view
+  tripal_library_add_mview_library_feature_count();
   
-  // Add cvterms
+  // add cvterms
   tripal_library_add_cvterms();
 }
 
-/**
- * Implementation of hook_schema().
- *
- * @ingroup tripal_library
- */
-function tripal_library_schema() {
-  $schema = tripal_library_get_schemas();
-  return $schema;
-}
-
 /**
  * Implementation of hook_uninstall().
  *
  * @ingroup tripal_library
  */
 function tripal_library_uninstall() {
-  drupal_uninstall_schema('tripal_library');
-
-  // remove the materialized view
-  $sql = "SELECT * FROM {tripal_mviews} ".
-        "WHERE name = 'library_feature_count'";
-
-  if (db_table_exists('tripal_mviews')) {
-    $mview = db_fetch_object(db_query($sql));
-    if ($mview) {
-      tripal_mviews_action('delete', $mview->mview_id);
-    }
-  }
-
-  // Get the list of nodes to remove
-  $sql_lib_id = "SELECT nid, vid ".
-               "FROM {node} ".
-               "WHERE type='chado_library'";
-  $result = db_query($sql_lib_id);
-  while ($node = db_fetch_object($result)) {
-    node_delete($node->nid);
+  // Drop the MView table if it exists
+  if ($mview_id = tripal_mviews_get_mview_id('library_feature_count')) {
+    tripal_mviews_action("delete", $mview_id);
   }
 }
 
 /**
- * This function simply defines all tables needed for the module to work
- * correctly.  By putting the table definitions in a separate function we
- * can easily provide the entire list for hook_install or individual
- * tables for an update.
+ * Implementation of hook_schema().
  *
  * @ingroup tripal_library
  */
-function tripal_library_get_schemas() {
-  $schema = array();
+function tripal_library_schema() {
   $schema['chado_library'] = array(
     'fields' => array(
       'vid' => array(
@@ -98,49 +64,82 @@ function tripal_library_get_schemas() {
         'not null' => TRUE,
         'default' => 0
       ),
-    'nid' => array(
-      'type' => 'int',
-      'unsigned' => TRUE,
-      'not null' => TRUE,
-      'default' => 0
+      'nid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0
       ),
-    'library_id' => array(
-      'type' => 'int',
-      'not null' => TRUE,
-      'default' => 0
+      'library_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0
       )
-      ),
+    ),
     'indexes' => array(
-      'library_id' => array('library_id')
-      ),
+      'chado_library_idx1' => array('library_id')
+    ),
     'unique keys' => array(
-      'nid_vid' => array('nid', 'vid'),
-      'vid' => array('vid')
-      ),
+      'chado_library_uq1' => array('nid', 'vid'),
+      'chado_library_uq2' => array('vid')
+    ),
     'primary key' => array('nid'),
   );
   return $schema;
 }
+
 /**
- * Implementation of hook_requirements(). 
+ * @ingroup tripal_library
  */
-function tripal_library_requirements($phase) {
-  $requirements = array();
-  if ($phase == 'install') {
-    // make sure chado is installed
-    if (!tripal_core_is_chado_installed()) {
-      $requirements ['tripal_library'] = array(
-        'title' => "tripal_library",
-        'value' => "ERROR: Chado most be installed before this module can be enabled",
-        'severity' => REQUIREMENT_ERROR,
-      );
-    }
-  }
-  return $requirements;
+function tripal_library_add_mview_library_feature_count(){
+  $view_name = 'library_feature_count';
+  $comment = 'Provides count of feature by type that are associated with all libraries';
+  
+  $schema = array(
+    'table' => $view_name,
+    'description' => $comment,
+    'fields' => array(
+      'library_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'name' => array(
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+      'num_features' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'feature_type' => array(
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+    ),
+    'indexes' => array(
+      'library_feature_count_idx1' => array('library_id'),
+    ),
+  );
+  
+  $sql = "
+    SELECT 
+      L.library_id, L.name, 
+      count(F.feature_id) as num_features, 
+      CVT.name as feature_type 
+    FROM library L 
+      INNER JOIN library_feature LF  ON LF.library_id = L.library_id 
+      INNER JOIN feature F           ON LF.feature_id = F.feature_id 
+      INNER JOIN cvterm CVT          ON F.type_id     = CVT.cvterm_id 
+    GROUP BY L.library_id, L.name, CVT.name
+  ";
+  
+  tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
 }
 
-/*
- * 
+/**
+ * @ingroup tripal_library
  */
 function tripal_library_add_cvterms() {
   
@@ -163,17 +162,4 @@ function tripal_library_add_cvterms() {
     'tripal_library_types', 0, 1, 'tripal');
   tripal_cv_add_cvterm(array('name' => 'genomic_library','def' => 'Genomic Library'), 
     'tripal_library_types', 0, 1, 'tripal');
-}
-
-/**
- *  Update for Drupal 6.x, Tripal 1.0
- *  This update
- *   - adds the library types
- *
- * @ingroup tripal_library
- */
-function tripal_library_update_6000() {
-  // add in the missing library typ cv terms
-  tripal_library_add_cvterms();
-  return $ret;
 }

+ 164 - 177
tripal_library/tripal_library.module

@@ -42,12 +42,11 @@ function tripal_library_node_info() {
   $nodes = array();
   $nodes['chado_library'] = array(
     'name' => t('Library'),
-    'module' => 'chado_library',
+    'base' => 'chado_library',
     'description' => t('A library from the chado database'),
     'has_title' => FALSE,
     'title_label' => t('Library'),
     'has_body' => FALSE,
-    'body_label' => t('Library Description'),
     'locked' => TRUE
   );
   return $nodes;
@@ -60,13 +59,28 @@ function tripal_library_node_info() {
  *
  * @ingroup tripal_library
  */
-function tripal_library_perm() {
+function tripal_library_permisssions() {
   return array(
-    'access chado_library content',
-    'create chado_library content',
-    'delete chado_library content',
-    'edit chado_library content',
-    'administer tripal libraries',
+    'access chado_library content' => array(
+      'title' => t('View Libraries'),
+      'description' => t('Allow users to view library pages.'),
+    ),
+    'create chado_library content' => array(
+      'title' => t('Create Libraries'),
+      'description' => t('Allow users to create new library pages.'),
+    ),
+    'delete chado_library content' => array(
+      'title' => t('Delete Libraries'),
+      'description' => t('Allow users to delete library pages.'),
+    ),
+    'edit chado_library content' => array(
+      'title' => t('Edit Libraries'),
+      'description' => t('Allow users to edit library pages.'),
+    ),
+    'adminster tripal library' => array(
+      'title' => t('Administer Libraries'),
+      'description' => t('Allow users to administer all librarys.'),
+    ),
   );
 }
 /**
@@ -92,7 +106,7 @@ function tripal_library_perm() {
  *
  * @ingroup tripal_library
  */
-function chado_library_access($op, $node, $account) {
+function chado_library_node_access($op, $node, $account) {
   if ($op == 'create') {
     if (!user_access('create chado_library content', $account)) {
       return FALSE;
@@ -169,49 +183,27 @@ function tripal_library_views_api() {
 }
 
 /**
- * Implementation of hook_nodeapi().
- * Display library information for associated features or organisms
- * This function also provides contents for indexing
- *
  * @ingroup tripal_library
  */
-function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
-
-  switch ($op) {
-    // Note that this function only adds library view to an organism/feature
-    // node.
-    case 'view':
-      // add the library to the organism/feature search indexing
-    if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
-      $node->content['tripal_library_index_version'] = array(
-    '#value' => theme('tripal_library_search_index', $node),
-      );
-    }
-    elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
-      $node->content['tripal_library_index_version'] = array(
-    '#value' => theme('tripal_library_search_result', $node),
-      );
-    }
-    else {
-      switch ($node->type) {
-        case 'chado_organism':
-          // Show library if the organism/feature is not at teaser view
-          $node->content['tripal_organism_libraries'] = array(
-        '#value' => theme('tripal_organism_libraries', $node),
-          );
-        break;
-        case 'chado_feature':
-          // Show library if the organism/feature is not at teaser view
-          $node->content['tripal_feature_libraries'] = array(
-        '#value' => theme('tripal_feature_libraries', $node),
-          );
-        break;
-        }
+function tripal_library_node_view(&$node, $view_mode, $langcode) {
+
+  switch ($node->type) {
+    case 'chado_organism':
+      if ($view_mode == 'full') {
+        $node->content['tripal_organism_libraries'] = array(
+          '#value' => theme('tripal_organism_libraries', $node),
+        );
       }
-    break;
+      break;
+    case 'chado_feature':
+      if ($view_mode == 'full') {
+        $node->content['tripal_feature_libraries'] = array(
+          '#value' => theme('tripal_feature_libraries', $node),
+        );
+      }
+      break;
   }
 }
-
 /**
  *  We need to let drupal know about our theme functions and their arguments.
  *  We create theme functions to allow users of the module to customize the
@@ -278,17 +270,19 @@ function theme_tripal_library_search_index($node) {
   if ($node->type == 'chado_organism') {
     $content = "";
     // get the libraries for the organism
-    $sql = "SELECT * FROM {library} L ".
-            "WHERE L.organism_id = %d";
+    $sql = "SELECT * FROM {library} L WHERE L.organism_id = :organism_id";
     $libraries = array();
-    $results = chado_query($sql, $node->organism->organism_id);
-    while ($library = db_fetch_object($results)) {
+    $results = chado_query($sql, array(':organism_id' => $node->organism->organism_id));
+    while ($library = $results->fetchObject()) {
       // get the description
-      $sql = "SELECT * FROM {libraryprop} LP ".
-             "  INNER JOIN {CVTerm} CVT ON CVT.cvterm_id = LP.type_id ".
-            "WHERE LP.library_id = $library->library_id ".
-            "  AND CVT.name = 'library_description'";
-      $desc = db_fetch_object(chado_query($sql));
+      $sql = "
+        SELECT * 
+        FROM {libraryprop} LP 
+          INNER JOIN {cvterm} CVT ON CVT.cvterm_id = LP.type_id 
+        WHERE LP.library_id = :library_id
+          AND CVT.name = 'library_description'
+      ";
+      $desc = chado_query($sql, array(':library_id' => $library->library_id))->fetchObject();
       $library->description = $desc->value;
       $libraries[] = $library;
     }
@@ -303,12 +297,15 @@ function theme_tripal_library_search_index($node) {
   elseif ($node->type == 'chado_feature') {
     $content = "";
     $organism_id = $node->feature->organism_id;
-    $sql = "SELECT * FROM {library} L ".
-           "  INNER JOIN {Library_feature} LF ON L.library_id = LF.library_id ".
-            "WHERE LF.feature_id = " . $node->feature->feature_id;
+    $sql = "
+      SELECT * 
+      FROM {library} L 
+        INNER JOIN {library_feature} LF ON L.library_id = LF.library_id 
+      WHERE LF.feature_id = :feature_id
+    "; 
     $libraries = array();
-    $results = chado_query($sql);
-    while ($library = db_fetch_object($results)) {
+    $results = chado_query($sql, array(':feature_id' => $node->feature->feature_id));
+    while ($library = $results->fetchObject()) {
       $libraries[] = $library;
     }
     if (count($libraries) > 0) {
@@ -345,19 +342,22 @@ function theme_tripal_library_node_libraries($node) {
   // when a feature is previewed at its creation
   elseif ($node->type == 'chado_feature' && $node->feature->feature_id) {
     $organism_id = $node->feature->organism_id;
-    $sql = "SELECT * FROM {library} L ".
-           " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
-            "WHERE LF.feature_id = " . $node->feature->feature_id;
+    $sql = "
+      SELECT * 
+      FROM {library} L 
+        INNER JOIN Library_feature LF ON L.library_id = LF.library_id 
+      WHERE LF.feature_id = :feature_id
+    ";
     $libraries = array();
-    $results = chado_query($sql);
-    while ($library = db_fetch_object($results)) {
+    $results = chado_query($sql, array(':feature_id' => $node->feature->feature_id));
+    while ($library = $results->fetchObject()) {
       $libraries[] = $library;
   }
   if (count($libraries) > 0) {
     $lib_additions = array();
     foreach ($libraries as $library) {
-      $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
-      $lib_nid = db_result(db_query($sql, $library->library_id));
+      $sql = "SELECT nid FROM {chado_library} WHERE library_id = :library_id";
+      $lib_nid = db_query($sql, array(':library_id' => $library->library_id))->fetchField();
       if ($lib_nid) {
         $lib_url = url("node/$lib_nid");
       }
@@ -383,7 +383,7 @@ function tripal_library_cron() {
  *
  * @ingroup tripal_library
  */
-function tripal_library_library_access($op, $node, $account) {
+function tripal_library_node_access($op, $node, $account) {
   if ($op == 'create') {
     if (!user_access('create chado_library content', $account)) {
       return FALSE;
@@ -497,12 +497,12 @@ function chado_library_form($node) {
   );
 
   // get the list of organisms
-  $sql = "SELECT * FROM {Organism}";
+  $sql = "SELECT * FROM {organism}";
   $org_rset = chado_query($sql);
 
   $organisms = array();
   $organisms[''] = '';
-  while ($organism = db_fetch_object($org_rset)) {
+  while ($organism = $org_rset->fetchObject()) {
     $organisms[$organism->organism_id] =
     "$organism->genus $organism->species ($organism->common_name)";
   }
@@ -539,21 +539,19 @@ function chado_library_validate($node) {
   // check to make sure the unique name on the library is unique
   // before we try to insert into chado.
   if ($node->library_id) {
-    $sql = "SELECT * FROM ".
-           "{Library} WHERE ".
-           "uniquename = '%s' ".
-           "AND NOT library_id = %d";
-    $lib = db_fetch_object(chado_query($sql, $node->uniquename, $node->library_id));
+    $sql = "
+      SELECT * 
+      FROM {library} 
+      WHERE uniquename = :uname AND NOT library_id = :library_id
+    ";
+    $lib = chado_query($sql, array(':uname' => $node->uniquename, ':library_id' => $node->library_id))->fetchObject();
   }
   else {
-    $sql = "SELECT * FROM ".
-           "{Library} ".
-           "WHERE uniquename = '%s'";
-    $lib = db_fetch_object(chado_query($sql, $node->uniquename));
+    $sql = "SELECT * FROM {library} WHERE uniquename = :uname";
+    $lib = chado_query($sql, array(':uname' => $node->uniquename))->fetchObject();
   }
   if ($lib) {
-    form_set_error('uniquename', t('The unique library name already exists. '.
-                                  'Please choose another'));
+    form_set_error('uniquename', t('The unique library name already exists. Please choose another'));
   }
 }
 /**
@@ -588,18 +586,17 @@ function chado_library_insert($node) {
     $library_id = chado_get_id_for_node('library', $node->nid) ;
     if (!$library_id) {
        // next add the item to the drupal table
-      $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
-             "VALUES (%d, %d, %d)";
-      db_query($sql, $node->nid, $node->vid, $library['library_id']);
+      $sql = "
+        INSERT INTO {chado_library} (nid, vid, library_id) 
+        VALUES (:nid, :vid, :library_id)
+      ";
+      db_query($sql, array(':nid' => $node->nid, ':vid' => $node->vid, ':library_id' => $library['library_id']));
     }
   }
   else {
     drupal_set_message(t('Unable to add library.', 'warning'));
-    watchdog('tripal_library',
-    'Insert feature: Unable to create library where values: %values',
-    array('%values' => print_r($values, TRUE)),
-    WATCHDOG_WARNING
-    );
+    watchdog('tripal_library', 'Insert feature: Unable to create library where values: %values',
+      array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
   }
 }
 /**
@@ -663,8 +660,8 @@ function chado_library_view($node, $teaser = FALSE, $page = FALSE) {
     // but not genus/species/common_name. We need to get those from chado
     // database so they will show up in preview
     if (!$node->genus) {
-      $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
-      $data = db_fetch_object(chado_query($sql, $node->organism_id));
+      $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
+      $data = chado_query($sql, array(':organism_id' => $node->organism_id))->fetchObject();
       $node->genus = $data->genus;
       $node->species = $data->species;
       $node->common_name = $data->common_name;
@@ -689,97 +686,87 @@ function chado_library_delete(&$node) {
 
   // Remove data from {chado_library}, {node} and {node_revisions} tables of
   // drupal database
-  $sql_del = "DELETE FROM {chado_library} ".
-            "WHERE nid = %d ".
-            "AND vid = %d";
-  db_query($sql_del, $node->nid, $node->vid);
-  $sql_del = "DELETE FROM {node_revisions} ".
-            "WHERE nid = %d ".
-            "AND vid = %d";
-  db_query($sql_del, $node->nid, $node->vid);
-  $sql_del = "DELETE FROM {node} ".
-            "WHERE nid = %d ".
-            "AND vid = %d";
-  db_query($sql_del, $node->nid, $node->vid);
+  $sql_del = "DELETE FROM {chado_library} WHERE nid = :nid AND vid = :vid";
+  db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
+  $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
+  db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
+  $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
+  db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
 
   // Remove data from library and libraryprop tables of chado database as well
-  chado_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
-  chado_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
+  chado_query("DELETE FROM {libraryprop} WHERE library_id = :library_id", array(':library_id' => $library_id));
+  chado_query("DELETE FROM {library} WHERE library_id = :library_id", array(':library_id' => $library_id));
 }
 
 /**
- * Display block with libraries
- * @param op    - parameter to define the phase being called for the block
- * @param delta - id of the block to return (ignored when op is list)
- * @param edit  - when op is save, contains the submitted form data
- *
  * @ingroup tripal_library
  */
-function tripal_library_block($op = 'list', $delta = '0', $edit = array()) {
-  switch ($op) {
-    case 'list':
-
-    $blocks['libreferences']['info'] = t('Tripal Library Cross References');
-    $blocks['libreferences']['cache'] = BLOCK_NO_CACHE;
-
-    $blocks['libbase']['info'] = t('Tripal Library Details');
-    $blocks['libbase']['cache'] = BLOCK_NO_CACHE;
-
-    $blocks['libterms']['info'] = t('Tripal Library Terms');
-    $blocks['libterms']['cache'] = BLOCK_NO_CACHE;
-
-    $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
-    $blocks['libsynonyms']['cache'] = BLOCK_NO_CACHE;
-
-    $blocks['libproperties']['info'] = t('Tripal Library Properties');
-    $blocks['libproperties']['cache'] = BLOCK_NO_CACHE;
-
-    $blocks['featurelibs']['info'] = t('Tripal Feature Libraries');
-    $blocks['featurelibs']['cache'] = BLOCK_NO_CACHE;
-
-    $blocks['orglibs']['info'] = t('Tripal Organism Libraries');
-    $blocks['orglibs']['cache'] = BLOCK_NO_CACHE;
-
-    return $blocks;
-
-    case 'view':
-      if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
-        $nid = arg(1);
-        $node = node_load($nid);
-
-        $block = array();
-        switch ($delta) {
-          case 'libreferences':
-            $block['subject'] = t('Cross References');
-            $block['content'] = theme('tripal_library_references', $node);
-            break;
-          case 'libbase':
-            $block['subject'] = t('Library Details');
-            $block['content'] = theme('tripal_library_base', $node);
-            break;
-          case 'libsynonyms':
-            $block['subject'] = t('Synonyms');
-            $block['content'] = theme('tripal_library_synonyms', $node);
-            break;
-          case 'libproperties':
-            $block['subject'] = t('Properties');
-            $block['content'] = theme('tripal_library_properties', $node);
-            break;
-          case 'libterms':
-            $block['subject'] = t('Library Terms');
-            $block['content'] = theme('tripal_library_terms', $node);
-            break;
-          case 'featurelibs':
-            $block['subject'] = t('Libraries');
-            $block['content'] = theme('tripal_feature_libraries', $node);
-            break;
-          case 'orglibs':
-            $block['subject'] = t('Libraries');
-            $block['content'] = theme('tripal_organism_libraries', $node);
-            break;
-            default :
-        }
-        return $block;
-      }
+function tripal_library_block_info() {
+
+  $blocks['libreferences']['info'] = t('Tripal Library Cross References');
+  $blocks['libreferences']['cache'] = BLOCK_NO_CACHE;
+
+  $blocks['libbase']['info'] = t('Tripal Library Details');
+  $blocks['libbase']['cache'] = BLOCK_NO_CACHE;
+
+  $blocks['libterms']['info'] = t('Tripal Library Terms');
+  $blocks['libterms']['cache'] = BLOCK_NO_CACHE;
+
+  $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
+  $blocks['libsynonyms']['cache'] = BLOCK_NO_CACHE;
+
+  $blocks['libproperties']['info'] = t('Tripal Library Properties');
+  $blocks['libproperties']['cache'] = BLOCK_NO_CACHE;
+
+  $blocks['featurelibs']['info'] = t('Tripal Feature Libraries');
+  $blocks['featurelibs']['cache'] = BLOCK_NO_CACHE;
+
+  $blocks['orglibs']['info'] = t('Tripal Organism Libraries');
+  $blocks['orglibs']['cache'] = BLOCK_NO_CACHE;
+
+  return $blocks;
+}
+/**
+ * @ingroup tripal_library
+ */
+function tripal_library_block_view($delta = '') {
+  
+  if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
+    $nid = arg(1);
+    $node = node_load($nid);
+  
+    $block = array();
+    switch ($delta) {
+      case 'libreferences':
+        $block['subject'] = t('Cross References');
+        $block['content'] = theme('tripal_library_references', $node);
+        break;
+      case 'libbase':
+        $block['subject'] = t('Library Details');
+        $block['content'] = theme('tripal_library_base', $node);
+        break;
+      case 'libsynonyms':
+        $block['subject'] = t('Synonyms');
+        $block['content'] = theme('tripal_library_synonyms', $node);
+        break;
+      case 'libproperties':
+        $block['subject'] = t('Properties');
+        $block['content'] = theme('tripal_library_properties', $node);
+        break;
+      case 'libterms':
+        $block['subject'] = t('Library Terms');
+        $block['content'] = theme('tripal_library_terms', $node);
+        break;
+      case 'featurelibs':
+        $block['subject'] = t('Libraries');
+        $block['content'] = theme('tripal_feature_libraries', $node);
+        break;
+      case 'orglibs':
+        $block['subject'] = t('Libraries');
+        $block['content'] = theme('tripal_organism_libraries', $node);
+        break;
+        default :
+    }
+    return $block;
   }
 }

+ 4 - 11
tripal_organism/tripal_organism.module

@@ -400,17 +400,11 @@ function chado_organism_delete($node) {
   }
 
   // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
-  $sql_del = "DELETE FROM {chado_organism} " .
-             "WHERE nid = :nid " .
-             "AND vid = :vid";
+  $sql_del = "DELETE FROM {chado_organism} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-  $sql_del = "DELETE FROM {node} " .
-             "WHERE nid = :nid " .
-             "AND vid = :vid";
+  $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-  $sql_del = "DELETE FROM {node_revision} " .
-             "WHERE nid = ':nid' " .
-             "AND vid = ':vid'";
+  $sql_del = "DELETE FROM {node_revision} WHERE nid = ':nid' AND vid = ':vid'";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
 
   // Test dependency before deleting from chado database. If a library or
@@ -426,8 +420,7 @@ function chado_organism_delete($node) {
   }
   else {
     drupal_set_message(t("Organism deleted from drupal. Warning: at least one " .
-                       "library or feature depends on this organism. It was " .
-               "not removed from chado."));
+      "library or feature depends on this organism. It was not removed from chado."));
   }
 }
 /**