<?php global $session;
/**
 * Project:   astat - simple site engine
 * File:        /modules/text/edit.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 2025 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: f08e37ad7fd548fac8baccaa9356e622287ad9ca $
*/
If (!defined("IN_BCWE_ADMIN")) {
    die("Dieses Script kann nicht ausserhalb des Frameworks laufen!");
} 
$session->page_begin("edit Text", True);

class edit_text_panel extends admin_module{
 	
	function output(){
		global $config, $db,$panel, $session, $admin;
				$fail=FALSE;
			if(isset($_POST['send'])&& $_POST['send']==1){

				if(isset($_POST['id'])&& $_POST['id']!=""||isset($_POST['text'])&& $_POST['text']!=""||isset($_POST['url'])&& $_POST['url']!=""||isset($_POST['title'])&& $_POST['title']!=""){
					$id=intval($_POST['id']);
					$url=$db->escape($_POST['url']);
					$title=$db->escape($_POST['title']);
					$mid=intval($_POST['menue']);
					$result = $db->query("SELECT count(`id`) as `count` FROM `" . $config["prefix"] . "article` WHERE `url`='".$url."' AND `id`<>'".$id."'");
					$row = $db->fetch_array($result);

					if($row["count"]==0){
						$db->query("UPDATE `" . $config["prefix"] . "article` SET `text`='".$db->escape($_POST["text"])."',`menue`='".$mid."',`title`='".$title."',`url`='".$url."',`active`='".$_POST["active"]."',`eid`='".$session->userdata["uid"]."',`edittime`='".time()."' WHERE `id`='".$id."'");
						$db->query("DELETE FROM `" . $config["prefix"] . "article_category` where `a_id`='".$id."'");
						if($_POST["cats"]!=""){
							foreach($_POST["cats"] as $nid){
								$db->query("INSERT INTO `" . $config["prefix"] . "article_category` (`a_id`,`c_id`) VALUES ('".$id."','".$nid."')");
							}
						}
						$panel->admin_message("Danke", "Artikel erfolgreich ge&auml;ndert!",TRUE,"edit_text");
					}else{
						$fail="<span style=\"text-align: center; color: red;\">Diese URL ist vergeben!</span>";
					}
				}else{
						$fail="<span style=\"text-align: center; color: red;\">Du musst alle Felder ausf&uuml;llen!</span>";
				}
			
			}elseif(isset($_GET['id'])&& $_GET['id']!=""){
				if($_GET['action']=="edit"){
					$result = $db->query("SELECT * FROM " . $config["prefix"] . "article WHERE id='".intval($_GET['id'])."'");
					$_POST=$db->fetch_array($result);

					
					$panel->title="Text editieren";
					$panel->form(array("action"=>$config["path"]."/admin/index.php?panel=edit_text"));
					if($fail){
					$panel->content.=$fail;
					}
					$panel->content.="<h3>Titel:</h3>";
					$panel->field(array("name"=>"title","typ"=>"text","value"=>$_POST["title"]));
					$panel->content.="<h3>Url:</h3>";
					$panel->field(array("name"=>"url","typ"=>"text","value"=>$_POST["url"]));
					$panel->content.=".html";
					$resultc=$db->query("SELECT `id`,`name` FROM `" . $config["prefix"] . "article_menue`") or die($db->error());
					$karray["Keines"]="";
					while($row=$db->fetch_array($resultc)){
						$karray[$row["name"]]=$row["id"];
					}
					$panel->content.="<h3>Men&uuml;:</h3>";
					$panel->select($karray,$_POST["menue"],"menue");			
					$panel->content.="<br />";	
					$panel->content.="<h3>Kategorien:</h3>
					<table width=\"100%\">
					<tr>";
					$resultn = $db->query("SELECT `c_id` FROM `" . $config["prefix"] . "article_category` WHERE `a_id`='".intval($_GET['id'])."'");
					while($rowa=$db->fetch_array($resultn)){
						$cat[$rowa["c_id"]]=TRUE;
					}
					$resultk=$db->query("SELECT `id`,`categoryname` FROM `" . $config["prefix"] . "article_categories` WHERE `active`='true'");
					while($row=$db->fetch_array($resultk)){
						if($cat[$row["id"]]==TRUE){
							$row["checked"]="checked=\"checked\"";
						}
						$navs[]=$row;
					}
					$i=0;
					if(is_array($navs)){
						foreach($navs as $n){
							if($i % 5==0){
								$panel->content.="</tr><tr>";
							}
								$panel->checkbox(array("name"=>"cats[]","value"=>$n["id"]),$n["checked"]);
								$panel->content.=$n["categoryname"];
								$i++;
						}
					}else{
						$panel->content.="Keine Kategorien angelegt.";
					}
					$panel->content.="</tr>
					</table></br>";
					$panel->content.="<h3>Text:</h3>";
					$panel->textarea(array("name"=>"text","value"=>$_POST["text"],"rows"=>"30","cols"=>"80"),"class=\"mceEditor\"");
					$panel->content.="<h3>Aktiv:</h3>";
					$panel->select(array("Nein"=>"false","Ja"=>"true"),$_POST["active"],"active");			
					$panel->content.="<br />";			
					$panel->field(array("name"=>"send","typ"=>"hidden","value"=>"1"));
					$panel->field(array("name"=>"id","typ"=>"hidden","value"=>$_GET['id']));
					$panel->content.="<br />";
					$panel->submit();
					$panel->formClose();
				}elseif($_GET["action"]=="delete" && $_GET["id"]){
					$db->query("DELETE FROM `" . $config["prefix"] . "article`  WHERE `id`='".intval($_GET["id"])."'");
					$db->query("DELETE FROM `" . $config["prefix"] . "article_menue` where `a_id`='".$id."'");
					$panel->admin_message("Artikel gel&ouml;scht!","Der Artikel wurde erfolgreich gel&ouml;scht.",True,"edit_text",3);
				}
					
			}else{
				$result = $db->query("SELECT a.title,a.id,a.active,a.date,a.url,u.username FROM `" . $config["prefix"] . "article` a LEFT JOIN `" . $config["prefix"] . "users` u on a.`author`=u.`uid`");
				while($row=$db->fetch_array($result, $db->ASSOC)){
					$cresult=$db->query("SELECT a.`categoryname` FROM `" . $config["prefix"] . "article_categories` a LEFT JOIN `" . $config["prefix"] . "article_category` c on a.`id`=c.`c_id` WHERE c.`a_id`='".$row["id"]."'");
					while($crow=$db->fetch_array($cresult, $db->ASSOC)){
						if(!$row["categoryname"]){
							$row["categoryname"]=$crow["categoryname"];
						}else{
							$row["categoryname"].="<br />".$crow["categoryname"];
						}
					}
					$row["dates"]=date("d.m.Y",$row["date"]);
					$articles[]=$row;
				}
				$panel->title="Vorhandene Artikel";
				$panel->content.="<table class=\"sortable\" width=\"90%\" cellspacing=\"2\">
							<tr>	
							<th align=\"center\">ID</th>
							<th align=\"center\">Name</th>
							<th align=\"center\">URL</th>
							<th align=\"center\">Kategorien</th>
							<th align=\"center\">Aktiv</th>
							<th align=\"center\">User</th>
							<th class=\"nosort sortcol\" align=\"center\" width=\"18%\">Aktionen</th>
							</tr>";
				if(!empty($articles)){
					foreach($articles as $u){
						if($u["active"]=="true"){
							$u["a"]="Ja";
						}else{
							$u["a"]="Nein";
						}
						$panel->content.="<tr>
						<td align=\"center\">".$u["id"]."</td>
						<td align=\"center\">".$u["title"]."</td>
						<td align=\"center\">".$u["url"]."</td>
						<td align=\"center\">".$u["categoryname"]."</td>
						<td align=\"center\">".$u["a"]."</td>
						<td align=\"center\">".$u["username"]."</td>";
						$panel->content.="<td align=\"center\"><a onclick=\"return confirmLink(this, 'Willst du wirklich den Artikel ".$u["title"]." l&ouml;schen? ')\" href=\"".$config["path"]."/admin/index.php?panel=edit_text&amp;action=delete&amp;id=".$u["id"]."\"><img src=\"".$config["path"]."/admin/images/icons/delete.png\" /></a> <a href=\"".$config["path"]."/admin/index.php?panel=edit_text&amp;action=edit&amp;id=".$u["id"]."\"><img src=\"".$config["path"]."/admin/images/icons/wrench.png\" /></a></td>
						</tr>";
					}
				}else{
				$panel->content.="<tr>
						<td align=\"center\" colspan=\"7\">Keine Artikel vorhanden.</td>
						</tr>";
				}
				$panel->content.="</table><br />";
			}
	}

