From 20359c7306d9efcd4b7507546abdd622c97a447d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Tue, 3 Jun 2014 21:27:44 +0200 Subject: [PATCH] Add the number of elements to the tags. re #79 --- tag.php | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/tag.php b/tag.php index 31e909b..d34b991 100644 --- a/tag.php +++ b/tag.php @@ -10,13 +10,17 @@ require_once('base.php'); class tag extends Base { const ALL_TAGS_ID = "cops:tags"; + const TAG_COLUMNS = "tags.id as id, tags.name as name, count(*) as count"; + const SQL_ALL_TAGS = "select {0} from tags, books_tags_link where tags.id = tag group by tags.id, tags.name order by tags.name"; + const SQL_TAGS_FOR_SEARCH = "select {0} from tags, books_tags_link where tags.id = tag group by tags.id, tags.name order by tags.name"; + 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 () { @@ -36,26 +40,13 @@ class tag extends Base { $result = parent::getDb ()->prepare('select id, name from tags where id = ?'); $result->execute (array ($tagId)); if ($post = $result->fetchObject ()) { - return new Tag ($post->id, $post->name); + return new Tag ($post); } return NULL; } public static function getAllTags() { - $result = parent::getDb ()->query('select tags.id as id, tags.name as name, count(*) as count -from tags, books_tags_link -where tags.id = tag -group by tags.id, tags.name -order by tags.name'); - $entryArray = array(); - while ($post = $result->fetchObject ()) - { - $tag = new Tag ($post->id, $post->name); - array_push ($entryArray, new Entry ($tag->name, $tag->getEntryId (), - str_format (localize("bookword", $post->count), $post->count), "text", - array ( new LinkNavigation ($tag->getUri ())))); - } - return $entryArray; + return Base::getEntryArrayWithBookNumber (self::SQL_ALL_TAGS, self::TAG_COLUMNS, array (), "Tag"); } public static function getAllTagsByQuery($query, $n, $database = NULL, $numberPerPage = NULL) { @@ -65,7 +56,7 @@ order by tags.name'); $entryArray = array(); while ($post = $result->fetchObject ()) { - $tag = new Tag ($post->id, $post->name); + $tag = new Tag ($post); array_push ($entryArray, new Entry ($tag->name, $tag->getEntryId (), str_format (localize("bookword", $post->count), $post->count), "text", array ( new LinkNavigation ($tag->getUri ()))));