Browse Source

Adding automatic citation creation if form field is left blank... not quite done

spficklin 11 years ago
parent
commit
84442f9b84
3 changed files with 71 additions and 8 deletions
  1. 13 2
      tripal_pub/api/tripal_pub.api.inc
  2. 22 5
      tripal_pub/includes/pub_form.inc
  3. 36 1
      tripal_pub/tripal_pub.module

+ 13 - 2
tripal_pub/api/tripal_pub.api.inc

@@ -1077,6 +1077,7 @@ function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
  *   A text string containing the citation
  */
 function tripal_pub_create_citation($pub) {
+	dpm($pub);
 	$citation = '';
 	$pub_type = '';
 	
@@ -1122,7 +1123,12 @@ function tripal_pub_create_citation($pub) {
 	  elseif ($pub['Series Abbreviation']) {
       $citation .= $pub['Series Abbreviation'] . '. ';
     }
-	  $citation .= $pub['Publication Date'];
+    if ($pub['Publication Date']) {
+	    $citation .= $pub['Publication Date'];
+    }
+    elseif ($pub['Year']) {
+    	$citation .= $pub['Year'];
+    }
 	  if ($pub['Volume'] or $pub['Issue'] or $pub['Pages']) {
 	    $citation .= '; ';
 	  }
@@ -1167,7 +1173,12 @@ function tripal_pub_create_citation($pub) {
     elseif ($pub['Series Abbreviation']) {
       $citation .= $pub['Series Abbreviation'] . '. ';
     }
-    $citation .= $pub['Publication Date'];
+    if ($pub['Publication Date']) {
+      $citation .= $pub['Publication Date'];
+    }
+    elseif ($pub['Year']) {
+      $citation .= $pub['Year'];
+    }
     if ($pub['Volume'] or $pub['Issue'] or $pub['Pages']) {
       $citation .= '; ';
     }

+ 22 - 5
tripal_pub/includes/pub_form.inc

@@ -122,20 +122,22 @@ function chado_pub_form($node, $form_state) {
     '#default_value' => $d_pyear,
     '#required' => TRUE,
     '#size' => 5,
+    '#description' => t('Enter the year of publication. Also, if available, please add a <b>Publication Date</b> property to specify the full date of publication.'),
   );
   $form['uniquename'] = array(
     '#type' => 'textarea',
     '#title' => t('Citation'),
     '#default_value' => $d_uniquename,
-    '#description' => t('All publications must have a unique citation. Please enter the full citation for this publication. 
-      For PubMed style citations list 
+    '#description' => t('All publications must have a unique citation. 
+      <b>Please enter the full citation for this publication or leave blank and one will be generated 
+      automatically if possible</b>.  For PubMed style citations list 
       the last name of the author followed by initials. Each author should be separated by a comma. Next comes 
       the title, followed by the series title (e.g. journal name), publication date (4 digit year, 3 character Month, day), volume, issue and page numbers. You may also use HTML to provide a link in the citation.  
       Below is an example: <pre>Medeiros PM, Ladio AH, Santos AM, Albuquerque UP. <a href="http://www.ncbi.nlm.nih.gov/pubmed/23462414" target="_blank">Does the selection of medicinal plants by Brazilian local populations 
         suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
-    '#required' => TRUE,
   );
 
+
   // add in the properties that are actually stored in the pub table fields.
   $num_properties = chado_pub_node_form_add_pub_table_props($form, $form_state, $properties_list,
   $d_properties, $d_removed, $d_volume, $d_volumetitle, $d_issue, $d_pages, $d_series_name);
@@ -195,11 +197,14 @@ function chado_pub_validate($node, &$form) {
   $pub_id       = $node->pub_id;
   $num_properties = $node->num_properties;
   $num_new = $node->num_new;
+  
+  $pub = array();
 
   // if this is a delete then don't validate
   if($node->op == 'Delete') {
     return;
   }
+  
 
   // make sure the year is four digits
   if(!preg_match('/^\d{4}$/', $pyear)){
@@ -215,9 +220,9 @@ function chado_pub_validate($node, &$form) {
     $message = t('Invalid publication type.');
     form_set_error('type_id', $message);
     return;
-  }
+  }  
 
-  // get the media name looking at the properties
+  // get the media name looking at the properties  
   foreach ($node as $element => $value) {
     // if this is an existing property (either previously in the database or
     // added via AHAH/AJAX callback)
@@ -230,6 +235,7 @@ function chado_pub_validate($node, &$form) {
       if($prop_type->name == 'Citation') {
         $uniquename = $value;
       }
+      $pub[$prop_type->name] = $value;
     }
     // if this is a new property (added by this submit of the form)
     elseif ($element = 'new_id') {
@@ -240,6 +246,17 @@ function chado_pub_validate($node, &$form) {
       if($prop_type->name == 'Citation') {
         $uniquename = $value;
       }
+      $pub[$prop_type->name] = $value;
+    }
+  }
+  // if the citation is missing then try to generate one
+  if (!$uniquename) {
+    $pub['Title'] = $title;
+    $pub['Publication Type'][0] = $cvterm[0]->name;
+    $pub['Year'] = $pyear;
+    $uniquename = tripal_pub_create_citation($pub);
+    if (!$uniquename) {
+    	form_set_error('uniquename', 'Cannot automatically generate a citation for this publication type. Please add one manually.');
     }
   }
 

+ 36 - 1
tripal_pub/tripal_pub.module

@@ -323,6 +323,10 @@ function chado_pub_access($op, $node, $account ) {
  */
 function chado_pub_insert($node) {
 
+	// we need an array suitable for the tripal_pub_create_citation() function
+	// to automatically generate a citation if a uniquename doesn't already exist
+	$pub_arr = array();
+	
   // if a pub_id already exists for this node then it already exists in Chado and
   // we get here because we are syncing the node.  Therefore, we can skip the insert
   // but we always want to set the URL path alias to be the Chado pub ID
@@ -378,6 +382,10 @@ function chado_pub_insert($node) {
     // iterate through all of the properties and remove those that really are
     // part of the pub table fields
     foreach ($properties as $name => $element) {
+    	// populate our $pub_array for building a citation
+    	$pub_arr[$name] = $value;
+    	
+    	// remove properties that are stored in the pub table
       $value = trim($element[0]);
       if ($name == "Volume") {
         $volume = $value;
@@ -413,7 +421,17 @@ function chado_pub_insert($node) {
         $cross_refs[] = $value;
       }
     }
-
+    
+    // generate a citation for this pub if one doesn't already exist 
+    if (!$node->uniquename and array_key_exists('Citation', $properties)) {
+    	$pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
+    	$pub_arr['Title'] = $node->pubtitle;
+      $pub_arr['Publication Type'][0] = $pub_type->name;
+      $pub_arr['Year'] = $node->pyear;
+      $node->uniquename = tripal_pub_create_citation($pub_arr);
+      $properties['Citation'][0] = $node->uniquename;
+    }
+    
     // insert the pub record
     $values = array(
       'title'       => trim($node->pubtitle),
@@ -497,6 +515,10 @@ function chado_pub_update($node) {
     // there is no way to handle revisions in Chado but leave
     // this here just to make not we've addressed it.
   }
+  
+  // we need an array suitable for the tripal_pub_create_citation() function
+  // to automatically generate a citation if a uniquename doesn't already exist
+  $pub_arr = array();
 
   // get the publication ID for this publication
   $pub_id = chado_get_id_for_node('pub', $node) ;
@@ -558,6 +580,10 @@ function chado_pub_update($node) {
   // part of the pub table fields
   foreach ($properties as $name => $element) {
     foreach ($element as $index => $value) {
+    	// populate our $pub_array for building a citation
+      $pub_arr[$name] = $value;
+      
+      // remove properties that are stored in the pub table
       if ($name == "Volume") {
         $volume = $value;
         unset($properties[$name]);
@@ -593,6 +619,15 @@ function chado_pub_update($node) {
       }
     }
   }
+  // generate a citation for this pub if one doesn't already exist 
+  if (!$node->uniquename) {
+    $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
+    $pub_arr['Title'] = $node->pubtitle;
+    $pub_arr['Publication Type'][0] = $pub_type->name;
+    $pub_arr['Year'] = $node->pyear;
+    $node->uniquename = tripal_pub_create_citation($pub_arr);
+    $properties['Citation'][0] = $node->uniquename;
+  }
 
   // update the pub record
   $match = array(