<?
session_name("e_learning");
session_start();

include_once('../class/sql.class.php');
include_once('../class/rs.class.php');
include_once('../class/main.class.php');

$main = new Main(); // inicializacia hlavneho objektu
$sql = new sql(); 	// inicializacia objektu pre pracu s db
$rs = new Rs();	// inicializacia hlavneho objektu
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Rs index</title>
	<link rel="stylesheet" type="text/css" href="../style.css" />
</head>

<body>
	<?php

if	( isset($_FILES['pic']['name']) && (trim($_FILES['pic']['name']) != "") && isset($_POST['add_pic']) )
	{
	$img = getimagesize($_FILES['pic']['tmp_name']);
	$koncovka = strtolower(substr($_FILES['pic']['name'], strpos($_FILES['pic']['name'], '.')+1));
	if ($img)
		{
		if ( ($_FILES['pic']['error'] == UPLOAD_ERR_OK) )
			{
			if ( ($koncovka == 'jpg') || ($koncovka == 'jpeg') || ($koncovka == 'png') || ($koncovka == 'gif') )
				{
				if (is_uploaded_file($_FILES['pic']['tmp_name']))
					{
					$nazov = $_FILES['pic']['name'];

					$location = '../images/uploaded';
					
					$sql_get_num = "UPDATE foto_inc SET kolko=kolko+1;";
					$result_get_num = mysql_query($sql_get_num);
					$sql_get_num = "SELECT kolko FROM foto_inc";
					$result_get_num = mysql_query($sql_get_num);
					$row_get_num = mysql_fetch_assoc($result_get_num);

					$num = (100000 + $row_get_num['kolko']);
					$location .= '/' . $num . '.jpg';
					move_uploaded_file($_FILES['pic']['tmp_name'], $location);
					chmod($location, 0777);
					if ( $_SESSION['uploadnute'] == '' )
						{
						$_SESSION['uploadnute'] = '<div><img style="width: 120px; border: 1px solid #CCCCCC;" src="' . $location . '" /><br /><div>' . str_replace('../', '', $location) . '</div>';
						}
					else
						{
						$_SESSION['uploadnute'] .= '<br />' . '<img style="text-align: center; width: 120px; border: 1px solid #CCCCCC;" src="' . $location . '" /><br /><div style="text-align: center;">' . str_replace('../', '', $location) . '</div>';
						}
					}
				}
			else
				{
				echo '<span style="color: red;">Nie je to súbor s koncovkou jpg/jpeg/png/gif</span><br />';
				}
			}
		else
			{
			echo '<p style="color: #FF0000;">Chyba</p>';
			}
		}
	else
		{
		echo '<p style="color: #FF0000;">Súbor má priveľké rozmery alebo to nieje obrázok typu jpeg/jpg/png/gif.</p>';
		}
	}
	
	
	
		/**********************************************************************
			Version: FreeRichTextEditor.com Version 1.00.
			License: http://creativecommons.org/licenses/by/2.5/
			Description: Example of how to preload content into freeRTE using PHP.
			Author: Copyright (C) 2006  Steven Ewing
		**********************************************************************/
		
		function freeRTE_Preload($content)
			{
			// Strip newline characters.
			$content = str_replace(chr(10), " ", $content);
			$content = str_replace(chr(13), " ", $content);
			// Replace single quotes.
			$content = str_replace(chr(145), chr(39), $content);
			$content = str_replace(chr(146), chr(39), $content);
			// Return the result.
			return $content;
			}
		// Send the preloaded content to the function.
		
		if ( isset($_POST['add_article']) && isset($_POST['freeRTE_content']) && ( (trim($_POST['freeRTE_content']) != '') && (trim($_POST['freeRTE_content']) != '<br />') ) && isset($_POST['heading']) && (trim($_POST['heading'])!='') && isset($_POST['category']) && (trim($_POST['category'])!='--') )
			{
			if ( $main->is_logged($_SESSION['id_user']) )
				{
				$sql->sql_connect('e_learning');
				echo $sql_insert_article = "INSERT INTO articles (id_category, id_branch, add_by, nadpis, popis, date, show_web, utime)
				VALUES (" . $_POST['subject'] . ", " . $_POST['category'] . ", " . $_SESSION['id_user'] . ", '" . $_POST['heading'] . "', '" . $_POST['freeRTE_content'] . "', '" . date("Y-m-d G:i:s") . "', 1, " . time() . ")";
				
				if ( $result_insert_article = mysql_query($sql_insert_article) )
					{				
					echo 'sended<br />';
					$_SESSION['uploadnute'] = array();
					?>
					<script type="text/javascript">
						document.location.href='index.php';
					</script>
					<?
					}
				else
					{
					echo 'error';
					}
				
				}
			else
				{
				echo '<h3 style="color: #FF0000;">Musite byt prihlaseny.</h3>';
				}
			}
		else
			{
			$content = freeRTE_Preload($_POST['freeRTE_content']);
			}
		?>
		<form enctype="multipart/form-data" method="post" action="<?=$_SERVER['REQUEST_URI']?>">
			<div style="float: left; padding: 10px">
				Heading:<br /><input type="text" value="<?=$_POST['heading']?>" name="heading" style="font-size: 11px;" /><br />
				<input type="text" style="visibility: hidden;" name="main_id" value="<? if ( isset($_POST['main_id']) ) {echo $_POST['main_id'];} else {echo '-1';} ?>" />
				<div>
				<?
				$rs->return_section();
				?>
				</div>
				<script src="./js/richtext.js" type="text/javascript" language="javascript"></script>
				<script src="./js/config.js" type="text/javascript" language="javascript"></script>
				<script>
					initRTE('<?= $content ?>', 'example.css');
				</script>
				<br />
				<input type="submit" name="add_article" value="Uloz clanok" />
			</div>
			<div style="float: right; width: 35%; margin-right: 3%; padding: 5px;">
					<input type="file" accept="image/gif,image/jpeg,image/png,image/jpg" name="pic" value="" />
					<input type="submit" style="font-size: 11px" value="Pridaj obrazok" name="add_pic" />
			</div>
		</form>
		<?
		echo '<h4>Adresy vami uploadnutych obrazkov: <span style="font-size: 10px;">(budete ju potrebovat pri doplnani obrazkov do clankov)</span></h4>';
		if ( ($_SESSION['uploadnute'] != '') && (sizeof($_SESSION['uploadnute']) > 0) )
			{
			echo '<div style="float: right; width: 380px;">';
			echo substr($_SESSION['uploadnute'],5) . '</div>';
			echo '</div>';
			}
		?>
</body>
</html>