0) { $cond = " AND $cond"; } $sql = sprintf("SELECT TOP 1 [FirstName] AS firstName, [LastName] AS lastName, [DisplayName] AS displayName, [UserDefined1] AS freeOne, [ActualAddressID] AS addressId, [Email] AS mail, [TelephoneMobile] AS mobilePhone, [TelephonePrivate] AS privatePhone, [Telephone] AS officePhone, [%s] AS id, REPLACE(UseraccountNr, ' ', '') AS login FROM [%s] AS u LEFT JOIN [%s] AS a ON a.[%s] = u.[ActualAddressID] WHERE REPLACE(UseraccountNr, ' ', '') = '%s' AND disabled = 1 %s;", self::$idColumn, self::$tableName, self::$addressTableName, self::$addressIdColumn, $login, $cond); $results = Connection::execute($sql, $raiseError); return $results->current() !== false ? new User($results->current()) : null; } private function _getCirculations($table, $sort = "ItemNr ASC") { $sql = sprintf("SELECT NoticeID, CheckOutDate, ItemNr FROM %s AS c INNER JOIN Items AS i ON i.ItemId = c.ItemId WHERE c.UseraccountId = %s ORDER BY %s", $table, $this->id, $sort); $result = Connection::execute($sql); $circulations = $result->to_array(); $ids = array_map(function($c) { return $c['NoticeID']; }, $circulations); $books = BookSearch::GetBooks($ids, 'id'); foreach($circulations as $c) { $books[$c['NoticeID']]['date'] = $c['CheckOutDate']; $books[$c['NoticeID']]['itemNr'] = $c['ItemNr']; } return $books; } public function getCirculations() { return $this->_getCirculations('Circulations'); } public function getOldCirculations() { return $this->_getCirculations('OldCirculations', 'CheckOutDate DESC'); } /** * Add a book to the wish list if it is not already inside. * @param string $noticeNr * @return bool */ public function addWish($noticeNr) { if ($this->hasWish($noticeNr)) { return false; } $sql = "UPDATE Counters SET WishID = WishID + 1 OUTPUT INSERTED.WishID;"; $result = Connection::execute($sql, true); $row = $result->current(); $employee_id = Configuration::get('www_employee_id'); $library_id = Configuration::get('www_library_id'); $sql = sprintf("INSERT INTO %s (WishID, NoticeID, %s, CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate) SELECT %s , NoticeID, %s, GETDATE() , %s , %s , '' , GETDATE() FROM Notices WHERE LTRIM(RTRIM(NoticeNr)) = '%s';", User::$wishTableName, User::$idColumn, $row['WishID'], $this->id, $employee_id, $library_id, $noticeNr); Connection::execute($sql); return true; } /** * Return true if the book is in the wish list * @param string $noticeNr * @return bool */ private function hasWish($noticeNr) { $sql = sprintf("SELECT w.NoticeID FROM Wishes AS w INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID WHERE LTRIM(RTRIM(n.NoticeNr)) = '%s' AND w.UseraccountId = %s;", $noticeNr, $this->id); $result = Connection::execute($sql); return $result->current() !== false; } /** * Wishes are all the books that this user want to read. * @param int $limit * @return array */ public function getWishes($limit = 50) { $sql = sprintf("SELECT TOP $limit NoticeID FROM %s WHERE %s = %s ORDER BY CreationDate DESC" ,User::$wishTableName, User::$idColumn, $this->id); $result = Connection::execute($sql); $ids = array_map(function($r) { return $r['NoticeID']; }, $result->to_array()); return BookSearch::GetBooks($ids, 'id'); } /** * Remove a book from the wish list * @param string $noticeNr */ public function deleteWish($noticeNr) { $sql = sprintf("DELETE w FROM %s AS w INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID WHERE LTRIM(RTRIM(n.NoticeNr)) = '%s' AND %s = %s;", User::$wishTableName, $noticeNr, User::$idColumn, $this->id); Connection::execute($sql, true); } }