|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* COPS (Calibre OPDS PHP Server) class file |
|
4
|
|
|
* |
|
5
|
|
|
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
|
6
|
|
|
* @author Sébastien Lucas <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
class CustomColumnTypeRating extends CustomColumnType |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
1 |
|
protected function __construct($pcustomId) |
|
12
|
|
|
{ |
|
13
|
1 |
|
parent::__construct($pcustomId, self::CUSTOM_TYPE_RATING); |
|
14
|
1 |
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get the name of the sqlite table for this column |
|
18
|
|
|
* |
|
19
|
|
|
* @return string|null |
|
20
|
|
|
*/ |
|
21
|
5 |
|
private function getTableName() |
|
22
|
|
|
{ |
|
23
|
5 |
|
return "custom_column_{$this->customId}"; |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the name of the linking sqlite table for this column |
|
28
|
|
|
* (or NULL if there is no linktable) |
|
29
|
|
|
* |
|
30
|
|
|
* @return string|null |
|
31
|
|
|
*/ |
|
32
|
5 |
|
private function getTableLinkName() |
|
33
|
|
|
{ |
|
34
|
5 |
|
return "books_custom_column_{$this->customId}_link"; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get the name of the linking column in the linktable |
|
39
|
|
|
* |
|
40
|
|
|
* @return string|null |
|
41
|
|
|
*/ |
|
42
|
4 |
|
private function getTableLinkColumn() |
|
43
|
|
|
{ |
|
44
|
4 |
|
return "value"; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function getQuery($id) |
|
48
|
|
|
{ |
|
49
|
2 |
|
if ($id == 0) { |
|
50
|
2 |
|
$query = str_format(Book::SQL_BOOKS_BY_CUSTOM_RATING_NULL, "{0}", "{1}", $this->getTableLinkName(), $this->getTableName(), $this->getTableLinkColumn()); |
|
|
|
|
|
|
51
|
2 |
|
return array($query, array()); |
|
52
|
|
|
} else { |
|
53
|
2 |
|
$query = str_format(Book::SQL_BOOKS_BY_CUSTOM_RATING, "{0}", "{1}", $this->getTableLinkName(), $this->getTableName(), $this->getTableLinkColumn()); |
|
|
|
|
|
|
54
|
2 |
|
return array($query, array($id)); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function getCustom($id) |
|
59
|
|
|
{ |
|
60
|
2 |
|
return new CustomColumn ($id, str_format(localize("customcolumn.stars", $id / 2), $id / 2), $this); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
protected function getAllCustomValuesFromDatabase() |
|
64
|
|
|
{ |
|
65
|
1 |
|
$queryFormat = "SELECT coalesce({0}.value, 0) AS value, count(*) AS count FROM books LEFT JOIN {1} ON books.id = {1}.book LEFT JOIN {0} ON {0}.id = {1}.value GROUP BY coalesce({0}.value, -1)"; |
|
|
|
|
|
|
66
|
1 |
|
$query = str_format($queryFormat, $this->getTableName(), $this->getTableLinkName()); |
|
67
|
1 |
|
$result = Base::getDb()->query($query); |
|
68
|
|
|
|
|
69
|
1 |
|
$countArray = array(0 => 0, 2 => 0, 4 => 0, 6 => 0, 8 => 0, 10 => 0); |
|
70
|
1 |
|
while ($row = $result->fetchObject()) { |
|
71
|
1 |
|
$countArray[$row->value] = $row->count; |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$entryArray = array(); |
|
75
|
|
|
|
|
76
|
1 |
|
for ($i = 0; $i <= 5; $i++) { |
|
77
|
1 |
|
$count = $countArray[$i * 2]; |
|
78
|
1 |
|
$name = str_format(localize("customcolumn.stars", $i), $i); |
|
|
|
|
|
|
79
|
1 |
|
$entryid = $this->getEntryId($i * 2); |
|
80
|
1 |
|
$content = str_format(localize("bookword", $count), $count); |
|
|
|
|
|
|
81
|
1 |
|
$linkarray = array(new LinkNavigation($this->getUri($i * 2))); |
|
82
|
1 |
|
$entry = new Entry($name, $entryid, $content, $this->datatype, $linkarray, "", $count); |
|
|
|
|
|
|
83
|
1 |
|
array_push($entryArray, $entry); |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $entryArray; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
public function getDescription() |
|
90
|
|
|
{ |
|
91
|
3 |
|
return localize("customcolumn.description.rating"); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
public function getCustomByBook($book) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$queryFormat = "SELECT {0}.value AS value FROM {0}, {1} WHERE {0}.id = {1}.{2} AND {1}.book = {3}"; |
|
|
|
|
|
|
97
|
2 |
|
$query = str_format($queryFormat, $this->getTableName(), $this->getTableLinkName(), $this->getTableLinkColumn(), $book->id); |
|
98
|
|
|
|
|
99
|
2 |
|
$result = Base::getDb()->query($query); |
|
100
|
2 |
|
if ($post = $result->fetchObject()) { |
|
101
|
1 |
|
return new CustomColumn($post->value, str_format(localize("customcolumn.stars", $post->value / 2), $post->value / 2), $this); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
1 |
|
return new CustomColumn(NULL, localize("customcolumn.rating.unknown"), $this); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
public function isSearchable() |
|
107
|
|
|
{ |
|
108
|
3 |
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.