Browse Source

Fixed problem with contact relationships template

Stephen Ficklin 11 years ago
parent
commit
48d3e87fe4

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

@@ -572,4 +572,24 @@ function tripal_contact_node_view($node, $view_mode, $langcode) {
       }
       break;
   }
+}
+
+/**
+ *
+ * @param $node
+ */
+function tripal_contact_node_presave($node) {
+  switch ($node->type) {
+    case 'chado_contact':
+      // for a form submission the fields part of the node object
+      // but for a sync the feilds are in an object of the node
+      if(property_exists($node, 'contact')) {
+        // set the title
+        $node->title = $node->contact->name;
+      }
+      else {
+        // the title is an element of the form and does not need to be set
+      }
+      break;
+  }
 }

+ 63 - 47
tripal_contact/theme/tripal_contact.theme.inc

@@ -5,31 +5,28 @@
  * @ingroup tripal_contact
  */
 function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
-  // we want to provide a new variable that contains the matched contacts.
   $contact = $variables['node']->contact;
+  
+   // expand the contact object to include the contact relationships.
+  $options = array(
+    'return_array' => 1,
+    // we don't want to fully recurse we only need information about the
+    // relationship type and the object and subject contacts (including contact type)
+    'include_fk' => array(
+      'type_id' => 1,
+      'object_id' => array(
+        'type_id' => 1,
+      ),
+      'subject_id'  => array(
+        'type_id' => 1,
+      ),
+    ),
+  );
+  $contact = tripal_core_expand_chado_vars($contact, 'table', 'contact_relationship', $options);
 
-  // normally we would use tripal_core_expand_chado_vars to expand our
-  // organism object and add in the relationships, however whan a large
-  // number of relationships are present this significantly slows the
-  // query, therefore we will manually perform the query
-  $sql = "
-    SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
-    FROM {contact_relationship} PR
-      INNER JOIN {contact} C              ON PR.object_id  = C.contact_id
-      INNER JOIN {cvterm} CVT             ON PR.type_id    = CVT.cvterm_id
-      LEFT  JOIN public.chado_contact CP  ON C.contact_id  = CP.contact_id
-    WHERE PR.subject_id = :contact_id
-  ";
-  $as_subject = chado_query($sql, array(':contact_id' => $contact->contact_id));
-  $sql = "
-    SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
-    FROM {contact_relationship} PR
-      INNER JOIN {contact} C              ON PR.subject_id = C.contact_id
-      INNER JOIN {cvterm} CVT             ON PR.type_id    = CVT.cvterm_id
-      LEFT  JOIN public.chado_contact CP  ON C.contact_id  = CP.contact_id
-    WHERE PR.object_id = :contact_id
-  ";
-  $as_object = chado_query($sql, array(':contact_id' => $contact->contact_id));
+  // get the subject relationships
+  $srelationships = $contact->contact_relationship->subject_id;
+  $orelationships = $contact->contact_relationship->object_id;
 
   // combine both object and subject relationshisp into a single array
   $relationships = array();
@@ -37,36 +34,55 @@ function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
   $relationships['subject'] = array();
 
   // iterate through the object relationships
-  while ($relationship = $as_object->fetchObject()) {
+  if ($orelationships) {
+    foreach ($orelationships as $relationship) {
+      $rel = new stdClass();
+      $rel->record = $relationship;
+       
+      // get the relationship and child types
+      $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
+      $child_type = $relationship->subject_id->type_id->name;
+       
+      // get the node id of the subject
+      $sql = "SELECT nid FROM {chado_contact} WHERE contact_id = :contact_id";
+      $n = db_query($sql, array(':contact_id' => $relationship->subject_id->contact_id))->fetchObject();
+      if ($n) {
+        $rel->record->nid = $n->nid;
+      }
 
-    // get the relationship and child types
-    $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
-    $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
-
-    if (!array_key_exists($rel_type, $relationships['object'])) {
-      $relationships['object'][$rel_type] = array();
-    }
-    if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
-      $relationships['object'][$rel_type][$sub_type] = array();
+      if (!array_key_exists($rel_type, $relationships['object'])) {
+        $relationships['object'][$rel_type] = array();
+      }
+      if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
+        $relationships['object'][$rel_type][$child_type] = array();
+      }
+      $relationships['object'][$rel_type][$child_type][] = $rel;
     }
-    $relationships['object'][$rel_type][$sub_type][] = $relationship;
   }
 
   // now add in the subject relationships
