<?php
/**
 * Project:   astat - simple site engine
 * File:        /admin/editgroup.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.astat.org SVN: $URL$
 * @copyright 2009 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package astat core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: 47a176a1b3e4e2f04e8da6c7767ee03f82277c70 $
*/
If (!defined("in_BL_ADMIN")) {
    die("Dieses Script kann nicht ausserhalb des Frameworks laufen!");
} 
$addnav["right"]=FALSE;
$session->page_begin("editgroup", True);

class editgroup_panel extends admin_module{

	function output(){
		global $session,$config,$db,$panel,$admin,$module,$root,$firephp,$log;
			if(isset($_GET["editgroup"]) && $_GET["editgroup"]!=""){
				if(isset($_POST["send"]) && !empty($_POST["name"]) && !empty($_POST["group"])){
						if(!empty($_POST["name"])){
						$id=intval($_POST["group"]);
						$db->query("UPDATE `" . $config["prefix"] . "role` SET `role_name`='".$db->escape($_POST["name"])."' WHERE id='".$id."'");
						unset($_POST["name"]);
						unset($_POST["submit"]);
						unset($_POST["send"]);
						unset($_POST["group"]);
						$db->query("DELETE FROM `" . $config["prefix"] . "roleset` WHERE `role_id`='".$id."'");
						foreach($_POST as $key=>$value){
							$db->query("INSERT INTO `" . $config["prefix"] . "roleset` (`role_id`,`role_value_id`,`value`) VALUES ('".$id."','".intval($key)."','".$db->escape($value)."')");
						}
						$panel->admin_message("Daten &uuml;bernommen!","Die Daten wurden erfolgreich &uuml;bernommen.",True,"editgroup",3);
					}else{
						$panel->admin_message("Fehler!","Der Rollenname muss ausgef&uuml;llt werden.",True,"editgroup",3);
					}
				}else{
				
					$result = $db->query("SELECT * FROM `" . $config["prefix"] . "role` WHERE id='".intval($_GET["editgroup"])."'");
					$data = $db->fetch_array($result);
					$panel->title="Gruppe editieren";
					$panel->content='Hier kann man Benutzergruppen bearbeiten.';
					$panel->form(array("action"=>$config["path"]."/admin/index.php?panel=editgroup&amp;editgroup=".$data["id"]));
					$panel->content.="<h3>Gruppename:</h3>";
					$panel->field(array("name"=>"name","typ"=>"text","value"=>$data["role_name"]));
					$panel->content.="<br /><br />";
					$panel->content.="<table width=\"90%\" cellspacing=\"2\">
								<tr>	
								    <th><strong>Rechte</strong></th>
								</tr>";
					$result = $db->query("SELECT * FROM " . $config["prefix"] . "role_values");
					while ($row = $db->fetch_array($result)){
						$rval =$db->fetch_array($db->query("SELECT value FROM `" . $config["prefix"] . "roleset`  WHERE role_value_id='".$row["id"]."' AND role_id='".$data["id"]."'"));
						$row["value"]=$rval["value"];
						$rdata[]=$row;
					}
					foreach ($rdata as $d){
						$panel->content.="<tr><td><strong>".$d["text"].":</strong><br />".$this->get_option($d["type"],$d["id"],$d["value"])."</td></tr>";
					}
					$panel->content.="</table><br />";
					$panel->field(array("name"=>"send","typ"=>"hidden","value"=>"1"));
					$panel->field(array("name"=>"group","typ"=>"hidden","value"=>$data["id"]));
					$panel->content.="<br />";
					$panel->submit();
					$panel->formClose();
				
					$panel->parse_page();
				}
			}elseif($_GET["action"]=="delete" && $_GET["group"]){
				$result=$db->query("SELECT id FROM `" . $config["prefix"] . "role` where `role_name`='User' LIMIT 1");
				$row = $db->fetch_array($result);
				$db->query("UPDATE `" . $config["prefix"] . "users` SET `role`='".$row["id"]."' WHERE `role`='".intval($_GET["group"])."'");
				$db->query("DELETE FROM `" . $config["prefix"] . "role`  WHERE id='".intval($_GET["group"])."'");
				$db->query("DELETE FROM `" . $config["prefix"] . "roleset`  WHERE role_id='".intval($_GET["group"])."'");
				$panel->admin_message("Usergruppe gel&ouml;scht!","Die Gruppe wurde erfolgreich gel&ouml;scht.",True,"editgroup",3);
						
			}else{
				$result=$db->query("SELECT * FROM `" . $config["prefix"] . "role`");
				while ($row = $db->fetch_array($result)){
					$role[]=$row;
				}
				$panel->title="Vorhandene Rollen";
				$panel->content.="<table class=\"sortable\" width=\"90%\" cellspacing=\"2\">
							<tr>	
							    <th align=\"center\">ID</th>
							<th align=\"center\">Name</th>
							<th class=\"nosort sortcol\" align=\"center\" width=\"18%\">Aktionen</th>
							</tr>";
				if(!empty($role)){
					foreach($role as $u){
						$panel->content.="<tr>
						<td align=\"center\">".$u["id"]."</td>
						<td align=\"center\">".$u["role_name"]."</td>";
						if(!$u["special_group"]){
							$panel->content.="<td align=\"center\"><a onclick=\"return confirmLink(this, 'Willst du wirklich die Gruppe ".$u["role_name"]." l&ouml;schen? Alle Mitglieder dieser Gruppe werden der Gruppe User zugeordnet')\" href=\"".$config["path"]."/admin/index.php?panel=editgroup&amp;action=delete&amp;group=".$u["id"]."\"><img src=\"".$config["path"]."/admin/images/icons/delete.png\" /></a> <a href=\"".$config["path"]."/admin/index.php?panel=editgroup&amp;editgroup=".$u["id"]."\"><img src=\"".$config["path"]."/admin/images/icons/wrench.png\" /></a></td>
						</tr>";
						}else{
						$panel->content.="<td align=\"center\"><a href=\"".$config["path"]."/admin/index.php?panel=editgroup&amp;editgroup=".$u["id"]."\"><img src=\"".$config["path"]."/admin/images/icons/wrench.png\" /></a></td></td>
						</tr>";
						}
					}
				}else{
				$panel->content.="<tr>
						<td align=\"center\" colspan=\"5\">Keine Rollen vorhanden.</td>
						</tr>";
				}
				$panel->content.="</table><br />";
		}
	}

