next()) { if($row['coverdisplay'] == 2) { $row['cover'] = 'http://fichiers.bibliothequesonore.ch:8089/netbiblio/images/covers/Cover'.$row['id'].'_Original.jpg'; } else if(strlen($row['cover']) > 0) { $row['cover'] = 'http://ecx.images-amazon.com/images/I/'.$row['cover'].'._SL320_.jpg'; } $books[] = $raw ? $row : new AudioBook($row); } return $multiple ? $books : reset($books); } /** * @param $code * @return int * @throws Exception * @throws SqlException */ public static function findIdByCode($code) { $sql = "SELECT NoticeId FROM Notices WHERE LTRIM(RTRIM(NoticeNr)) = '$code';"; $result = Connection::execute($sql, false); if ($result === false || $result->length == 0) { throw new Exception("NotFoundException"); } $row = $result->current(); return $row['NoticeId']; } /** * Retrieve the last books for each type. If 'type' is an empty string, then * it returns all types. */ public static function lastBooksByType($type, $itemsByGroup = 10) { $type_before_escape = $type; $type = Connection::escape(utf8_decode($type)); if ($type_before_escape == "Jeunesse") { if ($itemsByGroup < 20) $itemsByGroup = 20; $sqlQuery = " SELECT top " . $itemsByGroup . " Notices.NoticeId AS id, "; $sqlQuery .= " Notices.title AS title, "; $sqlQuery .= " Notices.author AS author, "; $sqlQuery .= " LTRIM(RTRIM(Notices.NoticeNr)) AS code, "; $sqlQuery .= " 'Jeunesse' AS type, 0 as score, "; $sqlQuery .= " convert(varchar, CreationDate, 102) as date "; $sqlQuery .= " FROM Notices "; $sqlQuery .= " WHERE Notices.NoticeNr NOT LIKE '%~%' "; $sqlQuery .= " AND Notices.NoticeNr NOT LIKE '%V%' "; $sqlQuery .= " AND Notices.NoticeNr NOT LIKE '%T%' "; $sqlQuery .= " AND Notices.MediaType1Code = 'CDD' "; $sqlQuery .= " AND Notices.Visible = 1 "; $sqlQuery .= " AND Notices.AgeCode in ('E', 'J') "; $sqlQuery .= " ORDER BY date DESC;"; } else { $sqlQuery = "WITH cte AS ( "; $sqlQuery .= " SELECT Notices.NoticeId AS id, "; $sqlQuery .= " Notices.title AS title, "; $sqlQuery .= " Notices.author AS author, "; $sqlQuery .= " LTRIM(RTRIM(Notices.NoticeNr)) AS code, "; $sqlQuery .= " Codes.TextFre AS type, "; $sqlQuery .= " convert(varchar, CreationDate, 102) as date, "; $sqlQuery .= " rank() OVER ( "; $sqlQuery .= " PARTITION BY Codes.TextFre "; $sqlQuery .= " ORDER BY cast(LTRIM(RTRIM(Notices.NoticeNr)) as int) DESC "; $sqlQuery .= " ) AS num "; $sqlQuery .= " FROM Notices "; $sqlQuery .= " INNER JOIN Codes "; $sqlQuery .= " ON Notices.MediaType2Code = Codes.Code "; $sqlQuery .= " WHERE Codes.Type = 2 "; if (strlen($type)) $sqlQuery .= " AND Codes.TextFre = " . $type . " "; $sqlQuery .= " AND Notices.NoticeNr NOT LIKE '%~%' "; $sqlQuery .= " AND Notices.NoticeNr NOT LIKE '%V%' "; $sqlQuery .= " AND Notices.NoticeNr NOT LIKE '%T%' "; $sqlQuery .= " AND Notices.MediaType1Code = 'CDD' "; $sqlQuery .= " AND Notices.Visible = 1 "; $sqlQuery .= " GROUP BY Codes.TextFre, "; $sqlQuery .= " Notices.NoticeNr, "; $sqlQuery .= " Notices.NoticeId, "; $sqlQuery .= " Notices.title, "; $sqlQuery .= " Notices.author, "; $sqlQuery .= " Notices.CreationDate "; $sqlQuery .= ") "; $sqlQuery .= "SELECT id, title, author, code, type, 0 as score, date "; $sqlQuery .= "FROM cte "; $sqlQuery .= "WHERE num <= " . intval($itemsByGroup) . " "; $sqlQuery .= "ORDER BY date DESC;"; } $resultSet = Connection::execute($sqlQuery); $result = array(); while (($row = $resultSet->next())) $result[] = new AudioBook($row, TRUE); return $result; } /** * Retrieve the list of all readers (volunteers) having read at least 4 books (2 notices per book). * Returns an associative array containing $lastname and $firstname */ public static function listOfReaders() { $sql = "SELECT count(*), ContentShortPart AS name FROM noticefields WHERE Tag=901 GROUP BY ContentShortPart HAVING count(*) > 6 ORDER BY SUBSTRING(ContentShortPart, CHARINDEX(' ', ContentShortPart)+1, 15);"; $results = Connection::execute($sql); return array_map(function($row) { $fullname = str_replace("*", "", $row['name']); $parts = explode(" ", $fullname); $firstname = array_shift($parts); $lastname = implode(" ", $parts); return array( 'lastname' => $lastname, 'firstname' => $firstname); }, $results->to_array()); } /** * Retrieve the list of all categories */ public static function listOfCategories() { $sql = "SELECT LTRIM(RTRIM(Code)) as code, TextFre AS text FROM Codes WHERE type=2 AND Code!='-' ORDER BY TextFre;"; $results = Connection::execute($sql); return array_map(function($row) { return array( 'code' => $row['code'], 'text' => $row['text'], ); }, $results->to_array()); } /** * Retrieve the list of all type available in the database. */ public static function listOfTypes() { $sql = "SELECT DISTINCT Codes.TextFre AS type FROM Codes INNER JOIN Notices ON Codes.Code = Notices.MediaType2Code WHERE Codes.Type = 2 AND Notices.NoticeNr NOT LIKE '%~%' AND Notices.NoticeNr NOT LIKE '%V%' AND Notices.NoticeNr NOT LIKE '%T%' AND Notices.MediaType1Code = 'CDD';"; $results = Connection::execute($sql); $results = array_map(function($r) { return $r['type']; }, $results->to_array()); array_unshift($results, "Jeunesse"); return $results; } /** * Retrieve the list of all books currently lended to readers. */ public static function inReading() { $sql = "SELECT noticenr, title, author, displayName FROM notices, items, circulations, useraccounts WHERE mediatype1code='N' and NoticeNr not like '%~%' AND items.noticeid = notices.noticeid AND items.ItemID=circulations.ItemID AND useraccounts.useraccountid=circulations.useraccountid ORDER BY author, title;"; $results = Connection::execute($sql); return array_map(function($row) { return array( "noticenr" => $row['noticenr'], "auteur" => $row['author'], "titre" => $row['title'], "lecteur" => $row['displayName'] ); }, $results->to_array()); } public function to_array() { if (!$this->loaded) { $this->loadDetails(); } return parent::to_array(); } public function __set($name, $value) { if ($name == 'code' && is_string($value)) { $value = preg_replace('/[~a-zA-Z]/', '', $value); } parent::__set($name, $value); } }