Skip to content
Snippets Groups Projects
GetBoss.php 7.48 KiB
Newer Older
Gregor Gawol's avatar
Gregor Gawol committed
<?php
/**
 * Copyright (C) Leipzig University Library 2019.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author   Gregor Gawol <gawol@ub.uni-leipzig.de>
 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 */
Gregor Gawol's avatar
Gregor Gawol committed
namespace finc\Boss\AjaxHandler;
Gregor Gawol's avatar
Gregor Gawol committed

use fid\Service\Client;
use finc\Boss\Client\BossClient;
Gregor Gawol's avatar
Gregor Gawol committed
use VuFind\Record\Loader;
Gregor Gawol's avatar
Gregor Gawol committed
use Zend\Config\Config;
Gregor Gawol's avatar
Gregor Gawol committed
use Zend\Http\PhpEnvironment\Request;
Gregor Gawol's avatar
Gregor Gawol committed
use Zend\Mvc\Controller\Plugin\Params;
use Zend\View\Renderer\RendererInterface;

class GetBoss extends \VuFind\AjaxHandler\AbstractBase
{

    /**
     * Top-level configuration
     *
     * @var Config
     */
    protected $config;

    /**
     * HTTP Service
     *
Gregor Gawol's avatar
Gregor Gawol committed
     * @var BossClient
Gregor Gawol's avatar
Gregor Gawol committed
     */
Gregor Gawol's avatar
Gregor Gawol committed
    protected $bossClient;

    /**
     * Request
     *
     * @var Request
     */
    protected $request;

    /**
     * Record loader
     *
     * @var Loader
     */
    protected $recordLoader;

    /**
     * FID API Client
Gregor Gawol's avatar
Gregor Gawol committed
     *
     * @var Client
Gregor Gawol's avatar
Gregor Gawol committed
     */
    protected $fidClient;
Gregor Gawol's avatar
Gregor Gawol committed

    /**
     * View renderer
     *
     * @var RendererInterface
     */
    protected $renderer;

    /**
     * Constructor
     *
Gregor Gawol's avatar
Gregor Gawol committed
     * @param BossClient            $bossClient     http client
Gregor Gawol's avatar
Gregor Gawol committed
     * @param Config                $config         Top-level configuration
Gregor Gawol's avatar
Gregor Gawol committed
     * @param Loader                $loader         VuFind Record Loader
     * @param Request               $request        Request
     * @param Client                $fidClient      Resolver Library Mapper
Gregor Gawol's avatar
Gregor Gawol committed
     * @param RendererInterface     $renderer       view
     */
    public function __construct(
Gregor Gawol's avatar
Gregor Gawol committed
        BossClient $bossClient,
Gregor Gawol's avatar
Gregor Gawol committed
        Config $config,
Gregor Gawol's avatar
Gregor Gawol committed
        Loader $loader,
        Request $request,
        Client $fidClient,
Gregor Gawol's avatar
Gregor Gawol committed
        RendererInterface $renderer
    ) {
Gregor Gawol's avatar
Gregor Gawol committed
        $this->bossClient = $bossClient;
Gregor Gawol's avatar
Gregor Gawol committed
        $this->config = $config;
Gregor Gawol's avatar
Gregor Gawol committed
        $this->recordLoader = $loader;
        $this->request = $request;
        $this->fidClient = $fidClient;
Gregor Gawol's avatar
Gregor Gawol committed
        $this->renderer = $renderer;
    }

