diff --git a/NetBiblio.php b/NetBiblio.php index 2c2293b..e1b9ad6 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -15,149 +15,6 @@ class NetBiblio extends WebService private $login = ''; private $client = 'website'; - public function AddDownloadLog($client, $login, $code) - { - $client = str_replace("'", "", $client); - $login = str_replace("'", "", $login); - $code = ltrim(str_replace("'", "", $code), '0'); - $itemNr = $code . 'V'; - - $sql = "SELECT itemID FROM Items WHERE LTRIM(RTRIM(ItemNr)) = '$itemNr';"; - $result = Connection::execute($sql, false); - if ($row = $result->current()) { - $itemId = $row['itemID']; - } else { - throw new WebException("ItemNotFound", "cannot find item", -1030); - } - - $sql = "SELECT UserAccountID FROM UserAccounts WHERE LTRIM(RTRIM(UserAccountNr)) = '$login';"; - $result = Connection::execute($sql, false); - if ($row = $result->current()) { - $userId = $row['UserAccountID']; - } else { - throw new WebException("UserNotFound", "cannot find user", -1031); - } - - $sql = "SELECT circulationId - FROM OldCirculations - WHERE - useraccountID= $userId AND - itemID = $itemId AND - LTRIM(RTRIM(remark)) = '$client';"; - $result = Connection::execute($sql, false); - - if ($row = $result->current()) { - $id = $row['circulationId']; - $sql = "UPDATE OldCirculations - SET - CheckInDate=GETDATE(), - CheckOutDate=GETDATE() - WHERE circulationID = $id"; - Connection::execute($sql); - return true; - } - - $sql = "SELECT TOP 1 circulationID FROM OldCirculations ORDER BY CirculationID DESC"; - $result = Connection::execute($sql, false); - if ($row = $result->current()) { - $nextId = $row['circulationID'] + 1; - } else { - $nextId = 1; - } - - $sql = "UPDATE Useraccounts - SET - Circulations = Circulations + 1, - TotalCirculations = TotalCirculations + 1 - WHERE UseraccountID = $userId;"; - Connection::execute($sql); - - $sql = "UPDATE Items - SET - Circulations = Circulations + 1, - TotalCirculations = TotalCirculations + 1 - WHERE ItemID = $itemId;"; - Connection::execute($sql); - - $worker_id = Configuration::get('netbiblio_worker_id'); - $sql = "INSERT INTO OldCirculations ( - CirculationID, ItemID, UseraccountID, - Remark, - DueDate, CheckOutDate, CheckInDate, - CheckOutBranchofficeID, CheckOutEmployeeID, CheckInBranchofficeID, CheckInEmployeeID, - Reminders, Renewals, Prereminder, InfoCode, CheckOutSIP2Info, CheckInSIP2Info - ) VALUES ( - $nextId, $itemId, $userId, - '$client', - DATEADD(month, 2, GETDATE()), GETDATE(), GETDATE(), - 2, $worker_id, 2, $worker_id, - 0, 0, 1, '-', 1, 1 - );"; - Connection::execute($sql); - return true; - } - - 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->login = $login; - $this->client = $client; - - return $user->toArray(); - } - - public function Disconnect() - { - $_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"]); - } - - return array(); - } - - public function IsAuthenticated() - { - return $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) { @@ -179,38 +36,22 @@ class NetBiblio extends WebService $this->client = $client; } - public function FindAccount($login) - { - return $this->getUser($login)->toArray(); - } - - public function GetWishes() + private function getUser($login = null) { - $books = $this->getUser()->getWishes(); - return array_values($this->AddBookData($books)); - } + if (!$login) { + $login = $_SESSION["user"]["login"]; + } - public function GetCirculations() - { - $circulations = $this->getUser()->getCirculations(); - return array_values($this->AddBookData($circulations)); - } + $this->checkSession($login); + $user = User::find($this->login); - public function GetOldCirculations() - { - $circulations = $this->getUser()->getOldCirculations(); - return array_values($this->AddBookData($circulations)); - } + if (!$user) { + throw new WebException ("UserNotFound", "cannot find account", -130); + } - public function AddWish($bookNr) - { - return $this->getUser()->addWish($bookNr); + return $user; } - public function DeleteWish($bookNr) - { - $this->getUser()->deleteWish($bookNr); - } private function GetBooks(array $codes) { $bs = new BookSearch(); @@ -219,28 +60,6 @@ class NetBiblio extends WebService return $results['books']; } - public function FindBooks($codes) - { - $this->CheckSession(); - - $codes = json_decode($codes); - - // it is faster to do multiple small request to Solr rather than one big so separate - // in chunks if we are above the limit. 15 was found by testing and seems to be a sweet spot - $limit = 15; - if(count($codes) > $limit) { - $parts = array_chunk($codes, $limit); - $books = array(); - foreach($parts as $p) { - $books = array_merge($books, $this->GetBooks($p)); - } - } else { - $books = $this->GetBooks($codes); - } - - return $this->AddBookData($books); - } - private function GetFiles(array $ids) { $ids = array_map('intval', $ids); @@ -381,6 +200,185 @@ class NetBiblio extends WebService return $books; } + // ********************************** + // * Public methods * + // ********************************** + + public function AddDownloadLog($client, $login, $code) + { + $client = str_replace("'", "", $client); + $login = str_replace("'", "", $login); + $code = ltrim(str_replace("'", "", $code), '0'); + $itemNr = $code . 'V'; + + $sql = "SELECT itemID FROM Items WHERE LTRIM(RTRIM(ItemNr)) = '$itemNr';"; + $result = Connection::execute($sql, false); + if ($row = $result->current()) { + $itemId = $row['itemID']; + } else { + throw new WebException("ItemNotFound", "cannot find item", -1030); + } + + $sql = "SELECT UserAccountID FROM UserAccounts WHERE LTRIM(RTRIM(UserAccountNr)) = '$login';"; + $result = Connection::execute($sql, false); + if ($row = $result->current()) { + $userId = $row['UserAccountID']; + } else { + throw new WebException("UserNotFound", "cannot find user", -1031); + } + + $sql = "SELECT circulationId + FROM OldCirculations + WHERE + useraccountID= $userId AND + itemID = $itemId AND + LTRIM(RTRIM(remark)) = '$client';"; + $result = Connection::execute($sql, false); + + if ($row = $result->current()) { + $id = $row['circulationId']; + $sql = "UPDATE OldCirculations + SET + CheckInDate=GETDATE(), + CheckOutDate=GETDATE() + WHERE circulationID = $id"; + Connection::execute($sql); + return true; + } + + $sql = "SELECT TOP 1 circulationID FROM OldCirculations ORDER BY CirculationID DESC"; + $result = Connection::execute($sql, false); + if ($row = $result->current()) { + $nextId = $row['circulationID'] + 1; + } else { + $nextId = 1; + } + + $sql = "UPDATE Useraccounts + SET + Circulations = Circulations + 1, + TotalCirculations = TotalCirculations + 1 + WHERE UseraccountID = $userId;"; + Connection::execute($sql); + + $sql = "UPDATE Items + SET + Circulations = Circulations + 1, + TotalCirculations = TotalCirculations + 1 + WHERE ItemID = $itemId;"; + Connection::execute($sql); + + $worker_id = Configuration::get('netbiblio_worker_id'); + $sql = "INSERT INTO OldCirculations ( + CirculationID, ItemID, UseraccountID, + Remark, + DueDate, CheckOutDate, CheckInDate, + CheckOutBranchofficeID, CheckOutEmployeeID, CheckInBranchofficeID, CheckInEmployeeID, + Reminders, Renewals, Prereminder, InfoCode, CheckOutSIP2Info, CheckInSIP2Info + ) VALUES ( + $nextId, $itemId, $userId, + '$client', + DATEADD(month, 2, GETDATE()), GETDATE(), GETDATE(), + 2, $worker_id, 2, $worker_id, + 0, 0, 1, '-', 1, 1 + );"; + Connection::execute($sql); + return true; + } + + 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->login = $login; + $this->client = $client; + + return $user->toArray(); + } + + public function Disconnect() + { + $_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"]); + } + + return array(); + } + + public function IsAuthenticated() + { + return $this->getUser()->toArray(); + } + + public function FindAccount($login) + { + return $this->getUser($login)->toArray(); + } + + public function GetWishes() + { + $books = $this->getUser()->getWishes(); + return array_values($this->AddBookData($books)); + } + + public function GetCirculations() + { + $circulations = $this->getUser()->getCirculations(); + return array_values($this->AddBookData($circulations)); + } + + public function GetOldCirculations() + { + $circulations = $this->getUser()->getOldCirculations(); + return array_values($this->AddBookData($circulations)); + } + + public function AddWish($bookNr) + { + return $this->getUser()->addWish($bookNr); + } + + public function DeleteWish($bookNr) + { + $this->getUser()->deleteWish($bookNr); + } + + public function FindBooks($codes) + { + $this->CheckSession(); + + $codes = json_decode($codes); + + // it is faster to do multiple small request to Solr rather than one big so separate + // in chunks if we are above the limit. 15 was found by testing and seems to be a sweet spot + $limit = 15; + if(count($codes) > $limit) { + $parts = array_chunk($codes, $limit); + $books = array(); + foreach($parts as $p) { + $books = array_merge($books, $this->GetBooks($p)); + } + } else { + $books = $this->GetBooks($codes); + } + + return $this->AddBookData($books); + } + public function FindBook($code) { $this->CheckSession();