From 7fd6fdf478fe801d8e1743e8b3565c92cf2b766d Mon Sep 17 00:00:00 2001
From: Cameron <e107inc@gmail.com>
Date: Fri, 11 Mar 2022 12:23:31 -0800
Subject: [PATCH] Fixes #3778 - SEF URL issue on custom page comments. Also
 introduces pageHelper class with use added to e_related, e_search and
 e_sitelink.

---
 e107_handlers/comment_class.php            |  4 +-
 e107_handlers/e107_class.php               |  4 ++
 e107_plugins/comment_menu/comment_menu.php |  2 +
 e107_plugins/page/e_related.php            | 49 +++------------
 e107_plugins/page/e_search.php             | 24 +------
 e107_plugins/page/e_sitelink.php           | 60 ++++--------------
 e107_plugins/page/includes/pageHelper.php  | 73 ++++++++++++++++++++++
 7 files changed, 104 insertions(+), 112 deletions(-)
 create mode 100644 e107_plugins/page/includes/pageHelper.php

diff --git a/e107_handlers/comment_class.php b/e107_handlers/comment_class.php
index b4d7c0e48..50e9c9a08 100644
--- a/e107_handlers/comment_class.php
+++ b/e107_handlers/comment_class.php
@@ -1566,7 +1566,9 @@ class comment
 							{
 								$row2 = $sql2->fetch();
 
-								$route = ($row2['page_chapter'] == 0) ? "page/view/other" : "page/view/index"; // Determine if page belongs to book/chapter. 
+								$row2 = pageHelper::addSefFields($row2);
+
+								$route = empty($row2['page_chapter']) ? "page/view/other" : "page/view/index"; // Determine if page belongs to book/chapter.
 
 								$ret['comment_type'] = COMLAN_TYPE_PAGE;
 								$ret['comment_title'] = $ret['comment_subject'] ? $ret['comment_subject']:
diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php
index 39fa8d2f2..9aff2358e 100644
--- a/e107_handlers/e107_class.php
+++ b/e107_handlers/e107_class.php
@@ -259,6 +259,9 @@ class e107
 		'e107MailManager'                => '{e_HANDLER}mail_manager_class.php',
 		'e_library_manager'              => '{e_HANDLER}library_manager.php',
 		'error_page'                     => '{e_HANDLER}error_page_class.php',
+
+		// Core plugin auto-loaders.
+		'pageHelper'                     => '{e_PLUGIN}page/includes/pageHelper.php'
 	);
 
 	/**
@@ -5861,6 +5864,7 @@ class e107
 			// autoload doesn't REQUIRE files, because this will break things like call_user_func()
 			include($filename);
 		}
+
 	}
 
 	/**
diff --git a/e107_plugins/comment_menu/comment_menu.php b/e107_plugins/comment_menu/comment_menu.php
index 3ecf58594..b0ce3ca81 100644
--- a/e107_plugins/comment_menu/comment_menu.php
+++ b/e107_plugins/comment_menu/comment_menu.php
@@ -39,6 +39,8 @@ if (!defined('e107_INIT'))
 // $cobj = e107::getObject('comment');
 
 e107::lan('comment_menu', null);
+
+/** @var comment $cobj */
 $cobj = e107::getComment();
 
 $menu_pref = e107::getConfig('menu')->getPref();
