Browse Source

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

Chun-Huai Cheng 11 years ago
parent
commit
ecfcee46b2

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

@@ -1122,7 +1122,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 +1172,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 .= '; ';
     }

+ 28 - 7
tripal_pub/includes/pub_form.inc

@@ -63,6 +63,9 @@ function chado_pub_form($node, $form_state) {
       $d_type_id = $pub_type->cvterm_id;
     }
   }
+  
+  // reset the default to use the stored variable if one exists
+  $d_type_id = variable_get('tripal_pub_default_type', $d_type_id);
 
   // get publication properties list
   $properties_select = array();
@@ -122,20 +125,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 +200,15 @@ function chado_pub_validate($node, &$form) {
   $pub_id       = $node->pub_id;
   $num_properties = $node->num_properties;
   $num_new = $node->num_new;
+  dpm($node);
+  
+  $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 +224,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,16 +239,28 @@ 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') {
+    elseif ($element == 'new_id') {    	 
       $prop_type = tripal_cv_get_cvterm_by_id($value);
       if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
         $series_name = $node->new_value;
       }
       if($prop_type->name == 'Citation') {
-        $uniquename = $value;
+        $uniquename = $node->new_value;
       }
+      $pub[$prop_type->name] = $node->new_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.');
     }
   }
 

+ 50 - 1
tripal_pub/includes/tripal_pub.admin.inc

@@ -20,6 +20,7 @@ function tripal_pub_admin() {
   if (!$active_jobs) {
     get_tripal_pub_admin_form_select_search_list($form);
     get_tripal_pub_admin_form_importing_set($form);
+    get_tripal_pub_admin_form_default_type($form);
     get_tripal_pub_admin_form_cleanup_set($form);
   }
   else {
@@ -34,6 +35,51 @@ function tripal_pub_admin() {
 
   return system_settings_form($form);
 }
+
+/**
+ * 
+ * @param $form
+ */
+function get_tripal_pub_admin_form_default_type(&$form) {
+
+  // get the list of publication types.  In the Tripal publication
+  // ontologies these are all grouped under the term 'Publication Type'
+  // we want the default to be 'Journal Article'
+  $sql = "
+    SELECT CVTS.cvterm_id, CVTS.name
+    FROM {cvtermpath} CVTP
+      INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
+      INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
+      INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
+    WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Type' and 
+      NOT CVTS.is_obsolete = 1
+    ORDER BY CVTS.name ASC 
+  ";
+  $results = chado_query($sql);
+  $pub_types = array();
+  while ($pub_type = db_fetch_object($results)) {
+    $pub_types[$pub_type->cvterm_id] = $pub_type->name;
+    if (strcmp($pub_type->name,"Journal Article") == 0) {
+      $d_type_id = $pub_type->cvterm_id;
+    }
+  }
+  
+  // override the default by using the stored variable
+  $d_type_id = variable_get('tripal_pub_default_type', $d_type_id);
+  
+  $form['default_type'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Default Publication Type'),
+  );
+  $form['default_type']['type_id'] = array(
+    '#type' => 'select',
+    '#title' => t('Publication Type'),
+    '#options' => $pub_types,
+    '#description' => t('Please set a default publiation type used for manual entry of a new publication.  This is useful in the event that someone is manually adding the same
+      publication repetitively'),
+    '#default_value' => $d_type_id
+  );
+}
 /**
  *
  *
@@ -51,7 +97,7 @@ function get_tripal_pub_admin_form_importing_set(&$form) {
       'title_year' => t('Title and Year'), 
       'title_year_media' => t('Title, Year, Media name (e.g. Journal Name, etc.)'), 
       'title_year_type' => t('Title, Year, Media type (e.g. Journal, Conference Proceedings, etc.'),
-  ),
+    ),
     '#description' => t('During import, Tripal will attempt to find duplicate publications. 
        There are several options for how to find a duplicate publication.  Choose the
        option that best suits your needs.'),
@@ -142,6 +188,9 @@ function tripal_pub_admin_validate($form, &$form_state) {
 
   $import_duplicate_check = $form_state['values']['import_duplicate_check'];
   variable_set('tripal_pub_import_duplicate_check', $import_duplicate_check);
+  
+  $default_type = $form_state['values']['type_id'];
+  variable_set('tripal_pub_default_type', $default_type);
 
   // -------------------------------------
   // Submit the Cleanup Job if selected

+ 37 - 2
tripal_pub/tripal_pub.module

@@ -332,6 +332,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
@@ -386,8 +390,13 @@ 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) {
+    foreach ($properties as $name => $element) {    	    
       $value = trim($element[0]);
+      
+    	// 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]);
@@ -422,7 +431,16 @@ 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),
@@ -506,6 +524,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) ;
@@ -567,6 +589,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]);
@@ -602,6 +628,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(