<?php
/**
 * Project:   BeCast Webengine - simple site engine
 * File:      /inc/panel.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.becast.at
 * @copyright 2009-2025 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package BcWe core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: a91f72e7b7e337cdee82c948c49485869c693178 $
*/

$module["panel"]["name"]="Panel Class";
$module["panel"]["ver"]="0.3.7";
class panel {
	var string $sidebar="";
	var string $page="";
	var string $bodyext="";
	var string $title="";
	var string $content="";
	var string $meta="";
	var string $form="";
	var string $foot="";
	var string $head="";
 	var string $menu_items ="";

	function page($additional_title="",$meta=""){
		global $config,$db,$core;
		return '<!DOCTYPE html>
				<html lang="de">
				<head>
				<title>'.$config['sitetitle'].'</title>
				<link rel="stylesheet" type="text/css" href="admin.css" />
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				'.$this->meta.'
				</head>
				<body '.$this->bodyext.'>
				<div id="container">
				'.$this->menu().'
				<div id="content">
				'.$this->page.'
				</div>
				<div id="footer">
				&copy; 2016 - 2025 Becast.at | mySQL Queries: '.$db->querys().'
				<br/>
				Powered by
				<a href="http://www.becast.at">BeCast WebEngine</a>
				'.$core->ver_major.'.'.$core->ver_minor.".".$core->ver_rev.' - '.$core->ver_codename.'
				</div>
				</div>
				'.$this->foot.'
				</body>
				</html>';	
   	}
	
	function parse_page(){
		$this->page.="<h2>".$this->title."</h2>
			<p>".$this->content."</p>";
		$this->title="";
		$this->content="";
		echo $this->page();
		exit();
    	}
	
	function menu_item($panel,$text, $img=""): void
    {
		global $config, $userdata, $userinfo, $session, $meta;
		if($panel=="group_only"){
			$this->menu_items.="<li><strong>".$text."</strong></li>";
		}elseif($panel=="admin_home"){
			$this->menu_items.="<li><a href=\"".$config["path"]."/admin/index.php\"><img src=\"".$config["path"]."/admin/images/icons/".$img.".png\" alt='".$img."'> ".$text."</a></li>";
		}elseif($panel=="page_home"){
			$this->menu_items.="<li><a href=\"".$config["path"]."/index.php\"><img src=\"".$config["path"]."/admin/images/icons/".$img.".png\" alt='".$img."'> ".$text."</a></li>";
		}else{
			if($img!=""){
				$this->menu_items.="<li><a href=\"".$config["path"]."/admin/index.php?panel=".$panel."\"><img src=\"".$config["path"]."/admin/images/icons/".$img.".png\" alt='".$img."'> ".$text."</a></li>";
			}else{
				$this->menu_items.="<li><a href=\"".$config["path"]."/admin/index.php?panel=".$panel."\">".$text."</a></li>";
			}
	   	}
	}
		
	function admin_message($title,$message,$redirect=FALSE,$panel="",$time="4"): void
    {
		global $config,$session,$lang;
		$session->page_begin("Admin Message", True);
		if($panel!="" && $redirect){
			$this->content=$message."<br /><a href=\"".$config["path"]."/admin/index.php?panel=".$panel."\">".$lang->_('CLICKREDIRECT')."</a>";
		}elseif($panel!="" && !$redirect){
			$this->content=$message."<br /><a href=\"".$config["path"]."/admin/index.php?panel=".$panel."\">".$lang->_('CLICKCONTINUE')."</a>";
		}else{
			$this->content=$message;
		}
		if($redirect && $panel!="")		
		$this->meta.="<meta http-equiv=\"refresh\" content=\"".$time."; URL=".$config["path"]."/admin/index.php?panel=".$panel."\" />";
		$this->title=$title;
		$this->parse_page();

	}

	function menu(): string
    {
	
	return '<div id="menu">
		<ul>'.$this->menu_items.'</ul></div>';

   	}