diff --git a/e107_plugins/page/e_related.php b/e107_plugins/page/e_related.php
index 007a8c4e2..17546505e 100644
--- a/e107_plugins/page/e_related.php
+++ b/e107_plugins/page/e_related.php
@@ -17,43 +17,12 @@ if (!defined('e107_INIT')) { exit; }
 
 class page_related // replace 'e_' with 'plugin-folder_' 
 {
-	private $chapterSef = array();
-	private $chapterParent = array();
-	private $chapterName = array();
-	
-	function __construct()
-	{
-		$sql = e107::getDb();
-		
-		$books = $sql->retrieve("SELECT chapter_id,chapter_sef,chapter_parent,chapter_name 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 getSef($chapter)
-	{
-		return vartrue($this->chapterSef[$chapter],'--sef-not-assigned--');		
-	}
-	
-	private function getParent($chapter)
-	{
-		return varset($this->chapterParent[$chapter], false);			
-	}
-	
-
-	function compile($tags,$parm=array()) 
+	public function compile($tags,$parm=array())
 	{
 		$sql = e107::getDb();
 		$items = array();
-		
-		
+
 		$tag_regexp = "'(^|,)(".str_replace(",", "|", $tags).")(,|$)'";
 		
 		$query = "SELECT * FROM #page WHERE page_id != ".$parm['current']." AND page_class REGEXP '".e_CLASS_REGEXP."'  AND page_metakeys REGEXP ".$tag_regexp."  ORDER BY page_datestamp DESC LIMIT ".$parm['limit'];
@@ -62,16 +31,15 @@ class page_related // replace 'e_' with 'plugin-folder_'
 		{		
 			while($row = $sql->fetch())
 			{
-				$row['chapter_sef'] = $this->getSef($row['page_chapter']);
-				$book 				= $this->getParent($row['page_chapter']);
-				$row['book_sef']	= $this->getSef($book); 
+				$row    = pageHelper::addSefFields($row);
 				
 				$id = $row['page_chapter'];
 				$title = (vartrue($this->chapterName[$id])) ? $this->chapterName[$id]." | ".$row['page_title'] : $row['page_title'];
-				
+				$route = !empty($row['page_chapter']) ? 'page/view/index' : 'page/view/other';
+
 				$items[] = array(
 					'title'			=> $title,
-					'url'			=> e107::getUrl()->create('page/view/index',$row), // '{e_BASE}news.php?extend.'.$row['news_id'],
+					'url'			=> e107::getUrl()->create($route, $row), // '{e_BASE}news.php?extend.'.$row['news_id'],
 					'summary'		=> $row['page_metadscr'],
 					'image'			=> $row['menu_image']
 				);
@@ -79,10 +47,7 @@ class page_related // replace 'e_' with 'plugin-folder_'
 			
 			return $items;
 	    }
-		//else
-		//{
-			// return array(array('title'=>$query,'url'=>''));	
-		//}
+
 	}
 	
 }
diff --git a/e107_plugins/page/e_search.php b/e107_plugins/page/e_search.php
index 6caff88ba..1f97aee26 100644
--- a/e107_plugins/page/e_search.php
+++ b/e107_plugins/page/e_search.php
@@ -38,16 +38,6 @@ class page_search extends e_search // include plugin-folder in the name.
 	{
 		return varset($this->catList[$chapter]['chapter_name'], false);			
 	}	
-	
-	private function getSef($chapter)
-	{
-		return vartrue($this->catList[$chapter]['chapter_sef'],false);		
-	}
-	
-	private function getParent($chapter)
-	{
-		return varset($this->catList[$chapter]['chapter_parent'], false);			
-	}	
 
 	private function isVisible($chapter)
 	{
@@ -102,17 +92,9 @@ class page_search extends e_search // include plugin-folder in the name.
 	{
 		$tp = e107::getParser();
 		
-		$book 				= $this->getParent($row['page_chapter']);
-		$row['chapter_sef'] = $this->getSef($row['page_chapter']);
-		$row['book_sef']	= $this->getSef($book); 
-			
-		if(empty($row['page_sef']))
-		{
-			$row['page_sef'] = '--sef-not-assigned--';	
-		}
+		$row    = pageHelper::addSefFields($row);
 
-
-		if($row['page_chapter'] == 0) // Page without category. 
+		if(empty($row['page_chapter'])) // Page without category.
 		{
 			$route = 'page/view/other';
 			$pre = ''; 	
@@ -120,7 +102,7 @@ class page_search extends e_search // include plugin-folder in the name.
 		else // Page with book/chapter 
 		{
 			$route = 'page/view/index'; 	
-			$pre = $tp->toHTML($this->getName($book),false,'TITLE').' &raquo; '. $tp->toHTML($this->getName($row['page_chapter']),false,'TITLE'). " | ";
+			$pre = $tp->toHTML($row['book_name'],false,'TITLE').' &raquo; '. $tp->toHTML($row['chapter_name'],false,'TITLE'). " | ";
 		}
 		
 				
diff --git a/e107_plugins/page/e_sitelink.php b/e107_plugins/page/e_sitelink.php
index 36813afd7..5e44f155a 100644
--- a/e107_plugins/page/e_sitelink.php
+++ b/e107_plugins/page/e_sitelink.php
@@ -14,7 +14,6 @@ if (!defined('e107_INIT')) { exit; }
 class page_sitelink // include plugin-folder in the name.
 {
 	private $chapterSef = array();
-	private $chapterParent = array();
 	private $chapterName = array();
 	
 	function __construct()
@@ -27,7 +26,6 @@ class page_sitelink // include plugin-folder in the name.
 		{
 			$id = $row['chapter_id'];
 			$this->chapterSef[$id] = $row['chapter_sef'];
-			$this->chapterParent[$id] = $row['chapter_parent'];
 			$this->chapterName[$id] = $row['chapter_name'];
 		}	
 		
@@ -44,12 +42,7 @@ class page_sitelink // include plugin-folder in the name.
 	{
 		return vartrue($this->chapterSef[$chapter],'--sef-not-assigned--');		
 	}
-	
-	private function getParent($chapter)
-	{
-		return varset($this->chapterParent[$chapter], false);			
-	}
-	
+
 	
 	function config()
 	{	
@@ -173,19 +166,8 @@ class page_sitelink // include plugin-folder in the name.
 
 		foreach($pages as $row)
 		{
-			$chapter_parent = $this->getParent($row['page_chapter']);
-
-			$row['book_sef'] = $this->getSef($chapter_parent);
-
-			$row['chapter_sef'] = $this->getSef($row['page_chapter']);
-
-			//if(!vartrue($row['chapter_sef']))
-			//{
-			//	$row['chapter_sef'] = '--sef-not-assigned--';
-		//	}
-
+			$row    = pageHelper::addSefFields($row);
 			$arr[]  = $this->pageArray($row);
-
 		}
 
 		return $arr;
@@ -205,10 +187,12 @@ class page_sitelink // include plugin-folder in the name.
 			$link_name = !empty($row['page_title']) ? $row['page_title'] : 'No title';   // FIXME lan
 		}
 
+		$route = !empty($row['page_chapter']) ? 'page/view/index' : 'page/view/other';
+
 		return array(
 				'link_id'			=> $row['page_id'],
 				'link_name'			=> $link_name,
-				'link_url'			=> e107::getUrl()->create('page/view', $row, array('allow' => 'page_sef,page_title,page_id,chapter_sef,book_sef')),
+				'link_url'			=> e107::getUrl()->create($route, $row, array('allow' => 'page_sef,page_title,page_id,chapter_sef,book_sef')),
 				'link_description'	=> '',
 				'link_button'		=> (!empty($options['icon']) && $options['icon'] === 'menu_image') ? $row['menu_image'] : '',
 				'link_category'		=> '',
@@ -247,9 +231,9 @@ class page_sitelink // include plugin-folder in the name.
 				$id = $row['chapter_id'];
 
 				$sef = $row;
-				$sef['chapter_sef'] = $this->getSef($row['chapter_id']);	
+				$sef['chapter_sef'] = $this->getSef($row['chapter_id']);
 				$sef['book_sef']	= $this->getSef($row['chapter_parent']);
-				
+
 				$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'],
@@ -367,31 +351,11 @@ class page_sitelink // include plugin-folder in the name.
 		foreach($data as $row)
 		{
 			$pid = $row['page_chapter'];
-			
-			$row['chapter_sef'] = $this->getSef($row['page_chapter']);
-			$book 				= $this->getParent($row['page_chapter']);
-			$row['book_sef']	= $this->getSef($book); 
-			
-			if(!vartrue($row['page_sef']))
-			{
-				$row['page_sef'] = '--sef-not-assigned--';	
-			}
-			
-			$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')),
-				'link_description'	=> '',
-				'link_button'		=> $row['menu_image'],
-				'link_category'		=> '',
-				'link_order'		=> $row['page_order'],
-				'link_parent'		=> $row['page_chapter'],
-				'link_open'			=> '',
-				'link_class'		=> intval($row['page_class']),
-				'link_active'		=> (isset($options['cpage']) && $row['page_id'] == $options['cpage']),
-				'link_identifier'	=> 'page-nav-'.intval($row['page_id']) // used for css id. 
-	
-			);*/
+
+			$row = pageHelper::addSefFields($row);
+
+
+			$sublinks[$pid][] = $_pdata[] = $this->pageArray($row,$options);
 		}
 
 		$filter = "chapter_visibility IN (".USERCLASS_LIST.") " ;
diff --git a/e107_plugins/page/includes/pageHelper.php b/e107_plugins/page/includes/pageHelper.php
new file mode 100644
index 000000000..ea4a3d245
--- /dev/null
+++ b/e107_plugins/page/includes/pageHelper.php
@@ -0,0 +1,73 @@
+<?php
+
+
+class pageHelper
+{
+
+	public static function load()
+	{
+
+		$books = e107::getDb('pageHelper')->retrieve("SELECT chapter_id,chapter_sef,chapter_name,chapter_parent FROM #page_chapters ORDER BY chapter_id ASC", true);
+
+		$chapter = array();
+		foreach($books as $row)
+		{
+			$id = (int) $row['chapter_id'];
+			$chapter[$id] = $row;
+		}
+
+		return $chapter;
+
+	}
+
+	/**
+	 * Takes an existing array (eg. from page table) and adds in chapter and book field data based on the given chapter field.
+	 * @param array $row
+	 * @param string $chapterField
+	 * @return array|false
+	 */
+	public static function addSefFields(&$row = array(), $chapterField = 'page_chapter')
+	{
+		if($chapterField ==='page_chapter' && empty($row['page_sef']))
+		{
+			$row['page_sef'] = '--sef-not-assigned--';
+		}
+
+		if(empty($row[$chapterField])) // nothing to add, so return what was sent.
+		{
+			return $row;
+		}
+
+		$chapID = (int) $row[$chapterField];
+
+		static $chaptersList;
+
+		if(empty($chaptersList))
+		{
+			$chaptersList = self::load();
+		}
+
+		// merge in the chapter data.
+		foreach($chaptersList[$chapID] as $k => $v)
+		{
+			if(!isset($row[$k]))
+			{
+				$row[$k] = $v;
+			}
+		}
+
+/*		if(isset($row['book_id']))
+		{
+			return $row;
+		}*/
+
+		// merge in the book data.
+		$chapter = (int) $row['chapter_id'];
+
+		$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--');
+
+		return $row;
+	}
+}
\ No newline at end of file