Completed
Pull Request — master (#233)
by
unknown
05:24
created

VirtualLib::isVLEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 0
crap 1
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     Klaus Broelemann <[email protected]>
7
 */
8
9
require_once('base.php');
10
11
/**
12
 * Read and filter virtual libraries
13
 */
14
class VirtualLib {
15
	const SQL_VL_KEY = "virtual_libraries";  // Key for the virtual library entries.
16
	
17
	/**
18
	 * Checks if the support for virtual libraries is enabled in the settings.
19
	 * 
20
	 * @return boolean true, if virtual libraries are enabled.
21
	 */
22 41
	public static function isVLEnabled() {
23 41
		global $config;
24 41
		return ($config['enable_virtual_libraries'] == 1);
25
	}
26
	
27
	/**
28
	 * Gets a list of all virtual libraries in a database.
29
	 * 
30
	 * @param int $database id of the database
31
	 * @return array An array of virtual libraries with the names as keys and the filter strings as values.  
32
	 */
33 22
	public static function getVLList($database = NULL) {
34
		// Load list from Database
35 22
		$vLibs = json_decode(Base::getCalibreSetting(self::SQL_VL_KEY, $database), true);
36
		// Add "All Books" at the beginning
37 22
		if (is_null($vLibs))
38 22
			return array(localize ("allbooks.title") => "");
39
		else
40
			return array_merge(array(localize ("allbooks.title") => ""), $vLibs);
41
	}
42
	
43
	/**
44
	 * Gets a list of all virtual libraries in a database.
45
	 *
46
	 * @param int $database id of the database
47
	 * @return array An array of virtual libraries with the names as keys and the filter strings as values.
48
	 */
49 22
	public static function getVLNameList($database = NULL) {
50 22
		return array_keys(self::getVLList($database));
51
	}
52
	
53
	/**
54
	 * Combines the database and virtual lib names into a merged name.
55
	 * 
56
	 * The resulting name has the form "{$dbName} - {$vlName}". If one of these parameters is empty, the dash will also be removed.
57
	 * If the support for virtual libraries is not enabled, this function simply returns the database name.
58
	 * 
59
	 * @param string $dbName The Database Name
60
	 * @param string $vlName The Name of the virtual library
61
	 */
62 1
	public static function getDisplayName($dbName, $vlName) {
63 1
		if (self::isVLEnabled())
64 1
			return trim(str_format('{0} - {1}', $dbName, $vlName), ' -');
65
		else
66 1
			return $dbName;
67
	}
68
}