/**
* Check for mandatory parameters and finalize the e-book.
* Once finalized, the book is locked for further additions.
*
* @return bool $success
*/
function finalize()
{
if ($this->isFinalized || $this->chapterCount == 0 || empty($this->title) || empty($this->language)) {
return false;
}
if (empty($this->identifier) || empty($this->identifierType)) {
$this->setIdentifier(StringHelper::createUUID(4), EPub::IDENTIFIER_UUID);
}
if ($this->date == 0) {
$this->date = time();
}
if (empty($this->sourceURL)) {
$this->sourceURL = URLHelper::getCurrentPageURL();
}
if (empty($this->publisherURL)) {
$this->sourceURL = URLHelper::getCurrentServerURL();
}
// Generate OPF data:
$this->opf->setIdent("BookId");
$this->opf->initialize($this->title, $this->language, $this->identifier, $this->identifierType);
$DCdate = new DublinCore(DublinCore::DATE, gmdate($this->dateformat, $this->date));
$DCdate->addOpfAttr("event", "publication");
$this->opf->metadata->addDublinCore($DCdate);
if (!empty($this->description)) {
$this->opf->addDCMeta(DublinCore::DESCRIPTION, StringHelper::decodeHtmlEntities($this->description));
}
if (!empty($this->publisherName)) {
$this->opf->addDCMeta(DublinCore::PUBLISHER, StringHelper::decodeHtmlEntities($this->publisherName));
}
if (!empty($this->publisherURL)) {
$this->opf->addDCMeta(DublinCore::RELATION, StringHelper::decodeHtmlEntities($this->publisherURL));
}
if (!empty($this->author)) {
$author = StringHelper::decodeHtmlEntities($this->author);
$this->opf->addCreator($author, StringHelper::decodeHtmlEntities($this->authorSortKey), MarcCode::AUTHOR);
$this->ncx->setDocAuthor($author);
}
if (!empty($this->rights)) {
$this->opf->addDCMeta(DublinCore::RIGHTS, StringHelper::decodeHtmlEntities($this->rights));
}
if (!empty($this->coverage)) {
$this->opf->addDCMeta(DublinCore::COVERAGE, StringHelper::decodeHtmlEntities($this->coverage));
}
if (!empty($this->sourceURL)) {
$this->opf->addDCMeta(DublinCore::SOURCE, $this->sourceURL);
}
if (!empty($this->relation)) {
$this->opf->addDCMeta(DublinCore::RELATION, StringHelper::decodeHtmlEntities($this->relation));
}
if ($this->isCoverImageSet) {
$this->opf->addMeta("cover", "CoverImage");
}
if (!empty($this->generator)) {
$gen = StringHelper::decodeHtmlEntities($this->generator);
$this->opf->addMeta("generator", $gen);
$this->ncx->addMetaEntry("dtb:generator", $gen);
}
if ($this->EPubMark) {
$this->opf->addMeta("generator", "EPub (Version " . self::VERSION . ") by A. Grandt, http://www.phpclasses.org/package/6115 or https://github.com/Grandt/PHPePub/");
}
reset($this->ncx->chapterList);
list($firstChapterName, $firstChapterNavPoint) = each($this->ncx->chapterList);
/** @var $firstChapterNavPoint NavPoint */
$firstChapterFileName = $firstChapterNavPoint->getContentSrc();
$this->opf->addReference(Reference::TEXT, StringHelper::decodeHtmlEntities($firstChapterName), $firstChapterFileName);
$this->ncx->setUid($this->identifier);
$this->ncx->setDocTitle(StringHelper::decodeHtmlEntities($this->title));
$this->ncx->referencesOrder = $this->referencesOrder;
if ($this->isReferencesAddedToToc) {
$this->ncx->finalizeReferences();
}
$this->finalizeTOC();
if (!$this->isEPubVersion2()) {
$this->addEPub3TOC("epub3toc.xhtml", $this->buildEPub3TOC());
}
$opfFinal = StringHelper::fixEncoding($this->opf->finalize());
$ncxFinal = StringHelper::fixEncoding($this->ncx->finalize());
if (mb_detect_encoding($opfFinal, 'UTF-8', true) === "UTF-8") {
$this->zip->addFile($opfFinal, $this->bookRoot . "book.opf");
} else {
$this->zip->addFile(mb_convert_encoding($opfFinal, "UTF-8"), $this->bookRoot . "book.opf");
}
if (mb_detect_encoding($ncxFinal, 'UTF-8', true) === "UTF-8") {
$this->zip->addFile($ncxFinal, $this->bookRoot . "book.ncx");
} else {
$this->zip->addFile(mb_convert_encoding($ncxFinal, "UTF-8"), $this->bookRoot . "book.ncx");
}
$this->opf = null;
$this->ncx = null;
$this->isFinalized = true;
return true;
}