Browse Source

Fixed stock URLs

Stephen Ficklin 11 years ago
parent
commit
f209bcf528

+ 4 - 4
tripal_feature/includes/tripal_feature.admin.inc

@@ -63,7 +63,7 @@ function tripal_feature_admin() {
        '#type' => 'fieldset',
        '#type' => 'fieldset',
        '#title' => t('Feature Browser'),
        '#title' => t('Feature Browser'),
        '#collapsible' => TRUE,
        '#collapsible' => TRUE,
-       '#collapsed' => TRUE,
+       '#collapsed' => FALSE,
     );
     );
     $allowedoptions1  = array(
     $allowedoptions1  = array(
       'show_feature_browser' => "Show the feature browser on the organism page. The browser loads when page loads. This may be slow for large sites.",
       'show_feature_browser' => "Show the feature browser on the organism page. The browser loads when page loads. This may be slow for large sites.",
@@ -125,7 +125,7 @@ function tripal_feature_admin() {
        '#type' => 'fieldset',
        '#type' => 'fieldset',
        '#title' => t('Feature Summary Report'),
        '#title' => t('Feature Summary Report'),
        '#collapsible' => TRUE,
        '#collapsible' => TRUE,
-       '#collapsed' => TRUE,
+       '#collapsed' => FALSE,
     );
     );
     $allowedoptions2 ['show_feature_summary'] = "Show the feature summary on the organism page. The summary loads when page loads.";
     $allowedoptions2 ['show_feature_summary'] = "Show the feature summary on the organism page. The summary loads when page loads.";
     $allowedoptions2 ['hide_feature_summary'] = "Hide the feature summary on the organism page. Disables the feature summary.";
     $allowedoptions2 ['hide_feature_summary'] = "Hide the feature summary on the organism page. Disables the feature summary.";
@@ -306,7 +306,7 @@ function get_tripal_feature_admin_form_title_set(&$form) {
     '#type' => 'fieldset',
     '#type' => 'fieldset',
     '#title' => t('Feature Page Titles'),
     '#title' => t('Feature Page Titles'),
     '#collapsible' => TRUE,
     '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
+    '#collapsed' => FALSE,
   );
   );
   $form['title']['desc'] = array(
   $form['title']['desc'] = array(
     '#type'        => 'markup',
     '#type'        => 'markup',
@@ -345,7 +345,7 @@ function get_tripal_feature_admin_form_url_set(&$form) {
     '#type' => 'fieldset',
     '#type' => 'fieldset',
     '#title' => t('Feature URL Path'),
     '#title' => t('Feature URL Path'),
     '#collapsible' => TRUE,
     '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
+    '#collapsed' => FALSE,
   );
   );
 
 
   $options = array(
   $options = array(

+ 22 - 25
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -749,11 +749,6 @@ function tripal_feature_node_insert($node) {
   // know the feature_id in the presave
   // know the feature_id in the presave
   switch ($node->type) {
   switch ($node->type) {
     case 'chado_feature':
     case 'chado_feature':
-      if (!property_exists($node,'feature_id')) {
-        $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
-        $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
-        $node->feature_id = $chado_feature->feature_id;
-      }
 
 
       // remove any previous alias
       // remove any previous alias
       db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
       db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
@@ -766,6 +761,28 @@ function tripal_feature_node_insert($node) {
   }
   }
 }
 }
 
 
+/**
+ * Implements hook_node_update().
+ * Acts on all content types.
+ *
+ * @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 source = :source", array(':source' => "node/$node->nid"));
+
+      // set the URL for this feature page
+      $url_alias = tripal_feature_get_feature_url($node);
+      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
+      path_save($path_alias);
+      break;
+  }
+}
+
 /**
 /**
  * Return the url alias for a feature
  * Return the url alias for a feature
  *
  *
@@ -931,24 +948,4 @@ function tripal_feature_node_view($node, $view_mode, $langcode) {
   }
   }
 }
 }
 
 
-/**
- * Implements hook_node_update().
- * Acts on all content types.
- *
- * @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 source = :source", array(':source' => "node/$node->nid"));
-
-      // set the URL for this feature page
-      $url_alias = tripal_feature_get_feature_url($node);
-      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
-      path_save($path_alias);
-      break;
-  }
-}

+ 1 - 1
tripal_genetic/theme/tripal_stock/tripal_stock_genotypes.tpl.php

@@ -27,7 +27,7 @@ $options = array(
   ),
   ),
   'fk_include' => array(
   'fk_include' => array(
     'genotype_id' => 1
     'genotype_id' => 1
-  );
+  ),
 );
 );
 $stock = tripal_core_expand_chado_vars($stock, 'table', 'stock_genotype', $options); 
 $stock = tripal_core_expand_chado_vars($stock, 'table', 'stock_genotype', $options); 
 $stock_genotypes = $stock->stock_genotype;
 $stock_genotypes = $stock->stock_genotype;

+ 33 - 109
tripal_stock/includes/tripal_stock.admin.inc

@@ -50,64 +50,13 @@ function tripal_stock_admin_stock_view() {
 function tripal_stock_admin() {
 function tripal_stock_admin() {
   $form = array();
   $form = array();
 
 
-  get_tripal_stock_admin_form_title_set($form);
-  get_tripal_stock_admin_form_url_set($form);
-  get_tripal_stock_admin_form_vocabulary_set($form);
-  get_tripal_stock_admin_form_cleanup_set($form);
-
-
-  return system_settings_form($form);
-
-}
-
-/**
- * Implements hook_form_validate(): Validates user input
- *
- * @param $form
- *   An array describing the form that was rendered
- * @param $form_state
- *   An array describing the current state of the form including user input
- *
- * @ingroup tripal_stock
- */
-function tripal_stock_admin_validate($form, &$form_state) {
-  global $user;  // we need access to the user info
-  $job_args = array();
-
-  variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']);
-  variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
-  variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
-  variable_set('chado_stock_url_string', $form_state['values']['chado_stock_url_string']);
-
-  switch ($form_state['values']['op']) {
-    case  t('Set Controlled Vacabularies') :
-      break;
-
-    case t('Clean up orphaned stocks') :
-      tripal_add_job('Cleanup orphaned stocks', 'tripal_stock',
-         'tripal_stock_cleanup', $job_args, $user->uid);
-      break;
-
-    case t('Set Stock URLs') :
-      tripal_add_job('Set Stock URLs', 'tripal_stock',
-        'tripal_stock_set_urls', $job_args, $user->uid);
-      break;
-  }
-
-}
-
-/**
- * Form: Set the title type to be used for all stocks
- *
- * @ingroup tripal_stock
- */
-function get_tripal_stock_admin_form_title_set(&$form) {
 
 
+  // STOCK PAGE TITLES CONFIGURATION
   $form['title'] = array(
   $form['title'] = array(
     '#type' => 'fieldset',
     '#type' => 'fieldset',
     '#title' => t('Stock Page Titles'),
     '#title' => t('Stock Page Titles'),
     '#collapsible' => TRUE,
     '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
+    '#collapsed' => FALSE,
   );
   );
   $form['title']['desc'] = array(
   $form['title']['desc'] = array(
     '#type'  => 'markup',
     '#type'  => 'markup',
@@ -133,20 +82,14 @@ function get_tripal_stock_admin_form_title_set(&$form) {
     '#options'       => $options,
     '#options'       => $options,
     '#default_value' => variable_get('chado_stock_title', 'unique_constraint'),
     '#default_value' => variable_get('chado_stock_title', 'unique_constraint'),
   );
   );
-}
-
-/**
- * Form: Set UR for stocks
- *
- * @ingroup tripal_stock
- */
-function get_tripal_stock_admin_form_url_set(&$form) {
+  
 
 
+  // STOCK URL CONFIGURATION
   $form['url'] = array(
   $form['url'] = array(
     '#type' => 'fieldset',
     '#type' => 'fieldset',
     '#title' => t('Stock URL Path'),
     '#title' => t('Stock URL Path'),
     '#collapsible' => TRUE,
     '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
+    '#collapsed' => FALSE,
   );
   );
 
 
   $options = array(
   $options = array(
@@ -200,61 +143,42 @@ function get_tripal_stock_admin_form_url_set(&$form) {
     '#type' => 'submit',
     '#type' => 'submit',
     '#value' => t('Set Stock URLs'),
     '#value' => t('Set Stock URLs'),
   );
   );
+
+  return system_settings_form($form);
+
 }
 }
 
 
 /**
 /**
- * Set Controlled vocabularies used in stock tables
+ * Implements hook_form_validate(): Validates user input
+ *
+ * @param $form
+ *   An array describing the form that was rendered
+ * @param $form_state
+ *   An array describing the current state of the form including user input
  *
  *
  * @ingroup tripal_stock
  * @ingroup tripal_stock
  */
  */
-function get_tripal_stock_admin_form_vocabulary_set(&$form) {
-
-  $form['set_cv'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Set Stock Controlled Vocabularies'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-
-  $form['set_cv']['message'] = array(
-       '#value' => t("This setting allows you to set which chado controlled vocabularies (cv)"
-              ." are used. Cvs are used to control user input for the type of stock,"
-        ." any properties they enter for a stock & the types of relationships"
-        ." between stocks. Only cvs already loaded into chado can be selected here.")
-  );
-
-  // get the list of CVs for the next form element
-  $sql = "SELECT * FROM {cv} ORDER BY name";
-  $results = chado_query($sql);
-  $cv_options = array();
-  while ($r = $results->fetchObject()) {
-    $cv_options[$r->cv_id] = $r->name;
-  }
+function tripal_stock_admin_validate($form, &$form_state) {
+  global $user;  // we need access to the user info
+  $job_args = array();
 
 
-  $form['set_cv']['stock_types_cv'] = array(
-   '#type' => 'select',
-   '#title' => t('Controlled Vocabulary governing Stock Types'),
-   '#options' => $cv_options,
-   '#default_value' => variable_get('chado_stock_types_cv', 0)
-  );
+  variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']);
+  variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
+  variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
+  variable_set('chado_stock_url_string', $form_state['values']['chado_stock_url_string']);
 
 
-  $form['set_cv']['stock_prop_types_cv'] = array(
-   '#type' => 'select',
-   '#title' => t('Controlled Vocabulary governing Types of Stock Properties'),
-   '#description' => t("This cv must contain a cvterm entry where name='synonym'."),
-   '#options' => $cv_options,
-   '#default_value' => variable_get('chado_stock_prop_types_cv', 0)
-  );
+  switch ($form_state['values']['op']) {
+    case  t('Set Controlled Vacabularies') :
+      break;
 
 
-  $form['set_cv']['stock_relationship_cv'] = array(
-   '#type' => 'select',
-   '#title' => t('Controlled Vocabulary governing Types of Relationsips between Stocks'),
-   '#options' => $cv_options,
-   '#default_value' => variable_get('chado_stock_relationship_cv', 0)
-  );
+    case t('Clean up orphaned stocks') :
+      tripal_add_job('Cleanup orphaned stocks', 'tripal_stock',
+         'tripal_stock_cleanup', $job_args, $user->uid);
+      break;
 
 
-  $form['set_cv']['button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Set Controlled Vacabularies')
-  );
+    case t('Set Stock URLs') :
+      tripal_add_job('Set Stock URLs', 'tripal_stock',
+        'tripal_stock_set_urls', $job_args, $user->uid);
+      break;
+  }
 }
 }

+ 118 - 48
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -776,7 +776,7 @@ function tripal_stock_node_presave($node) {
   switch ($node->type) {
   switch ($node->type) {
     case 'chado_stock':
     case 'chado_stock':
       // for a form submission the fields part of the node object
       // for a form submission the fields part of the node object
-      // but for a sync the feilds are in an object of the node
+      // but for a sync the fields are in an object of the node
       $organism_id = null;
       $organism_id = null;
       $sname = '';
       $sname = '';
       $uniquename = '';
       $uniquename = '';
@@ -799,59 +799,13 @@ function tripal_stock_node_presave($node) {
       $values = array('organism_id' => $organism_id);
       $values = array('organism_id' => $organism_id);
       $organism = chado_select_record('organism', array('genus','species'), $values);
       $organism = chado_select_record('organism', array('genus','species'), $values);
       $node->title = "$sname, $uniquename ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
       $node->title = "$sname, $uniquename ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
-      if ($name == $uname) {
+      if ($name == $uniquename) {
         $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
         $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
       }
       }
       break;
       break;
   }
   }
 }
 }
 
 
-/**
- * Implements hook_node_insert(). Acts on all content types.
- *
- * @ingroup tripal_stock
- */
-function tripal_stock_node_insert($node) {
-  switch ($node->type) {
-    case 'chado_stock':
-      if (!$node->stock_id) {
-        $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
-        $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
-        $node->stock_id = $chado_stock->stock_id;
-      }
-
-      // remove any previous alias
-      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
-
-      // set the URL for this stock page
-      $url_alias = tripal_stock_get_stock_url($node);
-      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
-      path_save($path_alias);
-      break;
-  }
-}
-
-/**
- * Implements hook_node_update(). Acts on all content types.
- *
- * @ingroup tripal_stock
- */
-function tripal_stock_node_update($node) {
-
-  switch ($node->type) {
-    case 'chado_stock':
-
-      // remove any previous alias
-      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
-
-      // set the URL for this stock page
-      $url_alias = tripal_stock_get_stock_url($node);
-      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
-      path_save($path_alias);
-      break;
-  }
-}
-
 /**
 /**
  * Implements hook_node_view(). Acts on all content types.
  * Implements hook_node_view(). Acts on all content types.
  *
  *
@@ -916,3 +870,119 @@ function tripal_stock_node_view($node, $view_mode, $langcode) {
       break;
       break;
   }
   }
 }
 }
+
+/**
+ * Implements hook_node_insert().
+ * Acts on all content types.
+ *
+ * @ingroup tripal_stock
+ */
+function tripal_stock_node_insert($node) {
+
+  // set the URL path after inserting.  We do it here because we do not
+  // know the stock_id in the presave
+  switch ($node->type) {
+    case 'chado_stock':
+
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
+
+      // set the URL for this stock page
+      $url_alias = tripal_stock_get_stock_url($node);
+      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
+      path_save($path_alias);
+      break;
+  }
+}
+
+/**
+ * Implements hook_node_update().
+ * Acts on all content types.
+ *
+ * @ingroup tripal_stock
+ */
+function tripal_stock_node_update($node) {
+
+  // add items to other nodes, build index and search results
+  switch ($node->type) {
+    case 'chado_stock':
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
+
+      // set the URL for this stock page
+      $url_alias = tripal_stock_get_stock_url($node);
+      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
+      path_save($path_alias);
+      break;
+  }
+}
+/**
+ * Return the url alias for a stock
+ *
+ * @param $node
+ *   A node object containing at least the stock_id and nid
+ * @param $url_alias
+ *   Optional.  This should be the URL alias syntax string that contains
+ *   placeholders such as [id], [genus], [species], [name], [uniquename],
+ *   and [type].  These placeholders will be substituted for actual values.
+ *   If this parameter is not provided then the value of the
+ *   chado_stock_url_string Drupal variable will be used.
+ *
+ * @ingroup tripal_stock
+ */
+function tripal_stock_get_stock_url($node, $url_alias = NULL) {
+
+  // get the starting URL alias
+  if(!$url_alias) {
+    $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
+    if (!$url_alias) {
+      $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
+    }
+    $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
+  }
+
+  // get the stock
+  $values = array('stock_id' => $node->stock_id);
+  $stock = chado_select_record('stock', array('*'), $values);
+  if (!$stock) {
+    tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id));
+    return FALSE;
+  }
+  $stock = (object) $stock[0];
+
+  // get the organism
+  $values = array('organism_id' => $stock->organism_id);
+  $organism  = chado_select_record('organism', array('*'), $values);
+  if (!$organism) {
+    tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id));
+    return FALSE;
+  }
+  $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
+  $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
+
+  // get the type
+  $values = array('cvterm_id' => $stock->type_id);
+  $cvterm = chado_select_record('cvterm', array('name'), $values);
+  if (!$cvterm) {
+    tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id));
+    return FALSE;
+  }
+  $type = preg_replace('/\s/', '_', $cvterm[0]->name);
+
+  // now substitute in the values
+  $url_alias = preg_replace('/\[id\]/', $stock->stock_id, $url_alias);
+  $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
+  $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
+  $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
+  $url_alias = preg_replace('/\[name\]/', $stock->name, $url_alias);
+  $url_alias = preg_replace('/\[uniquename\]/', $stock->uniquename, $url_alias);
+
+  // the dst field of the url_alias table is only 128 characters long.
+  // if this is the case then simply return the node URL, we can't set this one
+  if (strlen($url_alias) > 128) {
+    tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
+    return "node/" . $node->nid;
+  }
+
+  return $url_alias;
+}