From a1b43c2c7894b8bd5281b5d6a3684b38dd0dd3b4 Mon Sep 17 00:00:00 2001 From: Guillermo Dev Date: Wed, 10 Oct 2018 01:14:09 +0200 Subject: [PATCH] structured project --- .gitignore | 1 + CHANGELOG.md | 0 CONTRIBUTING.md | 0 LICENSE.md | 0 README.md | 0 User.php | 330 --------------------------- composer.json | 25 +- Connection.php => src/Connection.php | 8 +- DbHelper.php => src/DbHelper.php | 4 +- DbMapping.php => src/DbMapping.php | 4 +- src/InvalidAttributeException.php | 8 + 11 files changed, 27 insertions(+), 353 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.md create mode 100644 README.md delete mode 100644 User.php rename Connection.php => src/Connection.php (98%) rename DbHelper.php => src/DbHelper.php (98%) rename DbMapping.php => src/DbMapping.php (97%) create mode 100644 src/InvalidAttributeException.php diff --git a/.gitignore b/.gitignore index 22d0d82..4de37ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor +*.lock diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/User.php b/User.php deleted file mode 100644 index 2e38b6f..0000000 --- a/User.php +++ /dev/null @@ -1,330 +0,0 @@ - 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, - [UserAccountID] AS id, - REPLACE(UserAccountNr, ' ', '') AS login - FROM [UserAccounts] AS u - LEFT JOIN [Addresses] AS a ON a.[AddressID] = u.[ActualAddressID] - WHERE LTRIM(RTRIM(UserAccountNr)) = '%s' AND disabled = 1 %s AND CategoryCode in ('A', 'M', 'G', 'D', 'I', 'EMP');", - $login, $cond); - - $results = Connection::execute($sql, $raiseError); - return $results->current() !== false ? new User($results->current()) : null; - } - - /** - * Circulations as needed for the BSR internal tools - */ - public function GetCirculations() - { - $sql = sprintf("SELECT - n.NoticeID, - ItemNr, - LTRIM(RTRIM(n.NoticeNr)) AS code, - LTRIM(RTRIM(n.Title)) AS Title, - LTRIM(RTRIM(n.Author)) AS author, - Fields.[300] AS media, - Fields.[901] AS readBy - FROM Circulations AS c - INNER JOIN Items AS i ON i.ItemId = c.ItemId - INNER JOIN Notices AS n ON n.NoticeID = i.NoticeID - LEFT OUTER JOIN ( - SELECT * - FROM ( - SELECT - NoticeID, - Tag AS Field, - NoticeFields.ContentShortPart AS Data - FROM NoticeFields - WHERE Tag IN ('901', '300') - ) AS src - PIVOT ( - MIN(Data) - FOR Field IN ([901], [300]) - ) AS pvt - ) Fields ON n.NoticeID = Fields.NoticeID - WHERE - c.UserAccountID = %s - ORDER BY ItemNr ASC", $this->id); - - $result = Connection::execute($sql); - return $result ? $result->to_array() : array(); - } - - public function GetOldLoansNrs() - { - $sql = sprintf("SELECT - n.NoticeNr - FROM OldCirculations AS c - INNER JOIN Items AS i ON i.ItemId = c.ItemId - INNER JOIN Notices AS n ON n.NoticeID = i.NoticeID - WHERE - c.UserAccountID = %s AND - n.MediaType1Code in ('CDD', 'CDA', 'DVD', 'CDS') AND n.deleted=1 - ORDER BY ItemNr ASC", $this->id); - - $result = Connection::execute($sql); - return $result ? $result->to_array() : array(); - } - - public function getLoansData($table, $sort = "acquisitiondate DESC") - { - $sql = sprintf("SELECT top 50 - realn.NoticeId as NoticeID, - realn.NoticeNr, - CheckOutDate, - c.Remark, - ItemNr - FROM %s AS c - INNER JOIN Items AS i ON i.ItemId = c.ItemId - INNER JOIN Notices AS lentn ON lentn.NoticeID = i.NoticeID - INNER JOIN Notices AS realn ON REPLACE(ltrim(rtrim(lentn.noticenr)), 'V', '') = ltrim(rtrim(realn.noticenr)) - WHERE - c.UserAccountID = %s - ORDER BY %s", $table, $this->id, $sort); - - return Connection::execute($sql)->to_array(); - } - - private function _getLoans($table, $count, $sort) - { - $circulations = $this->getLoansData($table, $sort); - //Logger::log(print_r($circulations, true)); - // getting the intval of the NoticeNr will remove any 'V' or 'T' and thus we will have no issues with - // the virtual books that are used for Downloads and so. - $codes = array_unique(array_map(function($c) { - return trim($c['NoticeNr']); }, $circulations)); - - if($count) { - return count($circulations); - } - - $books = count($codes) > 0 ? BookSearch::GetBooks($codes) : array(); - //Logger::log(print_r($books, true)); - foreach($circulations as $c) { - $id = $c['NoticeID']; - if(isset($books[$id])) { - $books[$id]['checkoutDate'] = $c['CheckOutDate']; - $books[$id]['remark'] = $c['Remark']; - } - } - - return $books; - } - - /** - * Loans (Circulations) as needed on the website - * @param boolean $count return only the count - * @return array - */ - public function GetLoans($count = false) - { - return $this->_getLoans('Circulations', $count, "ItemNr ASC"); - } - - /** - * Old loans (OldCirculations) as needed on the website - * @param boolean $count return only the count - * @return array - */ - public function GetOldLoans($count = false) - { - return $this->_getLoans('OldCirculations', $count, 'CheckOutDate DESC'); - } - - /** - * Books eligible for feedback by the user. Lent or downloaded more than 2 weeks ago and less than 5 monthes. - * @return array - */ - public function GetBooksForFeedback() - { - $sql = sprintf("SELECT n.NoticeNr - FROM OldCirculations AS c - INNER JOIN Items AS i ON i.ItemId = c.ItemId - INNER JOIN Notices AS n ON n.NoticeID = i.NoticeID - WHERE - c.UserAccountID = %s - AND DATEDIFF(month, CheckOutDate, GETDATE()) < 5 - ", $this->id); - - return Connection::execute($sql)->to_array(); } - - /** - * 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 Wishes - (WishID, NoticeID, UserAccountID, CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate) - SELECT %s , NoticeID, %s, GETDATE() , %s , %s , '' , GETDATE() - FROM Notices - WHERE LTRIM(RTRIM(NoticeNr)) = '%s';", - $row['WishID'], $this->id, $employee_id, $library_id, $noticeNr); - - $status = Connection::execute($sql); - return $status && ! $status->is_error() && $status->get_num_rows() > 0; - } - - /** - * 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 boolean $count return only the count - * @param int $limit - * @return array - */ - public function getWishes($count = false, $limit = 200) - { - $sql = sprintf("SELECT TOP $limit - NoticeID, CreationDate - FROM Wishes - WHERE UserAccountID = %s - ORDER BY CreationDate DESC", $this->id); - - $result = Connection::execute($sql); - - $wishList = $result->to_array(); - $ids = array_map(function($r) { return $r['NoticeID']; }, $wishList); - if($count) { - return count($ids); - } - - $books = BookSearch::GetBooks($ids, 'id'); - foreach($wishList as $w) { - $id = $w['NoticeID']; - if(isset($books[$id])) { - $books[$id]['creationDate'] = $w['CreationDate']; - } - } - - $creationDates = array(); - foreach ($books as $key => $book) - { - $creationDates[$key] = $book['creationDate']; - } - array_multisort($creationDates, SORT_DESC, $books); - - return $books; - } - - /** - * Remove a book from the wish list - * @param string $noticeNr - * @return boolean Was the deletion was successful or not ? - */ - public function deleteWish($noticeNr) - { - $sql = sprintf("DELETE w - FROM Wishes AS w - INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID - WHERE - LTRIM(RTRIM(n.NoticeNr)) = '%s' - AND UserAccountID = %s;", $noticeNr, $this->id); - $status = Connection::execute($sql, true); - return $status && ! $status->is_error() && $status->get_num_rows() > 0; - } -} diff --git a/composer.json b/composer.json index 9b39359..781a351 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,6 @@ { - "name" : "bsr/callioapi", - "description" : "Implementation of bsr/webservice as Callioplayer server api", - "repositories" : [ - { - "type" : "vcs", - "url" : "https://usrpath@bitbucket.org/usrpath/webservice.git" - }, - { - "type" : "vcs", - "url" : "https://usrpath@bitbucket.org/usrpath/bsrdb.git" - } - ], + "name" : "bsr/db", + "description" : "Bsr database connection module", "authors" : [ { "name" : "Simon" @@ -19,12 +9,17 @@ "name" : "Guillermo" } ], + "repositories" : [ + { + "type" : "vcs", + "url" : "https://usrpath@bitbucket.org/usrpath/bsrconf.git" + } + ], "require" : { - "bsr/webservice" : "self.version", - "bsr/db" : "self.version" + "bsr/conf" : "self.version" }, "autoload": { - "psr-4": {"BSR\\CallioApi\\" : "src/"} + "psr-4": {"BSR\\Db\\" : "src/"} }, "require-dev": { "pds/skeleton": "^1.0", diff --git a/Connection.php b/src/Connection.php similarity index 98% rename from Connection.php rename to src/Connection.php index be1d6a8..61b2e07 100644 --- a/Connection.php +++ b/src/Connection.php @@ -1,9 +1,9 @@ offsetExists($this->cursor_index); } -} \ No newline at end of file +} diff --git a/DbHelper.php b/src/DbHelper.php similarity index 98% rename from DbHelper.php rename to src/DbHelper.php index abe8eb0..479f5e3 100644 --- a/DbHelper.php +++ b/src/DbHelper.php @@ -1,6 +1,6 @@ to_array()); } -} \ No newline at end of file +} diff --git a/DbMapping.php b/src/DbMapping.php similarity index 97% rename from DbMapping.php rename to src/DbMapping.php index b78c0ab..2fd93f8 100644 --- a/DbMapping.php +++ b/src/DbMapping.php @@ -1,8 +1,8 @@