Skip to content

Commit

Permalink
Always return both regular and sort form of author name and book title
Browse files Browse the repository at this point in the history
This way sorting can be done independently from the format chosen for display

Apart from affected files, also changed bookTest.php so it knows which is the sortform and to test for errors.
PHPunit test did not reveal errors but complained about missing methods, so probably a version mismatch with PHPUnit used to make the original test
```
ReflectionException: Method suite does not exist in phar://E:/XAMPP/pear/PHPUnit38.phar/phpunit/Runner/BaseTestRunner.php on line 113

Call Stack:
    1.0311     375848   1. {main}() E:\XAMPP\pear\PHPUnit38.phar:0
    1.0331     566872   2. PHPUnit_TextUI_Command::main() E:\XAMPP\pear\PHPUnit38.phar:614
    1.0331     570280   3. PHPUnit_TextUI_Command->run() phar://E:/XAMPP/pear/PHPUnit38.phar/phpunit/TextUI/Command.php:129
    1.0371     749464   4. PHPUnit_Runner_BaseTestRunner->getTest() phar://E:/XAMPP/pear/PHPUnit38.phar/phpunit/TextUI/Command.php:150
    1.0631    2471312   5. ReflectionClass->getMethod() phar://E:/XAMPP/pear/PHPUnit38.phar/phpunit/Runner/BaseTestRunner.php:113
```

TODO: make it so series indices are sorted numerically instead of alphanumeric.
  • Loading branch information
