|
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
|
|
|
/** |
|
10
|
|
|
* A single calibre custom column |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class CustomColumnType |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
const ALL_CUSTOMS_ID = "cops:custom"; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
const CUSTOM_TYPE_TEXT = "text"; // type 1 + 2 |
|
|
|
|
|
|
17
|
|
|
const CUSTOM_TYPE_COMMENT = "comments"; // type 3 |
|
|
|
|
|
|
18
|
|
|
const CUSTOM_TYPE_SERIES = "series"; // type 4 |
|
|
|
|
|
|
19
|
|
|
const CUSTOM_TYPE_ENUM = "enumeration"; // type 5 |
|
|
|
|
|
|
20
|
|
|
const CUSTOM_TYPE_DATE = "datetime"; // type 6 |
|
|
|
|
|
|
21
|
|
|
const CUSTOM_TYPE_FLOAT = "float"; // type 7 |
|
|
|
|
|
|
22
|
|
|
const CUSTOM_TYPE_INT = "int"; // type 8 |
|
|
|
|
|
|
23
|
|
|
const CUSTOM_TYPE_RATING = "rating"; // type 9 |
|
|
|
|
|
|
24
|
|
|
const CUSTOM_TYPE_BOOL = "bool"; // type 10 |
|
|
|
|
|
|
25
|
|
|
const CUSTOM_TYPE_COMPOSITE = "composite"; // type 11 + 12 |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** @var array[integer]CustomColumnType */ |
|
28
|
|
|
private static $customColumnCacheID = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** @var array[string]CustomColumnType */ |
|
31
|
|
|
private static $customColumnCacheLookup = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** @var integer the id of this column */ |
|
34
|
|
|
public $customId; |
|
35
|
|
|
/** @var string name/title of this column */ |
|
36
|
|
|
public $columnTitle; |
|
37
|
|
|
/** @var string the datatype of this column (one of the CUSTOM_TYPE_* constant values) */ |
|
38
|
|
|
public $datatype; |
|
39
|
|
|
/** @var null|Entry[] */ |
|
40
|
|
|
private $customValues = NULL; |
|
41
|
|
|
|
|
42
|
14 |
|
protected function __construct($pcustomId, $pdatatype) |
|
43
|
|
|
{ |
|
44
|
14 |
|
$this->columnTitle = self::getTitleByCustomID($pcustomId); |
|
45
|
14 |
|
$this->customId = $pcustomId; |
|
46
|
14 |
|
$this->datatype = $pdatatype; |
|
47
|
14 |
|
$this->customValues = NULL; |
|
48
|
14 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The URI to show all book swith a specific value in this column |
|
52
|
|
|
* |
|
53
|
|
|
* @param string|integer $id the id of the value to show |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
13 |
|
public function getUri($id) |
|
57
|
|
|
{ |
|
58
|
13 |
|
return "?page=" . Base::PAGE_CUSTOM_DETAIL . "&custom={$this->customId}&id={$id}"; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The URI to show all the values of this column |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
25 |
|
public function getUriAllCustoms() |
|
67
|
|
|
{ |
|
68
|
25 |
|
return "?page=" . Base::PAGE_ALL_CUSTOMS . "&custom={$this->customId}"; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The EntryID to show all book swith a specific value in this column |
|
73
|
|
|
* |
|
74
|
|
|
* @param string|integer $id the id of the value to show |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
17 |
|
public function getEntryId($id) |
|
78
|
|
|
{ |
|
79
|
17 |
|
return self::ALL_CUSTOMS_ID . ":" . $this->customId . ":" . $id; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* The EntryID to show all the values of this column |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
37 |
|
public function getAllCustomsId() |
|
88
|
|
|
{ |
|
89
|
37 |
|
return self::ALL_CUSTOMS_ID . ":" . $this->customId; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* The title of this column |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
37 |
|
public function getTitle() |
|
98
|
|
|
{ |
|
99
|
37 |
|
return $this->columnTitle; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* The description of this column as it is definied in the database |
|
104
|
|
|
* |
|
105
|
|
|
* @return string|null |
|
106
|
|
|
*/ |
|
107
|
20 |
|
public function getDatabaseDescription() |
|
108
|
|
|
{ |
|
109
|
20 |
|
$result = Base::getDb()->prepare('SELECT display FROM custom_columns WHERE id = ?'); |
|
110
|
20 |
|
$result->execute(array($this->customId)); |
|
111
|
20 |
|
if ($post = $result->fetchObject()) { |
|
112
|
20 |
|
$json = json_decode($post->display); |
|
113
|
20 |
|
return (isset($json->description) && !empty($json->description)) ? $json->description : NULL; |
|
114
|
|
|
} |
|
115
|
|
|
return NULL; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the Entry for this column |
|
120
|
|
|
* This is used in the initializeContent method to display e.g. the index page |
|
121
|
|
|
* |
|
122
|
|
|
* @return Entry |
|
123
|
|
|
*/ |
|
124
|
14 |
|
public function getCount() |
|
125
|
|
|
{ |
|
126
|
14 |
|
$ptitle = $this->getTitle(); |
|
127
|
14 |
|
$pid = $this->getAllCustomsId(); |
|
128
|
14 |
|
$pcontent = $this->getDescription(); |
|
129
|
14 |
|
$pcontentType = $this->datatype; |
|
130
|
14 |
|
$plinkArray = array(new LinkNavigation($this->getUriAllCustoms())); |
|
131
|
14 |
|
$pclass = ""; |
|
|
|
|
|
|
132
|
14 |
|
$pcount = $this->getDistinctValueCount(); |
|
133
|
|
|
|
|
134
|
14 |
|
return new Entry($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass, $pcount); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the amount of distinct values for this column |
|
139
|
|
|
* |
|
140
|
|
|
* @return int |
|
141
|
|
|
*/ |
|
142
|
16 |
|
protected function getDistinctValueCount() |
|
143
|
|
|
{ |
|
144
|
16 |
|
return count($this->getAllCustomValues()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Encode a value of this column ready to be displayed in an HTML document |
|
149
|
|
|
* |
|
150
|
|
|
* @param integer|string $value |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
7 |
|
public function encodeHTMLValue($value) |
|
154
|
|
|
{ |
|
155
|
7 |
|
return htmlspecialchars($value); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get the datatype of a CustomColumn by its customID |
|
160
|
|
|
* |
|
161
|
|
|
* @param integer $customId |
|
162
|
|
|
* @return string|null |
|
163
|
|
|
*/ |
|
164
|
17 |
View Code Duplication |
private static function getDatatypeByCustomID($customId) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
17 |
|
$result = Base::getDb()->prepare('SELECT datatype FROM custom_columns WHERE id = ?'); |
|
167
|
17 |
|
$result->execute(array($customId)); |
|
168
|
17 |
|
if ($post = $result->fetchObject()) { |
|
169
|
16 |
|
return $post->datatype; |
|
170
|
|
|
} |
|
171
|
1 |
|
return NULL; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Create a CustomColumnType by CustomID |
|
176
|
|
|
* |
|
177
|
|
|
* @param integer $customId the id of the custom column |
|
178
|
|
|
* @return CustomColumnType|null |
|
179
|
|
|
* @throws Exception If the $customId is not found or the datatype is unknown |
|
180
|
|
|
*/ |
|
181
|
42 |
|
public static function createByCustomID($customId) |
|
182
|
|
|
{ |
|
183
|
|
|
// Reuse already created CustomColumns for performance |
|
184
|
42 |
|
if (array_key_exists($customId, self::$customColumnCacheID)) |
|
185
|
42 |
|
return self::$customColumnCacheID[$customId]; |
|
186
|
|
|
|
|
187
|
17 |
|
$datatype = self::getDatatypeByCustomID($customId); |
|
188
|
|
|
|
|
189
|
|
|
switch ($datatype) { |
|
190
|
17 |
|
case self::CUSTOM_TYPE_TEXT: |
|
191
|
5 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeText($customId); |
|
192
|
12 |
|
case self::CUSTOM_TYPE_SERIES: |
|
193
|
2 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeSeries($customId); |
|
194
|
10 |
|
case self::CUSTOM_TYPE_ENUM: |
|
195
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeEnumeration($customId); |
|
196
|
9 |
|
case self::CUSTOM_TYPE_COMMENT: |
|
197
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeComment($customId); |
|
198
|
8 |
|
case self::CUSTOM_TYPE_DATE: |
|
199
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeDate($customId); |
|
200
|
7 |
|
case self::CUSTOM_TYPE_FLOAT: |
|
201
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeFloat($customId); |
|
202
|
6 |
|
case self::CUSTOM_TYPE_INT: |
|
203
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeInteger($customId); |
|
204
|
5 |
|
case self::CUSTOM_TYPE_RATING: |
|
205
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeRating($customId); |
|
206
|
4 |
|
case self::CUSTOM_TYPE_BOOL: |
|
207
|
1 |
|
return self::$customColumnCacheID[$customId] = new CustomColumnTypeBool($customId); |
|
208
|
3 |
|
case self::CUSTOM_TYPE_COMPOSITE: |
|
209
|
2 |
|
return NULL; //TODO Currently not supported |
|
210
|
1 |
|
default: |
|
211
|
1 |
|
throw new Exception("Unkown column type: " . $datatype); |
|
|
|
|
|
|
212
|
1 |
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Create a CustomColumnType by its lookup name |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $lookup the lookup-name of the custom column |
|
219
|
|
|
* @return CustomColumnType|null |
|
220
|
|
|
*/ |
|
221
|
33 |
|
public static function createByLookup($lookup) |
|
222
|
|
|
{ |
|
223
|
|
|
// Reuse already created CustomColumns for performance |
|
224
|
33 |
|
if (array_key_exists($lookup, self::$customColumnCacheLookup)) |
|
225
|
33 |
|
return self::$customColumnCacheLookup[$lookup]; |
|
226
|
|
|
|
|
227
|
17 |
|
$result = Base::getDb()->prepare('SELECT id FROM custom_columns WHERE label = ?'); |
|
228
|
17 |
|
$result->execute(array($lookup)); |
|
229
|
17 |
|
if ($post = $result->fetchObject()) { |
|
230
|
16 |
|
return self::$customColumnCacheLookup[$lookup] = self::createByCustomID($post->id); |
|
231
|
|
|
} |
|
232
|
1 |
|
return self::$customColumnCacheLookup[$lookup] = NULL; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Return an entry array for all possible (in the DB used) values of this column |
|
237
|
|
|
* These are the values used in the getUriAllCustoms() page |
|
238
|
|
|
* |
|
239
|
|
|
* @return Entry[] |
|
240
|
|
|
*/ |
|
241
|
36 |
|
public function getAllCustomValues() |
|
242
|
|
|
{ |
|
243
|
|
|
// lazy loading |
|
244
|
36 |
|
if ($this->customValues == NULL) |
|
245
|
36 |
|
$this->customValues = $this->getAllCustomValuesFromDatabase(); |
|
246
|
|
|
|
|
247
|
36 |
|
return $this->customValues; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get the title of a CustomColumn by its customID |
|
252
|
|
|
* |
|
253
|
|
|
* @param integer $customId |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
14 |
View Code Duplication |
protected static function getTitleByCustomID($customId) |
|
|
|
|
|
|
257
|
|
|
{ |
|
258
|
14 |
|
$result = Base::getDb()->prepare('SELECT name FROM custom_columns WHERE id = ?'); |
|
259
|
14 |
|
$result->execute(array($customId)); |
|
260
|
14 |
|
if ($post = $result->fetchObject()) { |
|
261
|
14 |
|
return $post->name; |
|
262
|
|
|
} |
|
263
|
|
|
return ""; |
|
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Get the query to find all books with a specific value of this column |
|
268
|
|
|
* the returning array has two values: |
|
269
|
|
|
* - first the query (string) |
|
270
|
|
|
* - second an array of all PreparedStatement parameters |
|
271
|
|
|
* |
|
272
|
|
|
* @param string|integer $id the id of the searched value |
|
273
|
|
|
* @return array |
|
274
|
|
|
*/ |
|
275
|
|
|
abstract public function getQuery($id); |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Get a CustomColumn for a specified (by ID) value |
|
279
|
|
|
* |
|
280
|
|
|
* @param string|integer $id the id of the searched value |
|
281
|
|
|
* @return CustomColumn |
|
282
|
|
|
*/ |
|
283
|
|
|
abstract public function getCustom($id); |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Return an entry array for all possible (in the DB used) values of this column by querying the database |
|
287
|
|
|
* |
|
288
|
|
|
* @return Entry[] |
|
289
|
|
|
*/ |
|
290
|
|
|
abstract protected function getAllCustomValuesFromDatabase(); |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* The description used in the index page |
|
294
|
|
|
* |
|
295
|
|
|
* @return string |
|
296
|
|
|
*/ |
|
297
|
|
|
abstract public function getDescription(); |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Find the value of this column for a specific book |
|
301
|
|
|
* |
|
302
|
|
|
* @param Book $book |
|
303
|
|
|
* @return CustomColumn |
|
304
|
|
|
*/ |
|
305
|
|
|
public abstract function getCustomByBook($book); |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Is this column searchable by value |
|
309
|
|
|
* only searchable columns can be displayed on the index page |
|
310
|
|
|
* |
|
311
|
|
|
* @return bool |
|
312
|
|
|
*/ |
|
313
|
|
|
public abstract function isSearchable(); |
|
314
|
|
|
} |
|
315
|
|
|
|
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.