PHPePub\Core\EPub::buildTOC PHP Method

buildTOC() public method

Build the Table of Contents. This is not strictly necessary, as most eReaders will build it from the navigation structure in the .ncx file.
public buildTOC ( string $cssFileName = null, string $tocCSSClass = "toc", string $title = "Table of Contents", boolean $addReferences = true, boolean $addToIndex = false, string $tocFileName = "TOC.xhtml" ) : boolean
$cssFileName string Include a link to this css file in the TOC html.
$tocCSSClass string The TOC is a
, if you need special formatting, you can add a css class for that div. Default is "toc".
$title string Title of the Table of contents. Default is "Table of Contents". Use this for ie. languages other than English.
$addReferences boolean include reference pages in the TOC, using the $referencesOrder array to determine the order of the pages in the TOC. Default is TRUE.
$addToIndex boolean Add the TOC to the NCX index at the current leve/position. Default is FALSE
$tocFileName string Change the default name of the TOC file. The default is "TOC.xhtml"
return boolean
    function buildTOC($cssFileName = null, $tocCSSClass = "toc", $title = "Table of Contents", $addReferences = true, $addToIndex = false, $tocFileName = "TOC.xhtml")
    {
        if ($this->isFinalized) {
            return false;
        }
        $this->buildTOC = true;
        $this->tocTitle = $title;
        $this->tocFileName = FileHelper::normalizeFileName($tocFileName);
        if (!empty($cssFileName)) {
            $this->tocCssFileName = FileHelper::normalizeFileName($cssFileName);
        }
        $this->tocCSSClass = $tocCSSClass;
        $this->tocAddReferences = $addReferences;
        $this->opf->addReference(Reference::TABLE_OF_CONTENTS, $title, $this->tocFileName);
        if (!$this->tocNavAdded) {
            $this->opf->addItemRef("ref_" . Reference::TABLE_OF_CONTENTS, false);
            if ($addToIndex) {
                $navPoint = new NavPoint(StringHelper::decodeHtmlEntities($title), $this->tocFileName, "ref_" . Reference::TABLE_OF_CONTENTS);
                $this->ncx->addNavPoint($navPoint);
            } else {
                $this->ncx->referencesList[Reference::TABLE_OF_CONTENTS] = $this->tocFileName;
                $this->ncx->referencesName[Reference::TABLE_OF_CONTENTS] = $title;
            }
        }
        return true;
    }

Usage Example

 /**
  * @inheritdoc
  */
 public function initialization(Config $config)
 {
     $this->epub = new EPub();
     $this->epub->setTitle($config->get('title', 'untitled'));
     if ($isbn = $config->get('ISBN')) {
         $this->epub->setIdentifier($isbn, EPub::IDENTIFIER_ISBN);
     } else {
         $this->epub->setIdentifier($config->get('uri'), EPub::IDENTIFIER_URI);
     }
     $this->epub->setLanguage($config->get('language', 'zh-cn'));
     // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
     $this->epub->setDescription($config->get('description'));
     $this->epub->setAuthor($config->get('author'), $config->get('author'));
     $this->epub->setPublisher($config->get('publisher.name'), $config->get('publisher.url'));
     // I hope this is a non existent address :)
     $this->epub->setDate(time());
     // Strictly not needed as the book date defaults to time().
     $this->epub->setRights($config->get('copyright'));
     // As this is generated, this _could_ contain the name or licence information of the user who purchased the book, if needed. If this is used that way, the identifier must also be made unique for the book.
     $this->epub->setSourceURL($config->get('publisher.url'));
     $this->epub->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
     $this->epub->buildTOC(NULL, "toc", "目录", TRUE, TRUE);
 }
All Usage Examples Of PHPePub\Core\EPub::buildTOC