    /**
     * Handle a request.
     *
     * @param Params $params Parameter helper from controller
     *
     * @return array [response data, HTTP status code]
     */
    public function handleRequest(Params $params)
    {
        if (!isset($this->config->General)) {
Gregor Gawol's avatar
Gregor Gawol committed
            return $this->formatResponse(
                'Configuration file boss.ini not found',
                self::STATUS_HTTP_BAD_REQUEST
            );
        }

        if ($this->fidClient->isLoggedOn()) {
            $user = $this->fidClient->requestUserDetails();
            $libraries = $this->fidClient->requestLibraryList();
            $homeLibrary = $user->getHomeLibrary();
            // Obtain user information from ILS:
            $library = isset($libraries[$homeLibrary]) ?
                $libraries[$homeLibrary] : $libraries['AAAAA'];
            $network = $library->getNetwork();
            $boss = $library->getBoss();

            $driver = $this->recordLoader->load(
                $params->fromQuery('id'),
                $params->fromQuery('source')
            );
            $seq = isset($this->config->Search->sequence) ?
                $this->config->Search->sequence : [];
            $fields = $driver->getRawData();
            $results = false;
            foreach ($seq as $data) {
                if (isset($fields[$data])) {
                    $method = $this->config->Search->$data;
                    $value = $driver->tryMethod($method);
                    $methodName = "getRequest" . strtoupper($data);
                    $results = $this->bossClient->$methodName(
                        $value, strtoupper($network)
                    );
                    if (!empty($results['data'])) {
                        break;
                    }
Gregor Gawol's avatar
Gregor Gawol committed
                }
            }
            // default search of boss
            if (empty($results)) {
                $author = $driver->tryMethod('getCombinedAuthors');
                $author = count($author) > 0 ? $author[0] : '';
                $title = $driver->tryMethod('getTitle');
                $year = $driver->tryMethod('getPublishDateSort');
                $results = $this->bossClient->getRequestQuery(
                    $author, $title, $year, strtoupper($network)
                );
            }
Gregor Gawol's avatar
Gregor Gawol committed

            $isilCallNumber = $this->getISILCallNumber($results['data']);
            $networks = isset($this->config->General->networks) ?
Gregor Gawol's avatar
Gregor Gawol committed
                $this->config->General->networks->toArray() : [];
            $addNetworks = isset($this->config->General->addNetworks) ?
                $this->config->General->addNetworks->toArray() : [];
            $network = strtolower($network);
            $inArray = in_array($network, $networks);
            $zdbId = $driver->tryMethod('getZdbId');
            $isbns = $driver->tryMethod('getISBNs');
            $isbns = count($isbns) > 0 ? $isbns[0] : '';
Gregor Gawol's avatar
Gregor Gawol committed
            $results['param'] = !empty($results['param']) ? $results['param'] : $isbns;
            $isISXNZBD = (!empty($isbns) || !empty($zdbId)) ? true : false;
            $tmp = $isilCallNumber;
            unset($tmp['isils']);
            $callnumber = $this->getCallnumbers($boss, $tmp);
            $isISIL = $inArray && $isISXNZBD ?
                $this->isISIL($isilCallNumber['isils'], $boss) : false;

                'homeLibrary' => ($homeLibrary == "AAAAA") ? true : false,
                'isISXNZBD' => $isISXNZBD,
                'isAddNetwork' => in_array($network, $addNetworks) ? true : false,
Gregor Gawol's avatar
Gregor Gawol committed
                'url' => sprintf($this->config->SearchUrls->$network, $results['param']),
Gregor Gawol's avatar
Gregor Gawol committed
                'callnumber' => $callnumber
            ];

            $html = $this->renderer->render('ajax/boss.phtml', $view);
            return $this->formatResponse(compact('html', 'isISIL'));
Gregor Gawol's avatar
Gregor Gawol committed
    }
Gregor Gawol's avatar
Gregor Gawol committed

Gregor Gawol's avatar
Gregor Gawol committed
    private function getCallnumbers($namespace, $data)
    {
        if ($this->isISIL(array_keys($data), $namespace)) {
Gregor Gawol's avatar
Gregor Gawol committed
            $cnValue = [];
            foreach ($data as $key => $value) {
                if (preg_match('/' . $namespace . '/', $key) && !empty($value)) {
Gregor Gawol's avatar
Gregor Gawol committed
                    $cnValue[] = $value;
                }
            }
            return $cnValue;
        }
        return [];
    }

Gregor Gawol's avatar
Gregor Gawol committed
    private function getISILCallNumber($results)
    {
        $retval = [];
Gregor Gawol's avatar
Gregor Gawol committed
        $results = is_null($results) ? [] : $results;
Gregor Gawol's avatar
Gregor Gawol committed
        foreach ($results as $result) {
            foreach ($result as $holding) {
                $tmp[] = $holding['isil'];
                $retval[($holding['isil'])] = isset($holding['callnumber']) ?
                    $holding['callnumber'] : '';
            }
        }

        $retval['isils'] = !empty($tmp) ? $tmp : [];

Gregor Gawol's avatar
Gregor Gawol committed
        if (empty($results)) {
            $retval['isils'] = [];
        }

Gregor Gawol's avatar
Gregor Gawol committed
        return $retval;
    }

    private function isISIL($data, $isil)
    {
        return preg_grep('/'.$isil.'/', $data) ?
            true : false;
    }
Gregor Gawol's avatar
Gregor Gawol committed
}