You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
416 lines
13 KiB
PHP
416 lines
13 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright Mathieu Schroeter for the BSR, 2013
|
|
* Modif Simon Schulé pour la BSR, 2013. 2014
|
|
* Copyright Gilles Crettenand for the BSR, 2015
|
|
*/
|
|
|
|
require_once "global.php";
|
|
require_once "lib/AudioBook.php";
|
|
require_once "lib/User.php";
|
|
require_once "lib/BookSearch.php";
|
|
|
|
require_once "mobile.webservice.php";
|
|
|
|
class NetBiblio extends WebService
|
|
{
|
|
private $data = array();
|
|
|
|
private $login = '';
|
|
private $client = 'website';
|
|
|
|
/**
|
|
* @param $IP
|
|
* @param $client
|
|
* @param $login
|
|
* @param $code
|
|
* @throws SqlException
|
|
*/
|
|
public function AddDownloadLog($IP, $client, $login, $code)
|
|
{
|
|
$this->data = array();
|
|
|
|
$client = str_replace("'", "", $client);
|
|
$login = str_replace("'", "", $login);
|
|
$code = ltrim(str_replace("'", "", $code), '0');
|
|
$itemNr = $code . 'V';
|
|
$itemId = '';
|
|
$userId = '';
|
|
|
|
/* Récupération de l'id de l'exemplaire */
|
|
$sql = "SELECT itemID from Netbiblio3.dbo.items where ltrim(rtrim(itemnr))='$itemNr';";
|
|
$result = Connection::execute($sql, false);
|
|
|
|
if ($row = $result->next()) {
|
|
$itemId = $row['itemID'];
|
|
}
|
|
|
|
/* Récupération de l'id du compte */
|
|
$sql = "SELECT useraccountID from Netbiblio3.dbo.UserAccounts where ltrim(rtrim(useraccountnr))='$login';";
|
|
$result = Connection::execute($sql, false);
|
|
if ($row = $result->next()) {
|
|
$userId = $row['useraccountID'];
|
|
}
|
|
|
|
$sql = "SELECT circulationId from Netbiblio3.dbo.OldCirculations where useraccountID=$userId AND itemID=$itemId AND ltrim(rtrim(remark))='$client';";
|
|
$result = Connection::execute($sql, false);
|
|
|
|
if ($existingEntry = $result->next()) {
|
|
$existingId = $existingEntry['circulationId'];
|
|
$logSql = "UPDATE OldCirculations SET CheckInDate=GETDATE(), CheckOutDate=GETDATE() WHERE circulationID=$existingId";
|
|
} else {
|
|
$sql = "SELECT TOP 1 circulationID FROM oldcirculations ORDER BY CirculationID DESC";
|
|
$result = Connection::execute($sql, false);
|
|
if ($row = $result->next()) {
|
|
$nextId = $row['circulationID'] + 1;
|
|
} else {
|
|
$nextId = 1;
|
|
}
|
|
|
|
/* Ajout d'un ancien prêt dans OldCirculations */
|
|
$worker_id = Configuration::get('netbiblio_worker_id');
|
|
$logSql = "INSERT INTO Netbiblio3.dbo.OldCirculations (" .
|
|
" CirculationID, " .
|
|
" ItemID, " .
|
|
" UseraccountID, " .
|
|
" DueDate, " .
|
|
" Remark, " .
|
|
" CheckOutDate, " .
|
|
" CheckOutBranchofficeID, " .
|
|
" CheckOutEmployeeID, " .
|
|
" CheckInDate, " .
|
|
" CheckInBranchofficeID, " .
|
|
" CheckInEmployeeID, " .
|
|
" Reminders, " .
|
|
" Renewals, " .
|
|
" Prereminder, " .
|
|
" InfoCode, " .
|
|
" CheckOutSIP2Info, " .
|
|
" CheckInSIP2Info " .
|
|
") VALUES ( " .
|
|
" $nextId, " .
|
|
" $itemId, " .
|
|
" $userId, " .
|
|
" DATEADD(month, 2, GETDATE()), " .
|
|
" '$client', " .
|
|
" GETDATE(), " .
|
|
" 2, " .
|
|
" $worker_id, " .
|
|
" GETDATE(), " .
|
|
" 2, " .
|
|
" $worker_id, " .
|
|
" 0, " .
|
|
" 0, " .
|
|
" 1, " .
|
|
" '-', " .
|
|
" 1, " .
|
|
" 1 " .
|
|
");";
|
|
|
|
/* Incrément du compteur de prêts "Circulations" dans Items (exemplaires) */
|
|
$incrementUserCountersSQL =
|
|
"UPDATE Useraccounts " .
|
|
"SET Circulations=Circulations+1, TotalCirculations=TotalCirculations+1 " .
|
|
"WHERE UseraccountID=$userId;";
|
|
Connection::execute($incrementUserCountersSQL);
|
|
|
|
/* Incrément du compteur de prêts "TotalCirculations" dans UserAccounts (comptes auditeurs) */
|
|
$incrementItemCountersSQL =
|
|
"UPDATE Items " .
|
|
"SET Circulations=Circulations+1, TotalCirculations=TotalCirculations+1 " .
|
|
"WHERE ItemID=$itemId;";
|
|
Connection::execute($incrementItemCountersSQL);
|
|
}
|
|
Connection::execute($logSql);
|
|
}
|
|
|
|
public function Authenticate($login, $password, $client = "website")
|
|
{
|
|
session_unset(); /* destroy all session vars */
|
|
|
|
$user = User::authenticate($login, $password);
|
|
|
|
if (!$user) {
|
|
throw new WebException ("AuthenticateBad", "authentication failed", -100);
|
|
}
|
|
|
|
$_SESSION["user"]["login"] = $login;
|
|
$_SESSION["user"]["client"] = $client;
|
|
|
|
$this->data = $user->toArray();
|
|
$this->login = $login;
|
|
$this->client = $client;
|
|
}
|
|
|
|
public function Disconnect()
|
|
{
|
|
$this->data = array();
|
|
$_SESSION = array();
|
|
|
|
if (ini_get("session.use_cookies")) {
|
|
$params = session_get_cookie_params();
|
|
setcookie(session_name(), '', time() - 42000,
|
|
$params["path"], $params["domain"],
|
|
$params["secure"], $params["httponly"]);
|
|
}
|
|
}
|
|
|
|
public function IsAuthenticated()
|
|
{
|
|
$this->data = $this->getUser()->toArray();
|
|
}
|
|
|
|
/**
|
|
* Adds entries to OldCirculations in Netbiblio database and increments counters on items and useraccounts tables
|
|
* For now, keeps a separate log in BSRDownload Database to store IPs
|
|
* In case a download has already been logged, only the date of the existing entry is updated, no counter incremented.
|
|
* @param string $login
|
|
* @return User
|
|
* @throws WebException in case the login cannot be found in the database
|
|
*/
|
|
private function getUser($login = null)
|
|
{
|
|
if (!$login) {
|
|
$login = $_SESSION["user"]["login"];
|
|
}
|
|
|
|
$this->checkSession($login);
|
|
$user = User::find($this->login);
|
|
|
|
if (!$user) {
|
|
throw new WebException ("UserNotFound", "cannot find account", -130);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
private function CheckSession($login = null, $client = null)
|
|
{
|
|
if (!isset ($_SESSION["user"]["login"])) {
|
|
return;
|
|
}
|
|
|
|
if(!$client) {
|
|
$client = isset($_SESSION["user"]["client"]) ? $_SESSION["user"]["client"] : 'website';
|
|
}
|
|
|
|
if (!$login) {
|
|
$login = $_SESSION["user"]["login"];
|
|
} else if ($_SESSION["user"]["login"] !== $login) {
|
|
throw new WebException ("CheckSessionBadAuth", "bad authentication", -1001);
|
|
}
|
|
|
|
$this->login = $login;
|
|
$this->client = $client;
|
|
}
|
|
|
|
public function FindAccount($login)
|
|
{
|
|
$this->data = $this->getUser($login)->toArray();
|
|
}
|
|
|
|
public function GetWishes()
|
|
{
|
|
$books = $this->getUser()->getWishes();
|
|
$this->data = array_map(array($this, 'AddFiles'), $books);
|
|
}
|
|
|
|
public function GetCirculations()
|
|
{
|
|
$circulations = $this->getUser()->getCirculations();
|
|
$this->data = array_map(array($this, 'AddFiles'), $circulations);
|
|
}
|
|
|
|
public function GetOldCirculations()
|
|
{
|
|
$circulations = $this->getUser()->getOldCirculations();
|
|
$this->data = array_map(array($this, 'AddFiles'), $circulations);
|
|
}
|
|
|
|
public function AddWish($bookNr)
|
|
{
|
|
$bookNr = intval($bookNr);
|
|
$bookId = AudioBook::findIdByCode($bookNr);
|
|
$this->data[] = $this->getUser()->addWish($bookId);
|
|
}
|
|
|
|
public function DeleteWish($bookNr)
|
|
{
|
|
$bookNr = intval($bookNr);
|
|
$bookId = AudioBook::findIdByCode($bookNr);
|
|
$this->getUser()->deleteWish($bookId);
|
|
}
|
|
|
|
public function FindBooks($codes)
|
|
{
|
|
$this->CheckSession();
|
|
|
|
$codeList = array_map('intval', json_decode($codes, true));
|
|
foreach ($codeList as $code) {
|
|
if ($code != 0) {
|
|
$id = AudioBook::findIdByCode($code);
|
|
$this->data[] = $this->AddFiles(AudioBook::find($id));
|
|
}
|
|
}
|
|
}
|
|
|
|
private function AddFiles(AudioBook $book)
|
|
{
|
|
$book = $book->toArray();
|
|
|
|
$uri = sprintf("%s%s",
|
|
Configuration::get('checkfile_url'),
|
|
http_build_query(array(
|
|
"client" => $this->client,
|
|
"login" => $this->login,
|
|
"book" => intval($book['code'])
|
|
))
|
|
);
|
|
|
|
$ch = curl_init($uri);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
$json = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$files = json_decode($json, true);
|
|
if (is_array($files)) {
|
|
$book['files'] = $files;
|
|
}
|
|
return $book;
|
|
}
|
|
|
|
public function FindBook($code)
|
|
{
|
|
$this->CheckSession();
|
|
|
|
$code = intval($code);
|
|
$id = AudioBook::findIdByCode($code);
|
|
$this->data = $this->AddFiles(AudioBook::find($id));
|
|
}
|
|
|
|
public function Search($query, $start, $limit)
|
|
{
|
|
$query = array(
|
|
'queryText' => $query,
|
|
'queryType' => is_numeric($query) && strlen($query) <= 5 ? 'code' : 'text',
|
|
'count' => $limit,
|
|
'page' => ($start / $limit),
|
|
);
|
|
$this->NewSearch(json_encode($query));
|
|
|
|
// remove fields that are not used in "old" search
|
|
unset($this->data['count']);
|
|
unset($this->data['facets']);
|
|
}
|
|
|
|
public function NewSearch($values)
|
|
{
|
|
$this->CheckSession();
|
|
|
|
$queryArray = json_decode($values, true);
|
|
if(! is_array($queryArray)) {
|
|
throw new WebException("CallArg", "Argument must be valid JSON.", -42);
|
|
}
|
|
|
|
$bs = new BookSearch();
|
|
|
|
if (isset($queryArray['queryType'])) {
|
|
$bs->addSortField('author', SolrQuery::ORDER_ASC);
|
|
$bs->addSortField('title', SolrQuery::ORDER_ASC);
|
|
$bs->addSortField('producer');
|
|
$bs->addSortField('mediaType', SolrQuery::ORDER_ASC);
|
|
} else {
|
|
$bs->addSortField('availabilityDate');
|
|
$bs->addSortField('author', SolrQuery::ORDER_ASC);
|
|
$bs->addSortField('title', SolrQuery::ORDER_ASC);
|
|
}
|
|
|
|
if (isset($queryArray['queryText']) && strlen($queryArray['queryText']) > 0) {
|
|
$bs->addQuery($queryArray['queryText'], $queryArray['queryType']);
|
|
}
|
|
|
|
if(isset($queryArray['reader']) && strlen($queryArray['reader']) > 0) {
|
|
$bs->addQuery($queryArray['reader'], 'reader');
|
|
}
|
|
|
|
if(isset($queryArray['category']) && is_array($queryArray['category'])) {
|
|
$selectedCategories = array_filter($queryArray['category'], function ($c) {
|
|
return $c != '0';
|
|
});
|
|
if (count($selectedCategories) > 0) {
|
|
$selectedCategories = array_map(function ($c) {
|
|
return "categoryCode: $c";
|
|
}, $selectedCategories);
|
|
$bs->addQuery('(' . implode(' OR ', $selectedCategories) . ')');
|
|
}
|
|
}
|
|
|
|
if(isset($queryArray['producer']) && strlen($queryArray['producer']) > 0) {
|
|
$bs->addQuery($queryArray['producer'], 'producer');
|
|
}
|
|
|
|
if(isset($queryArray['jeunesse']) && $queryArray['jeunesse']['filtrer'] === 'filtrer') {
|
|
$bs->addQuery(1, 'jeunesse');
|
|
}
|
|
|
|
$count = isset($queryArray['count']) ? (int) $queryArray['count'] : Configuration::get('solr.result_count');
|
|
$start = isset($queryArray['page']) ? $queryArray['page'] * $count : 0;
|
|
|
|
try {
|
|
$results = $bs->getResults($start, $count);
|
|
} catch(SolrClientException $e) {
|
|
throw new WebException ("SolrError", $e->getMessage(), -700);
|
|
}
|
|
|
|
$this->data['count'] = $results['response']['numFound'];
|
|
$this->data['facets'] = $results['facet_counts']['facet_fields'];
|
|
|
|
foreach ($results['response']['docs'] as $doc) {
|
|
$book = AudioBook::find($doc['id']);
|
|
if($book) {
|
|
$this->data[] = $this->AddFiles($book);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function ListOfReaders()
|
|
{
|
|
$this->data = AudioBook::listOfReaders();
|
|
}
|
|
|
|
public function ListOfCategories()
|
|
{
|
|
$this->data = AudioBook::listOfCategories();
|
|
}
|
|
|
|
public function ListOfTypes()
|
|
{
|
|
$this->data = array_filter(AudioBook::listOfTypes(), function ($t) {
|
|
return strlen($t) > 0;
|
|
});
|
|
}
|
|
|
|
public function LastBooksByType($type, $itemsByGroup)
|
|
{
|
|
$this->checkSession();
|
|
|
|
$books = AudioBook::lastBooksByType($type, $itemsByGroup);
|
|
$books = array_map(array($this, 'AddFiles'), $books);
|
|
foreach ($books as $book) {
|
|
$this->data[$book['type']][] = $book;
|
|
}
|
|
}
|
|
|
|
public function InReadingBooks()
|
|
{
|
|
$this->data = AudioBook::inReading();
|
|
}
|
|
|
|
protected function Output()
|
|
{
|
|
return $this->data;
|
|
}
|
|
}
|