From 270c2817418b166347e4c4680eed6d9574ffb665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Mon, 2 Jun 2014 21:31:55 +0200 Subject: [PATCH] Centralize a little the code Should be easier to add the count of element. re #79 --- author.php | 23 +++++++---------------- base.php | 13 +++++++++++++ serie.php | 44 ++++++++++---------------------------------- 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/author.php b/author.php index 7dc8927..ff60ff4 100644 --- a/author.php +++ b/author.php @@ -20,10 +20,10 @@ class Author extends Base { public $name; public $sort; - public function __construct($pid, $pname, $psort) { - $this->id = $pid; - $this->name = str_replace("|", ",", $pname); - $this->sort = $psort; + public function __construct($post) { + $this->id = $post->id; + $this->name = str_replace("|", ",", $post->name); + $this->sort = $post->sort; } public function getUri () { @@ -71,23 +71,14 @@ order by substr (upper (sort), 1, 1)", "substr (upper (sort), 1, 1) as title, co } public static function getEntryArray ($query, $params) { - list (, $result) = parent::executeQuery ($query, self::AUTHOR_COLUMNS, "", $params, -1); - $entryArray = array(); - while ($post = $result->fetchObject ()) - { - $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 ())), "", $post->count)); - } - return $entryArray; + return Base::getEntryArrayWithBookNumber ($query, self::AUTHOR_COLUMNS, $params, "Author"); } public static function getAuthorById ($authorId) { $result = parent::getDb ()->prepare('select ' . self::AUTHOR_COLUMNS . ' from authors where id = ?'); $result->execute (array ($authorId)); $post = $result->fetchObject (); - return new Author ($post->id, $post->name, $post->sort); + return new Author ($post); } public static function getAuthorByBookId ($bookId) { @@ -97,7 +88,7 @@ and book = ?'); $result->execute (array ($bookId)); $authorArray = array (); while ($post = $result->fetchObject ()) { - array_push ($authorArray, new Author ($post->id, $post->name, $post->sort)); + array_push ($authorArray, new Author ($post)); } return $authorArray; } diff --git a/base.php b/base.php index 6aba04b..7a20692 100644 --- a/base.php +++ b/base.php @@ -1266,6 +1266,19 @@ abstract class Base return $entry; } + public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) { + list (, $result) = self::executeQuery ($query, $columns, "", $params, -1); + $entryArray = array(); + while ($post = $result->fetchObject ()) + { + $instance = new $category ($post); + array_push ($entryArray, new Entry ($post->sort, $instance->getEntryId (), + str_format (localize("bookword", $post->count), $post->count), "text", + array ( new LinkNavigation ($instance->getUri ())), "", $post->count)); + } + return $entryArray; + } + public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) { $totalResult = -1; diff --git a/serie.php b/serie.php index 7d7e18a..bd5ad83 100644 --- a/serie.php +++ b/serie.php @@ -10,13 +10,16 @@ require_once('base.php'); class Serie extends Base { const ALL_SERIES_ID = "cops:series"; + const SERIES_COLUMNS = "series.id as id, series.name as name, series.sort as sort, count(*) as count"; + const SQL_ALL_SERIES = "select {0} from series, books_series_link where series.id = series group by series.id, series.name, series.sort order by series.sort"; + const SQL_SERIES_FOR_SEARCH = "select {0} from series, books_series_link where series.id = series and upper (series.name) like ? group by series.id, series.name, series.sort order by series.sort"; public $id; public $name; - public function __construct($pid, $pname) { - $this->id = $pid; - $this->name = $pname; + public function __construct($post) { + $this->id = $post->id; + $this->name = $post->name; } public function getUri () { @@ -38,7 +41,7 @@ from books_series_link, series where series.id = series and book = ?'); $result->execute (array ($bookId)); if ($post = $result->fetchObject ()) { - return new Serie ($post->id, $post->name); + return new Serie ($post); } return NULL; } @@ -47,43 +50,16 @@ where series.id = series and book = ?'); $result = parent::getDb ()->prepare('select id, name from series where id = ?'); $result->execute (array ($serieId)); if ($post = $result->fetchObject ()) { - return new Serie ($post->id, $post->name); + return new Serie ($post); } return NULL; } public static function getAllSeries() { - $result = parent::getDb ()->query('select series.id as id, series.name as name, series.sort as sort, count(*) as count -from series, books_series_link -where series.id = series -group by series.id, series.name, series.sort -order by series.sort'); - $entryArray = array(); - while ($post = $result->fetchObject ()) - { - $serie = new Serie ($post->id, $post->sort); - array_push ($entryArray, new Entry ($serie->name, $serie->getEntryId (), - str_format (localize("bookword", $post->count), $post->count), "text", - array ( new LinkNavigation ($serie->getUri ())))); - } - return $entryArray; + return Base::getEntryArrayWithBookNumber (self::SQL_ALL_SERIES, self::SERIES_COLUMNS, array (), "Serie"); } public static function getAllSeriesByQuery($query) { - $columns = "series.id as id, series.name as name, series.sort as sort, count(*) as count"; - $sql = 'select {0} from series, books_series_link -where series.id = series and upper (series.name) like ? -group by series.id, series.name, series.sort -order by series.sort'; - list (, $result) = parent::executeQuery ($sql, $columns, "", array ('%' . $query . '%'), -1); - $entryArray = array(); - while ($post = $result->fetchObject ()) - { - $serie = new Serie ($post->id, $post->sort); - array_push ($entryArray, new Entry ($serie->name, $serie->getEntryId (), - str_format (localize("bookword", $post->count), $post->count), "text", - array ( new LinkNavigation ($serie->getUri ())))); - } - return $entryArray; + return Base::getEntryArrayWithBookNumber (self::SQL_SERIES_FOR_SEARCH, self::SERIES_COLUMNS, array ('%' . $query . '%'), "Serie"); } }