At-Libitum committed Dec 17, 2013
1 parent a099a5a commit 184ca88
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
14 changes: 8 additions & 6 deletions author.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class Author extends Base {
public $name;
public $sort;

public function __construct($pid, $pname) {
public function __construct($pid, $pname, $psort) {
$this->id = $pid;
$this->name = $pname;
$this->sort = $psort;
}

public function getUri () {
Expand Down Expand Up @@ -72,7 +73,7 @@ public static function getEntryArray ($query, $params) {
$entryArray = array();
while ($post = $result->fetchObject ())
{
$author = new Author ($post->id, $post->sort);
$author = new Author ($post->id, $post->name, $post->sort);
array_push ($entryArray, new Entry ($post->sort, $author->getEntryId (),
str_format (localize("bookword", $post->count), $post->count), "text",
array ( new LinkNavigation ($author->getUri ()))));
Expand All @@ -81,20 +82,21 @@ public static function getEntryArray ($query, $params) {
}

public static function getAuthorById ($authorId) {
$result = parent::getDb ()->prepare('select sort from authors where id = ?');
$result = parent::getDb ()->prepare('select name, sort from authors where id = ?');
$result->execute (array ($authorId));
return new Author ($authorId, $result->fetchColumn ());
$post = $result->fetchObject ();
return new Author ($authorId, $post->name, $post->sort );
}

public static function getAuthorByBookId ($bookId) {
$result = parent::getDb ()->prepare('select authors.id as id, authors.sort as sort
$result = parent::getDb ()->prepare('select authors.id as id, authors.name, authors.sort as sort
from authors, books_authors_link
where author = authors.id
and book = ?');
$result->execute (array ($bookId));
$authorArray = array ();
while ($post = $result->fetchObject ()) {
array_push ($authorArray, new Author ($post->id, $post->sort));
array_push ($authorArray, new Author ($post->id, $post->name, $post->sort ));
}
return $authorArray;
}
Expand Down
32 changes: 21 additions & 11 deletions book.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require_once('publisher.php');
require_once('tag.php');
require_once('language.php');
require_once("customcolumn.php");
require_once('customcolumn.php');
require_once('data.php');
require_once('resources/php-epub-meta/epub.php');

Expand Down Expand Up @@ -49,7 +49,7 @@ class Book extends Base {
const ALL_BOOKS_UUID = "urn:uuid";
const ALL_BOOKS_ID = "cops:books";
const ALL_RECENT_BOOKS_ID = "cops:recentbooks";
const BOOK_COLUMNS = "books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating";
const BOOK_COLUMNS = "books.id as id, books.title as title, books.sort as titlesort, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating";

const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN;
const SQL_BOOKS_ALL = SQL_BOOKS_ALL;
Expand All @@ -67,6 +67,7 @@ class Book extends Base {

public $id;
public $title;
public $titleSort;
public $timestamp;
public $pubdate;
public $path;
Expand All @@ -88,6 +89,7 @@ class Book extends Base {
public function __construct($line) {
$this->id = $line->id;
$this->title = $line->title;
$this->titleSort = $line->titlesort;
$this->timestamp = strtotime ($line->timestamp);
$this->pubdate = strtotime ($line->pubdate);
$this->path = Base::getDbDirectory () . $line->path;
Expand Down Expand Up @@ -127,6 +129,7 @@ public function getContentArray () {
array_push ($preferedData, array ("url" => $data->getHtmlLink (), "name" => $format));
}
}
$nameArray = $this->getAuthorNames ();

$publisher = $this->getPublisher();
if (is_null ($publisher)) {
Expand Down Expand Up @@ -158,12 +161,14 @@ public function getContentArray () {
"publisherurl" => $pu,
"pubDate" => $this->getPubDate (),
"languagesName" => $this->getLanguages (),
"authorsName" => $this->getAuthorsName (),
"authorsName" => $nameArray[0],
"authorsSort" => $nameArray[1],
"tagsName" => $this->getTagsName (),
"seriesName" => $sn,
"seriesIndex" => $this->seriesIndex,
"seriesCompleteName" => $scn,
"seriesurl" => $su);
"seriesurl" => $su,
"titleSort" => $this->titleSort);

}
public function getFullContentArray () {
Expand Down Expand Up @@ -192,7 +197,6 @@ public function getFullContentArray () {
$link = new LinkNavigation ($tag->getUri ());
array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
}
;
return $out;
}

Expand All @@ -205,6 +209,9 @@ public function getDetailUrl ($permalink = false) {
public function getTitle () {
return $this->title;
}
public function getTitleSort () {
return $this->titleSort;
}

public function getAuthors () {
if (is_null ($this->authors)) {
Expand Down Expand Up @@ -232,8 +239,10 @@ public static function getFilterString () {
return "and " . $result;
}

public function getAuthorsName () {
return implode (", ", array_map (function ($author) { return $author->name; }, $this->getAuthors ()));
// return both forms in one call and use & as separator.
public function getAuthorNames () {
return array ( implode (" & ", array_map (function ($author) { return $author->name; }, $this->getAuthors ())),
implode (" & ", array_map (function ($author) { return $author->sort; }, $this->getAuthors ())));
}

public function getPublisher () {
Expand Down Expand Up @@ -294,9 +303,9 @@ public function getDatas ()

while ($post = $result->fetchObject ())
{
array_push ($this->datas, new Data ($post, $this));
array_push ($this->datas, new Data ($post, $this));
}
}
}
return $this->datas;
}

Expand Down Expand Up @@ -415,6 +424,7 @@ public function getUpdatedEpub ($idData)

$epub->Title ($this->title);
$authorArray = array ();
// unlucky choice to use 'name' for the authorsort form.
foreach ($this->getAuthors() as $author) {
$authorArray [$author->sort] = $author->name;
}
Expand Down Expand Up @@ -608,11 +618,11 @@ public static function getBooksByQuery($query, $n, $database = NULL, $numberPerP
}
else {
if (array_key_exists ($key, $query)) {
$critArray [$i] = $query [$key];
$critArray [$i] = $query [$key];
} else {
$critArray [$i] = $query ["all"];
}
}
}
$i++;
}
return self::getEntryArray (self::SQL_BOOKS_QUERY, $critArray, $n, $database, $numberPerPage);
Expand Down
3 changes: 2 additions & 1 deletion data.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function getFilename () {
}

public function getUpdatedFilename () {
return $this->book->getAuthorsName () . " - " . $this->book->title;
$nameArray = $this->book->getAuthorNames ();
return $nameArray[1] . " - " . $this->book->title;
}

public function getUpdatedFilenameEpub () {
Expand Down
4 changes: 2 additions & 2 deletions sendtomail.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
if (empty ($emailAddress)) { continue; }
$mail->AddAddress($emailAddress);
}

$Authors = $book->getAuthorNames ();
$mail->AddAttachment($data->getLocalPath ());

$mail->IsHTML(true);
$mail->Subject = 'Sent by COPS : ' . $data->getUpdatedFilename ();
$mail->Body = "<h1>" . $book->title . "</h1><h2>" . $book->getAuthorsName () . "</h2>" . $book->getComment ();
$mail->Body = "<h1>" . $book->title . "</h1><h2>" . $Authors[1] . "</h2>" . $book->getComment ();
$mail->AltBody = "Sent by COPS";

if (!$mail->Send()) {
Expand Down
11 changes: 11 additions & 0 deletions styles/style-base.css
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ padding:0;
border:0;
}

/*
titlesort h2 span .oksort
title h2 span .nosort
authorsort div span .oksort
authorname div span .nosort
*/

h2 > .oksort,
div > .nosort {
display:none;
}
/* =============================================================================
Main container stuff goes here and other globals
========================================================================== */
Expand Down
6 changes: 4 additions & 2 deletions templates/default/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ <h2 class="download">
</h2>
<a class="fancydetail" href="{{=str_format (it.c.url.detailUrl, entry.book.id, it.databaseId)}}">
<div class="fullclickpopup">
<h2><span class="st">{{=htmlspecialchars (entry.title)}}</span>
<h2><span class="nosort" title="{{=htmlspecialchars (entry.title)}}">{{=htmlspecialchars (entry.title)}}</span>
<span class="oksort st" title="{{=htmlspecialchars (entry.book.titleSort)}}">{{=htmlspecialchars (entry.book.titleSort)}}</span>
{{? entry.book.pubDate != ""}}<span class="sp">({{=entry.book.pubDate}})</span>{{?}}
{{? entry.book.rating != ""}}<span class="sr">{{=entry.book.rating}}</span>{{?}}
</h2>
<h4>{{=it.c.i18n.authorsTitle}} : </h4><span class="sa">{{=htmlspecialchars (entry.book.authorsName)}}</span><br />
<h4>{{=it.c.i18n.authorsTitle}} : </h4><span class="nosort">{{=htmlspecialchars (entry.book.authorsName)}}</span>
<span class="oksort sa">{{=htmlspecialchars (entry.book.authorsSort)}}</span><br />
{{? entry.book.tagsName != ""}}<h4>{{=it.c.i18n.tagsTitle}} : </h4><span class="se">{{=htmlspecialchars (entry.book.tagsName)}}</span><br />{{?}}
{{? entry.book.seriesName != ""}}<h4>{{=it.c.i18n.seriesTitle}} : </h4><span class="ss">{{=htmlspecialchars (entry.book.seriesName)}} ({{=entry.book.seriesIndex}})</span><br />{{?}}
</div></a>
Expand Down
5 changes: 4 additions & 1 deletion test/bookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,17 @@ public function testGetBookById ()
{
// also check most of book's class methods
$book = Book::getBookById(2);
$authorname = $book->getAuthorNames ();

$linkArray = $book->getLinkArray ();
$this->assertCount (5, $linkArray);

$this->assertEquals ("The Return of Sherlock Holmes", $book->getTitle ());
$this->assertEquals ("Return of Sherlock Holmes, The", $book->getTitleSort ());
$this->assertEquals ("urn:uuid:87ddbdeb-1e27-4d06-b79b-4b2a3bfc6a5f", $book->getEntryId ());
$this->assertEquals ("index.php?page=13&id=2", $book->getDetailUrl ());
$this->assertEquals ("Doyle, Arthur Conan", $book->getAuthorsName ());
$this->assertEquals ("Arthur Conan Doyle", $authorname[0]);
$this->assertEquals ("Doyle, Arthur Conan", $authorname[1]);
$this->assertEquals ("Fiction, Mystery & Detective, Short Stories", $book->getTagsName ());
$this->assertEquals ('<p class="description">The Return of Sherlock Holmes is a collection of 13 Sherlock Holmes stories, originally published in 1903-1904, by Arthur Conan Doyle.<br />The book was first published on March 7, 1905 by Georges Newnes, Ltd and in a Colonial edition by Longmans. 30,000 copies were made of the initial print run. The US edition by McClure, Phillips &amp; Co. added another 28,000 to the run.<br />This was the first Holmes collection since 1893, when Holmes had "died" in "The Adventure of the Final Problem". Having published The Hound of the Baskervilles in 1901–1902 (although setting it before Holmes\' death) Doyle came under intense pressure to revive his famous character.</p>', $book->getComment (false));
$this->assertEquals ("English", $book->getLanguages ());
Expand Down

1 comment on commit 184ca88

@At-Libitum
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! false alarm on the PHPUnit thing. xdebug.show_exception_trace got zapped in the upgrade to 5.5.6, restored and all is well.

Please sign in to comment.