<?php
#######################################################|#
#|                    HabboMCMS - TheRox
#|                    Powered by HoloCMS 
#|            Copyright 2011-2012 ALL RIGHTS RESERVED
#######################################################|#


require_once('../global.php');
if($hkzone !== true){ header("Location: index.php?throwBack=true"); exit; }
if(!session_is_registered(acp)){ header("Location: index.php?p=login"); exit; }

$pagename = "Voir les articles existants";

if($do == "delete" && is_numeric($key)){

	$check = mysql_query("SELECT id FROM site_news WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){
		mysql_query("DELETE FROM site_news WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
		$msg = "<div class='alert'>L'article a bien été supprimée.</div>";
	} else {
		$msg = "<div class='alert'>Impossible de supprimer l'article!</div>";
	}

} elseif($do == "edit" && is_numeric($key)){

	$check = mysql_query("SELECT * FROM site_news WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){
		$article = mysql_fetch_assoc($check);
		$editor_mode = true;
	} else {
		$msg = "<div class='alert'>Unable to edit article; this article does not exist!</div>";
	}

} elseif($do == "save" && is_numeric($key) && isset($_POST['topstory'])){

	$check = mysql_query("SELECT id FROM site_news WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){

		$num = $key;

		$title = Filter($_POST['title']);
		$category = Filter($_POST['category']);
		$seoURL = Filter($_POST['url']);
		$topstory = Filter($_POST['topstory']);
		$short_story = Filter($_POST['short_story']);
		$story = $_POST['story'];
		$com = Filter($_POST['com']);
		

		mysql_query("UPDATE site_news SET title = '".$title."', com = '".$com."', seo_link = '".$seoURL."', category_id = '".$category."', topstory_image = '".$topstory."', snippet = '".$short_story."', body = '".addslashes($story)."', timestamp = '".time()."' WHERE id = '".$num."' LIMIT 1") or die(mysql_error());

		$msg = "<div class='alert'>L'article a bien été editée.</div>";
		$editor_mode = false;

	} else {

		$msg = "Impossible d'éditer cet news, l'article n'existe pas.";

	}

} elseif($do == "bounce"){

	$msg = "<div class='alert'>L'article a bien été postée.</div>";

}


require_once('subheader.php');
require_once('header.php');
?>
<script type="text/javascript" src="./tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	mode : "exact",
	elements : "story",
	theme : "advanced",
	plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "",
	theme_advanced_buttons4 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_resizing : true,
	theme_advanced_statusbar_location : "bottom"
});
</script>
<table cellpadding='0' cellspacing='8' width='100%' id='tablewrap'>
<tr> <td width='22%' valign='top' id='leftblock'>
 <div>
 <!-- LEFT CONTEXT SENSITIVE MENU -->
<?php require_once('sitemenu.php'); ?>
 <!-- / LEFT CONTEXT SENSITIVE MENU -->
 </div>
 </td>
 <td width='78%' valign='top' id='rightblock'>
 <div><!-- RIGHT CONTENT BLOCK -->
 
