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

class add_text_panel extends admin_module{
 	
	function output(){
		global $config, $db,$panel, $session;
				$fail=FALSE;
				if(isset($_POST['send'])&& $_POST['send']==1){
					if(isset($_POST['text'])&& $_POST['text']!=""||isset($_POST['url'])&& $_POST['url']!=""||isset($_POST['title'])&& $_POST['title']!=""){
						$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."'");
						$row = $db->fetch_array($result);
						if($row["count"]==0){
							$db->query("BEGIN");
							$db->query("INSERT INTO `" . $config["prefix"] . "article` (`text`,`date`,`author`,`menue`,`title`,`url`,`active`) VALUES ('".$db->escape($_POST["text"])."','".time()."','".$session->userdata["uid"]."','".$mid."','".$title."','".$url."','".$_POST["active"]."')");
							$aid=$db->last_id();
							if($_POST["cats"]!=""){
								foreach($_POST["cats"] as $nid){
								$db->query("INSERT INTO `" . $config["prefix"] . "article_category` (`a_id`,`c_id`) VALUES ('".$aid."','".$nid."')");
								}
							}
							$db->query("COMMIT");
							$panel->admin_message("Danke", "Der Artikel wurde erfolgreich eingetragen!.",TRUE,"add_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($_POST['send']) || $fail){
					$panel->title="Text hinzuf&uuml;gen";
					$panel->form(array("action"=>$config["path"]."/admin/index.php?panel=add_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"]),"id=\"url\"");
					$panel->content.=".html";
					$panel->content.="    <span id=\"msgbox\" style=\"display:none\"></span><br />";
					$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>";
					$resultk=$db->query("SELECT `id`,`categoryname` FROM `" . $config["prefix"] . "article_categorys` WHERE `active`='true'") or die($db->error());
					while($row=$db->fetch_array($resultk)){
						$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"]));
								$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->content.="<br />";
					$panel->submit();
					$panel->formClose();
				}
	}

	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(\"".$config["path"]."/ajax.php\",{ task:'text',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"].'/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="'.$config["path"].'/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;
	}
}
?>