protected function createToc($book_contents, $metadata)
{
$vars = array('post_title' => '', 'stylesheet' => $this->stylesheet, 'post_content' => '', 'isbn' => @$metadata['pb_ebook_isbn'], 'lang' => $this->lang);
$options = get_option('pressbooks_theme_options_global');
foreach (array('copyright_license') as $requiredGlobalOption) {
if (!isset($options[$requiredGlobalOption])) {
$options[$requiredGlobalOption] = 0;
}
}
// Start by inserting self into correct manifest position
$array_pos = $this->positionOfToc();
$file_id = 'table-of-contents';
$filename = "{$file_id}.{$this->filext}";
$vars['post_title'] = __('Table Of Contents', 'pressbooks');
$this->manifest = array_slice($this->manifest, 0, $array_pos + 1, true) + array($file_id => array('ID' => -1, 'post_title' => $vars['post_title'], 'filename' => $filename)) + array_slice($this->manifest, $array_pos + 1, count($this->manifest) - 1, true);
// HTML
$li_count = 0;
$i = 1;
$html = '<div id="toc"><h1>' . __('Contents', 'pressbooks') . '</h1><ul>';
foreach ($this->manifest as $k => $v) {
// We only care about front-matter, part, chapter, back-matter
// Skip the rest
$subtitle = '';
$author = '';
$license = '';
$title = Sanitize\strip_br($v['post_title']);
if (preg_match('/^front-matter-/', $k)) {
$class = 'front-matter ';
$class .= \Pressbooks\Taxonomy::getFrontMatterType($v['ID']);
$subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true));
$author = trim(get_post_meta($v['ID'], 'pb_section_author', true));
$license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : '';
} elseif (preg_match('/^part-/', $k)) {
$class = 'part';
if (get_post_meta($v['ID'], 'pb_part_invisible', true) == 'on') {
$class .= ' display-none';
}
} elseif (preg_match('/^chapter-/', $k)) {
$class = 'chapter';
$class .= \Pressbooks\Taxonomy::getChapterType($v['ID']);
$subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true));
$author = trim(get_post_meta($v['ID'], 'pb_section_author', true));
$license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : '';
if ($this->numbered && \Pressbooks\Taxonomy::getChapterType($v['ID']) !== 'numberless') {
$title = " {$i}. " . $title;
}
if (\Pressbooks\Taxonomy::getChapterType($v['ID']) !== 'numberless') {
++$i;
}
} elseif (preg_match('/^back-matter-/', $k)) {
$class = 'back-matter ';
$class .= \Pressbooks\Taxonomy::getBackMatterType($v['ID']);
$subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true));
$author = trim(get_post_meta($v['ID'], 'pb_section_author', true));
$license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : '';
} else {
continue;
}
$html .= sprintf('<li class="%s"><a href="%s"><span class="toc-chapter-title">%s</span>', $class, $v['filename'], Sanitize\decode($title));
if ($subtitle) {
$html .= ' <span class="chapter-subtitle">' . Sanitize\decode($subtitle) . '</span>';
}
if ($author) {
$html .= ' <span class="chapter-author">' . Sanitize\decode($author) . '</span>';
}
if ($license) {
$html .= ' <span class="chapter-license">' . $license . '</span> ';
}
$html .= '</a>';
if (\Pressbooks\Modules\Export\Export::isParsingSubsections() == true) {
$sections = \Pressbooks\Book::getSubsections($v['ID']);
if ($sections) {
$html .= '<ul class="sections">';
foreach ($sections as $id => $title) {
$html .= '<li class="section"><a href="' . $v['filename'] . '#' . $id . '"><span class="toc-subsection-title">' . Sanitize\decode($title) . '</span></a></li>';
}
$html .= '</ul>';
}
}
$html .= "</li>\n";
++$li_count;
}
if (0 == $li_count) {
$html .= '<li></li>';
}
$html .= "</ul></div>\n";
// Create file
$vars['post_content'] = $html;
file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/epub201/html.php', $vars));
}