Skip to content

Commit

Permalink
Add ignore tags option for publication import
Browse files Browse the repository at this point in the history
For #101
  • Loading branch information
winkm89 committed Aug 1, 2021
1 parent 982571c commit dfd7fdf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
14 changes: 9 additions & 5 deletions admin/import-publications.php
Expand Up @@ -134,8 +134,9 @@ public static function import_actions ($post) {

$settings = array(
'keyword_separator' => htmlspecialchars($post['keyword_option']),
'author_format' => htmlspecialchars($post['author_format']),
'overwrite' => isset( $post['overwrite']) ? true : false
'author_format' => htmlspecialchars($post['author_format']),
'overwrite' => isset( $post['overwrite'] ) ? true : false,
'ignore_tags' => isset( $post['ignore_tags'] ) ? true : false,
);

// echo $bibtex;
Expand All @@ -145,9 +146,10 @@ public static function import_actions ($post) {

// import from PubMed
elseif ( isset($post['tp_pmid']) ) {
// TP_PubMed_Import only cares about overwrite.

$settings = array(
'overwrite' => isset( $post['overwrite'] )
'overwrite' => isset( $post['overwrite'] ),
'ignore_tags' => isset( $post['ignore_tags'] ) ? true : false
);
$entries = TP_PubMed_Import::init($post['tp_pmid'], $settings);
}
Expand Down Expand Up @@ -180,7 +182,7 @@ public static function import_tab () {
<div style="text-align: center;">
<p style="text-align: center; font-weight: bold;"><?php _e('or','teachpress'); ?></p>
<label for="tp_pmid">PMID</label>
<input name="tp_pmid" id="tp_pmid" title="<?php _e('Comma-separated list of PubMed identifiers','teachpress'); ?>" type="text">
<input name="tp_pmid" id="tp_pmid" style="width:350px;" title="<?php _e('Comma-separated list of PubMed identifiers','teachpress'); ?>" type="text">
<p style="text-align: center; font-size: small;"><a href="https://www.ncbi.nlm.nih.gov/home/about/policies" target="_blank">NCBI Website and Data Usage Policies and Disclaimers</a></p>
</div>
</div>
Expand All @@ -191,6 +193,8 @@ public static function import_tab () {
<div class="inside">
<p><strong><label for="overwrite"><?php _e('Publications','teachpress'); ?></label></strong></p>
<?php echo TP_Admin::get_checkbox('overwrite', __('Update existing publications','teachpress'), '', __('If the bibtex key is similar with a publication in the database, teachPress updates this publication with the import information.','teachpress')); ?>
<br/>
<?php echo TP_Admin::get_checkbox('ignore_tags', __('Ignore Tags','teachpress'), '', __('Ignore tags or keywords in the import data.','teachpress'));?>
</div>
<?php } ?>
<div id="major-publishing-actions" style="text-align: center;">
Expand Down
3 changes: 2 additions & 1 deletion core/publications/class-bibtex-import.php
Expand Up @@ -141,10 +141,11 @@ public static function init ($input, $settings, $test = false) {
protected static function import_publication_to_database ($entry, $tags, $settings) {
$check = true;
if ( $settings['overwrite'] === true ) {
$entry['entry_id'] = TP_Publications::change_publication_by_key($entry['bibtex'], $entry, $tags);
$entry['entry_id'] = TP_Publications::change_publication_by_key($entry['bibtex'], $entry, $tags, $settings['ignore_tags']);
$check = ( $entry['entry_id'] === false ) ? false : true;
}
if ( $settings['overwrite'] === false || $check === false ) {
$tags = ( $settings['ignore_tags'] === true ) ? '' : $tags;
$entry['entry_id'] = TP_Publications::add_publication($entry, $tags, '');
}
return $entry['entry_id'];
Expand Down
26 changes: 16 additions & 10 deletions core/publications/class-db-publications.php
Expand Up @@ -555,14 +555,15 @@ public static function change_publication($pub_id, $data, $bookmark, $delbox, $t

/**
* Update a publication by key (import option); Returns FALSE if there is no publication with the given key
* @param string $key The BibTeX key
* @param array $input_data An associative array of publication data
* @param string $tags An associative array of tags
* @param string $key The BibTeX key
* @param array $input_data An associative array of publication data
* @param string $tags An associative array of tags
* @param boolean $ignore_tags Update the tags o not, default: false
* @return boolean|int
* @since 5.0.0
* @version 2
* @version 3
*/
public static function change_publication_by_key($key, $input_data, $tags) {
public static function change_publication_by_key($key, $input_data, $tags, $ignore_tags = false) {
global $wpdb;

// Search if there is a publication with the given bibtex key
Expand All @@ -575,12 +576,17 @@ public static function change_publication_by_key($key, $input_data, $tags) {
$data = wp_parse_args( $input_data, $search_pub );
self::change_publication($search_pub['pub_id'], $data, '', '', '');

// Delete existing tags
$wpdb->query( "DELETE FROM " . TEACHPRESS_RELATION . " WHERE `pub_id` = " . $search_pub['pub_id'] );
// Update tags
if ( $ignore_tags === false ) {

// Delete existing tags
$wpdb->query( "DELETE FROM " . TEACHPRESS_RELATION . " WHERE `pub_id` = " . $search_pub['pub_id'] );

// Add new tags
if ( $tags != '' ) {
TP_Publications::add_relation($search_pub['pub_id'], $tags);
}

// Add new tags
if ( $tags != '' ) {
TP_Publications::add_relation($search_pub['pub_id'], $tags);
}

return $search_pub['pub_id'];
Expand Down

0 comments on commit dfd7fdf

Please sign in to comment.