<?php global $session;
/**
 * Project:   BeCast WebEngine - simple site engine
 * File:      /admin/config.apnl.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: fd17a9f6fd69d06ce15ec735ef227ebeda2d652e $
 */
If (!defined("IN_BCWE_ADMIN")) {
    die("Dieses Script kann nicht ausserhalb des Frameworks laufen!");
} 
$addnav["right"]=FALSE;
$session->page_begin("config", True);


class config_panel extends admin_module{

	function output(){
		global $module,$config,$db,$panel,$admin,$module,$root,$firephp,$log,$lang;
		
			if(isset($_POST["send"]) && $_POST["send"]==1){
				foreach($_POST as $post=>$value){
					if($post!="send" and $config[$post]!=$value){
						$field=$db->escape($post);	
						$update=$db->escape($value);		
						$db->query("UPDATE " . $config["prefix"] . "config SET `value`='".$update."' WHERE `name`='".$field."'");
					}
				}
				$result = $db->query("SELECT name, value FROM " . $config["prefix"] . "config");
				while ($row = $db->fetch_array($result)){
				    	$config[$row["name"]] = $row["value"];
				    }
			}
			$result = $db->query("SELECT * FROM `" . $config["prefix"] . "config` v LEFT JOIN `" . $config["prefix"] . "config_categories` c ON v.`category`=c.`cid`");
			while ($row = $db->fetch_array($result)){
				$data[$row["categoryname"]][]=$row;
			}
			$panel->title="Konfiguration";
			$panel->form(array("action"=>$config["path"]."/admin/index.php?panel=config"));
			$panel->content.="<div id=\"vertical_container\">";
			foreach($data as $key=>$val){
				$panel->content.="<h2 class=\"accordion_toggle\">".$key."</h2>
				<div class=\"accordion_content\"><table width=\"700px\">";
				foreach($val as $v){
					$panel->content.="<tr><td><strong>".$v["title"]."</strong><br /><span style=\"font-size:small;\">".$v["description"]."</span><br />".$this->get_option($v["option"],$v["name"])."</td></tr>";
				}
				
				$panel->content.="</table></div>";
			}
			$panel->content.="</div><br /><br />";
			$panel->field(array("typ"=>"hidden","name"=>"send","value"=>1));
			$panel->submit();
			$panel->formClose();
			$panel->foot.='<script type="text/javascript">
			var bottomAccordion = new accordion(\'vertical_container\');
			//bottomAccordion.activate($$(\'vertical_container .accordion_toggle\')[0]);
			</script>';
	}

	function get_option($option,$name) {
		global $config,$db,$panel,$userinfo,$admin,$root,$firephp,$lang;
 		
		if($option=="yesno"){
			$checked["yes"]="";
			$checked["no"]="";
			if($config[$name]==1||$config[$name]==True||$config[$name]=="yes"){
				$checked["yes"]='checked="checked"';
			}else{
				$checked["no"]='checked="checked"';
			}
			$yn=$panel->radio(array("name"=>$name,"value"=>"1"),$checked["yes"],TRUE);
			$yn.="Ja";
			$yn.=$panel->radio(array("name"=>$name,"value"=>"0"),$checked["no"],TRUE);
			$yn.="Nein";
			return $yn;
		}
		if($option=="onoff"){
			$checked["on"]="";
			$checked["off"]="";
			if($config[$name]==1||$config[$name]==True||$config[$name]=="on"){
				$checked["on"]='checked="checked"';
			}else{
				$checked["off"]='checked="checked"';
			}
			$yn=$panel->radio(array("name"=>$name,"value"=>"1"),$checked["on"],TRUE);
			$yn.="An";
			$yn.=$panel->radio(array("name"=>$name,"value"=>"0"),$checked["off"],TRUE);
			$yn.="Aus";
			return $yn;
		}

		if($option=="language"){
			$langs=$lang->getlanguages();
			foreach($langs as $lang){
				$values[$lang['name']]=$lang['short'];
			}
			$select=$panel->select($values,$config[$name],$name,"",TRUE);
			return $select;
		}

		if($option=="text"){
			$text=$panel->field(array("name"=>$name,"typ"=>"text","value"=>$config[$name]),"",TRUE);
			return $text;
		}

		if(str_contains($option, "wysiwyg")){
			$string=str_replace("wysiwyg(","",substr($option, 0, -1));
			$sarr=explode(";",$string);
			foreach($sarr as $str){
				$substr=explode("|",$str);
				$a_name[]=$substr[0];
				$value[]=$substr[1];
			}
			if(is_array($sarr)){
				$text=$panel->textarea(array("name"=>$name,"value"=>$config[$name], $a_name[0]=>$value[0],$a_name[1]=>$value[1]),"class=\"mceEditor\"",TRUE);
			}else{
				$text=$panel->textarea(array("name"=>$name,"value"=>$config[$name]),"class=\"mceEditor\"",TRUE);
			}
			return $text;
		}

		if(str_contains($option, "textarea")){
			$string=str_replace("textarea(","",substr($option, 0, -1));
			$sarr=explode(";",$string);
			if(count($sarr)>1) {
				foreach ($sarr as $str) {
					$substr = explode("|", $str);
					$a_name[] = $substr[0];
					$value[] = $substr[1];
				}
				$text=$panel->textarea(array("name"=>$name,"value"=>$config[$name], $a_name[0]=>$value[0],$a_name[1]=>$value[1]),"",TRUE);
			}else{
				$text=$panel->textarea(array("name"=>$name,"value"=>$config[$name]),"",TRUE);
			}
			return $text;
		}

		if(str_contains($option, "select")){
			//$text=$panel->field("text",$name,"value=\"".$config[$name]."\"",TRUE);
			$string=str_replace("select(","",substr($option, 0, -1));
			$sarr=explode(";",$string);
			foreach($sarr as $str){
				$substr=explode("|",$str);
				$values[$substr[0]]=$substr[1];
			}
			$select=$panel->select($values,$config[$name],$name,"",TRUE);
			return $select;
		}
		
	}

		function meta() {
			global $module,$config,$db,$panel,$userinfo,$admin,$root;
			$meta='<script type="text/javascript" src="/thirdparty/tiny/tiny_mce_gzip.js"></script>
				<script type="text/javascript">
				tinyMCE_GZ.init({
				theme : "advanced",
				mode : "textareas",
				language : "de",
				plugins : "table,advhr,advimage,advlink,insertdatetime,searchreplace",
				disk_cache : true,
				debug : false
				});
				</script>
			<script type="text/javascript" src="/thirdparty/tiny/tiny_mce.js"></script>
			<script type="text/javascript">
			tinyMCE.init({
				theme : "advanced",
				mode : "textareas",
				language : "de",
				plugins : "table,advhr,advimage,advlink,insertdatetime,searchreplace",
				theme_advanced_disable : "styleselect,formatselect ",
				theme_advanced_buttons1_add : "fontselect,fontsizeselect",
				theme_advanced_buttons2_add : "seperator,forecolor,removeformat,advhr",
				theme_advanced_buttons3 : "tablecontrols,seperator",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "center",
				remove_linebreaks : false,
				convert_urls : false,
				editor_selector : "mceEditor",
				editor_deselector : "mceNoEditor"
			});
			</script>

				<script type="text/javascript" src="'.$config["path"].'/js/scriptaculous/prototype.js"></script>
				<script type="text/javascript" src="'.$config["path"].'/js/scriptaculous/effects.js"></script>
				<script type="text/javascript" src="'.$config["path"].'/js/scriptaculous/accordion.js"></script>';
			return $meta;
				
		}

	
}
?>
