From 898e08165632f62376d9c11845c115c05b166581 Mon Sep 17 00:00:00 2001
From: Cameron <e107inc@gmail.com>
Date: Tue, 17 Oct 2017 16:22:56 -0700
Subject: [PATCH] Page Navigation work.

---
 e107_handlers/db_debug_class.php           |  28 +++-
 e107_handlers/sitelinks_class.php          |  10 +-
 e107_plugins/page/e_menu.php               |  61 +++++++
 e107_plugins/page/e_shortcode.php          |   4 +-
 e107_plugins/page/e_sitelink.php           | 179 ++++++++++++++++-----
 e107_plugins/page/page_navigation_menu.php |   9 +-
 6 files changed, 240 insertions(+), 51 deletions(-)
 create mode 100644 e107_plugins/page/e_menu.php

diff --git a/e107_handlers/db_debug_class.php b/e107_handlers/db_debug_class.php
index a6f9b19a3..b0aa765b9 100644
--- a/e107_handlers/db_debug_class.php
+++ b/e107_handlers/db_debug_class.php
@@ -768,7 +768,31 @@ class e107_db_debug {
 			return $text;
 		}
 	}
-	
+
+
+	/**
+	 * var_dump to debug log
+	 * @param mixed $message
+	 */
+	function dump($message, $TraceLev= 1)
+	{
+		ob_start();
+	    var_dump($message);
+	    $content = ob_get_contents();
+	    ob_end_clean();
+
+	    $bt = debug_backtrace();
+
+		$this->aLog[] =	array(
+			'Message'   => $content,
+			'Function'	=> (isset($bt[$TraceLev]['type']) && ($bt[$TraceLev]['type'] == '::' || $bt[$TraceLev]['type'] == '->') ? $bt[$TraceLev]['class'].$bt[$TraceLev]['type'].$bt[$TraceLev]['function'].'()' : $bt[$TraceLev]['function']).'()',
+				'File'	=> varset($bt[$TraceLev]['file']),
+				'Line'	=> varset($bt[$TraceLev]['line'])
+			);
+
+	   // $this->aLog[] =	array ('Message'   => $content, 'Function' => '', 	'File' => '', 'Line' => '' 	);
+
+	}
 //
 // Simple debug-level 'console' log
 // Record a "nice" debug message with
