Parcourir la source

Fixed sync API so that the hook_chado_node_sync_select_query callback could be used to add different categories

Stephen Ficklin il y a 10 ans
Parent
commit
d77c4b20db

+ 14 - 0
tripal_contact/includes/tripal_contact.chado_node.inc

@@ -696,4 +696,18 @@ function tripal_contact_node_update($node) {
  */
 function chado_contact_chado_node_default_title_format() {
   return '[contact.name]';
+}
+
+/**
+ * Implements [content_type]_chado_node_sync_select_query().
+ *
+ * Adds a where clause to the query to exclude the NULL contact.
+ */
+function chado_contact_chado_node_sync_select_query($query) {
+  $query['where_clauses']['title'][] = 'contact.name <> :contact_name_null1';
+  $query['where_clauses']['title'][] = 'contact.name <> :contact_name_null2';
+  $query['where_args']['title'][':contact_name_null1'] = 'null';
+  $query['where_args']['title'][':contact_name_null2'] = 'NULL';
+
+  return $query;
 }

+ 61 - 39
tripal_core/api/tripal_core.chado_nodes.api.inc

@@ -510,41 +510,47 @@ function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id =
   // Allow module to add to query
   $hook_query_alter = $linking_table . '_chado_node_sync_select_query';
   if (function_exists($hook_query_alter)) {
-    call_user_func($hook_query_alter, $select, $joins, $where_clauses, $where_args);
+    $update = call_user_func($hook_query_alter, array(
+      'select' => $select, 
+      'joins' => $joins, 
+      'where_clauses' => $where_clauses, 
+      'where_args' => $where_args,
+    ));
+    // Now add in any new changes
+    if ($update and is_array($update)) {
+      $select = $update['select'];
+      $joins = $update['joins'];
+      $where_clauses = $update['where_clauses'];
+      $where_args = $update['where_args'];
+    }
   }
-
   // Build Query, we do a left join on the chado_xxxx table in the Drupal schema
   // so that if no criteria are specified we only get those items that have not
   // yet been synced.
   $query = "
-    SELECT " . implode(', ',$select) . ' ' .
+    SELECT " . implode(', ', $select) . ' ' .
     'FROM {' . $base_table . '} ' . $base_table . ' ' . implode(' ', $joins) . ' '.
     "  LEFT JOIN public.$linking_table CT ON CT.$base_table_id = $base_table.$base_table_id " .
-    "WHERE CT.$base_table_id IS NULL AND";
+    "WHERE CT.$base_table_id IS NULL ";
 
   // extend the where clause if needed
   $where = '';
   $sql_args = array();
-  if (count($where_clauses['type']) > 0) {
-    $where .= '(' . implode(' OR ', $where_clauses['type']) . ') AND';
-    $sql_args = array_merge($sql_args, $where_args['type']);
-  }
-  if (count($where_clauses['organism']) > 0) {
-    $where .= '(' . implode(' OR ', $where_clauses['organism']) . ') AND';
-    $sql_args = array_merge($sql_args, $where_args['organism']);
-  }
-  if (count($where_clauses['id']) > 0) {
-    $where .= '(' . implode(' OR ', $where_clauses['id']) . ') AND';
-    $sql_args = array_merge($sql_args, $where_args['id']);
+  foreach ($where_clauses as $category => $items) {
+    $where .= ' AND (';
+    foreach ($items as $item) {
+      $where .= $item . ' OR ';
+    }
+    $where = substr($where, 0, -4); // remove the trailing 'OR'
+    $where .= ') ';
+    $sql_args = array_merge($sql_args, $where_args[$category]);
   }
+  
   if ($where) {
     $query .= $where;
   }
-  $query = substr($query, 0, -4); // remove the trailing 'AND'
   $query .- " ORDER BY " . $base_table_id;
 
-print $query."\n";
-
   // If Maximum number to Sync is supplied
   if ($max_sync) {
     $query .= " LIMIT $max_sync";
@@ -836,7 +842,7 @@ function hook_chado_node_sync_form_submit ($form, $form_state) {
 
 
 /**
- * Alter the query for the chado database which gets the chado records to be sync'd (optional)
+ * Alter the query that retrieves records to be sync'd (optional)
  *
  * This might be necessary if you need fields from other chado tables to create your node
  * or if your chado node type only supports a subset of a given table (ie: a germplasm node
@@ -846,27 +852,43 @@ function hook_chado_node_sync_form_submit ($form, $form_state) {
  * Note: For your own module, replace hook in the function name with the machine-name of
  * your chado node type (ie: chado_feature).
  *
- * @param $select:
- *   An array of select clauses
- * @param $joins:
- *   An array of joins (ie: a single join could be 'LEFT JOIN {chadotable} alias ON base.id=alias.id')
- * @param $where_clauses:
- *   An array of where clauses which will all be AND'ed together. Use :placeholders for values.
- * @param $where_args:
- *   An associative array of arguments to be subbed in to the where clause where the
- *   key = :placeholder and the value is the actual argument to be subbed in.
+ * @param $query 
+ *   An array containing the following: 
+ *    'select': An array of select clauses
+ *    'joins:  An array of joins (ie: a single join could be 
+ *      'LEFT JOIN {chadotable} alias ON base.id=alias.id')
+ *    'where_clauses: An array of where clauses which will all be AND'ed 
+ *      together. Use :placeholders for values.
+ *    'where_args: An associative array of arguments to be subbed in to the 
+ *      where clause where the
  *
  * @ingroup tripal_chado_node_api
  */
-function hook_chado_node_sync_select_query (&$select, &$joins, &$where_clauses, &$where_args) {
-
-  // You can add fields to be selected
-  $select[] = 'example.myfavfield';
-
-  // Or joins to important tables
-  $joins[] = 'LEFT JOIN {exampleprop} PROP ON PROP.example_id=EXAMPLE.example_id';
-
-  // Or filter the query using where clauses
-  $where_clauses[] = 'example.myfavfield = :favvalue';
-  $where_args[':favvalue'] = 'awesome-ness';
+function hook_chado_node_sync_select_query($query) {
+
+  // You can add fields to be selected. Be sure to prefix each field with the 
+  // tale name.
+  $query['select'][] = 'example.myfavfield';
+
+  // Provide any join you may need to the joins array. Be sure to wrap the 
+  // table name in curly brackets.
+  $query['joins'][] = 'LEFT JOIN {exampleprop} PROP ON PROP.example_id=EXAMPLE.example_id';
+
+  // The category should be a unique id for a group of items that will be 
+  // concatenated together via an SQL 'OR'.  By default the $where_clases
+  // variable will come with categories of 'id', 'organism' and 'type'.  
+  // you can add your own unique category or alter the contents of the existing
+  // categories.  Be sure to make sure the category doesn't already exist
+  // in the $query['where_clauses']
+  $category = 'my_category'; 
+  
+  // Provide any aditionall where clauses and their necessary arguments.
+  // Be sure to prefix the field with the table name.   Be sure that the 
+  // placeholder is unique across all categories (perhaps add a unique 
+  // prefix/suffix).
+  $query['where_clauses'][$category][] = 'example.myfavfield = :favvalue';
+  $query['where_args'][$category][':favvalue'] = 'awesome-ness';
+  
+  // Must return the updated query
+  return $query;
 }

+ 12 - 0
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -1203,3 +1203,15 @@ function tripal_pub_node_presave($node) {
 function chado_pub_chado_node_default_title_format() {
   return '[pub.title]';
 }
+
+/**
+ * Implements [content_type]_chado_node_sync_select_query().
+ * 
+ * Adds a where clause to the query to exclude the NULL pub.
+ */
+function chado_pub_chado_node_sync_select_query($query) {
+  $query['where_clauses']['title'][] = 'pub.title <> :pub_title_null';
+  $query['where_args']['title'][':pub_title_null'] = 'NULL';
+  
+  return $query;
+}