-  while ($relationship = $as_subject->fetchObject()) {
-
-    // get the relationship and child types
-    $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
-    $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
-
-    if (!array_key_exists($rel_type, $relationships['subject'])) {
-      $relationships['subject'][$rel_type] = array();
-    }
-    if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
-      $relationships['subject'][$rel_type][$obj_type] = array();
+  if ($srelationships) {
+    foreach ($srelationships as $relationship) {
+      $rel = new stdClass();
+      $rel->record = $relationship;
+      $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
+      $parent_type = $relationship->object_id->type_id->name;
+       
+      // get the node id of the subject
+      $sql = "SELECT nid FROM {chado_contact} WHERE contact_id = :contact_id";
+      $n = db_query($sql, array(':contact_id' => $relationship->object_id->contact_id))->fetchObject();
+      if ($n) {
+        $rel->record->nid = $n->nid;
+      }
+       
+      if (!array_key_exists($rel_type, $relationships['subject'])) {
+        $relationships['subject'][$rel_type] = array();
+      }
+      if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
+        $relationships['subject'][$rel_type][$parent_type] = array();
+      }
+      $relationships['subject'][$rel_type][$parent_type][] = $rel;
     }
-    $relationships['subject'][$rel_type][$obj_type][] = $relationship;
   }
-
   $contact->all_relationships = $relationships;
 }

+ 1 - 1
tripal_contact/theme/tripal_contact/tripal_contact_relationships.tpl.php

@@ -29,7 +29,7 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
     // first add in the subject relationships.  
     foreach ($subject_rels as $rel_type => $rels){
       foreach ($rels as $obj_type => $objects){ ?>
-        <p>This <?php print $contact->type_id->name;?> is <?php print $rel_type ?> the following <b><?php print $obj_type ?></b> contact(s): <?php
+        <p>This <?php print strtolower($contact->type_id->name);?>  <b><?php print $rel_type ?></b> with the following <?php print strtolower($obj_type) ?> contact(s): <?php
          
         // the $headers array is an array of fields to use as the colum headers.
         // additional documentation can be found here

+ 9 - 0
tripal_contact/tripal_contact.install

@@ -202,4 +202,13 @@ function tripal_contact_add_custom_tables(){
     ),
   );
   tripal_core_create_custom_table('contactprop', $schema, TRUE);
+}
+
+/**
+ * This is the required update for tripal_contact when upgrading from Drupal core API 6.x.
+ */
+function tripal_contact_update_7000() {
+
+  tripal_contact_add_cvs();
+  tripal_contact_add_cvterms();
 }

+ 64 - 48
tripal_pub/theme/tripal_pub.theme.inc

@@ -5,31 +5,28 @@
  * @ingroup tripal_pub
  */
 function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
-  // we want to provide a new variable that contains the matched pubs.
-  $pub = $variables['node']->pub;
+   $pub = $variables['node']->pub;
+  
+   // expand the pub object to include the pub relationships.
+  $options = array(
+    'return_array' => 1,
+    // we don't want to fully recurse we only need information about the
+    // relationship type and the object and subject pubs (including pub type)
+    'include_fk' => array(
+      'type_id' => 1,
+      'object_id' => array(
+        'type_id' => 1,
+      ),
+      'subject_id'  => array(
+        'type_id' => 1,
+      ),
+    ),
+  );
+  $pub = tripal_core_expand_chado_vars($pub, 'table', 'pub_relationship', $options);
 