@@ -777,6 +801,8 @@ class e107_db_debug {
 	function log($message,$TraceLev=1)
 	{
 
+
+
 		if(is_array($message) || is_object($message))
 		{
 			$message = "<pre>".print_r($message,true)."</pre>";
diff --git a/e107_handlers/sitelinks_class.php b/e107_handlers/sitelinks_class.php
index 02253737d..8b1818a0d 100644
--- a/e107_handlers/sitelinks_class.php
+++ b/e107_handlers/sitelinks_class.php
@@ -1805,7 +1805,7 @@ i.e-cat_users-32{ background-position: -555px 0; width: 32px; height: 32px; }
 			return true;	
 		}
 		
-		if(vartrue($data['link_active'])) // Can be used by e_sitelink.php
+		if(!empty($data['link_active'])) // Can be used by e_sitelink.php
 		{
 			return true;		
 		}
@@ -1894,9 +1894,9 @@ class navigation_shortcodes extends e_shortcode
 	 */
 	function sc_link_name($parm='')
 	{
-		if(!varset($this->var['link_name']))
+		if(empty($this->var['link_name']))
 		{
-			return;	
+			return null;
 		}
 		
 		if(substr($this->var['link_name'],0,8) == 'submenu.') // BC Fix. 
@@ -2024,7 +2024,7 @@ class navigation_shortcodes extends e_shortcode
 	{
 		$tp = e107::getParser();
 				
-		if (!vartrue($this->var['link_button'])) return '';
+		if (empty($this->var['link_button'])) return '';
 		
 	//	if($icon = $tp->toGlyph($this->var['link_button']))
 	//	{
@@ -2089,7 +2089,7 @@ class navigation_shortcodes extends e_shortcode
 		{
 			$active	= (e107::getNav()->isActive($val, $this->activeSubFound, true)) ? "_active" : "";
 			$this->setVars($val);	// isActive is allowed to alter data
-			$tmpl = vartrue($val['link_sub']) ? varset($this->template['submenu_loweritem'.$active]) : varset($this->template['submenu_item'.$active]);	
+			$tmpl = !empty($val['link_sub']) ? varset($this->template['submenu_loweritem'.$active]) : varset($this->template['submenu_item'.$active]);
 			$text .= e107::getParser()->parseTemplate($tmpl, TRUE, $this);
 			if($active) $this->activeSubFound = true;		
 		}
diff --git a/e107_plugins/page/e_menu.php b/e107_plugins/page/e_menu.php
new file mode 100644
index 000000000..ea1618bee
--- /dev/null
+++ b/e107_plugins/page/e_menu.php
@@ -0,0 +1,61 @@
+<?php
+/*
+ * e107 website system
+ *
+ * Copyright (C) 2008-2015 e107 Inc (e107.org)
+ * Released under the terms and conditions of the
+ * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
+ *
+ *
+*/
+
+if (!defined('e107_INIT')) { exit; }
+
+//v2.x Standard for extending menu configuration within Menu Manager. (replacement for v1.x config.php)
+// TODO add fields
+class page_menu
+{
+	function __construct()
+	{
+		// e107::lan('_blank','menu',true); // English_menu.php or {LANGUAGE}_menu.php
+	}
+
+	/**
+	 * Configuration Fields.
+	 * @return array
+	 */
+	public function config($menu='')
+	{
+
+		$fields = array();
+		$fields['caption']      = array('title'=> LAN_CAPTION, 'type'=>'text', 'multilan'=>true, 'writeParms'=>array('size'=>'xxlarge'));
+
+	//	$fields['blankCount']        = array('title'=> "Enabled", 'type'=>'number');
+	//	$fields['blankCustom']       = array('title'=> "Enabled", 'type'=>'method'); // see below.
+
+        return $fields;
+
+	}
+
+}
+
+// optional - for when using custom methods above.
+/*
+class _blank_menu_form extends e_form
+{
+
+	function blankCustom($curVal)
+	{
+
+		$frm = e107::getForm();
+		$opts = array(1,2,3,4);
+		$frm->select('blankCustom', $opts, $curVal);
+
+
+	}
+
+
+}*/
+
+
+?>
\ No newline at end of file
diff --git a/e107_plugins/page/e_shortcode.php b/e107_plugins/page/e_shortcode.php
index 9e0b0df7f..66ac4d1c8 100644
--- a/e107_plugins/page/e_shortcode.php
+++ b/e107_plugins/page/e_shortcode.php
@@ -41,7 +41,7 @@ class page_shortcodes extends e_shortcode
 		 * Page Navigation
 		 * @example {PAGE_NAVIGATION: template=navdoc&auto=1} in your Theme template. 
 		 */	
-		function sc_page_navigation($parm='') // TODO when No $parm provided, auto-detect based on URL which book/chapters to display. 
+		function sc_page_navigation($parm=null) // TODO when No $parm provided, auto-detect based on URL which book/chapters to display.
 		{
 		//	$parm = eHelper::scParams($parm);
 				
@@ -102,7 +102,7 @@ class page_shortcodes extends e_shortcode
 				$data = $data['body'];
 			}
 			
-			if(empty($data)){ return; }
+			if(empty($data)){ return null; }
 			
 			return e107::getNav()->render($data, $template) ;
 			
diff --git a/e107_plugins/page/e_sitelink.php b/e107_plugins/page/e_sitelink.php
index 97518df0a..8656e7709 100644
--- a/e107_plugins/page/e_sitelink.php
+++ b/e107_plugins/page/e_sitelink.php
@@ -15,23 +15,31 @@ class page_sitelink // include plugin-folder in the name.
 {
 	private $chapterSef = array();
 	private $chapterParent = array();
+	private $chapterName = array();
 	
 	function __construct()
 	{
 		$sql = e107::getDb();
 		
-		$books = $sql->retrieve("SELECT chapter_id,chapter_sef,chapter_parent FROM #page_chapters ORDER BY chapter_id ASC" , true);
+		$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->chapterParent[$id] = $row['chapter_parent'];
+			$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--');		
@@ -53,7 +61,20 @@ class page_sitelink // include plugin-folder in the name.
 			'function'		=> "bookNav",
 			'description' 	=> "A list of all books"
 		);
-		
+
+		/* // TODO - get these working.
+		$links[] = array(
+			'name'			=> "All Books &amp; chapters",
+			'function'		=> "bookNavChapters",
+			'description' 	=> "A list of all books and their chapters"
+		);
+
+		$links[] = array(
+			'name'			=> "All Books, Chapters &amp; Pages",
+			'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)
@@ -64,6 +85,13 @@ class page_sitelink // include plugin-folder in the name.
 				'parm'			=> $row['chapter_id'],
 				'description' 	=> "A list of all chapters from the book ".$row['chapter_name']
 			);
+
+			$links[] = array(
+				'name'			=> "All Chapters &amp; Pages from ".$row['chapter_name'],
+				'function'		=> "chapterNavPages",
+				'parm'			=> $row['chapter_id'],
+				'description' 	=> "A list of all chapters and pages from the book ".$row['chapter_name']
+			);
 		}
 
 		$chaps = $sql->retrieve("SELECT * FROM #page_chapters WHERE chapter_parent !=0 ORDER BY chapter_order ASC" , true);
@@ -91,14 +119,37 @@ class page_sitelink // include plugin-folder in the name.
 	/**
 	 * Return a tree of all books and their chapters. 
 	 */
-	public function bookNav($book)
+	public function bookNav($book=0)
 	{
-		return $this->pageNav('book=0');
+		$parm = array('book'=>$book);
+		return $this->pageNav($parm);
+	}
+
+
+	//TODO
+	public function bookNavChapters($book=0)
+	{
+		$parm = array('book'=>$book, 'chapters'=>true);
+		return $this->pageNav($parm);
+	}
+
+
+	//TODO
+	public function bookNavChaptersPages($book=0)
+	{
+		$parm = array('book'=>$book, 'chapters'=>true, 'pages'=>true);
+		return $this->pageNav($parm);
+	}
+
+
+
+	public function chapterNavPages($id)
+	{
+		return $this->chapterNav($id, true);
 	}
 
 	public function pagesFromChapter($id)
 	{
-
 		return $this->pageList($id);
 	}
 
@@ -133,12 +184,22 @@ class page_sitelink // include plugin-folder in the name.
 			//	$row['chapter_sef'] = '--sef-not-assigned--';
 			}
 
-			$arr[]  = array(
+			$arr[]  = $this->pageArray($row);
+
+		}
+
+		return $arr;
+
+	}
+
+	private function pageArray($row,$options=array())
+	{
+		return array(
 				'link_id'			=> $row['page_id'],
 				'link_name'			=> $row['page_title'] ? $row['page_title'] : 'No title', // FIXME lan
 				'link_url'			=> e107::getUrl()->create('page/view', $row, array('allow' => 'page_sef,page_title,page_id,chapter_sef,book_sef')),
 				'link_description'	=> '',
-			//	'link_button'		=> $row['menu_image'],
+				'link_button'		=> (!empty($options['icon']) && $options['icon'] === 'menu_image') ? $row['menu_image'] : '',
 				'link_category'		=> '',
 				'link_order'		=> $row['page_order'],
 				'link_parent'		=> $row['page_chapter'],
@@ -149,31 +210,36 @@ class page_sitelink // include plugin-folder in the name.
 
 			);
 
-		}
-
-		return $arr;
 
 	}
 
 	/**
 	 * Return a list of all chapters from a sepcific book. 
 	 */
-	public function chapterNav($book)
+	public function chapterNav($book, $loadPages=false)
 	{
 		$sql = e107::getDb();
 		$tp = e107::getParser();
+
+
+
+
 		
 		if($sql->select("page_chapters", "*", "chapter_parent = ".intval($book)." AND chapter_visibility IN (".USERCLASS_LIST.")  ORDER BY chapter_order ASC "))
 		{
-			$sublinks = array();
-			
+			$chapters = array();
+			$ids = array();
+
 			while($row = $sql->fetch())
 			{
+				$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']);
 				
-				$sublinks[] = array(
+				$chapters[$id] = array(
 					'link_name'			=> $tp->toHtml($row['chapter_name'],'','TITLE'),
 					'link_url'			=> e107::getUrl()->create('page/chapter/index', $sef), // 'page.php?ch='.$row['chapter_id'],
 					'link_description'	=> '',
@@ -183,23 +249,48 @@ class page_sitelink // include plugin-folder in the name.
 					'link_parent'		=> $row['chapter_parent'],
 					'link_open'			=> '',
 					'link_class'		=> 0,
+					'link_sub'          => array(),
 					'link_identifier'	=> 'page-nav-'.intval($row['chapter_id']) // used for css id. 
 				);
 
 			}
+
+
+			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);
+				foreach($pages as $row)
+				{
+					$chap = $row['page_chapter'];
+					$chapters[$chap]['link_sub'][] = $this->pageArray($row);
+
+				}
+
+			//	e107::getDebug()->log($pages);
+			}
+
+
+
+
 			
-			return $sublinks;
+			return $chapters;
 			
 		}
 	}
 
-	function pageNav($parm='') 
+	function pageNav($parm=null)
 	{
 		$frm = e107::getForm();
 		$options = array();
-		if(vartrue($parm))
+
+		if(!empty($parm))
 		{
-			parse_str($parm,$options);	
+			if(is_string($parm))
+			{
+				parse_str($parm,$options);
+			}
+
+			$options = $parm;
 		}
 
 		$sql 		= e107::getDb();
@@ -252,7 +343,14 @@ class page_sitelink // include plugin-folder in the name.
 		
 		$data 		= $sql->retrieve($query, true);
 		$_pdata 	= array();
-				
+/*
+		if(empty($data))
+		{
+			e107::getDebug()->log($query);
+			e107::getDebug()->dump($data);
+		}
+*/
+
 		foreach($data as $row)
 		{
 			$pid = $row['page_chapter'];
@@ -266,7 +364,7 @@ class page_sitelink // include plugin-folder in the name.
 				$row['page_sef'] = '--sef-not-assigned--';	
 			}
 			
-			$sublinks[$pid][] = $_pdata[] = array(
+			$sublinks[$pid][] = $_pdata[] = $this->pageArray($row,$options); /* array(
 				'link_id'			=> $row['page_id'],
 				'link_name'			=> $row['page_title'] ? $row['page_title'] : 'No title', // FIXME lan
 				'link_url'			=> e107::getUrl()->create('page/view', $row, array('allow' => 'page_sef,page_title,page_id,chapter_sef,book_sef')),
@@ -280,33 +378,31 @@ class page_sitelink // include plugin-folder in the name.
 				'link_active'		=> (isset($options['cpage']) && $row['page_id'] == $options['cpage']),
 				'link_identifier'	=> 'page-nav-'.intval($row['page_id']) // used for css id. 
 	
-			);
+			);*/
 		}
 
 		$filter = "chapter_visibility IN (".USERCLASS_LIST.") " ;
 		
-		if(vartrue($options['chapter']))
+		if(!empty($options['chapter']))
 		{
-			//$filter = "chapter_id > ".intval($options['chapter']);
-			
 			$title = $sql->retrieve('page_chapters', 'chapter_name', 'chapter_id='.intval($options['chapter']).' AND chapter_visibility IN ('.USERCLASS_LIST.')' );
 			$outArray 	= array();
-			if(!$title) return e107::getNav()->compile($_pdata, $outArray, $options['chapter']);	
+
+			if(!$title)
+			{
+				return e107::getNav()->compile($_pdata, $outArray, $options['chapter']);
+			}
+
 			return array('title' => $title, 'body' => e107::getNav()->compile($_pdata, $outArray, $options['chapter']));
 		}
 
 		$parent = 0;
 		$title = false;
-		if(vartrue($options['book']))
+
+		if(!empty($options['book']))
 		{
-			
-			// XXX discuss the idea here
-			//$filter = "chapter_id > ".intval($options['book']);
 			$filter = "chapter_parent = ".intval($options['book']);
-			$parent = intval($options['book']);
-			$title = $sql->retrieve('page_chapters', 'chapter_name', 'chapter_id='.intval($options['book']));
-			
-		//	print_a('parent='.$parent);
+			$title = $this->getName($options['book']); // set the caption as main book title.
 		}
 
 		
@@ -315,17 +411,15 @@ class page_sitelink // include plugin-folder in the name.
 		{
 			$row['book_sef'] = $this->getSef($row['chapter_parent']);
 			
-			if(!vartrue($row['chapter_sef']))
+			if(empty($row['chapter_sef']))
 			{
 				$row['chapter_sef'] = '--sef-not-assigned--';		
 			}
-			
+
 			$arr[] = array(
 				'link_id'			=> $row['chapter_id'],
 				'link_name'			=> $row['chapter_name'],
 				'link_url'			=> ($row['chapter_parent'] == 0) ? e107::getUrl()->create('page/book/index', $row) : e107::getUrl()->create('page/chapter/index', $row), // ,'page.php?bk='.$row['chapter_id'] : 'page.php?ch='.$row['chapter_id'], 
-			//	'link_url'			=> vartrue($row['chapter_sef'],'#'),
-				
 				'link_description'	=> '',
 				'link_button'		=> $row['chapter_icon'],
 				'link_category'		=> '',
@@ -333,16 +427,21 @@ 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']]) : '', //XXX always test with docs template in bootstrap before changing.
+				'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_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;
+
 		$ret =  e107::getNav()->compile($arr, $outArray, $parent);		
 
 		if(!$title) return $ret;
diff --git a/e107_plugins/page/page_navigation_menu.php b/e107_plugins/page/page_navigation_menu.php
index b21bee63b..42c52ade9 100644
--- a/e107_plugins/page/page_navigation_menu.php
+++ b/e107_plugins/page/page_navigation_menu.php
@@ -20,7 +20,7 @@ $parm = eHelper::scParams($parm);
 $tmpl = e107::getCoreTemplate('chapter','nav',true,true); // always merge and allow override
 
 $template = $tmpl['showPage'];
-
+/*
 $request = e107::getRegistry('core/page/request');
 if($request && is_array($request))
 {
@@ -58,7 +58,11 @@ if(isset($data['title']) && !vartrue($template['noAutoTitle']))
 }
 
 if(empty($data)) return;
-$text = e107::getNav()->render($data, $template) ;
+$text = e107::getNav()->render($data, $template) ;*/
+$pg = new page_shortcodes;
+$text = $pg->sc_page_navigation($parm);
+
+
 
 /**
  * Expandable menu support. 
@@ -75,4 +79,3 @@ if($expandable)
 ### Render
 e107::getRender()->tablerender($template['caption'], $text, 'page-navigation-menu');
 
-?>
\ No newline at end of file