	function form ($args = array(),$return=FALSE,$extra=""){

		$method = $args["method"] ?? 'POST';
		$action = $args["action"] ?? $_SERVER['PHP_SELF'];
		$form = '<form action="' .$action. '" method="' .$method. '"' .$extra. '>'; 
		if(!$return){
			$this->content .= $form; 
		}else{
			return $form;
		}
	}
	function formClose ($return=FALSE)
	{

		$form = '</form>'; 
		if(!$return){
			$this->content .= $form; 
		}else{
			return $form;
		}
	}

	function button ($text = '', $name='', $extra='', $return=FALSE)
	{

		$fld = '<input type="button" name="'.$name.'" value="'.$text.'" ' .$extra. ' />';
		if(!$return){
			$this->content .= $fld; 
		}else{
			return $fld;
		}
	}

	function radio ($args=array(),$extra="",$return=FALSE)
	{
		$name = $args["name"] ?? 'radiobutton';
		$value = $args["value"];
		if($value!=""){
			$extra .= ' value="'.$value.'"';
		}
		$fld=$this->radioOrCheck ("radio",$name,$extra,$return);
		if($return){
			return $fld;
		}
	}
	function checkbox ($args=array(),$extra="",$return=FALSE)
	{
		$name = $args["name"] ? $args["name"] : 'checkbox';
		$value = $args["value"];
		if($value!=""){
			$extra .= ' value="'.$value.'"';
		}
		$fld=$this->radioOrCheck ("checkbox",$name,$extra,$return);
		if($return){
			return $fld;
		}
	}
	/* private */
	function radioOrCheck ($type,$name="",$extra="",$return=FALSE)
	{
		$fld = '<input type="'.$type.'" name="'.$name.'" ' .$extra. ' />';
		if(!$return){
			$this->content .= $fld; 
		}else{
			return $fld;
		}
	}

	function field ($args=array(),$extra="",$return=FALSE)
	{
		$typ = $args["typ"] ?? 'text';
		$name = $args["name"] ?? 'textfield';
		$value = $args["value"] ?? '';
		$fld = '<input type="' .$typ. '" name="' .$name. '" value="' .$value. '"' .$extra. ' />';  // html: form field
		if(!$return){
			$this->content .= $fld; 
		}else{
			return $fld;
		}
	}
	function submit ($args=array(),$extra="",$return=FALSE)
	{
		$name = $args["name"] ?? 'submit';
		$fld = '<input type="submit" name="'.$name.'" ' .$extra. ' />';  // html: form submit button
		if(!$return){
			$this->content .= $fld; 
		}else{
			return $fld;
		}
	}

	/*
		$args[value] used for default text (as in "field" function)
	*/
	function textarea ($args = array(),$extra="",$return=FALSE)
	{
		$name = $args["name"] ?? 'field';
		$rows = $args["rows"] ?? '3';
		$cols = $args["cols"] ?? '30';
		$text = $args["value"] ?? '';

		$fld = '<textarea name="' .$name. '" rows="' .$rows. '" cols="' .$cols. '"' .$extra. '>' .$text. '</textarea>';
		if(!$return){
			$this->content .= $fld; 
		}else{
			return $fld;
		}
	}

	/*
		$values = array containing values (key = processing value, data = displaying value)
	*/
	function select ($values=array(),$selected="",$name="",$extra="",$return=FALSE)
	{
		$fld='<select name="'.$name.'" '.$extra.'>';
		foreach($values as $name=>$val){
			if($val==$selected){			
				$fld.='<option value="'.$val.'" selected="selected">'.$name.'</option>';
			}else{
				$fld.='<option value="'.$val.'">'.$name.'</option>';
			}
		}
		$fld.="</select>";
		if(!$return){
			$this->content .= $fld; 
		}else{
			return $fld;
		}
	}

}
?>
