From cc368749d744edd41507a2113b7a26d760b495eb Mon Sep 17 00:00:00 2001 From: ppound Date: Fri, 15 Oct 2021 11:32:49 -0300 Subject: [PATCH] EDS Articles block now works and code cleanup --- .../js/eds_results_article.js | 148 +++++++++--------- .../roblib_search_eds.module | 108 +------------ .../Plugin/Block/RoblibSearchEdsArticles.php | 21 ++- .../roblib-search-eds-articles.html.twig | 4 +- modules/roblib_search_eds/templates/theme.inc | 60 ------- 5 files changed, 97 insertions(+), 244 deletions(-) delete mode 100644 modules/roblib_search_eds/templates/theme.inc diff --git a/modules/roblib_search_eds/js/eds_results_article.js b/modules/roblib_search_eds/js/eds_results_article.js index b88695a..adb09a8 100644 --- a/modules/roblib_search_eds/js/eds_results_article.js +++ b/modules/roblib_search_eds/js/eds_results_article.js @@ -1,85 +1,89 @@ Drupal.behaviors.roblib_search_eds_articles = { - attach: function (context, settings) { - $url = settings.roblib_search_eds_articles.search_url; - articles_profile = settings.roblib_search_eds_articles.eds_profile; - proxy_url = "https://proxy.library.upei.ca/login?url="; - jQuery.getJSON($url, function (data) { - var items = []; - var numberOfDocs = 0; - try { - numberOfDocs = data.numFound; - } catch (err) { - // do nothing leave docLength at 0 - } - if (numberOfDocs < 1) { - jQuery('#' + 'roblib-search-content-eds').empty().append('No Results'); - jQuery('.' + 'pane-roblib-search-eds-roblib-search-eds-articles').hide(); - } else { - var counter = 0; - var divs = new Array(); - var content = new Array(); - var queries = []; - jQuery.each(data.queries, function (key7, query) { - queries.push(query.query); - }) - var query_str = encodeURI(data.queries[0].query); - var href_str = 'https://search.ebscohost.com/login.aspx?direct=true&site=eds-live&scope=site&type=1&custid=uprince&groupid=main&profile=eds&mode=bool&lang=en&bquery='; - jQuery.each(data.documents, function (key, val) { - id = 'roblib-search-eds-article-' + counter; - divs[counter++] = id; + attach: function (context, settings) { + if (context == document) { + $query = $current_path.replace('roblib_search/', ''); + $url = settings.eds_article_search_url + $query; + profile = settings.eds_article_profile; + proxy_url = "https://proxy.library.upei.ca/login?url="; + jQuery.getJSON($url, function (data) { + var items = []; + var numberOfDocs = 0; + try { + numberOfDocs = data.numFound; + } catch (err) { + // do nothing leave docLength at 0 + } + if (numberOfDocs < 1) { + jQuery('#' + 'roblib-search-content-eds').empty().append('No Results'); + jQuery('.' + 'pane-roblib-search-eds-roblib-search-eds-articles').hide(); + } else { + var counter = 0; + var divs = new Array(); + var content = new Array(); + var queries = []; + jQuery.each(data.queries, function (key7, query) { + queries.push(query.query); + }) + var query_str = encodeURI(data.queries[0].query); + var href_str = 'https://search.ebscohost.com/login.aspx?direct=true&site=eds-live&scope=site&type=1&custid=uprince&groupid=main&profile=eds&mode=bool&lang=en&bquery='; + jQuery.each(data.documents, function (key, val) { + id = 'roblib-search-eds-article-' + counter; + divs[counter++] = id; - if (typeof val.Items !== 'undefined') { - items.push('
'); - if (typeof val.Items.Title !== 'undefined') { - items.push(''); - } - pubType = val.PubType; + if (typeof val.Items !== 'undefined') { + items.push('
'); + if (typeof val.Items.Title !== 'undefined') { + items.push(''); + } + pubType = val.PubType; - pubYear = val.RecordInfo.BibRelationships.IsPartOfRelationships["date"]; - !pubYear ? pubYear = " " : pubYear = pubYear[0]["Y"]; + pubYear = val.RecordInfo.BibRelationships.IsPartOfRelationships["date"]; + !pubYear ? pubYear = " " : pubYear = pubYear[0]["Y"]; - items.push('
' + pubYear + ' - ' + pubType + '
'); + items.push('
' + pubYear + ' - ' + pubType + '
'); - var roblib_authors = []; - jQuery.each(val.RecordInfo.BibRelationships.HasContributorRelationships, function (key3, author) { - roblib_authors.push(author.NameFull); - }) + var roblib_authors = []; + jQuery.each(val.RecordInfo.BibRelationships.HasContributorRelationships, function (key3, author) { + roblib_authors.push(author.NameFull); + }) - items.push('
'); - roblib_authors = roblib_shorten(roblib_authors.join('; '), 50, " "); - items.push(roblib_authors); - items.push('
'); + items.push('
'); + roblib_authors = roblib_shorten(roblib_authors.join('; '), 50, " "); + items.push(roblib_authors); + items.push('
'); - if (typeof val.Items.TitleSource !== 'undefined') { - items.push('
In: ' + val.Items.TitleSource.Data + '
'); - } - if (typeof val.PLink !== 'undefined' && typeof val.PDF !== 'undefined' && val.PDF == 'pdflink') { - items.push(''); - } - if (typeof val.PLink !== 'undefined' && typeof val.HTML !== 'undefined' && val.HTML == '1') { - items.push(''); - } + if (typeof val.Items.TitleSource !== 'undefined') { + items.push('
In: ' + val.Items.TitleSource.Data + '
'); + } + if (typeof val.PLink !== 'undefined' && typeof val.PDF !== 'undefined' && val.PDF == 'pdflink') { + items.push(''); + } + if (typeof val.PLink !== 'undefined' && typeof val.HTML !== 'undefined' && val.HTML == '1') { + items.push(''); + } - items.push('
'); - } else { - items.push('
You must login to view this result, click here to login
' );} - }); - jQuery('#' + 'roblib-search-content-eds-articles').empty().append(items.join('')); - } - var non_upei_link = ''; - var upei_link = 'SEE ALL (' + data.recordCount + ') '; - if(!data.is_local_ip) { - non_upei_link = '[Non-UPEI]'; - upei_link = upei_link + non_upei_link; - } - jQuery('#roblib-search-eds-article-more').empty().append(upei_link); - jQuery('#roblib-eds-articles-more-results').empty().append(upei_link); - jQuery('#roblib-eds-articles-toc').empty().append('Articles (' + data.recordCount + ')'); + items.push('
'); + } else { + items.push('
You must login to view this result, click here to login
'); + } + }); + jQuery('#' + 'roblib-search-content-eds-articles').empty().append(items.join('')); + } + var non_upei_link = ''; + var upei_link = 'SEE ALL (' + data.recordCount + ') '; + if (!data.is_local_ip) { + non_upei_link = '[Non-UPEI]'; + upei_link = upei_link + non_upei_link; + } + jQuery('#roblib-search-eds-article-more').empty().append(upei_link); + jQuery('#roblib-eds-articles-more-results').empty().append(upei_link); + jQuery('#roblib-eds-articles-toc').empty().append('Articles (' + data.recordCount + ')'); - }); - } + }); + } + } } diff --git a/modules/roblib_search_eds/roblib_search_eds.module b/modules/roblib_search_eds/roblib_search_eds.module index 22571ff..dda428d 100644 --- a/modules/roblib_search_eds/roblib_search_eds.module +++ b/modules/roblib_search_eds/roblib_search_eds.module @@ -2,78 +2,9 @@ /** * @file - * Implementation of Roblib search for searching several targets. + * Implementation of Roblib search for searching EDSAPI (EBSCO) targets. */ - -/** - * Implements hook_menu(). - */ -function roblib_search_eds_menu() { - $items = array(); - - $items['admin/roblib_search/eds_search'] = array( - 'title' => 'EDS search Target configuration', - 'description' => 'Configuration for the Roblib eds search target', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('roblib_search_eds_config_form'), - 'access arguments' => array('access administration pages'), - 'type' => MENU_NORMAL_ITEM, - ); - - $items['roblib_search/eds/ajax/%'] = array( - 'title' => 'eds ajax', - 'page callback' => 'roblib_search_eds_ajax', - 'page arguments' => array(3), - 'type' => MENU_CALLBACK, - 'access arguments' => array('search roblib eds'), - ); - $items['roblib_search/eds/articles/%'] = array( - 'title' => 'eds ajax', - 'page callback' => 'roblib_search_eds_articles', - 'page arguments' => array(3), - 'type' => MENU_CALLBACK, - 'access arguments' => array('search roblib eds'), - ); - - $items['roblib_search/eds/info/%'] = array( - 'title' => 'eds info', - 'page callback' => 'roblib_search_eds_info', - 'page arguments' => array(3), - 'type' => MENU_CALLBACK, - 'access arguments' => array('search roblib eds'), - ); - - return $items; -} - -function roblib_search_eds_info($type = 'Articles') { - $config = roblib_search_eds_build_config_arr($type); - $eds_api = new EBSCOAPI($config); - $output['type'] = $type; - $output['profile'] = $eds_api->getInfo(); - print json_encode($output); - - exit(); -} - -/** - * Implements hook_permission(). - */ -/*function roblib_search_eds_permission() { - return array( - 'search roblib eds' => array( - 'title' => t('Search the eds target'), - 'description' => t('Search Roblib eds target. This permission exposes the search blocks and allows you to see search results.'), - ), - 'administer roblib search eds' => array( - 'title' => t('Administer Roblib Search EDS'), - 'description' => t('Administer settings for the Roblib eds search client.'), - ), - ); -}*/ - - /** * Implements hook_theme(). */ @@ -91,41 +22,6 @@ function roblib_search_eds_theme($existing, $type, $theme, $path) { ]; } - - -/*function roblib_search_eds_ajax($query) { - print roblib_search_eds_get_results($query, 'Books'); - exit(); -} - -function roblib_search_eds_articles($query) { - print roblib_search_eds_get_results($query, 'Articles'); - exit(); -}*/ - - - -/** - * For each result query ebsco again to get the detailed record. - * - * and insert a new DetailedRecord element in each record. - * - * @param array $output - * The orginal search results array - * @param string $query - * The orginal query - * @param object $eds_api - * an ebco API PHP object - */ -function roblib_search_eds_get_detailed_result(&$output, $query, &$eds_api) { - foreach ($output['records'] as &$record) { - $db = $record['DbId']; - $an = $record['An']; - $arr = $eds_api->apiRetrieve($an, $db, $query); - $record['DetailedRecord'] = $arr['Items']; - } -} - /** * Implements hook_help(). * @@ -138,7 +34,7 @@ function roblib_search_eds_help($path, $arg) { case 'admin/help#roblib_search_eds': return t( '

- provides a target for the Roblib search module. This target uses javascript + provides a EDSAPI target for the Roblib search module. This target uses javascript to render the results.

' ); diff --git a/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php b/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php index a3dd6dc..3aa9950 100644 --- a/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php +++ b/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php @@ -15,13 +15,28 @@ use Drupal\Core\Block\BlockBase; */ class RoblibSearchEdsArticles extends BlockBase { - /** + /** * {@inheritdoc} */ public function build() { - return [ + $host = \Drupal::request()->getSchemeAndHttpHost(); + $search_url = $host . '/roblib_search/eds/articles/'; + $eds_profile = \Drupal::config('roblib_search_eds.settings') + ->get('eds_article_profile'); + $settings = [ + 'eds_article_search_url' => $search_url, + 'eds_article_profile' => $eds_profile, + ]; + return [ '#theme' => 'roblib_search_eds_articles', - '#query' => $query, + 'query' => $query, + 'spinner_path' => 'test', + '#attached' => [ + 'library' => [ + 'roblib_search_eds/eds-article' + ], + 'drupalSettings' => $settings, + ], ]; } } diff --git a/modules/roblib_search_eds/templates/roblib-search-eds-articles.html.twig b/modules/roblib_search_eds/templates/roblib-search-eds-articles.html.twig index b24bb89..ff6c144 100644 --- a/modules/roblib_search_eds/templates/roblib-search-eds-articles.html.twig +++ b/modules/roblib_search_eds/templates/roblib-search-eds-articles.html.twig @@ -1,10 +1,8 @@
- - +
-
diff --git a/modules/roblib_search_eds/templates/theme.inc b/modules/roblib_search_eds/templates/theme.inc deleted file mode 100644 index 00e1596..0000000 --- a/modules/roblib_search_eds/templates/theme.inc +++ /dev/null @@ -1,60 +0,0 @@ - array( - 'search_url' => $search_url, - 'eds_profile' => - variable_get('roblib_search_eds_book_profile', 'apilite') - ) - ), array('type' => 'setting')); - * */ - - -} - -function roblib_search_eds_preprocess_roblib_search_eds_articles(&$variables) { - global $base_url; - if (!isset($query)) { - if (isset($_GET['roblib_query'])) { - $query = $_GET['roblib_query']; - } - else { - return ''; - } - } - $spinner_path = $base_url . '/' . drupal_get_path('module', 'roblib_search') . '/img/' . 'spinner.gif'; - $variables['spinner_path'] = $spinner_path; - $search_url = $base_url . '/roblib_search/eds/articles/' . urlencode($query); - drupal_add_js(drupal_get_path('module', 'roblib_search_eds') . '/js/eds_results_article.js'); - drupal_add_js(array( - 'roblib_search_eds_articles' => array( - 'search_url' => $search_url, - 'eds_profile' => - variable_get('roblib_search_eds_profile', 'edsapi') - ) - ), array('type' => 'setting')); - -} - -?>