From da0efd72beb7962bf6bec3c9177e1f93360e0304 Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Wed, 3 Jun 2015 10:02:28 +0200 Subject: [PATCH] Various fixes and cleanup : * Always return at least success information * Fix issues with Wishes * Fix issues with Circulations * Fix typos in exceptions --- Lib/Search/BookSearch.php | 12 ++++++++---- Lib/WebService.php | 4 ++-- Lib/db/Connection.php | 7 +++++++ Lib/db/User.php | 21 +++++++++++++-------- NetBiblio.php | 18 ++++++++++++------ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Lib/Search/BookSearch.php b/Lib/Search/BookSearch.php index 254604f..e173431 100644 --- a/Lib/Search/BookSearch.php +++ b/Lib/Search/BookSearch.php @@ -76,9 +76,12 @@ class BookSearch throw new WebException ("SolrError", $e->getMessage(), -700); } - $books = isset($results['response']['docs']) && $results['response']['docs'] ? - array_map(function($o) { return (array) $o; }, $results['response']['docs']) : - array(); + $books = array(); + if(isset($results['response']['docs']) && is_array($results['response']['docs'])) { + foreach($results['response']['docs'] as $r) { + $books[''.$r->id] = (array) $r; + } + } return array( 'count' => $results['response']['numFound'], @@ -105,7 +108,8 @@ class BookSearch $parts = array_chunk($codes, $limit); $books = array(); foreach($parts as $p) { - $books = array_merge($books, self::GetBooks($p)); + // if we use array_merge here the numerical keys (book code) will be lost + $books += self::GetBooks($p, $field); } return $books; } diff --git a/Lib/WebService.php b/Lib/WebService.php index c7c5925..5f5d260 100644 --- a/Lib/WebService.php +++ b/Lib/WebService.php @@ -95,10 +95,10 @@ abstract class WebService /* Check the number of arguments. */ if ($nbParams < $nbArgsFix) { - throw new UsageException("TooManyArgs", "You must provide at least $nbArgsFix arguments.", UsageException::TOO_MANY_ARGS); + throw new UsageException("TooFewArgs", "You must provide at least $nbArgsFix arguments.", UsageException::TOO_FEW_ARGS); } if ($nbParams > $nbArgs) { - throw new UsageException("TooFewArgs", "You must provide at most $nbArgs arguments.", UsageException::TOO_FEW_ARGS); + throw new UsageException("TooManyArgs", "You must provide at most $nbArgs arguments.", UsageException::TOO_MANY_ARGS); } $this->log("Calling '".$this->func."'"); diff --git a/Lib/db/Connection.php b/Lib/db/Connection.php index 8627761..be1d6a8 100644 --- a/Lib/db/Connection.php +++ b/Lib/db/Connection.php @@ -64,6 +64,7 @@ class OdbcResultSet implements \Iterator, \ArrayAccess private $results; private $error; private $num_fields; + private $num_rows; private $cursor_index; public function __construct($odbc_result) @@ -74,6 +75,7 @@ class OdbcResultSet implements \Iterator, \ArrayAccess try { $this->results = array(); $this->num_fields = odbc_num_fields($odbc_result); + $this->num_rows = odbc_num_rows($odbc_result); if ($this->num_fields > 0) { while ($row = odbc_fetch_row($odbc_result)) { @@ -94,6 +96,11 @@ class OdbcResultSet implements \Iterator, \ArrayAccess } } + public function get_num_rows() + { + return $this->num_rows; + } + public function is_error() { return ($this->error ? true : false); diff --git a/Lib/db/User.php b/Lib/db/User.php index 7111eb8..8ee8d59 100644 --- a/Lib/db/User.php +++ b/Lib/db/User.php @@ -76,7 +76,7 @@ class User extends DbMapping REPLACE(UserAccountNr, ' ', '') AS login FROM [UserAccounts] AS u LEFT JOIN [Addresses] AS a ON a.[AddressID] = u.[ActualAddressID] - WHERE REPLACE(UserAccountNr, ' ', '') = '%s' AND disabled = 1 %s;", + WHERE LTRIM(RTRIM(UserAccountNr)) = '%s' AND disabled = 1 %s;", $login, $cond); $results = Connection::execute($sql, $raiseError); @@ -98,11 +98,14 @@ class User extends DbMapping $circulations = $result->to_array(); $ids = array_map(function($c) { return $c['NoticeID']; }, $circulations); - $books = BookSearch::GetBooks($ids, 'id'); + $books = count($ids) > 0 ? BookSearch::GetBooks($ids, 'id') : array(); foreach($circulations as $c) { - $books[$c['NoticeID']]['date'] = $c['CheckOutDate']; - $books[$c['NoticeID']]['itemNr'] = $c['ItemNr']; + $id = $c['NoticeID']; + if(isset($books[$id])) { + $books[$id]['date'] = $c['CheckOutDate']; + $books[$id]['itemNr'] = $c['ItemNr']; + } } return $books; @@ -145,8 +148,8 @@ class User extends DbMapping WHERE LTRIM(RTRIM(NoticeNr)) = '%s';", $row['WishID'], $this->id, $employee_id, $library_id, $noticeNr); - Connection::execute($sql); - return true; + $status = Connection::execute($sql); + return $status && ! $status->is_error() && $status->get_num_rows() > 0; } /** @@ -176,7 +179,7 @@ class User extends DbMapping { $sql = sprintf("SELECT TOP $limit NoticeID - FROM Wished + FROM Wishes WHERE UserAccountID = %s ORDER BY CreationDate DESC", $this->id); @@ -188,6 +191,7 @@ class User extends DbMapping /** * Remove a book from the wish list * @param string $noticeNr + * @return boolean Was the deletion was successful or not ? */ public function deleteWish($noticeNr) { @@ -197,6 +201,7 @@ class User extends DbMapping WHERE LTRIM(RTRIM(n.NoticeNr)) = '%s' AND UserAccountID = %s;", $noticeNr, $this->id); - Connection::execute($sql, true); + $status = Connection::execute($sql, true); + return $status && ! $status->is_error() && $status->get_num_rows() > 0; } } \ No newline at end of file diff --git a/NetBiblio.php b/NetBiblio.php index 73d92ce..ea1953a 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -42,7 +42,7 @@ class NetBiblio extends WebService private function getUser($login = null) { if(! is_null($login) && $_SESSION["user"]["login"] !== $login) { - throw new AuthenticationException("BadLogin", "Login '$login' is invalid.'", AuthenticationException::BAD_LOGIN); + throw new AuthenticationException("BadLogin", "Login '$login' is invalid.", AuthenticationException::BAD_LOGIN); } $this->checkSession(); @@ -50,7 +50,7 @@ class NetBiblio extends WebService $user = User::find($this->login); if (!$user) { - throw new AuthenticationException("UserNotFound", "No user found for '{$this->login}.", AuthenticationException::USER_NOT_FOUND); + throw new AuthenticationException("UserNotFound", "No user found for '{$this->login}'.", AuthenticationException::USER_NOT_FOUND); } return $user; @@ -157,8 +157,12 @@ class NetBiblio extends WebService $b['files'] = $files; } + // date will be already set if we are updating Circulations + if(! isset($b['date'])) { + $b['date'] = date('Y.m.d', strtotime($b['availabilityDate'])); + } + // add fields for mobile apps compatibility - $b['date'] = date('Y.m.d', strtotime($b['availabilityDate'])); $b['readBy'] = isset($b['reader']) ? $b['reader'] : 'Lecteur inconnu'; $b['category'] = $b['genre']; $b['code3'] = $b['producerCode']; @@ -373,18 +377,20 @@ class NetBiblio extends WebService public function AddWish($bookNr) { - return $this->getUser()->addWish($bookNr); + $status = $this->getUser()->addWish($bookNr); + return array('success' => $status); } public function DeleteWish($bookNr) { - $this->getUser()->deleteWish($bookNr); + $status = $this->getUser()->deleteWish($bookNr); + return array('success' => $status); } public function FindBooks($codes) { $this->CheckSession(); - return $this->AddBookData(BookSearch::GetBooks(json_decode($codes))); + return array_values($this->AddBookData(BookSearch::GetBooks(json_decode($codes)))); } public function FindBook($code)