function addChapter($chapterName, $fileName, $chapterData = null, $autoSplit = false, $externalReferences = EPub::EXTERNAL_REF_IGNORE, $baseDir = "")
{
if ($this->isFinalized) {
return false;
}
$fileName = RelativePath::getRelativePath($fileName);
$fileName = preg_replace('#^[/\\.]+#i', "", $fileName);
$navPoint = false;
$chapter = $chapterData;
if ($autoSplit && is_string($chapterData) && mb_strlen($chapterData) > $this->splitDefaultSize) {
$splitter = new EPubChapterSplitter();
$splitter->setSplitSize($this->splitDefaultSize);
$chapterArray = $splitter->splitChapter($chapterData);
if (count($chapterArray) > 1) {
$chapter = $chapterArray;
}
}
if (!empty($chapter) && is_string($chapter)) {
if ($externalReferences !== EPub::EXTERNAL_REF_IGNORE) {
$htmlDirInfo = pathinfo($fileName);
$htmlDir = preg_replace('#^[/\\.]+#i', "", $htmlDirInfo["dirname"] . "/");
$this->processChapterExternalReferences($chapter, $externalReferences, $baseDir, $htmlDir);
}
if ($this->encodeHTML === true) {
$chapter = StringHelper::encodeHtml($chapter);
}
$this->chapterCount++;
$this->addFile($fileName, "chapter" . $this->chapterCount, $chapter, "application/xhtml+xml");
$this->extractIdAttributes("chapter" . $this->chapterCount, $chapter);
$this->opf->addItemRef("chapter" . $this->chapterCount);
$navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount);
$this->ncx->addNavPoint($navPoint);
$this->ncx->chapterList[$chapterName] = $navPoint;
} elseif (is_array($chapter)) {
$this->log->logLine("addChapter: \$chapterName: {$chapterName} ; \$fileName: {$fileName} ; ");
$fileNameParts = pathinfo($fileName);
$extension = $fileNameParts['extension'];
$name = $fileNameParts['filename'];
$partCount = 0;
$this->chapterCount++;
$oneChapter = each($chapter);
while ($oneChapter) {
/** @noinspection PhpUnusedLocalVariableInspection */
list($k, $v) = $oneChapter;
if ($this->encodeHTML === true) {
$v = StringHelper::encodeHtml($v);
}
if ($externalReferences !== EPub::EXTERNAL_REF_IGNORE) {
$this->processChapterExternalReferences($v, $externalReferences, $baseDir);
}
$partCount++;
$partName = $name . "_" . $partCount;
$this->addFile($partName . "." . $extension, $partName, $v, "application/xhtml+xml");
$this->extractIdAttributes($partName, $v);
$this->opf->addItemRef($partName);
$oneChapter = each($chapter);
}
$partName = $name . "_1." . $extension;
$navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $partName, $partName);
$this->ncx->addNavPoint($navPoint);
$this->ncx->chapterList[$chapterName] = $navPoint;
} elseif (!isset($chapterData) && strpos($fileName, "#") > 0) {
$this->chapterCount++;
//$this->opf->addItemRef("chapter" . $this->chapterCount);
$id = preg_split("/[#]/", $fileName);
if (sizeof($id) == 2 && $this->isLogging) {
$name = preg_split('/[\\.]/', $id[0]);
if (sizeof($name) > 1) {
$name = $name[0];
}
$rv = $this->opf->getItemByHref($name, true);
if ($rv != false) {
/** @var Item $item */
foreach ($rv as $item) {
if ($item->hasIndexPoint($id[1])) {
$fileName = $item->getHref() . "#" . $id[1];
break;
}
}
}
}
$navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount);
$this->ncx->addNavPoint($navPoint);
$this->ncx->chapterList[$chapterName] = $navPoint;
} elseif (!isset($chapterData) && $fileName == "TOC.xhtml") {
$this->chapterCount++;
$this->opf->addItemRef("toc");
$navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount);
$this->ncx->addNavPoint($navPoint);
$this->ncx->chapterList[$chapterName] = $navPoint;
$this->tocNavAdded = true;
}
return $navPoint;
}