1
0
mirror of https://github.com/e107inc/e107.git synced 2025-06-06 02:46:42 +02:00

Issue #3778 - page/e_sitelink cleanup and optimization. Test added. Fixed bookNav() which should only return book links.

This commit is contained in:
Cameron 2022-03-11 18:01:56 -08:00
parent 7fd6fdf478
commit 4032c2954f
5 changed files with 664 additions and 70 deletions

View File

@ -41,7 +41,7 @@ class page_shortcodes extends e_shortcode
* Page Navigation
* @example {PAGE_NAVIGATION: template=navdoc&auto=1} in your Theme template.
* @example {PAGE_NAVIGATION: chapter=4}
* @example {PAGE_NAVIGATION: book=3&pages=true}
* @example {PAGE_NAVIGATION: book=3&chapters=true&pages=true}
*/
function sc_page_navigation($parm=null) // TODO when No $parm provided, auto-detect based on URL which book/chapters to display.
{
@ -95,7 +95,7 @@ class page_shortcodes extends e_shortcode
/** @var page_sitelink $links */
$links = e107::getAddon('page', 'e_sitelink');
$data = $links->pageNav($parm);
$data = $links->pageNav($parm, true);
if(isset($data['title']) && !vartrue($template['noAutoTitle']))
{

View File

@ -13,38 +13,15 @@ if (!defined('e107_INIT')) { exit; }
class page_sitelink // include plugin-folder in the name.
{
private $chapterSef = array();
private $chapterName = array();
function __construct()
{
$sql = e107::getDb();
$books = $sql->retrieve("SELECT chapter_id,chapter_name,chapter_sef,chapter_parent FROM #page_chapters ORDER BY chapter_id ASC" , true);
foreach($books as $row)
{
$id = $row['chapter_id'];
$this->chapterSef[$id] = $row['chapter_sef'];
$this->chapterName[$id] = $row['chapter_name'];
}
}
private function getName($chapter)
{
return varset($this->chapterName[$chapter], null);
}
private function getSef($chapter)
{
return vartrue($this->chapterSef[$chapter],'--sef-not-assigned--');
}
function config()
public function config()
{
$links = array();
$sql = e107::getDb();
@ -55,7 +32,6 @@ class page_sitelink // include plugin-folder in the name.
'description' => "A list of all books"
);
/* // TODO - get these working.
$links[] = array(
'name' => "All Books & chapters",
'function' => "bookNavChapters",
@ -67,7 +43,7 @@ class page_sitelink // include plugin-folder in the name.
'function' => "bookNavChaptersPages",
'description' => "A list of all books, chapters and pages"
);
*/
$books = $sql->retrieve("SELECT * FROM #page_chapters WHERE chapter_parent =0 ORDER BY chapter_order ASC" , true);
foreach($books as $row)
@ -110,24 +86,26 @@ class page_sitelink // include plugin-folder in the name.
}
/**
* Return a tree of all books and their chapters.
* Return a tree of all books
*/
public function bookNav($book=0)
{
$parm = array('book'=>$book);
$parm = array('book'=>$book, 'chapters'=>false, 'pages'=>false);
return $this->pageNav($parm);
}
//TODO
/**
* Return a tree of all books and their chapters.
*/
public function bookNavChapters($book=0)
{
$parm = array('book'=>$book, 'chapters'=>true);
$parm = array('book'=>$book, 'chapters'=>true, 'pages'=>false);
return $this->pageNav($parm);
}
//TODO
/**
* Return a tree of all books, their chapters and pages.
*/
public function bookNavChaptersPages($book=0)
{
$parm = array('book'=>$book, 'chapters'=>true, 'pages'=>true);
@ -135,7 +113,11 @@ class page_sitelink // include plugin-folder in the name.
}
/**
* Return a list of all chapters from a sepcific book.
* @param $id
* @return array|void
*/
public function chapterNavPages($id)
{
return $this->chapterNav($id, true);
@ -166,7 +148,6 @@ class page_sitelink // include plugin-folder in the name.
foreach($pages as $row)
{
$row = pageHelper::addSefFields($row);
$arr[] = $this->pageArray($row);
}
@ -189,6 +170,8 @@ class page_sitelink // include plugin-folder in the name.
$route = !empty($row['page_chapter']) ? 'page/view/index' : 'page/view/other';
$row = pageHelper::addSefFields($row);
return array(
'link_id' => $row['page_id'],
'link_name' => $link_name,
@ -216,23 +199,19 @@ class page_sitelink // include plugin-folder in the name.
$sql = e107::getDb();
$tp = e107::getParser();
$query = "chapter_parent = ".intval($book)." AND chapter_visibility IN (".USERCLASS_LIST.") ORDER BY chapter_order ASC ";
if($sql->select("page_chapters", "*", "chapter_parent = ".intval($book)." AND chapter_visibility IN (".USERCLASS_LIST.") ORDER BY chapter_order ASC "))
if($data = $sql->retrieve("page_chapters", "*", $query, true))
{
$chapters = array();
$ids = array();
while($row = $sql->fetch())
foreach($data as $row)
{
$ids[] = $row['chapter_id'];
$id = $row['chapter_id'];
$sef = $row;
$sef['chapter_sef'] = $this->getSef($row['chapter_id']);
$sef['book_sef'] = $this->getSef($row['chapter_parent']);
$sef = pageHelper::addSefFields($row, 'chapter_id');
$chapters[$id] = array(
'link_name' => $tp->toHTML($row['chapter_name'],'','TITLE'),
@ -250,7 +229,6 @@ class page_sitelink // include plugin-folder in the name.
}
if($loadPages === true)
{
$pages = $sql->retrieve("SELECT * FROM #page WHERE page_title !='' AND page_chapter IN (".implode(",",$ids).") AND page_class IN (".USERCLASS_LIST.") ORDER BY page_order", true);
@ -258,7 +236,6 @@ class page_sitelink // include plugin-folder in the name.
{
$chap = $row['page_chapter'];
$chapters[$chap]['link_sub'][] = $this->pageArray($row);
}
// e107::getDebug()->log($pages);
@ -273,7 +250,12 @@ class page_sitelink // include plugin-folder in the name.
}
}
function pageNav($parm=null)
/**
* @param $parm
* @param bool $useTitle - when set to true, will return a title if found. - ie. for use in page navigation menu.
* @return array|null
*/
function pageNav($parm=null, $useTitle = false)
{
$frm = e107::getForm();
$options = array();
@ -333,7 +315,7 @@ class page_sitelink // include plugin-folder in the name.
{
$q[] = "page_title !='' && page_chapter IN (SELECT chapter_id FROM #page_chapters WHERE chapter_parent=".intval($options['book']).")";
}
// XXX discuss FIXED remove DB check, use default title - AND page_title !=''
$q[] = "page_class IN (".USERCLASS_LIST.")";
$query .= implode(' AND ', $q)." ORDER BY page_order";
@ -351,10 +333,7 @@ class page_sitelink // include plugin-folder in the name.
foreach($data as $row)
{
$pid = $row['page_chapter'];
$row = pageHelper::addSefFields($row);
$sublinks[$pid][] = $_pdata[] = $this->pageArray($row,$options);
}
@ -376,17 +355,21 @@ class page_sitelink // include plugin-folder in the name.
$parent = 0;
$title = false;
if(!empty($options['book']))
if(!empty($options['book']) || varset($options['chapters']) === false)
{
$filter = "chapter_parent = ".intval($options['book']);
$title = $this->getName($options['book']); // set the caption as main book title.
if($useTitle && !empty($row['book_name']))
{
$title = $row['book_name']; // set the caption as main book title.
}
}
$books = $sql->retrieve("SELECT * FROM #page_chapters WHERE ".$filter." ORDER BY chapter_order ASC" , true);
foreach($books as $row)
{
$row['book_sef'] = $this->getSef($row['chapter_parent']);
// $row['book_sef'] = $this->getSef($row['chapter_parent']);
$row = pageHelper::addSefFields($row,'chapter_parent');
if(empty($row['chapter_sef']))
{
@ -404,20 +387,15 @@ class page_sitelink // include plugin-folder in the name.
'link_parent' => $row['chapter_parent'],
'link_open' => '',
'link_class' => 0,
'link_sub' => ((empty($options['book']) || !empty($options['pages'])) && empty($options['auto'])) ? varset($sublinks[$row['chapter_id']]) : false, //XXX always test with docs template in bootstrap before changing.
'link_sub' => ((!empty($options['chapters']) && !empty($options['pages'])) && empty($options['auto'])) ? varset($sublinks[$row['chapter_id']]) : false,
'link_active' => $row['chapter_parent'] == 0 ? isset($options['cbook']) && $options['cbook'] == $row['chapter_id'] : isset($options['cchapter']) && $options['cchapter'] == $row['chapter_id'],
'link_identifier' => 'page-nav-'.intval($row['chapter_id']) // used for css id.
);
}
/*
if(!empty($options['book']))
{
e107::getDebug()->dump($arr);
}*/
$outArray = array();
$parent = vartrue($options['book']) ? intval($options['book']) : 0;
$parent = !empty($options['book']) ? (int) $options['book'] : 0;
$ret = e107::getNav()->compile($arr, $outArray, $parent);

View File

@ -62,11 +62,11 @@ class pageHelper
}*/
// merge in the book data.
$chapter = (int) $row['chapter_id'];
$parent = (int) $row['chapter_parent'];
$row['book_id'] = (int) $row['chapter_parent'];
$row['book_name'] = varset($chaptersList[$chapter]['chapter_name'], '--sef-not-assigned--');
$row['book_sef'] = vartrue($chaptersList[$chapter]['chapter_sef'], '--sef-not-assigned--');
$row['book_id'] = $parent;
$row['book_name'] = varset($chaptersList[$parent]['chapter_name'], '--sef-not-assigned--');
$row['book_sef'] = vartrue($chaptersList[$parent]['chapter_sef'], '--sef-not-assigned--');
return $row;
}

View File

@ -387,7 +387,7 @@ class e_parse_shortcodeTest extends \Codeception\Test\Unit
'link_parent' => '0',
'link_open' => '0',
'link_class' => '0',
'link_function' => 'page::bookNav',
'link_function' => 'page::bookNavChaptersPages',
'link_sefurl' => 'index',
'link_owner' => 'news'
);

View File

@ -0,0 +1,616 @@
<?php
class page_sitelinkTest extends \Codeception\Test\Unit
{
/** @var page_sitelink */
protected $nav;
protected function _before()
{
// Enable SEF Urls. book/chapter/page
e107::getConfig()->setPref('url_config/page', 'core/sef_chapters')->save(false, true, false);
require_once(e_PLUGIN . "page/e_sitelink.php");
// e107::getConfig()->set
try
{
$this->nav = $this->make('page_sitelink');
}
catch(Exception $e)
{
$this->fail($e->getMessage());
}
$this->nav->__construct();
}
protected function _after()
{
e107::getConfig()->setPref('url_config/page', 'core')->save(false, true, false); // disable SEF Urls.
}
/*
public function testPageNav()
{
}
public function testPageList()
{
}
public function testConfig()
{
}
*/
public function testBookNav()
{
$result = $this->nav->bookNav();
$expected = array(
0 =>
array(
'link_id' => '1',
'link_name' => 'General',
'link_url' => '/page/general',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '0',
'link_parent' => '0',
'link_open' => '',
'link_class' => 0,
'link_sub' => array(),
'link_active' => false,
'link_identifier' => 'page-nav-1',
),
);
$this->assertSame($expected, $result);
}
public function testBookNavChapters()
{
$result = $this->nav->bookNavChapters();
$expected = array(
0 =>
array(
'link_id' => '1',
'link_name' => 'General',
'link_url' => '/page/general',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '0',
'link_parent' => '0',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(
0 =>
array(
'link_id' => '2',
'link_name' => 'Chapter 1',
'link_url' => '/page/general/chapter-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '1',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(),
'link_active' => false,
'link_identifier' => 'page-nav-2',
),
1 =>
array(
'link_id' => '3',
'link_name' => 'Custom Fields',
'link_url' => '/page/general/customfields',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '2',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(),
'link_active' => false,
'link_identifier' => 'page-nav-3',
),
),
'link_active' => false,
'link_identifier' => 'page-nav-1',
),
);
$this->assertSame($expected, $result);
}
public function testBookNavChaptersPages()
{
$result = $this->nav->bookNavChaptersPages();
$expected = array(
0 =>
array(
'link_id' => '1',
'link_name' => 'General',
'link_url' => '/page/general',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '0',
'link_parent' => '0',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(
0 =>
array(
'link_id' => '2',
'link_name' => 'Chapter 1',
'link_url' => '/page/general/chapter-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '1',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(
0 =>
array(
'link_id' => '1',
'link_name' => 'Article 1',
'link_url' => '/page/general/chapter-1/article-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-1',
),
1 =>
array(
'link_id' => '2',
'link_name' => 'Article 2',
'link_url' => '/page/general/chapter-1/article-2',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-2',
),
2 =>
array(
'link_id' => '3',
'link_name' => 'Article 3',
'link_url' => '/page/general/chapter-1/article-3',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-3',
),
3 =>
array(
'link_id' => '5',
'link_name' => 'Feature 1',
'link_url' => '/page/general/chapter-1/feature-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-5',
),
4 =>
array(
'link_id' => '6',
'link_name' => 'Feature 2',
'link_url' => '/page/general/chapter-1/feature-2',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-6',
),
5 =>
array(
'link_id' => '7',
'link_name' => 'Feature 3',
'link_url' => '/page/general/chapter-1/feature-3',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-7',
),
),
'link_active' => false,
'link_identifier' => 'page-nav-2',
),
1 =>
array(
'link_id' => '3',
'link_name' => 'Custom Fields',
'link_url' => '/page/general/customfields',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '2',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(
0 =>
array(
'link_id' => '4',
'link_name' => 'Article 4',
'link_url' => '/page/general/customfields/article-4',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '3',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-4',
),
),
'link_active' => false,
'link_identifier' => 'page-nav-3',
),
),
'link_active' => false,
'link_identifier' => 'page-nav-1',
),
);
$this->assertSame($expected, $result);
}
public function testChapterNav()
{
$result = $this->nav->chapterNav(1);
$expected = array(
2 =>
array(
'link_name' => 'Chapter 1',
'link_url' => '/page/general/chapter-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(),
'link_identifier' => 'page-nav-2',
),
3 =>
array(
'link_name' => 'Custom Fields',
'link_url' => '/page/general/customfields',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(),
'link_identifier' => 'page-nav-3',
),
);
$this->assertSame($expected, $result);
}
public function testPagesFromChapter()
{
$result = $this->nav->pagesFromChapter(2);
$expected = array(
0 =>
array(
'link_id' => '1',
'link_name' => 'Article 1',
'link_url' => '/page/general/chapter-1/article-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-1',
),
1 =>
array(
'link_id' => '2',
'link_name' => 'Article 2',
'link_url' => '/page/general/chapter-1/article-2',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-2',
),
2 =>
array(
'link_id' => '3',
'link_name' => 'Article 3',
'link_url' => '/page/general/chapter-1/article-3',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-3',
),
3 =>
array(
'link_id' => '5',
'link_name' => 'Feature 1',
'link_url' => '/page/general/chapter-1/feature-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-5',
),
4 =>
array(
'link_id' => '6',
'link_name' => 'Feature 2',
'link_url' => '/page/general/chapter-1/feature-2',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-6',
),
5 =>
array(
'link_id' => '7',
'link_name' => 'Feature 3',
'link_url' => '/page/general/chapter-1/feature-3',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-7',
),
);
$this->assertSame($expected, $result);
}
public function testChapterNavPages()
{
$result = $this->nav->chapterNavPages(1);
$expected = array(
2 =>
array(
'link_name' => 'Chapter 1',
'link_url' => '/page/general/chapter-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(
0 =>
array(
'link_id' => '1',
'link_name' => 'Article 1',
'link_url' => '/page/general/chapter-1/article-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-1',
),
1 =>
array(
'link_id' => '2',
'link_name' => 'Article 2',
'link_url' => '/page/general/chapter-1/article-2',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-2',
),
2 =>
array(
'link_id' => '3',
'link_name' => 'Article 3',
'link_url' => '/page/general/chapter-1/article-3',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-3',
),
3 =>
array(
'link_id' => '5',
'link_name' => 'Feature 1',
'link_url' => '/page/general/chapter-1/feature-1',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-5',
),
4 =>
array(
'link_id' => '6',
'link_name' => 'Feature 2',
'link_url' => '/page/general/chapter-1/feature-2',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-6',
),
5 =>
array(
'link_id' => '7',
'link_name' => 'Feature 3',
'link_url' => '/page/general/chapter-1/feature-3',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '2',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-7',
),
),
'link_identifier' => 'page-nav-2',
),
3 =>
array(
'link_name' => 'Custom Fields',
'link_url' => '/page/general/customfields',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '',
'link_parent' => '1',
'link_open' => '',
'link_class' => 0,
'link_sub' =>
array(
0 =>
array(
'link_id' => '4',
'link_name' => 'Article 4',
'link_url' => '/page/general/customfields/article-4',
'link_description' => '',
'link_button' => '',
'link_category' => '',
'link_order' => '9999',
'link_parent' => '3',
'link_open' => '',
'link_class' => 0,
'link_active' => false,
'link_identifier' => 'page-nav-4',
),
),
'link_identifier' => 'page-nav-3',
),
);
$this->assertSame($expected, $result);
}
function testPageNavigationShortcode()
{
$result = e107::getParser()->parseTemplate('{PAGE_NAVIGATION: book=1&chapters=true&pages=true}');
$this->assertStringContainsString('Chapter 1', $result);
$this->assertStringContainsString('Article 3', $result);
$this->assertStringContainsString('Custom Fields', $result);
$this->assertStringContainsString('Article 4', $result);
}
}