From dfd7fdf93811df91b6d7a7196b193ed7553dc010 Mon Sep 17 00:00:00 2001 From: winkm89 Date: Sun, 1 Aug 2021 19:05:33 +0200 Subject: [PATCH] Add ignore tags option for publication import For #101 --- admin/import-publications.php | 14 +++++++---- core/publications/class-bibtex-import.php | 3 ++- core/publications/class-db-publications.php | 26 +++++++++++++-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/admin/import-publications.php b/admin/import-publications.php index 120bfdb..2805696 100644 --- a/admin/import-publications.php +++ b/admin/import-publications.php @@ -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; @@ -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); } @@ -180,7 +182,7 @@ public static function import_tab () {

- +

NCBI Website and Data Usage Policies and Disclaimers

@@ -191,6 +193,8 @@ public static function import_tab () {

+
+
diff --git a/core/publications/class-bibtex-import.php b/core/publications/class-bibtex-import.php index 1b9d309..b7c832a 100644 --- a/core/publications/class-bibtex-import.php +++ b/core/publications/class-bibtex-import.php @@ -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']; diff --git a/core/publications/class-db-publications.php b/core/publications/class-db-publications.php index 9f787ab..e6d69bb 100644 --- a/core/publications/class-db-publications.php +++ b/core/publications/class-db-publications.php @@ -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 @@ -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'];