<?php if($editor_mode !== true){ ?>
 
<form action='index.php?p=news_manage&do=save' method='post' name='theAdminForm' id='theAdminForm'>
 <div class='tableborder'>
 <div class='tableheaderalt'>Articles</div>
 <table cellpadding='4' cellspacing='0' width='100%'>
 <tr>
  <td class='tablesubheader' width='1%' align='center'>ID</td>
  <td class='tablesubheader' width='28%'>Titre</td>
  <td class='tablesubheader' width='10%' align='center'>Date</td>
  <td class='tablesubheader' width='10%' align='center'>Seo URL</td>
  <td class='tablesubheader' width='10%' align='center'>Modifier</td>
  <td class='tablesubheader' width='12%' align='center'>Supprimer</td>
 </tr>
<?php
$get_articles = mysql_query("SELECT id,title,seo_link,snippet,datestr FROM site_news ORDER BY timestamp DESC") or die(mysql_error());

while($row = mysql_fetch_assoc($get_articles)){
	printf(" <tr>
  <td class='tablerow1' align='center'>%s</td>
  <td class='tablerow2'><strong>%s</strong><div class='desctext'>%s</div></td>
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'><a href='index.php?p=news_manage&do=edit&key=%s'><img src='./images/edit.gif' alt='Edit'></a></td>
  <td class='tablerow2' align='center'><a href='index.php?p=news_manage&do=delete&key=%s'><img src='./images/delete.gif' alt='Delete'></a></td>															
</tr>", $row['id'], $row['title'], $row['snippet'], $row['datestr'], $row['seo_link'], $row['id'], $row['id']);
}
?>
 
 </table>
 <div class='tablefooter' align='center'><div class='fauxbutton-wrapper'><span class='fauxbutton'><a href='index.php?p=news_compose'>Ecrire un nouveau article</a></span></div></div>
</div>
<?php } else { ?>
<script type="text/javascript">
function suggestSEO(el)
{
	var suggested = el;
	
	suggested = suggested.toLowerCase();
	suggested = suggested.replace(/^\s+/, ''); 
	suggested = suggested.replace(/\s+$/, '');
	suggested = suggested.replace(/[^a-z 0-9]+/g, '');
	
	while (suggested.indexOf(' ') > -1)
	{
		suggested = suggested.replace(' ', '-');
	}
	
	document.getElementById('url').value = suggested;
}
</script>
<form action='index.php?p=news_manage&do=save&key=<?php echo $article['id']; ?>' method='post' name='theAdminForm' id='theAdminForm'>
<div class='tableborder'>
<div class='tableheaderalt'>Modifier l'article (<?php echo $article['title']; ?>)</div>

<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'>
<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Titre</b><div class='graytext'>Le titre complet de votre news</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='title' value="<?php echo $article['title']; ?>" size='30' onkeyup="suggestSEO(this.value);" class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>News Category</b></td>
<td class='tablerow2'  width='60%'  valign='middle'><select name='category' class='dropdown'>
<?php
$getOptions = mysql_query("SELECT * FROM site_news_categories ORDER BY caption ASC");

while ($option = mysql_fetch_assoc($getOptions))
{
	echo '<option value="' . $option['id'] . '" ' . (($option['id'] == $data['category_id']) ? 'selected' : '') . '>' . $option['caption'] . '</option>';
}
?>
</td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Topstory Image</b><div class='graytext'>L'URL de l'image topstory.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='topstory' value="<?php echo $article['topstory_image']; ?>" size='30' class='textinput'></td>
</tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>SEO URL</b><div class='graytext'>L'aper&ccedil;u de l'URL de l'article.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'>
<textarea id="url" name="url" cols='60' rows='1' wrap='soft' class='multitext'><?php echo $article['seo_link']; ?></textarea></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Introduction</b><div class='graytext'>Une petite introduction &agrave; l'article.<br />HTML n'est pas autoris&eacute; ici.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><textarea name='short_story' cols='60' rows='5' wrap='soft' id='sub_desc'   class='multitext'><?php echo HabboCMSTEXT($article['snippet']); ?></textarea></td>
</tr>

<tr>

<td class='tablerow1'  width='40%'  valign='middle'><b>Contenu</b><div class='graytext'>Votre news...<br />HTML est autoris&eacute; ici.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><textarea id='story' name='story' cols='100' rows='20' wrap='soft' id='sub_desc'   class='multitext'><?php echo HabboCMSTEXT($article['body']); ?></textarea></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Commentaire</b><div class='graytext'>Exemple: (<?php echo $name ?>, <?php echo $fonction ?> "Votre commentaire")</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='com' value="<?php echo $article['com']; ?>" size='30' class='textinput'></td>
</tr>

<tr>
<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value='Valider' class='realbutton' accesskey='s'></td></tr>
</form></table></div><br />
<?php } ?>
	 </div><!-- / RIGHT CONTENT BLOCK -->
	 </td></tr>
</table>
</div><!-- / OUTERDIV -->
<div align='center'><br />
<?php
$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;
printf('Time: %.3f', $totaltime);
?>
</div>