| <?php |
| |
| include ("books_xml.php"); |
| |
| function get_books() { |
| return library_to_html(); |
| } |
| |
| function library_to_html() { |
| $html = ""; |
| $library = get_library(); |
| $library->sort_by($_GET['sort']); |
| $library->to_html($html); |
| return $html; |
| } |
| |
| class Library { |
| var $books; |
| |
| function Library() { |
| $this->books = array (); |
| } |
| |
| function add_book(& $book) { |
| $this->books[] = $book; |
| } |
| |
| function to_html(& $html) { |
| $html .= "<div class=\"homeitem3col\">"; |
| $html .= "<h3>Books</h3>"; |
| $html .= "<ul>"; |
| foreach ($this->books as $book) { |
| $book->to_html($html); |
| } |
| $html .= "</ul>"; |
| $html .= "</div>"; |
| } |
| |
| function sort_by($field) { |
| if (!in_array($field, array("title", "date", "author"))) |
| $field = "date"; |
| usort($this->books, "sort_by_" . $field . "_cmp"); |
| } |
| } |
| |
| function sort_by_title_cmp($a, $b) { |
| $a_title = as_sortable_title($a->title); |
| $b_title = as_sortable_title($b->title); |
| return strcasecmp($a_title, $b_title); |
| } |
| |
| function as_sortable_title($title) { |
| $title = trim($title); |
| if (strcasecmp(substr($title, 0, 3), "the") == 0) { |
| $title = substr($title, 3); |
| } |
| return trim($title); |
| |
| } |
| |
| function sort_by_date_cmp($a, $b) { |
| if ($a->date == $b->date) |
| return 0; |
| return $a->date > $b->date ? -1 : 1; |
| } |
| |
| function sort_by_author_cmp($a, $b) { |
| $a_author = $a->authors[0]; |
| $b_author = $b->authors[0]; |
| $cmp = strcasecmp($a_author, $b_author); |
| if ($cmp == 0) return sort_by_date_cmp($a, $b); |
| return $cmp; |
| } |
| |
| class Book { |
| var $title; |
| var $authors; |
| var $date; |
| var $url; |
| var $description; |
| |
| function Book() { |
| $this->authors = array (); |
| } |
| |
| function add_author(& $author) { |
| $this->authors[] = $author; |
| } |
| |
| // Render the book as html. |
| function to_html(& $html) { |
| $html .= "<li><b><a href=\"$this->url\" target=\"_blank\">$this->title</a></b>"; |
| |
| // Get the collection of authors and render them. |
| $authors = $this->authors_to_html($this->authors); |
| if ($authors) { |
| $html .= "<br>"; |
| $html .= str_replace("[uuml]", "ü", $authors); |
| } |
| if ($this->date) { |
| $html .= "<br>"; |
| $html .= date("F Y", $this->date); |
| } |
| |
| $description = $this->description; |
| if ($description) |
| $html .= "<blockquote>$description</blockquote>"; |
| } |
| |
| function authors_to_html($authors) { |
| $count = count($authors); |
| |
| if ($count == 0) |
| return; |
| |
| $html = ' by '; |
| $html .= author_to_html($authors[0]); |
| |
| // Print each of the authors, excluding the first and the last |
| for ($index = 1; $index < $count -1; $index ++) { |
| $html .= ", "; |
| $html .= author_to_html($authors[$index]); |
| } |
| |
| // If there are more than two authors, then we need to separate |
| // the last one from the second last one with a comma. |
| if ($count > 2) { |
| $html .= ","; |
| } |
| |
| // If there was more than one author, then print the last author's |
| // information |
| if ($count > 1) { |
| $author = $authors[$count -1]; |
| if (strlen($author) == 0) // empty author means "et al." |
| $html .= " et al."; |
| else { |
| $html .= " and "; |
| $html .= author_to_html($author); |
| } |
| } |
| |
| return $html; |
| } |
| } |
| |
| function author_to_html($author) { |
| $parts = explode(",", $author); |
| if (count($parts) == 1) |
| return $author; |
| |
| return trim($parts[1])." ".trim($parts[0]); |
| } |
| |
| |
| ?> |
| |