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 PageQueryResult extends Page |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
const SCOPE_TAG = "tag"; |
|
|
|
|
12
|
|
|
const SCOPE_RATING = "rating"; |
|
|
|
|
13
|
|
|
const SCOPE_SERIES = "series"; |
|
|
|
|
14
|
|
|
const SCOPE_AUTHOR = "author"; |
|
|
|
|
15
|
|
|
const SCOPE_BOOK = "book"; |
|
|
|
|
16
|
|
|
const SCOPE_PUBLISHER = "publisher"; |
|
|
|
|
17
|
|
|
|
18
|
24 |
|
private function useTypeahead () { |
19
|
24 |
|
return !is_null (getURLParam ("search")); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
29 |
|
private function searchByScope ($scope, $limit = FALSE) { |
23
|
29 |
|
$n = $this->n; |
24
|
29 |
|
$numberPerPage = NULL; |
25
|
29 |
|
$queryNormedAndUp = trim($this->query); |
26
|
29 |
|
if (useNormAndUp ()) { |
27
|
7 |
|
$queryNormedAndUp = normAndUp ($this->query); |
28
|
|
|
} |
29
|
29 |
|
if ($limit) { |
30
|
22 |
|
$n = 1; |
31
|
22 |
|
$numberPerPage = 5; |
32
|
|
|
} |
33
|
|
|
switch ($scope) { |
34
|
29 |
|
case self::SCOPE_BOOK : |
|
|
|
|
35
|
23 |
|
$array = Book::getBooksByStartingLetter ('%' . $queryNormedAndUp, $n, NULL, $numberPerPage); |
36
|
23 |
|
break; |
37
|
28 |
|
case self::SCOPE_AUTHOR : |
|
|
|
|
38
|
23 |
|
$array = Author::getAuthorsForSearch ('%' . $queryNormedAndUp); |
39
|
23 |
|
break; |
40
|
25 |
|
case self::SCOPE_SERIES : |
|
|
|
|
41
|
22 |
|
$array = Serie::getAllSeriesByQuery ($queryNormedAndUp); |
42
|
22 |
|
break; |
43
|
24 |
|
case self::SCOPE_TAG : |
|
|
|
|
44
|
23 |
|
$array = Tag::getAllTagsByQuery ($queryNormedAndUp, $n, NULL, $numberPerPage); |
45
|
23 |
|
break; |
46
|
23 |
|
case self::SCOPE_PUBLISHER : |
|
|
|
|
47
|
23 |
|
$array = Publisher::getAllPublishersByQuery ($queryNormedAndUp); |
48
|
23 |
|
break; |
49
|
|
|
default: |
50
|
|
|
$array = Book::getBooksByQuery ( |
51
|
|
|
array ("all" => "%" . $queryNormedAndUp . "%"), $n); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
29 |
|
return $array; |
55
|
|
|
} |
56
|
|
|
|
57
|
22 |
|
public function doSearchByCategory () { |
58
|
22 |
|
$database = GetUrlParam (DB); |
59
|
22 |
|
$out = array (); |
60
|
22 |
|
$pagequery = Base::PAGE_OPENSEARCH_QUERY; |
61
|
22 |
|
$dbArray = array (""); |
|
|
|
|
62
|
22 |
|
$d = $database; |
63
|
22 |
|
$query = $this->query; |
64
|
|
|
// Special case when no databases were chosen, we search on all databases |
65
|
22 |
|
if (Base::noDatabaseSelected ()) { |
66
|
1 |
|
$dbArray = Base::getDbNameList (); |
67
|
1 |
|
$d = 0; |
68
|
|
|
} |
69
|
22 |
|
foreach ($dbArray as $key) { |
70
|
22 |
|
if (Base::noDatabaseSelected ()) { |
71
|
1 |
|
array_push ($this->entryArray, new Entry ($key, DB . ":query:{$d}", |
|
|
|
|
72
|
1 |
|
" ", "text", |
|
|
|
|
73
|
1 |
|
array ( new LinkNavigation ("?" . DB . "={$d}")), "tt-header")); |
|
|
|
|
74
|
1 |
|
Base::getDb ($d); |
75
|
|
|
} |
76
|
22 |
|
foreach (array (PageQueryResult::SCOPE_BOOK, |
77
|
|
|
PageQueryResult::SCOPE_AUTHOR, |
78
|
|
|
PageQueryResult::SCOPE_SERIES, |
79
|
|
|
PageQueryResult::SCOPE_TAG, |
80
|
|
|
PageQueryResult::SCOPE_PUBLISHER) as $key) { |
81
|
22 |
|
if (in_array($key, getCurrentOption ('ignored_categories'))) { |
82
|
3 |
|
continue; |
83
|
|
|
} |
84
|
22 |
|
$array = $this->searchByScope ($key, TRUE); |
85
|
|
|
|
86
|
22 |
|
$i = 0; |
87
|
22 |
|
if (count ($array) == 2 && is_array ($array [0])) { |
88
|
22 |
|
$total = $array [1]; |
89
|
22 |
|
$array = $array [0]; |
90
|
|
|
} else { |
91
|
22 |
|
$total = count($array); |
92
|
|
|
} |
93
|
22 |
|
if ($total > 0) { |
94
|
|
|
// Comment to help the perl i18n script |
95
|
|
|
// str_format (localize("bookword", count($array)) |
|
|
|
|
96
|
|
|
// str_format (localize("authorword", count($array)) |
|
|
|
|
97
|
|
|
// str_format (localize("seriesword", count($array)) |
|
|
|
|
98
|
|
|
// str_format (localize("tagword", count($array)) |
|
|
|
|
99
|
|
|
// str_format (localize("publisherword", count($array)) |
|
|
|
|
100
|
21 |
|
array_push ($this->entryArray, new Entry (str_format (localize ("search.result.{$key}"), $this->query), DB . ":query:{$d}:{$key}", |
|
|
|
|
101
|
21 |
|
str_format (localize("{$key}word", $total), $total), "text", |
|
|
|
|
102
|
21 |
|
array ( new LinkNavigation ("?page={$pagequery}&query={$query}&db={$d}&scope={$key}")), |
|
|
|
|
103
|
21 |
|
Base::noDatabaseSelected () ? "" : "tt-header", $total)); |
|
|
|
|
104
|
|
|
} |
105
|
22 |
|
if (!Base::noDatabaseSelected () && $this->useTypeahead ()) { |
106
|
6 |
|
foreach ($array as $entry) { |
107
|
6 |
|
array_push ($this->entryArray, $entry); |
108
|
6 |
|
$i++; |
109
|
22 |
|
if ($i > 4) { break; }; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
22 |
|
$d++; |
114
|
22 |
|
if (Base::noDatabaseSelected ()) { |
115
|
22 |
|
Base::clearDb (); |
116
|
|
|
} |
117
|
|
|
} |
118
|
22 |
|
return $out; |
119
|
|
|
} |
120
|
|
|
|
121
|
31 |
|
public function InitializeContent () |
|
|
|
|
122
|
|
|
{ |
123
|
31 |
|
$scope = getURLParam ("scope"); |
|
|
|
|
124
|
31 |
|
if (empty ($scope)) { |
125
|
24 |
|
$this->title = str_format (localize ("search.result"), $this->query); |
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
// Comment to help the perl i18n script |
128
|
|
|
// str_format (localize ("search.result.author"), $this->query) |
|
|
|
|
129
|
|
|
// str_format (localize ("search.result.tag"), $this->query) |
|
|
|
|
130
|
|
|
// str_format (localize ("search.result.series"), $this->query) |
|
|
|
|
131
|
|
|
// str_format (localize ("search.result.book"), $this->query) |
|
|
|
|
132
|
|
|
// str_format (localize ("search.result.publisher"), $this->query) |
|
|
|
|
133
|
7 |
|
$this->title = str_format (localize ("search.result.{$scope}"), $this->query); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
31 |
|
$crit = "%" . $this->query . "%"; |
|
|
|
|
137
|
|
|
|
138
|
|
|
// Special case when we are doing a search and no database is selected |
139
|
31 |
|
if (Base::noDatabaseSelected () && !$this->useTypeahead ()) { |
140
|
2 |
|
$i = 0; |
141
|
2 |
|
foreach (Base::getDbNameList () as $key) { |
142
|
2 |
|
Base::clearDb (); |
143
|
2 |
|
list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1); |
|
|
|
|
144
|
2 |
|
array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}", |
|
|
|
|
145
|
2 |
|
str_format (localize ("bookword", $totalNumber), $totalNumber), "text", |
|
|
|
|
146
|
2 |
|
array ( new LinkNavigation ("?" . DB . "={$i}&page=9&query=" . $this->query)), "", $totalNumber)); |
|
|
|
|
147
|
2 |
|
$i++; |
148
|
|
|
} |
149
|
2 |
|
return; |
150
|
|
|
} |
151
|
29 |
|
if (empty ($scope)) { |
152
|
22 |
|
$this->doSearchByCategory (); |
153
|
22 |
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
7 |
|
$array = $this->searchByScope ($scope); |
157
|
7 |
|
if (count ($array) == 2 && is_array ($array [0])) { |
158
|
2 |
|
list ($this->entryArray, $this->totalNumber) = $array; |
159
|
|
|
} else { |
160
|
5 |
|
$this->entryArray = $array; |
161
|
|
|
} |
162
|
7 |
|
} |
163
|
|
|
} |
164
|
|
|
|
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.