summaryrefslogtreecommitdiffstats
path: root/resources/epub-loader/BookExport.class.php
blob: 643982eb363d0723adc8ddf9124084d1561fb979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * BookExport class
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Didier Corbière <didier.corbiere@opale-concept.com>
 */

require_once(realpath(dirname(__FILE__)) . '/CsvExport.class.php');

class BookExport
{
	private $mExport = null;
	private $mNbBook = 0;

	const eExportTypeCsv = 1;
	const CsvSeparator = "\t";

	/**
	 * Open an export file (or create if file does not exist)
	 *
	 * @param string Export file name
	 * @param enum Export type
	 * @param boolean Force file creation
	 * @throws Exception if error
	 */
	public function __construct($inFileName, $inExportType, $inCreate = false)
	{
		switch ($inExportType) {
		case self::eExportTypeCsv:
			$this->mExport = new CsvExport($inFileName, $inCreate);
			break;
		default:
			$error = sprintf('Incorrect export type: %d', $inExportType);
			throw new Exception($error);
		}
	}

	/**
	 * Add an epub to the export
	 *
	 * @param string Epub file name
	 * @throws Exception if error
	 *
	 * @return string Empty string or error if any
	 */
	public function AddEpub($inFileName)
	{
		$error = '';

		try {
			// Load the book infos
			$bookInfos = new BookInfos();
			$bookInfos->LoadFromEpub($inFileName);
			// Add the book
			$this->AddBook($bookInfos);
		}
		catch (Exception $e) {
			$error = $e->getMessage();
		}

		return $error;
	}

	/**
	 * Add a new book to the export
	 *
	 * @param object BookInfo object
	 * @throws Exception if error
	 *
	 * @return void
	 */
	private function AddBook($inBookInfo)
	{
		// Add export header
		if ($this->mNbBook++ == 0) {
			$i = 1;
			$this->mExport->SetProperty($i++, 'Format');
			$this->mExport->SetProperty($i++, 'Path');
			$this->mExport->SetProperty($i++, 'Name');
			$this->mExport->SetProperty($i++, 'Uuid');
			$this->mExport->SetProperty($i++, 'Uri');
			$this->mExport->SetProperty($i++, 'Title');
			$this->mExport->SetProperty($i++, 'Authors');
			$this->mExport->SetProperty($i++, 'AuthorsSort');
			$this->mExport->SetProperty($i++, 'Language');
			$this->mExport->SetProperty($i++, 'Description');
			$this->mExport->SetProperty($i++, 'Subjects');
			$this->mExport->SetProperty($i++, 'Cover');
			$this->mExport->SetProperty($i++, 'Isbn');
			$this->mExport->SetProperty($i++, 'Rights');
			$this->mExport->SetProperty($i++, 'Publisher');
			$this->mExport->SetProperty($i++, 'Serie');
			$this->mExport->SetProperty($i++, 'SerieIndex');
			$this->mExport->SetProperty($i++, 'CreationDate');
			$this->mExport->SetProperty($i++, 'ModificationDate');
			$this->mExport->AddContent();
		}

		// Add book infos to the export
		$i = 1;
		$this->mExport->SetProperty($i++, $inBookInfo->mFormat);
		$this->mExport->SetProperty($i++, $inBookInfo->mPath);
		$this->mExport->SetProperty($i++, $inBookInfo->mName);
		$this->mExport->SetProperty($i++, $inBookInfo->mUuid);
		$this->mExport->SetProperty($i++, $inBookInfo->mUri);
		$this->mExport->SetProperty($i++, $inBookInfo->mTitle);
		$this->mExport->SetProperty($i++, implode(' - ', $inBookInfo->mAuthors));
		$this->mExport->SetProperty($i++, implode(' - ', array_keys($inBookInfo->mAuthors)));
		$this->mExport->SetProperty($i++, $inBookInfo->mLanguage);
		$this->mExport->SetProperty($i++, $inBookInfo->mDescription);
		$this->mExport->SetProperty($i++, implode(' - ', $inBookInfo->mSubjects));
		$this->mExport->SetProperty($i++, $inBookInfo->mCover);
		$this->mExport->SetProperty($i++, $inBookInfo->mIsbn);
		$this->mExport->SetProperty($i++, $inBookInfo->mRights);
		$this->mExport->SetProperty($i++, $inBookInfo->mPublisher);
		$this->mExport->SetProperty($i++, $inBookInfo->mSerie);
		$this->mExport->SetProperty($i++, $inBookInfo->mSerieIndex);
		$this->mExport->SetProperty($i++, $inBookInfo->mCreationDate);
		$this->mExport->SetProperty($i++, $inBookInfo->mModificationDate);

		$this->mExport->AddContent();
	}

	/**
	 * Download export and stop further script execution
	 */
	public function Download()
	{
		$this->mExport->Download();
	}

	/**
	 * Save export to file
	 */
	public function SaveToFile()
	{
		$this->mExport->SaveToFile();
	}

}

?>