-  // normally we would use tripal_core_expand_chado_vars to expand our
-  // organism object and add in the relationships, however whan a large
-  // number of relationships are present this significantly slows the
-  // query, therefore we will manually perform the query
-  $sql = "
-    SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
-    FROM {pub_relationship} PR
-      INNER JOIN {pub} P            ON PR.object_id = P.pub_id
-      INNER JOIN {cvterm} CVT       ON PR.type_id   = CVT.cvterm_id
-      LEFT JOIN public.chado_pub CP ON P.pub_id     = CP.pub_id
-    WHERE PR.subject_id = :subject_id
-  ";
-  $as_subject = chado_query($sql, array(':subject_id' => $pub->pub_id));
-  $sql = "
-    SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
-    FROM {pub_relationship} PR
-      INNER JOIN {pub} P            ON PR.subject_id = P.pub_id
-      INNER JOIN {cvterm} CVT       ON PR.type_id    = CVT.cvterm_id
-      LEFT JOIN public.chado_pub CP ON P.pub_id      = CP.pub_id
-    WHERE PR.object_id = :object_id
-  ";
-  $as_object = chado_query($sql, array(':object_id' => $pub->pub_id));
+  // get the subject relationships
+  $srelationships = $pub->pub_relationship->subject_id;
+  $orelationships = $pub->pub_relationship->object_id;
 
   // combine both object and subject relationshisp into a single array
   $relationships = array();
@@ -37,36 +34,55 @@ function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
   $relationships['subject'] = array();
 
   // iterate through the object relationships
-  while ($relationship = $as_object->fetchObject()) {
+  if ($orelationships) {
+    foreach ($orelationships as $relationship) {
+      $rel = new stdClass();
+      $rel->record = $relationship;
+       
+      // get the relationship and child types
+      $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
+      $child_type = $relationship->subject_id->type_id->name;
+       
+      // get the node id of the subject
+      $sql = "SELECT nid FROM {chado_pub} WHERE pub_id = :pub_id";
+      $n = db_query($sql, array(':pub_id' => $relationship->subject_id->pub_id))->fetchObject();
+      if ($n) {
+        $rel->record->nid = $n->nid;
+      }
 
-    // get the relationship and child types
-    $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
-    $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
-
-    if (!array_key_exists($rel_type, $relationships['object'])) {
-      $relationships['object'][$rel_type] = array();
-    }
-    if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
-      $relationships['object'][$rel_type][$sub_type] = array();
+      if (!array_key_exists($rel_type, $relationships['object'])) {
+        $relationships['object'][$rel_type] = array();
+      }
+      if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
+        $relationships['object'][$rel_type][$child_type] = array();
+      }
+      $relationships['object'][$rel_type][$child_type][] = $rel;
     }
-    $relationships['object'][$rel_type][$sub_type][] = $relationship;
   }
 
   // now add in the subject relationships
-  while ($relationship = $as_subject->fetchObject()) {
-
-    // get the relationship and child types
-    $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
-    $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
-
-    if (!array_key_exists($rel_type, $relationships['subject'])) {
-      $relationships['subject'][$rel_type] = array();
-    }
-    if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
-      $relationships['subject'][$rel_type][$obj_type] = array();
+  if ($srelationships) {
+    foreach ($srelationships as $relationship) {
+      $rel = new stdClass();
+      $rel->record = $relationship;
+      $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
+      $parent_type = $relationship->object_id->type_id->name;
+       
+      // get the node id of the subject
+      $sql = "SELECT nid FROM {chado_pub} WHERE pub_id = :pub_id";
+      $n = db_query($sql, array(':pub_id' => $relationship->object_id->pub_id))->fetchObject();
+      if ($n) {
+        $rel->record->nid = $n->nid;
+      }
+       
+      if (!array_key_exists($rel_type, $relationships['subject'])) {
+        $relationships['subject'][$rel_type] = array();
+      }
+      if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
+        $relationships['subject'][$rel_type][$parent_type] = array();
+      }
+      $relationships['subject'][$rel_type][$parent_type][] = $rel;
     }
-    $relationships['subject'][$rel_type][$obj_type][] = $relationship;
   }
-
   $pub->all_relationships = $relationships;
 }