	function meta(){
	global $config;
		$meta="<script type=\"text/javascript\" src=\"".$config["path"]."/js/jquery/jquery.min.js\"></script>  
			<script type=\"text/javascript\">
			$(document).ready(function()
			{
			$(\"#url\").blur(function()
			{
			 //remove all the class add the messagebox classes and start fading
			 $(\"#msgbox\").removeClass().addClass('messagebox').text('Warten...').fadeIn(\"slow\");
			 //check the username exists or not from ajax
			 $.post(\"ajax.php\",{ call:'url',url:$(this).val() } ,function(data)
			 {
			  if(data==1) //if username not avaiable
			  {
			   $(\"#msgbox\").fadeTo(200,0.1,function() //start fading the messagebox
			   {
			    //add message and change the class of the box and start fading
			    $(this).html('Url vergeben').addClass('messageboxerror').fadeTo(900,1);
			   });
			}
					  else
					  {
					  	$(\"#msgbox\").fadeTo(200,0.1,function()  //start fading the messagebox
						{ 
						  //add message and change the class of the box and start fading
						  $(this).html('').removeClass().fadeTo(900,1);	
						});
					  }
				
				});
			 
				});
			});

			</script>"; 
		$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>';
		$meta.='<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>';
		$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>';
		return $meta;
	}
}
?>
