<?php
/* Ulyxex version 1.6.7.8 ***************/
/* code http://ulyxex.logz.org **********/
/* Andre Lozano http://andre-lozano.org */
class Pages extends Admin {
	public int $pagePos;
	public string $Select;
	public string $From;
	public string $SelectCount;
	public string $OrderSort;
	public array $Cond;
	public array $sortMethods;
	public array $pageArtsTotal;
	
	public function __construct(){
		$this->h = New Htmlz();
		$this->t = New Translate();
		if (isset($_GET['pg'])) {
			$this->pagePos = $this->h->num($_GET['pg']);
			$_SESSION['pgp'] = $_GET['pg'];
		} elseif (isset($_GET['resetPos']))  {
			$this->pagePos = 0;
		} elseif (isset($_SESSION['pgp']) && !empty($_SESSION['pgp']))  {
			$this->pagePos = $this->h->num($_SESSION['pgp']);
			$_GET['pg'] = $_SESSION['pgp'];
		} else {
			$this->pagePos = 0;
		}
		$this->Select = SELECT.
							PAGES.".ID,".PAGES.".NAME,".PAGES.".HIDE,".PAGES.".TYPEPAGE,".PAGES.".DATEPAGE,".PAGES.".PARENTID,".PAGES.".PRIORITY,".
							USERS.".USERNAME,".USERS.".ID USERID,".USERS.".USEROWNER,".USERS.".USERLEVEL";
		$this->From = 	FROM.
							PAGES.
							JOIN.USERS._ON_.PAGES.".USERID = ".USERS.".ID";
		$this->SelectCount = 	SELECT."COUNT(".PAGES.".ID) AS NUM ";
		$this->OrderSort = "";
		$this->Cond = array();
		$this->sortMethods = array("",PAGES.".DATEPAGE DESC",PAGES.".NAME",USERS.".USERNAME",PAGES.".PRIORITY",PAGES.".HIDE DESC","");  
		if (!isset($_SESSION['admin_page_sort'])) $_SESSION['admin_page_sort'] = $this->sortMethods[1];
		$this->pageArtsTotal = $this->sum_arts_by_pages();
	}
	public function update_pages(){
		$h = $this->h;$t = $this->t;
		$formValues = array();
		$values = $_POST;
		foreach($values as $k=>$v){
			if ( preg_match("/\_/",$k) ){
				list($key,$id) = explode("_",$k);
				$formValues[$id][$key] = $v;
			}
		}
		foreach($formValues as $id=>$value){
			if (isset($value['delete'])){
				// put articles is sandbox
				$query = UPDATE.ARTICLES.SET."PAGEID = 2".WHERE."PAGEID = ".$id;
				$query = $this->q($query);
				// new parent id is sandbox
				$query = UPDATE.PAGES.SET."PARENTID = 2".WHERE."PARENTID = ".$id;
				$query = $this->q($query);
				$query = DELETE.FROM.PAGES.WHERE."ID = ".$id;
				$query = $this->q($query);
				
			}
			if (isset($value['hide']) && $value['oldhide'] == 0){
				$query = UPDATE.PAGES.SET."HIDE = 1".WHERE."ID = ".$id;
				$query = $this->q($query);
			} elseif (!isset($value['hide']) && $value['oldhide'] == 1){
				$query = UPDATE.PAGES.SET."HIDE = 0".WHERE."ID = ".$id;
				$query = $this->q($query);
			}
		}
		return $h->h3($t->w("some vals been modified !"),$h->v("class","warning"));
	}
	public function list_pages(){
		$h = $this->h;$t = $this->t;
		$tree = New Tree();
		$paths = $tree->paths();
		$pathById = array();
		$triangleDown = "▿";
		$triangleUp = "▵";
		$separator = " ⤷ ";
		foreach ($paths as $pth=>$arr) {
			$pathById[$arr[1]] = $pth;
		}
		$pgPos =($this->pagePos)?($this->pagePos - 1) * ITEMS:0;
		if (isset($_POST['sort'])) $_SESSION['admin_page_sort'] = $this->sortMethods[$_POST['sort']];
        if (isset($_GET['ownerFilter'])) $_SESSION['admin_pages_filter'] = PAGES.".USERID = ".$h->num($_GET['ownerFilter']);
		if (isset($_POST['search_title'])) $_SESSION['admin_pages_search_title'] = " MATCH(".PAGES.".NAME,".PAGES.".DESCRIPTION) AGAINST('".$h->safe($_POST['search_title'])."') ";
		if (isset($_POST['reset'])){
			if (isset($_SESSION['admin_pages_filter'])){$_SESSION['admin_pages_filter'] = "";}
			if (isset($_SESSION['admin_pages_search_title'])){$_SESSION['admin_pages_search_title'] = "";}
			if (isset($_SESSION['admin_page_sort'])) $_SESSION['admin_page_sort'] = $this->sortMethods[1];
		}
		// always page position = 1 if use search, only ITEMS first results
		if ( isset($_SESSION['admin_pages_search_title']) && !empty($_SESSION['admin_pages_search_title']) ) $pgPos = 1;
		$this->OrderSort = ORDERBY.$_SESSION['admin_page_sort']." LIMIT ".$pgPos.",".ITEMS;
		
		// 1) can create users etc.
		// 2) can create articles and pages
		// 3) can create articles everywhere
		// 4) can create articles only in sandbox
		switch ($_SESSION['level']){
			case 1: 
				if ($_SESSION['userid'] != 1){
					$this->Cond[] = "(".USERS.".USERLEVEL > ".$_SESSION['level']._OR_.USERS.".ID = ".$_SESSION['userid'].")"; // all level 2, 3 and 4 user's pages, user's pages 
					$this->Cond[] = USERS.".ID != 1"; // but not admin articles
				}
				if (isset($_SESSION['admin_pages_filter']) && !empty($_SESSION['admin_pages_filter']) ) $this->Cond[] = $_SESSION['admin_pages_filter'];
				break;
			case 2:
				$this->Cond[] = PAGES.".USERID = ".$_SESSION['userid'];
				if (isset($_SESSION['admin_pages_filter']) && !empty($_SESSION['admin_pages_filter']) ) $this->Cond[] = $_SESSION['admin_pages_filter']; 
				break;
		}
		
		//~ SEARCH !!!
		$srchOff = true;
		if ( isset($_SESSION['admin_pages_search_title']) && !empty($_SESSION['admin_pages_search_title']) ){
			$query = SELECT.PAGES.".ID,".PAGES.".NAME,".PAGES.".HIDE,".PAGES.".TYPEPAGE,".PAGES.".DATEPAGE,".PAGES.".PARENTID,".PAGES.".PRIORITY".FROM.PAGES;
			if ($_SESSION['userid'] == 1){
				$query .= WHERE.$_SESSION['admin_pages_search_title'];
			} else {
				$_ = array();
				$_[] = $_SESSION['admin_pages_search_title'];
				$_[] = PAGES.".USERID = ".$_SESSION['userid'];
				$query .= WHERE.$this->Do_And($_);
			}
			$srchOff = false;
		} else {
			$query = $this->Select.$this->From.$this->Where($this->Cond).$this->OrderSort;
			$srchOff = true;
		}
		$query = $this->q($query,true);
		
		$sort = array($t->w("date")=>1,$t->w("title")=>2,$t->w("user")=>3,$t->w("priority")=>4,$t->w("hidden")=>5);
		$type = array_search($_SESSION['admin_page_sort'],$this->sortMethods);
		$inputs = $h->p($h->select($sort,"sort",$type,$h->alt($t->w("sort/filter by")))." ".$h->input("submit",null,$t->w("sort/filter by")));
		$sortForm = $h->form($inputs,"sort","post","admin.php?action=pages");
		
		$searchValue = (isset($_POST['search_title']))?$_POST['search_title']:"";
		$searchForm = $this->search_form("search_title","search by title","pages",$searchValue);
		$goIdForm = $this->go_id("page","modify_page");
		$menuUsers = $h->p($tree->getListUsersMenu($via="pages"),$h->v("class","sortbyusers"));
		$resetForm = $this->reset_form("pages");
		
		$pageListSubmit = $h->p($h->input("submit",null,$t->w("update all")).$h->input("hidden","update",1));
		
		$pageListTitle = $h->h3($t->w("pages list")." ".$h->ahref("admin.php?action=add_page",$t->w("add page"),$h->v("class","addnew")));
		$pageList = "";
		while ( $line = $this->fetch($query) ){
			$id = "id".$h->f4d($line['ID']);
			$date = substr($line['DATEPAGE'],0,10);
			$arts =(isset($this->pageArtsTotal[$line['ID']]))
				?
				$h->ahref(
					"admin.php?action=articles&amp;resetPos=1&amp;pageFilter=".$line['ID'],
					$h->f4d($this->pageArtsTotal[$line['ID']]),
					$h->alt($t->wr("show").$t->wz("articles by page").$this->pageArtsTotal[$line['ID']])
				)
				:
				$h->f4d(0)
				;
			
			$pagesTypes = array("mix","search","random","liste");
			$arts = "arts".$arts;
			$checked = ($line['HIDE'])?$h->v("checked","checked"):null;
			$hide = $h->span(
				$h->lbl($t->wr("hide"),"hide_".$line['ID']).
				$h->input("checkbox","hide_".$line['ID'],"on",$checked).
				$h->input("hidden","oldhide_".$line['ID'],$line['HIDE'])
			,$h->alt($t->wz("hide").$line['NAME']));
			if ($line['TYPEPAGE'] == "page") { // visualisation of type, show type 
				$type = null;
			} elseif ($line['TYPEPAGE'] == "link"){
				$type = $h->span($t->w("link"),$h->v("style","color:DarkGreen;font-weight:bold;margin-left:2px;").$h->alt($t->w("redirect to link")));
			} elseif (in_array($line['TYPEPAGE'],$pagesTypes)){
				$type = $h->span($line['TYPEPAGE'],$h->v("style","color:DarkGreen;font-weight:bold;margin-left:2px;").$h->alt($t->wz("type").$line['TYPEPAGE']));
			} else {
				$type = $h->span($line['TYPEPAGE'],$h->v("style","color:DarkGreen;font-weight:bold;margin-left:2px;").$h->alt($t->wz("custom page").$line['TYPEPAGE']));
			}
			$modify = $h->ahref(
				"admin.php?action=modify_page&amp;page=".$line['ID'],$t->w("modify"),
				$h->v("class","linkbtn").$h->alt($t->wz("modify").$line['NAME'])
			);
			$delete = ($line['NAME'] == "SANDBOX")
				?
				$h->span(
					$h->span(
						$t->wlr("del").$h->input("checkbox","hidden-input",null,$h->v("readonly","readonly").$h->v("style","visibility: hidden;"))
						,$h->v("style","visibility: hidden;")
					)
				,$h->alt($t->w("cannot be deleted"))
				)
				:
				$h->span(
					$h->lbl($t->wlr("del"),"delete_".$line['ID']).$h->input("checkbox","delete_".$line['ID'],"on")
				,$h->alt($t->wz("delete").$line['NAME']))
				;
			$ahrefTitle = $t->wz("show").$line['NAME'];
			if (isset($pathById[intval($line['ID'])])) {
				$lineage = $tree->ripPriorityPath($pathById[intval($line['ID'])]);
				if ($lineage) $ahrefTitle .= "\n".$t->wz("lineage").$lineage;
				if (in_array($line['ID'],$tree->hasChild)) {
					$childs = implode(", ",$tree->childs[$line['ID']]);
					$ahrefTitle .= "\n".$t->wz("descendants").$line['NAME'].$separator.$childs;
				}
			} else {
				$ahrefTitle .= "\n".$t->wz("lineage")."error: no path found for parent page id-".$line['PARENTID']." !";// prevent orphan page
			}
			$triangleOrNot = "";
			$triangleOrNot .= empty($tree->hasAncestor[$line['ID']])?"":$triangleUp;
			$triangleOrNot .= in_array($line['ID'],$tree->hasChild)?$triangleDown:"";
			$hidecss = ($line['HIDE'])?$h->v("style","color:Gray;"):"";
			$view = $t->wx("title").
				$h->ahref(
					"index.php?k=".$line['TYPEPAGE']."&amp;v=".$line['ID'],
					$h->strWidth(strip_tags($line['NAME'].$triangleOrNot),28),
					$h->alt($ahrefTitle).$h->v("class","linkbtn").$h->v("target","blank").$hidecss
				);
			
			$priority = ($line['PRIORITY'] != "99")?$this->dashframe("^".$line['PRIORITY'],$h->alt($t->wr("priority").$line['PRIORITY'])):"";
			
			// only level 1 can browser pages of differents users
			if ($_SESSION['level'] == 1){
				$by = ($srchOff)
					?
					$t->wx("by").$h->ahref(
						"index.php?k=user&v=".$line['USERID'],
						$h->strWidth($line['USERNAME'],12),
						$h->v("class","linkbtn").
						$h->v("target","blank").
						$h->alt($t->wz("show").
						$line['USERNAME'].
						" [".$t->w("level").":".$line['USERLEVEL']."][id:".$line['USERID']."]")
					)
					:
					""
					;
			} else {
				$by = "";
			}
			
			$_ = array($this->dashframe($id." "),$this->dashframe($date." "),$this->dashframe($arts." "),$this->dashframe($hide." "),$modify,$this->dashframe($delete),$view,$by,$priority,$type);
			$content = implode("",$_);
			$pageList .= $h->p($content,$h->v("class","linebtns"));
		}
		
		$nextPages = ($srchOff)?$this->next_pages():"";
		$pageList = $pageListTitle.$pageListSubmit.$pageList.$pageListSubmit.$nextPages;
		return $sortForm.$searchForm.$goIdForm.$menuUsers.$resetForm.$h->form($pageList,"pages","post","admin.php?action=pages&amp;pg=".$this->pagePos);
	}
	public function next_pages(){
		$h = $this->h;$t = $this->t;
		$query = $this->SelectCount.$this->From.$this->Where($this->Cond);
		$query = $this->q($query);
		$dat = $this->fetch($query);
		$pagesNumbers = new NextPages($dat['NUM'],"action=pages");
		$result = $h->p($pagesNumbers->numbers(),$h->v("class","pagenumbers"));
		$result .= $h->p("(".$t->wr("total pages")."= ".$dat['NUM'].")",$h->v("style","text-align:center"));
		//~ $result .= $h->p("(".$t->wr("total articles")."= ".array_sum($this->pageArtsTotal).")",$h->v("style","text-align:center"));
		return $result;
	}
}
?>

page url