<?php
/* Ulyxex version 1.5.5.2 ***************/
/* code http://ulyxex.logz.org **********/
/* Andre Lozano http://andre-lozano.org */
class Tree extends Ulyxex {
public object $h;
public object $t;
public array $parentOf;
public array $name;
public array $type;
function __construct(){
$this->h = New Htmlz();
$this->t = New Translate();
}
public function paths($hide=null,$userId=null){
$paths = array();
if ($hide){
$query = SELECT."ID,NAME,TYPEPAGE,PARENTID".FROM.PAGES.WHERE."HIDE != 1";
if ($userId) $query .= _AND_."USERID = ".$userId;
} else {
$query = SELECT."ID,NAME,TYPEPAGE,PARENTID".FROM.PAGES;
if ($userId) $query .= WHERE."USERID = ".$userId;
}
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
$this->parentOf[$line['ID']] = $line['PARENTID'];
$this->name[$line['ID']] = $line['NAME'];
$this->type[$line['ID']] = $line['TYPEPAGE'];
}
foreach($this->parentOf as $child => $parent){
$ancestorId = 0;
$ancestorsNames = array();
if ($parent == 0){
// if parent is home
$lineage = "/".$this->name[$child]."/";
$paths[$lineage] = array($this->type[$child],$child);
} else {
// if parent other than home exist
// add parent
if (isset($this->name[$parent])){
// check if error in lineage
if ( in_array($this->name[$parent],$ancestorsNames) ){echo " error tree !";break;}
$ancestorsNames[] = $this->name[$parent];
}
// if a gran parent exist
if (array_key_exists($parent, $this->parentOf)){
$ancestorId = $this->parentOf[$parent];
// if others gran gran... parent exist
while ($ancestorId != 0){
if (isset($this->name[$ancestorId])){
// check if error in lineage
if ( in_array($this->name[$ancestorId],$ancestorsNames) ){echo " error tree !";break;}
$ancestorsNames[] = $this->name[$ancestorId];
}
if (isset($this->parentOf[$ancestorId]) && $this->parentOf[$ancestorId] != 0){
$ancestorId = $this->parentOf[$ancestorId];
} else {
$ancestorId = 0;
}
}
$ancestorsNames = array_reverse($ancestorsNames);
$lineage = "/".implode("/",$ancestorsNames)."/".$this->name[$child]."/";
$paths[$lineage] = array($this->type[$child],$child);
}
}
}
return $paths;
}
public function pathsSelect($selected=null,$root=1,$type=null,$userId=null){
$paths = $this->paths(null,$userId);
ksort($paths);
$options = array();
if ($root) $options["ROOT/"] = 0;
foreach($paths as $path => $idVal){
$pathDepth = explode("/",$path);
$path = "ROOT-";
$path .= str_repeat("/-",count($pathDepth)-3); // 3 for first "/", last "/" and array position
array_pop($pathDepth);
$path .= ">".array_pop($pathDepth);
if ($type) {
if ($idVal[0] == $type) {
$options[$path] = $idVal[1];
} elseif ($idVal[0] == "link") {
$options[$path." [link]"] = $idVal[1];
} else {
$options[$path." [other]"] = $idVal[1];
}
} else {
$options[$path] = $idVal[1];
}
}
return $this->h->select($options,"parent",$selected);
}
public function pathTest($page=0,$parent=0){
$paths = $this->paths();
$lineage = explode("/",array_search($parent,$paths));
return (in_array($this->name[$page],$lineage))?0:1;
}
public function pathPage($parent=0){
$paths = $this->paths();
return array_search($parent,$paths);
}
//~ menus type
function getMenuLine(){
$h = $this->h;$t = $this->t;
$menu = array();
$query = SELECT."ID,NAME,TYPEPAGE".FROM.PAGES.WHERE."HIDE != 1";
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
$menu[$line['NAME']] = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='menu'");
}
ksort($menu);
return implode(" ",array_values($menu));
}
function getMenuPosition($pageId=0){
$h = $this->h; $t = $this->t;
$homeId = $this->site_params()['ID'];
$homeSiteTab = $this->site_params()['SITETAB'];
$homeId = $this->site_params()['ID'];
//~ structure
//~ |4 home page |3 parent page of current page |2 current page |1 child pages
//~ 1 get child pages
$menuChild = array();
//~ $menuChildHash = array();
$query = SELECT."ID,NAME,TYPEPAGE,PARENTID".FROM.PAGES.WHERE."PARENTID = ".$pageId._AND_."HIDE != 1";
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
$menuChild[$line['NAME']] = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='menu'");
}
ksort($menuChild);
$menuChild = array_values($menuChild);
if (count($menuChild) > 0) { // if child exist add sign
array_unshift($menuChild,$homeSiteTab);
}
//~ 2 get current page if not home
$currentPage = "";
$currentPgParentId = 0;
$query = SELECT."ID,NAME,TYPEPAGE,PARENTID".FROM.PAGES.WHERE."ID = ".$pageId._AND_."HIDE != 1";
$query = $this->q($query);
$line = $this->fetch($query);
if ($pageId != $homeId && $line != null) { // if not home
$currentPgParentId = $line['PARENTID'];
$currentPage = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='menu'");
}
//~ 3 get parent page of current page
$parentPage = "";
if ($currentPgParentId != 0) {
$query = SELECT."ID,NAME,TYPEPAGE".FROM.PAGES.WHERE."ID = ".$currentPgParentId._AND_."HIDE != 1";
$query = $this->q($query);
$line = $this->fetch($query);
$parentPage = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='menu'");
}
//~ 4 get homes pages and root
$homePages = array();
if ($pageId == $homeId) { // lists the root pages if we are on the home page
$query = SELECT."ID,NAME,TYPEPAGE".FROM.PAGES.WHERE."PARENTID = 0"._AND_."HIDE != 1"._AND_."ID != ".$pageId;
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
$homePages[$line['NAME']] = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='menu'");
}
ksort($homePages);
$homePages = array_values($homePages);
}
/* home page root */
$query = SELECT."ID,NAME,TYPEPAGE".FROM.PAGES.WHERE."ID = ".$homeId._AND_."HIDE != 1";
$query = $this->q($query);
$line = $this->fetch($query);
$homePages[] = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='menu'");
return implode(" ",$homePages)." ".$parentPage." ".$currentPage." ".implode(" ",$menuChild);
}
function getMenuForm($pageId=0,$pageType=""){
$h = $this->h;$t = $this->t;
$paths = $this->paths($hide=1); // prevent showing hide links
ksort($paths);
$pages_paths = array();
$selectedPath = $pageType.",".$pageId;
$navigationTitle = "--".$t->w("Pages").str_repeat("-",20-strlen($t->w("Pages")));
// pages
$pages_paths[$navigationTitle] = "limit";
foreach($paths as $path => $idVal){
$pathDepth = explode("/",$path);
//~ if you use netspace 2.02 don't use blank space or - as first char
//~ you can usualy use chars
//~     IDEOGRAPHIC SPACE
//~     PUNCTUATION SPACE
//~ ↳ ↳ DOWNWARDS ARROW WITH TIP RIGHTWARDS
//~     FIGURE SPACE
//~ $path = str_repeat(SITETAB,count($pathDepth)-3); // 3 for first "/", last "/" and array position, and repeat SITETAB
$path = str_repeat(" ",count($pathDepth)-3); // 3 for first "/", last "/" and array position
if (count($pathDepth)-3 > 0) $path .= SITETAB." ";
array_pop($pathDepth);
$path .= array_pop($pathDepth);
//~ $pages_paths[$path.$h->comment($idVal[1])] = $idVal[0].",".(string)$idVal[1]; // with id in comment
$pages_paths[$path] = $idVal[0].",".(string)$idVal[1];
}
$options = $pages_paths;
/* rond ico ◉ oplus ⊕, loz ico ◊ phi ico &Phi*/
$menu = $h->p($h->selectOnChangeMenu($options,"navSelect",$selectedPath)." ".$h->input("submit","OK","Φ","class=\"navsubmit\""));
return $h->form($menu,"navform","post","index.php");
}
function getMenuList($type="ul"){
$h = $this->h;$t = $this->t;
$paths = $this->paths($hide=1); // prevent showing hide links
ksort($paths);
$depth = 2;
$listMenu = "";
foreach($paths as $path => $idVal){
$pathParts = explode("/",$path);
$name = $pathParts[count($pathParts)-2];
$pathDepth = count($pathParts);
// $pathDepth.":"
if ($pathDepth > $depth) {
for ($i=0; $i<$pathDepth-$depth; $i++) {
$listMenu .= "<".$type.">";
}
$depth = $pathDepth;
$listMenu .= $h->li($h->ahref("index.php?k=".$idVal[0]."&v=".$idVal[1],$name));
} elseif ($pathDepth < $depth) {
for ($i=0; $i<$depth-$pathDepth; $i++) {
$listMenu .= "</".$type.">";
}
$depth = $pathDepth;
$listMenu .= $h->li($h->ahref("index.php?k=".$idVal[0]."&v=".$idVal[1],$name));
} else {
$listMenu .= $h->li($h->ahref("index.php?k=".$idVal[0]."&v=".$idVal[1],$name));
}
}
return $h->div($listMenu,"id='menulist'");
}
//~ menus options
function getPageBottomLinks(){
$h = $this->h;$t = $this->t;
$menu = array();
$query = SELECT."ID,NAME,TYPEPAGE".FROM.PAGES.WHERE."HIDE != 1";
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
$menu[$line['NAME']] = $h->ahref("index.php?k=".$line['TYPEPAGE']."&v=".$line['ID'],$line['NAME'],"class='bottomlinks'");
}
ksort($menu);
return implode(" ",array_values($menu));
}
function getListPagesMenu(){
$h = $this->h;$t = $this->t;
$menu = array();
$query = SELECT.PAGES.".ID,".PAGES.".NAME,".USERS.".ID USERID";
$query .= FROM.PAGES.INNERJOIN.USERS._ON_.PAGES.".USERID = ".USERS.".ID";
switch ($_SESSION['level']){
case 1:
if ($_SESSION['userid'] != 1){
// all level 2, 3 and 4 user's pages, user's pages
$query .= WHERE."(".USERS.".USERLEVEL > ".$_SESSION['level']._OR_.USERS.".ID = ".$_SESSION['userid'].")"._AND_.USERS.".ID != 1"; // but not admin articles
}// else no restriction
break;
default:
$query .= WHERE.PAGES.".USERID = ".$_SESSION['userid']._AND_.USERS.".ID != 1";
}
//~ echo $query;
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
//~ ajoutez id pour éviter les pages ayant le même nom ne s'écrasent
$NAME = str_replace(" "," ",$line['NAME']); // espaces insécables pour éviter les coupures de texte
$menu[$NAME.$line['ID']] = $h->ahref("admin.php?action=articles&resetPos=1&pageFilter=".$line['ID'],$NAME,"class='formframe'");
}
ksort($menu);
return $t->wr("Sort by page").implode(" ",array_values($menu));
}
function getListUsersMenu(){
$h = $this->h;$t = $this->t;
$menu = array();
$query = SELECT.USERS.".ID,".USERS.".USERNAME".FROM.USERS;
switch ($_SESSION['level']){
case 1:
if ($_SESSION['userid'] != 1){
// all level 2, 3 and 4 user's pages, user's pages
$query .= WHERE."(".USERS.".USERLEVEL > ".$_SESSION['level']._OR_.USERS.".ID = ".$_SESSION['userid'].")"._AND_.USERS.".ID != 1"; // but not admin articles
}// else no restriction
break;
default:
$query .= WHERE."(".USERS.".ID = ".$_SESSION['userid']._OR_.USERS.".USEROWNER = ".$_SESSION['userid'].")"._AND_.USERS.".ID != 1";
}
//~ echo $query;
$query = $this->q($query);
while ( $line = $this->fetch($query) ){
//~ ajoutez id pour éviter les pages ayant le même nom ne s'écrasent
$NAME = str_replace(" "," ",$line['USERNAME']); // espaces insécables pour éviter les coupures de texte
$menu[$NAME.$line['ID']] = $h->ahref("admin.php?action=pages&resetPos=1&ownerFilter=".$line['ID'],$NAME,"class='formframe'");
}
ksort($menu);
return $t->wr("Sort by user").implode(" ",array_values($menu));
}
//~ selection
public function pagesNavigation($pageId=0,$pageType="page",$menuStyle=null){
$home = $this->site_params();
if ($menuStyle == null) $menuStyle = $home['NAVTYPE'];
if ($menuStyle == "one_line") {
return $this->getMenuLine();
} elseif($menuStyle == "line_nav") {
return $this->getMenuPosition($pageId=$pageId);
} elseif($menuStyle == "ul_list") {
return $this->getMenuList();
} elseif($menuStyle == "ol_list") {
return $this->getMenuList($type="ol");
} elseif($menuStyle == "bottom_links") {
return $this->getPageBottomLinks();
} else { // default tree;
return $this->getMenuForm($pageId=$pageId,$pageType=$pageType);
}
}
}
?>