	function get_option($option,$name,$value) {
		global $config,$db,$panel,$userinfo,$admin,$root,$firephp;
 		
		if($option=="yesno"){
			$checked["yes"]="";
			$checked["no"]="";
			if($value==1||$value==True||$value=="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($value==1||$value==True||$value=="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=="text"){
			$text=$panel->field(array("name"=>$name,"typ"=>"text","value"=>$value),"",TRUE);
			return $text;
		}

		if(strstr($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"=>$value, $a_name[0]=>$value[0],$a_name[1]=>$value[1]),"class=\"mceEditor\"",TRUE);
			}else{
				$text=$panel->textarea(array("name"=>$name,"value"=>$value),"class=\"mceEditor\"",TRUE);
			}
			return $text;
		}

		if(strstr($option,"textarea")){
			$string=str_replace("textarea(","",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"=>$value, $a_name[0]=>$value[0],$a_name[1]=>$value[1]),"",TRUE);
			}else{
				$text=$panel->textarea(array("name"=>$name,"value"=>$value),"",TRUE);
			}
			return $text;
		}

		if(strstr($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,$value,$name,"",TRUE);
			return $select;
		}
		
	}
	
	function meta() {
			global $module,$config,$db,$panel,$userinfo,$admin,$root;
			$meta='<script type="text/javascript" src="'.$config["path"].'/js/scriptaculous/prototype.js"></script>
				<script src="'.$config["path"].'/js/fastinit.js" type="text/javascript">
				</script>
				<script src="'.$config["path"].'/js/tablesort.js" type="text/javascript">
				</script>
				<script type="text/javascript">
				function deleteItem(item) {
					if(confirmLink(this, "Willst du wirklich dieses Modul deinstallieren? Alle Daten diese Moduls werden gel&ouml;scht!")==true){
					name="id_"+item;
					Effect.SwitchOff(name);
					new Ajax.Request(\'index.php?panel=navigation\' , {
					method: "post",
					parameters: {delete:name}});
					}
					return false;
				}
				
				</script>
			<style type="text/css">
				.loadlayer {
					position: absolute;
					padding: 15px;
					width: 250px;
					background: #ffffff;
					border: 3px solid #c90000;
					text-align: center;
					margin-top: 60px;
					left: 50%;
					margin-left: -150px;
					margin-top: 100px;
					font-weight: bold;
					z-index:5;
				}
				div.section,div#createNew {
					border: 1px solid #CCCCCC;
					margin: 30px 5px;
					padding: 0px 0px 10px 0px;
					background-color: #EFEFEF;
				}

				div#createNew input { margin-left: 5px; }

				div#createNew h3, div.section h3{
					font-size: 14px;
					padding: 2px 5px;
					margin: 0 0 10px 0;
					background-color: #CCCCCC;
					display: block;
				}

				div.section h3 {
					cursor: move;
				}

				div.lineitem {
					margin: 3px 10px;
					padding: 2px;
					background-color: #FFFFFF;
					cursor: move;
				}

				h1 {
					margin-bottom: 0;
					font-size: 18px;
				}
			</style>
			<script type="text/javascript">
				function confirmLink(theLink, text)
				{
				// Confirmation is not required in the configuration file
				// or browser is Opera (crappy js implementation)
				if (typeof(window.opera) != \'undefined\') {
				return true;
				}
				
				var is_confirmed = confirm(text);
				if (is_confirmed) {
				if ( typeof(theLink.href) != \'undefined\' ) {
				theLink.href += \'&is_js_confirmed=1\';
				} else if ( typeof(theLink.form) != \'undefined\' ) {
				theLink.form.action += \'?is_js_confirmed=1\';
				}
				}
				
				return is_confirmed;
				} 
				</script>';
			return $meta;
				
		}
}
?>
