@ -1,16 +1,16 @@
<?php
/**
* The EBSCO Document model class
* @file
* The EBSCO Document model class.
*
* It provides all the methods and properties needed for :
* - setting up and performing API calls
* - displaying results in UI
* - displaying statistics about the search, etc
* - displaying statistics about the search, etc.
*
* PHP version 5
*
*
* Copyright [2017] [EBSCO Information Services]
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -29,139 +29,148 @@
require_once 'EBSCOAPI.php';
require_once 'EBSCORecord.php';
class EBSCODocument
{
/**
* The EBSCOAPI object that performs the API calls
*
*/
class EBSCODocument {
/**
* The EBSCOAPI object that performs the API calls.
*
* @global object EBSCOAPI
*/
private $eds = null;
private $eds = NULL ;
/**
* The associative array of current request parameters
* The associative array of current request parameters.
*
* @global array
*/
private $params = array();
/**
* The associative array of EBSCO results returned by a Search API call
* #global array
* #global array.
*/
private $results = array();
/**
* The associative array of data returned by a Retrieve API call
* The associative array of data returned by a Retrieve API call.
*
* @global array
*/
private $result = array();
/**
* The array of data returned by an Info API call
* The array of data returned by an Info API call.
*
* @global array
*/
private $info = array();
/**
* The EBSCORecord model returned by a Retrieve API call
* #global object EBSCORecord
* #global object EBSCORecord.
*/
private $record = null ;
private $record = NULL ;
/**
* The array of EBSCORecord models returned by a Search API call
* #global array of EBSCORecord objects
* #global array of EBSCORecord objects.
*/
private $records = array();
/**
* The array of EBSCORecord models returned by a Search API call
* #global array of RelatedRecords
* #global array of RelatedRecords.
*/
private $relatedContent = array();
private $autoSuggestTerms = array();
/**
* The array of filters currently applied
* The array of filters currently applied.
*
* @global array
*/
private $filters = array();
/**
* Maximum number of results returned by Search API call
* Maximum number of results returned by Search API call .
*
* @global integer
*/
private $limit = 10;
/**
* Default level of data detail
* Default level of data detail.
*
* @global string
*/
private $amount = 'brief';
/**
* Maximum number of links displayed by the pagination
* Maximum number of links displayed by the pagination.
*
* @global integer
*/
private static $page_links = 10;
/**
* Limit options
* global array
* global array.
*/
private static $limit_options = array(
10 => 10,
20 => 20,
30 => 30,
40 => 40,
50 => 50
50 => 50,
);
/**
* Sort options
* global array
* global array.
*/
private static $sort_options = array(
'relevance' => 'Relevance',
'date_desc' => 'Date Descending',
'date_asc' => 'Date Ascending'
'date_asc' => 'Date Ascending',
);
/**
* Amount options
* global array
* global array.
*/
private static $amount_options = array(
'detailed' => 'Detailed',
'brief' => 'Brief',
'title' => 'Title Only'
'title' => 'Title Only',
);
/**
* Bool options
* global array
* global array.
*/
private static $bool_options = array(
'AND' => 'All terms',
'OR' => 'Any terms',
'NOT' => 'No terms'
'NOT' => 'No terms',
);
/**
* Search mode options
* global array
* global array.
*/
private static $mode_options = array(
'all' => 'All search terms',
'bool' => 'Boolean / Phrase',
'any' => 'Any search terms',
'smart' => 'SmartText Searching'
'smart' => 'SmartText Searching',
);
/**
* Basic search type options
* global array
* global array.
*/
private static $basic_search_type_options = array(
'AllFields' => 'All Text',
@ -169,12 +178,12 @@ class EBSCODocument
'Author' => 'Author',
'Subject' => 'Subject terms',
'Source' => 'Source',
'Abstract' => 'Abstract'
'Abstract' => 'Abstract',
);
/**
* Advanced search type options
* global array
* global array.
*/
private static $advanced_search_type_options = array(
'AllFields' => 'All Text',
@ -184,7 +193,7 @@ class EBSCODocument
'Source' => 'Journal Title/Source',
'Abstract' => 'Abstract',
'ISBN' => 'ISBN',
'ISSN' => 'ISSN'
'ISSN' => 'ISSN',
);
private $local_ips = "";
@ -192,10 +201,10 @@ class EBSCODocument
/**
* Constructor.
*
* @param array $data Raw data from the EBSCO search representing the record.
* @param array $data
* Raw data from the EBSCO search representing the record.
*/
public function __construct($params = null)
{
public function __construct($params = NULL) {
$this->eds = new EBSCOAPI(array(
'password' => variable_get('ebsco_password'),
'user' => variable_get('ebsco_user'),
@ -204,7 +213,7 @@ class EBSCODocument
'organization' => variable_get('ebsco_organization'),
'local_ip_address' => variable_get('ebsco_local_ips'),
'guest' => variable_get('ebsco_guest'),
'log' => variable_get('ebsco_log')
'log' => variable_get('ebsco_log'),
));
$this->params = $params ? $params : $_REQUEST;
@ -212,50 +221,46 @@ class EBSCODocument
$this->amount = variable_get('ebsco_default_amount') ? variable_get('ebsco_default_amount') : $this->amount;
}
/**
* Perform the API Info call
* Perform the API Info call.
*
* @return array
*/
public function info()
{
public function info() {
$this->info = $this->eds->apiInfo();
return $this->info;
}
/**
* Perform the API Retrieve call
* Perform the API Retrieve call.
*
* @return array
*/
public function retrieve()
{
list($an, $db) = isset($this->params['id']) ? explode('|', $this->params['id'], 2) : array(null, null);
public function retrieve() {
list($an, $db) = isset($this->params['id']) ? explode('|', $this->params['id'], 2) : array(NULL, NULL);
$this->result = $this->eds->apiRetrieve($an, $db);
return $this->result;
}
/**
* Perform the API Search call
* Perform the API Search call.
*
* @return array
*/
public function search()
{
public function search() {
$search = array();
if (isset($this->params['lookfor']) & & isset($this->params['type'])) {
$search = array(
'lookfor' => $this->params['lookfor'],
'index' => $this->params['type']
'index' => $this->params['type'],
);
} else if (isset($this->params['group'])) {
}
elseif (isset($this->params['group'])) {
$search = $this->params;
} else {
}
else {
return array();
}
@ -263,29 +268,27 @@ class EBSCODocument
$page = isset($this->params['page']) ? $this->params['page'] + 1 : 1;
$limit = $this->limit;
$sort = isset($this->params['sort']) ? $this->params['sort'] : 'relevance';
$amount = isset($this->params['amount']) ? $this->params['amount'] : 'brief'; //
$mode = isset($this->params['mode']) ? $this->params['mode'] : 'all';
$amount = isset($this->params['amount']) ? $this->params['amount'] : 'brief'; $mode = isset($this->params['mode']) ? $this->params['mode'] : 'all';
//check if research starters , EMP are active
// Check if research starters , EMP are active.
$info = $this->info();
$rs=false ;
$emp=false ;
$rs = FALSE ;
$emp = FALSE ;
if ($info["relatedContent"]) {
foreach ($info["relatedContent"] as $related) {
if (($related["Type"] == "rs") and ($related["DefaultOn"] == "y")) {
$rs=true ;
$rs = TRUE ;
}
if (($related["Type"] == "emp") and ($related["DefaultOn"] == "y")) {
$emp=true ;
$emp = TRUE ;
}
}
}
$autosug=false ;
$autosug = FALSE ;
if ($info["didYouMean"]) {
if ($info["didYouMean"][0]["DefaultOn"] == "y") {
$autosug=true ;
$autosug = TRUE ;
}
}
@ -294,14 +297,12 @@ class EBSCODocument
return $this->results;
}
/**
* Get the EBSCORecord model for the result
* Get the EBSCORecord model for the result.
*
** @return array
* * @return array.
*/
public function record()
{
public function record() {
if (empty($this->record) & & !(empty($this->result))) {
$this->record = new EBSCORecord($this->result);
}
@ -309,19 +310,17 @@ class EBSCODocument
return $this->record;
}
/**
* Get the EBSCORecord models array from results array
* Get the EBSCORecord models array from results array.
*
** @return array
* * @return array.
*/
public function records()
{
public function records() {
if ($this->record instanceof EBSCOException) {
return null ;
return NULL ;
}
if ($this->results instanceof EBSCOException) {
return null ;
return NULL ;
}
if (empty($this->records) & & !(empty($this->results))) {
foreach ($this->results['documents'] as $result) {
@ -332,148 +331,133 @@ class EBSCODocument
return $this->records;
}
public function relatedContent()
{
/**
*
*/
public function relatedContent() {
if ($this->results instanceof EBSCOException) {
return null ;
return NULL ;
}
$this->relatedContent = isset($this->results['relatedContent']) ? $this->results['relatedContent'] : array();
return $this->relatedContent;
}
public function autoSuggestTerms()
{
/**
*
*/
public function autoSuggestTerms() {
$this->autoSuggestTerms = isset($this->results['autoSuggestTerms']) ? $this->results['autoSuggestTerms'] : null;
$this->autoSuggestTerms = isset($this->results['autoSuggestTerms']) ? $this->results['autoSuggestTerms'] : NULL ;
return $this->autoSuggestTerms;
}
/**
* Get the pagination HTML string
* Get the pagination HTML string.
*
** @return HTML string
* * @return HTML string.
*/
public function pager()
{
$pager = null;
try
{
public function pager() {
$pager = NULL;
try {
if ($this->has_records()) {
pager_default_initialize($this->record_count() / $this->limit, 1);
$pager = theme('pager', array('tags' => null , 'quantity' => self::$page_links));
$pager = theme('pager', array('tags' => NULL , 'quantity' => self::$page_links));
$pager = preg_replace('/< li class = "pager-last last" > (.*)< \/li>/', '', $pager);
}
}
catch(Exception $e)
{
catch (Exception $e) {
}
return $pager;
}
/********************************************************
*
* Getters (class methods)
*
********************************************************/
/**
* Getter for sort options
* Getter for sort options.
*
* @return array
*/
public static function limit_options()
{
public static function limit_options() {
return self::$limit_options;
}
/**
* Getter for sort options
* Getter for sort options.
*
* @return array
*/
public static function sort_options()
{
public static function sort_options() {
return self::$sort_options;
}
/**
* Getter for amount options
* Getter for amount options.
*
* @return array
*/
public static function amount_options()
{
public static function amount_options() {
return self::$amount_options;
}
/**
* Getter for boolean options
* Getter for boolean options.
*
* @return array
*/
public static function bool_options()
{
public static function bool_options() {
return self::$bool_options;
}
/**
* Getter for search mode options
* Getter for search mode options.
*
* @return array
*/
public static function mode_options()
{
public static function mode_options() {
return self::$mode_options;
}
/**
* Getter for Basic search type options
* Getter for Basic search type options.
*
* @return array
*/
public static function basic_search_type_options()
{
public static function basic_search_type_options() {
return self::$basic_search_type_options;
}
/**
* Getter for Advanced search type options
* Getter for Advanced search type options.
*
* @return array
*/
public static function advanced_search_type_options()
{
public static function advanced_search_type_options() {
return self::$advanced_search_type_options;
}
/********************************************************
*
* Helper methods
*
********************************************************/
/**
* Get the expanders.
*
* @return array
*/
public function expanders()
{
public function expanders() {
$expanders = array();
try
{
if ($this->info instanceof EBSCOException)
{
try {
if ($this->info instanceof EBSCOException) {
return $expanders;
}
$actions = array();
@ -485,27 +469,23 @@ class EBSCODocument
$expanders = isset($this->info['expanders']) ? $this->info['expanders'] : array();
foreach ($expanders as $key => $expander) {
if (in_array($expander['Action'], $actions)) {
$expanders[$key]['selected'] = true ;
$expanders[$key]['selected'] = TRUE ;
}
}
}
catch(Exception $e)
{
catch (Exception $e) {
}
return $expanders;
}
/**
* Get the facets.
*
* @return array
*/
public function facets()
{
public function facets() {
if ($this->results instanceof EBSCOException) {
return array();
}
@ -518,9 +498,9 @@ class EBSCODocument
$facets = isset($this->results['facets']) ? $this->results['facets'] : array();
foreach ($facets as $key => $cluster) {
foreach ($cluster['Values'] as $k => $facet) {
$is_applied = false ;
$is_applied = FALSE ;
if (in_array($facet['Action'], $actions)) {
$is_applied = true ;
$is_applied = TRUE ;
}
$facets[$key]['Values'][$k]['applied'] = $is_applied;
}
@ -529,14 +509,12 @@ class EBSCODocument
return $facets;
}
/**
* Get the filters.
*
* @return array
*/
public function filters()
{
public function filters() {
if (!empty($_REQUEST['filter'])) {
$labels = array();
foreach ($this->info['limiters'] as $limiter) {
@ -549,17 +527,19 @@ class EBSCODocument
if (substr($temp, -1, 1) == ')') {
$temp = substr($temp, 0, -1);
}
// Do not display addfacetfilter, addlimiter or addexpander strings
// Do not display addfacetfilter, addlimiter or addexpander strings.
if (preg_match('/\:/', $filter)) {
list($field, $value) = explode(':', $temp, 2);
$displayField = isset($labels[$field]) ? $labels[$field] : $field;
$displayValue = $value == 'y' ? 'yes' : $value;
} else if (preg_match('/addexpander/', $filter)) {
}
elseif (preg_match('/addexpander/', $filter)) {
$field = $temp;
$value = 'y';
$displayField = isset($labels[$field]) ? $labels[$field] : $field;
$displayValue = 'yes';
} else {
}
else {
$field = $value = $displayField = $displayValue = $filter;
}
@ -576,17 +556,14 @@ class EBSCODocument
return $this->filters;
}
/**
* Get the limiters.
*
* @return array
*/
public function limiters()
{
public function limiters() {
$actions = array(); $ids = array();
if ($this->info instanceof EBSCOException)
{
if ($this->info instanceof EBSCOException) {
return array();
}
$filters = $this->filters();
@ -597,7 +574,7 @@ class EBSCODocument
$limiters = isset($this->info['limiters']) ? $this->info['limiters'] : array();
foreach ($limiters as $key => $cluster) {
// multi select limiter
// Multi select limiter.
if (!empty($cluster['Values'])) {
foreach ($cluster['Values'] as $limiter) {
$action = $limiter['Action'];
@ -605,17 +582,19 @@ class EBSCODocument
$limiters[$key]['selected'][] = $limiter['Action'];
}
}
// date limiter
} else if ($cluster['Type'] == 'ymrange') {
// Date limiter.
}
elseif ($cluster['Type'] == 'ymrange') {
$id = $cluster['Id'];
if (($k = array_search($id, $ids)) !== false ) {
if (($k = array_search($id, $ids)) !== FALSE ) {
$limiters[$key]['selected'] = $filters[$k]['action'];
}
// other limiters
} else {
// Other limiters.
}
else {
$action = str_replace('value', 'y', $cluster['Action']);
if (in_array($action, $actions)) {
$limiters[$key]['selected'] = true ;
$limiters[$key]['selected'] = TRUE ;
}
}
}
@ -623,28 +602,24 @@ class EBSCODocument
return $limiters;
}
/**
* Get the total number of records.
*
* @return integer
* @return int
*/
public function record_count()
{
public function record_count() {
if ($this->results instanceof EBSCOException) {
return 0;
}
return !empty($this->results) ? $this->results['recordCount'] : 0;
}
/**
* Get the number of end record.
*
* @return integer
* @return int
*/
public function record_end()
{
public function record_end() {
if ($this->results instanceof EBSCOException) {
return -1;
}
@ -653,28 +628,24 @@ class EBSCODocument
return $start + $count;
}
/**
* Get the number of start record.
*
* @return integer
* @return int
*/
public function record_start()
{
public function record_start() {
if ($this->results instanceof EBSCOException) {
return null ;
return NULL ;
}
return !empty($this->results) ? $this->results['start'] + 1 : 0;
}
/**
* Get the search time
* Get the search time.
*
* @return decimal number
*/
public function search_time()
{
public function search_time() {
if ($this->results instanceof EBSCOException) {
return 0;
}
@ -682,37 +653,34 @@ class EBSCODocument
isset($this->results['searchTime']) ? $this->results['searchTime'] : 0;
}
/**
* Get the search view : basic or advanced
* Get the search view : basic or advanced.
*
* @return string
*/
public function search_view()
{
public function search_view() {
if (isset($_REQUEST['group'])) {
return 'advanced';
} else {
}
else {
return 'basic';
}
}
/**
* Hidden params used by UpdateForm
* Hidden params used by UpdateForm.
*
* @return array
*/
public function search_params()
{
public function search_params() {
$params = $this->link_search_params();
// f ilter the params that have same values as sidebar checkboxes, otherwise they will produce duplicates
// F ilter the params that have same values as sidebar checkboxes, otherwise they will produce duplicates.
$not_allowed_values = array(
'addexpander(thesaurus)',
'addexpander(fulltext)',
'addlimiter(FT:y)',
'addlimiter(RV:y)',
'addlimiter(SO:y)'
'addlimiter(SO:y)',
);
$params = $this->array_filter_recursive($params, function ($item) use ($not_allowed_values) {
@ -722,20 +690,19 @@ class EBSCODocument
return array_filter($params);
}
/**
* Hidden params used by UpdateForm
* Hidden params used by UpdateForm.
*
* @return array
*/
public function link_search_params()
{
// filter the page parameter
public function link_search_params() {
// Filter the page parameter.
$not_allowed_keys = array('page', 'ui', 'has_js', 'op', 'submit', 'form_id', 'form_build_id');
$query = "";
if(isset($_SERVER['QUERY_STRING']))
{$query = urldecode($_SERVER['QUERY_STRING']);}
if (isset($_SERVER['QUERY_STRING'])) {
$query = urldecode($_SERVER['QUERY_STRING']);
}
parse_str($query, $params);
$params = $this->array_unset_recursive($params, $not_allowed_keys);
@ -743,28 +710,24 @@ class EBSCODocument
return $params;
}
/**
* Check if there are records in results array
* Check if there are records in results array.
*
** @return boolean
* * @return boolean.
*/
public function has_records()
{
public function has_records() {
if ($this->results instanceof EBSCOException) {
return false ;
return FALSE ;
}
return !empty($this->results) & & !empty($this->results['documents']);
}
/**
* Create the last search data
* Create the last search data.
*
* @return void
*/
public function search_create($query = null)
{
public function search_create($query = NULL) {
if ($this->results instanceof EBSCOException) {
return array();
}
@ -782,25 +745,21 @@ class EBSCODocument
return $last_search;
}
/**
* Save last search data in session
* Save last search data in session.
*
* @return void
*/
public function search_write($query = null)
{
public function search_write($query = NULL) {
$_SESSION['EBSCO']['last-search'] = $this->search_create($query);
}
/**
* Load last search data from session
* Load last search data from session.
*
* @return array
*/
public function search_read($id = null, $op = null)
{
public function search_read($id = NULL, $op = NULL) {
$params = array();
$lastSearch = $_SESSION['EBSCO']['last-search'];
if ($lastSearch) {
@ -810,9 +769,9 @@ class EBSCODocument
$params['page'] = (int) (isset($params['page']) ? $params['page'] : 0);
$index = array_search($id, $lastSearch['records']);
// i f this is not the first scroll and if this is not a page refresh
// I f this is not the first scroll and if this is not a page refresh.
if (isset($lastSearch['current']) & & $lastSearch['current'] != $id) {
// if we change page
// If we change page.
if (($op == 'Next' & & $index % $this->limit === 0) ||
($op == 'Previous' & & $index % $this->limit === 9)) {
$params['page'] = ($op == 'Next') ? $params['page'] + 1 : $params['page'] - 1;
@ -824,7 +783,8 @@ class EBSCODocument
if (count($lastSearch['records']) > 10) {
$records = array_slice($lastSearch['records'], $index - $index % $this->limit, $this->limit);
} else {
}
else {
$records = $lastSearch['records'];
}
@ -841,7 +801,8 @@ class EBSCODocument
$lastSearch['previous'] = isset($records[8]) ? $records[8] : '';
}
$lastSearch['next'] = isset($newSearch['records'][0]) ? $newSearch['records'][0] : '';
} else {
}
else {
$lastSearch['next'] = $lastSearch['records'][$index + 1];
}
@ -859,10 +820,12 @@ class EBSCODocument
if ($op == 'Previous') {
$lastSearch['next'] = isset($records[1]) ? $records[1] : '';
}
} else {
}
else {
$lastSearch['previous'] = '';
}
} else {
}
else {
$lastSearch['previous'] = $lastSearch['records'][$index - 1];
}
@ -875,14 +838,12 @@ class EBSCODocument
return $lastSearch;
}
/**
* A recursive array_filter
* A recursive array_filter.
*
* @return array
*/
private function array_filter_recursive($input, $callback = null)
{
private function array_filter_recursive($input, $callback = NULL) {
foreach ($input as & $value) {
if (is_array($value)) {
$value = $this->array_filter_recursive($value, $callback);
@ -891,9 +852,8 @@ class EBSCODocument
return array_filter($input, $callback);
}
/**
* Recursive filter an array using the given $keys
* Recursive filter an array using the given $keys.
*
* @return array
*/
@ -912,4 +872,5 @@ class EBSCODocument
return array_filter($input);
}
}