<?php

final class Main
{

    private $sql;
    var $cleaner = '<div style="clear: both;"></div>';
    var $include;
    var $var_paging = 5;
    var $margin = 0;

    public function __construct()
    {
        global $sql;
        $this->sql = &$sql;
    }

    public function back_button($first, $other) { //ocakava prvy prvok v url adrese a pole ostatnych aby neboli podmienky zbytocne --cize z url sa zrusi vsetko co neposlem v $other
        $url = '?' . $first . '=' . $_GET[$first];
        foreach ( $other as $hodnota ) {
            $url .= '&amp;' . $hodnota . '=' . $_GET[$hodnota];
        }
        return '<div class="back_button">
            <img src="images/back_button.png" />' .
            '<a href="' . $url . '" title="">o úroveň vyššie</a>
            <div style="clear: both;"></div>
        </div>';
    }

    public function show_hide() {
        $show_hide = '<div class="show_hide">
            <img id="v_toggle_img" src="images/show_button.png" />' .
            '<a id="v_toggle" href="#" title="Zobraz/Skry">viac</a>
            <div style="clear: both;"></div>
        </div>';
        return $show_hide;
    }

    public function rating_form($rated_id) {
        $this->sql->sql_connect('e_learning');
        $sql_get_rate = 'SELECT rate,rated FROM articles WHERE id=' . $rated_id;
        if ( $result_get_rate = mysql_query($sql_get_rate) ) {
            $row_get_rate = mysql_fetch_assoc($result_get_rate);
            $list = '<div style="text-align: center;">
                <ul class="star-rating" style="width: 105px;">
                    <li class="current-rating" style="width: ' . 20*($row_get_rate['rate']/$row_get_rate['rated']) . '%;">a</li>

                    <li><a href="' . str_replace('&', '&amp;', $_SERVER['REQUEST_URI']) . '&amp;rate=1&amp;rated_id=' . $rated_id . '" title="1/5" class="one-star">1</a></li>
                    <li><a href="' . str_replace('&', '&amp;', $_SERVER['REQUEST_URI']) . '&amp;rate=2&amp;rated_id=' . $rated_id . '" title="2/5" class="two-stars">2</a></li>
                    <li><a href="' . str_replace('&', '&amp;', $_SERVER['REQUEST_URI']) . '&amp;rate=3&amp;rated_id=' . $rated_id . '" title="3/5" class="three-stars">3</a></li>
                    <li><a href="' . str_replace('&', '&amp;', $_SERVER['REQUEST_URI']) . '&amp;rate=4&amp;rated_id=' . $rated_id . '" title="4/5" class="four-stars">4</a></li>
                    <li><a href="' . str_replace('&', '&amp;', $_SERVER['REQUEST_URI']) . '&amp;rate=5&amp;rated_id=' . $rated_id . '" title="5/5" class="five-stars">5</a></li>
                </ul><div style="float: left; color: #008E14; font-size: 11px; margin-top: 5px;">hodnotené: ' . $row_get_rate['rated'] . 'x</div>
                
                <img style="float: left;" onmouseout="document.getElementById(\'hodnotil\').style.display=\'none\'" onmouseover="document.getElementById(\'hodnotil\').style.display=\'block\'" src="images/wrong_button.png" title="Tento príspevok ste už hodnotili a váš daľší hlas nebude akceptovaný." alt="Nieje moýné znovu hlasoať" />
                <div style="border: 1px solid #CCCCCC; text-align: left; background-color: #DDDDDD; position: relative; top: -20px; left: -60px; z-index: 2; display: none; font-size: 10px;" id="hodnotil">Príspevok ste už hodnotili.</div>
                <div style="clear: both;"></div></div>';
            return $list; //
        } else {//document.getElementById(id).style.display = 'block';

            $this->return_error_text('Príspevok s daným ID neexistuje.');
            return false;
        }
    }

    public function rating_exe($id, $rate) {
        if ( $this->is_logged($_SESSION['id_user']) ) { //ked nieje nalogovany nech to nejde
            //musim skontrolovat ci uz hlasoval za dany clanok
            $this->sql->sql_connect('login');
            $sql_disable_rating = 'SELECT rated_id FROM users WHERE id_user=' . $_SESSION['id_user'];
            $result_disable_rating = mysql_query($sql_disable_rating);
            $row_disable_rating = mysql_fetch_assoc($result_disable_rating);
            $hodnotil = split(',', $row_disable_rating['rated_id']); //pole $hodnotil obsahuje vsetky indexi clankov ktore uzivatel hodnotil

            if ( !in_array($id, $hodnotil) ) {
                $sql_disable_rating = "UPDATE users SET rated_id='" . $row_disable_rating['rated_id']  . "," . $id . "' WHERE id_user=" . $_SESSION['id_user'] . ";";
                $result_disable_rating = mysql_query($sql_disable_rating);
                $this->sql->sql_close();
                $this->sql->sql_connect('e_learning');
                $sql_add_rate = "UPDATE articles SET rated=rated+1, rate=rate+" . $_GET['rate'] . " WHERE id=" . $id . ";";
                $result_add_rate = mysql_query($sql_add_rate);
            } else {
                echo $this->return_error_text('Nemôžete hodnotiť 2x ten istý príspevok');
            }
            $this->sql->sql_close();
            $this->sql->sql_connect('e_learning');
        } else {
            echo $this->return_error_text('Hodnotiť môžete len keď ste prihlásený');
            $this->sql->sql_connect('e_learning');
        }
    }

    public function return_error_text($text) {
        return '<h4 style="text-align: center; color: red; font-size: 12px;">' . $text . '</h4>';
    }

    /*** HEADER: START ***/

    public function return_login_box() {
        if ( !$this->is_logged($_SESSION['id_user']) ) {
            $logxx = '
                        <table cellpadding="3" cellspacing="0" border="0">
                            <tr>
                                <td>nick:</td>
                                <td><input type="text" name="log_name" value="" /></td>
                                <td>password:</td>
                                <td><input type="password" name="log_pass" value="" /> <div class="submit"><input type="submit" name="login" value="log in" /></div></td>
                            </tr>
                            <tr>
                                <td colspan="4"> or [<a href="?id=3">enter registration</a>]</td>
                            </tr>
                        </table>';
        } else {
            $logxx = '<div><input type="submit" name="logout" value="log out" style="background-color: #FFFFFF;" /></div>';
        }
        ?>
        <form action="<?=str_replace('&','&amp;',$_SERVER['REQUEST_URI'])?>" method="post">
            <?=$logxx?>
        </form>
        <?
    }

public function return_head_menu()
{
$this->sql->sql_connect('e_learning');
$sql = 'SELECT * FROM categories WHERE id_parent=0 AND show_web = 1 ORDER BY rank ASC';
$result = mysql_query($sql);

echo '<div style="margin-left: 20px;">';
echo	'<img src="images/divider.png" style="float: left;" alt="" />';
while ( $row = mysql_fetch_array($result) )
{
    if ( empty($_GET['id']) ) //ked je na indexe
    {
        $class = '';
    }
    elseif ( $_GET['id'] == $row['id_category'] ) //pokial je zakliknuta jedna z hlavnych kategorii
    {
        $class = ' class="active" ';
    }
    elseif ( !mysql_query('SELECT * FROM categories WHERE id_category=' . $_GET['id']) )
    {
        //nieje id v zozname
    }
    else //pokial je zakliknuta podkategoria hlavnej kategorie
    {
        $parent = mysql_result(mysql_query('SELECT id_parent FROM categories WHERE id_category=' . $_GET['id']), 0, 'id_parent');
        if ( $parent )
        {
            if ( $parent == $row['id_category'] )
            {
                $class = ' class="active" ';
            }
            else
            {
                $class = '';
            }
        }
        else
        {
            $class = '';
        }
    }
    echo	'<div style="padding: 18px 0px 18px 0px; text-align: center; float: left;">';
    echo		'<a href="index.php?id=' . $row["id_category"] . '" ' . $class . '>' . $row["category"] . '</a>';
    echo	'</div> ';
    echo	'<img src="images/divider.png" style="float: left;" alt="" />';
}
echo '</div>';
echo $this->cleaner;
$this->sql->sql_free_result($result);
$this->sql->sql_close();
}

public function return_head_submenu()
{
$this->sql->sql_connect('e_learning');
if ( isset($_GET['id']) )
{
    $sql = 'SELECT id_parent FROM categories WHERE id_category=' . $_GET['id'];
    if ( $result = mysql_query($sql) )
    {
        $id_parent = mysql_result($result, 0, 'id_parent');
    }

    if ( $id_parent > 0 )
    {
        $this->include = mysql_result(mysql_query('SELECT include FROM categories WHERE id_category=' . $id_parent), 0, 'include');
        $colcatpar = 'id_category';
    }
    elseif ( $id_parent == 0 )
    {
        $sql = 'SELECT include FROM categories WHERE id_category=' . $_GET['id'];
        if ( $result = mysql_query($sql) )
        {
            $this->include = mysql_result($result, 0, 'include');
        }
        $colcatpar = 'id_parent';
    }
    else
    {
        //$this->include = 'pages/home.php';
    }
}
else //pokial je homepage
{}

if ( $id_parent == 0 )
{
    $id = $_GET['id'];
}
else
{
    $id = $id_parent;
}


$sql = 'SELECT * FROM categories WHERE id_parent=' . $id . ' AND show_web=1 ORDER BY rank ASC';
$result = mysql_query($sql);
//echo '<span style="float: left; color: white;">|</span>';
echo	'<div style="float: left; margin-top: 3px;"><span style="color: white;">|</span>';
while ( $row = @mysql_fetch_array($result) )
{		
    if ( $_GET['id'] == $row['id_category'] )
    {
        $class = ' class="sub_active" ';
    }
    else
    {
        $class = ' class="sub" ';
    }
    echo		'<a href="?id=' . $row['id_category'] . '"' . $class . ' title="' . $row['category_alt'] . '">' . $row['category'] . '</a><span style="color: white;">|</span>';
}
echo	'</div>';
$this->sql->sql_free_result($result);
$this->sql->sql_close();
}

/*** HEADER: END ***/

/*** HEADING TEXT: START ***/

public function return_heading_text($text)
{
echo '<h3>' . $text . '</h3>';
}

/*** HEADING TEXT: END ***/

/*** POZNAMKY: START ***/

public function return_subjects($branch)
{
$this->sql->sql_connect('e_learning');
$sql_sub = 'SELECT DISTINCT category FROM articles AS a, categories AS c WHERE a.id_branch=' . $branch . ' AND a.id_category=c.id_category';
if ( $result_sub = mysql_query($sql_sub) )
{
    while ( $row_sub = mysql_fetch_assoc($result_sub) )
    {
        $sql_get_id = 'SELECT id_category, category_alt FROM categories WHERE category=\'' . $row_sub['category'] . '\' AND id_parent=' . $_GET['id'];
        if ( $result_get_id = mysql_query($sql_get_id) )
        {
            $row_get_id = mysql_fetch_assoc($result_get_id);
        }
        echo '[<a href="?id=' . $_GET['id'] . '&amp;sub=' . $row_get_id['id_category'] . '" title="' . $row_get_id['category_alt'] . '">' . $row_sub['category'] . '</a>] ';
    }

}

$this->sql->sql_close();
}

public function return_article_list($what)
{
$this->sql->sql_connect('e_learning');
echo 	'<h4>Zoznam uploadnutých ' . $what . ':</h4>';
echo	'<ul>';
$sql_parent = 'SELECT id_parent FROM categories WHERE id_category=' . $_GET['id'];
if ( $result_parent = mysql_query($sql_parent) )
{
    $row_parent = mysql_fetch_assoc($result_parent);
}

if ( ($row_parent['id_parent'] == 0) || ($row_parent['id_parent'] == '') )
{
    if ( isset($_GET['sub']) )
    {
        $sql_all = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ') AND (c.id_category=' . $_GET['sub'] . ')';
    }
    else
    {
        $sql_all = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ')';
    }

    if ( isset($_GET['page']) && ($_GET['page']!='') )
    {
        $sql_prednasky = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ') LIMIT ' . ($this->var_paging*$_GET['page']) . ", " . $this->var_paging;
    }
    else
    {
        $sql_prednasky = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ') LIMIT 0,' . $this->var_paging;
    }
}
else
{
    if ( isset($_GET['sub']) )
    {
        $sql_all = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND id_category=' . $_GET['sub'];
    }
    else
    {
        $sql_all = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'];
    }
    if ( isset($_GET['page']) && ($_GET['page']!='') )
    {
        if ( isset($_GET['sub']) )
        {
            $page = '&amp;sub=' . $_GET['sub'];
            $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND id_category=' . $_GET['sub'] . ' LIMIT ' . ($this->var_paging*$_GET['page']) . ', ' . $this->var_paging;
        }
        else
        {
            $page = '';
            $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' LIMIT ' . ($this->var_paging*$_GET['page']) . ', ' . $this->var_paging;
        }
        //$sql_prednasky = "SELECT * FROM articles WHERE id_branch=" . $_GET['id'] . " LIMIT " . (5*$_GET['page']) . ", 5";
    }
    else
    {
        if ( isset($_GET['sub']) )
        {
            $page = '&amp;sub=' . $_GET['sub'];
            $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND id_category=' . $_GET['sub'] . ' LIMIT 0,' . $this->var_paging;
        }
        else
        {
            $page = '';
            $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' LIMIT 0,' . $this->var_paging;
        }
    }
}

if ( $result_prednasky = mysql_query($sql_prednasky) )
{
    while ( $row_prednasky = mysql_fetch_assoc($result_prednasky) )
    {
        if ( strlen($row_prednasky['nadpis']) > 50 )
        {
            $etc = '...';
        }
        else
        {
            $etc = '';
        }
        echo '<li>';
        echo 	'<div><a href="?id=' . $_GET['id'] . '&amp;article=' . $row_prednasky['id'] . '" title="' . $row_prednasky['nadpis'] . '">' . substr($row_prednasky['nadpis'], 0, 50) . $etc . '</a></div>';
        $sql_sekcia = 'SELECT category FROM categories WHERE id_category=' . $row_prednasky['id_category'];
        if ( $result_sekcia = mysql_query($sql_sekcia) )
        {
            $row_sekcia = mysql_fetch_assoc($result_sekcia);
            echo '<div>[<a href="?id=' . $row_prednasky['id_category'] . '" title="' . $row_sekcia['category'] . '">' . $row_sekcia['category'] . '</a>]</div>';
        }
        echo '</li>';
    }
}
echo	'</ul>';

$this->paging($sql_all, $page);

$this->sql->sql_close();
}

public function paging($sql_all, $page)
{
$kolko = mysql_num_rows($result_all = mysql_query($sql_all));			
$pom = 0;
echo '<div style="clear: both; text-align: center; padding-top: 10px; font-size: 11px;">| ';
for ( $i = 1; $i <= $kolko; $i = $i+$this->var_paging )
{
    $pom++;
    if ($_GET['page']==($pom-1)){
        echo $pom . ' | ';
    }
    else {
        echo '<a href="?id=' . $_GET['id'] . $page . '&amp;page=' . ($pom-1) . '">' . $pom . '</a> | ';
    }
}
echo '</div>';
}

/*** POZNAMKY: END ***/

/*** FORUM: START ***/

public function forum_temy()
{
$sql_temy = 'SELECT * FROM forum_temy';
if ( $result_temy = mysql_query($sql_temy) )
{
    echo '<ul style="list-style: square; padding-left: 15px; margin: 0px; color: #00377E;">';
    while ( $row_temy = mysql_fetch_assoc($result_temy) )
    {
        echo '<li><a href="?id=' . $_GET['id'] . '&amp;tema=' . $row_temy['id'] . '" title="">' . $row_temy['nazov'] . '</a></li>';
    }
    echo '</ul>';
}
}

public function main_forum($tema)
{
$sql_main = 'SELECT * FROM forum WHERE id_parent=0 AND id_tema=' . $tema . ' ORDER BY id ASC';
if ( $result_main = mysql_query($sql_main) )
{
    while ( $row_main = mysql_fetch_assoc($result_main) )
    {
        $this->margin = 0;
        echo '<div style="margin-bottom: 15px; padding: 7px; border: 1px solid red; border: 1px solid #BBBBBB; background-color: #EEEEEE;">';
        echo 	$row_main['text'];
        echo 	'<div style="font-size: 11px; float: left;"><a href="?id=' . $_GET['id'] . '&amp;tema=' . $_GET['tema'] . '&amp;react=' . $row_main['id'] . '" title="">Reagovať</a></div>';
        echo 	'<div style="font-size: 11px; float: right;"><span style="font-weight: bold;">Pridané: </span>' . $row_main['date'] . '</div>';
        echo 	'<div style="clear: both;"></div>';
        if ( isset($_GET['react']) && ($_GET['react'] != '') && ($row_main['id'] == $_GET['react']) )
        {
            echo '<div style="border-top: 1px solid #00377E; margin-top: 10px; padding-top: 10px;">';
            echo 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&react')) ) . '">';
            echo		'<input type="hidden" name="react_id" value="' . $_GET['react'] . '" />';
            echo 		'<div><textarea name="reakcia" rows="3" cols="10" style="width: 100%;"></textarea></div>';
            echo 		'<div><input style="font-size: 11px;" value="Reaguj" type="submit" /></div>';
            echo 	'</form>';
            echo '</div>';
        }
        echo '</div>';
        $this->sub_forum($row_main['id']);
    }
}
}

public function sub_forum($id_parent)
{
$sql_sub = 'SELECT * FROM forum WHERE id_parent=' . $id_parent . ' ORDER BY id ASC';
if ( $result_sub = mysql_query($sql_sub) )
{
    while ( $row_sub = mysql_fetch_assoc($result_sub) )
    {
        $this->margin++;
        echo '<div style="margin-bottom: 15px; padding: 7px; border: 1px solid #BBBBBB; background-color: #EEEEEE; margin-left: ' . ($this->margin*20) . 'px;">';
        echo 	$row_sub['text'];
        echo 	'<div style="font-size: 11px; float: left;"><a href="?id=' . $_GET['id'] . '&amp;tema=' . $_GET['tema'] . '&amp;react=' . $row_sub['id'] . '" title="">Reagovať</a></div>';
        echo 	'<div style="font-size: 11px; float: right;"><span style="font-weight: bold;">Pridané: </span>' . $row_sub['date'] . '</div>';
        echo 	'<div style="clear: both;"></div>';
        if ( isset($_GET['react']) && ($_GET['react'] != '') && ($row_sub['id'] == $_GET['react']) )
        {
            echo '<div style="border-top: 1px solid #00377E; margin-top: 10px; padding-top: 10px;">';
            echo 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&react')) ) . '">';
            echo		'<input type="hidden" name="react_id" value="' . $_GET['react'] . '" />';
            echo 		'<div><textarea name="reakcia" rows="3" cols="10" style="width: 100%;"></textarea></div>';
            echo 		'<div><input style="font-size: 11px;" value="Reaguj" type="submit" /></div>';
            echo 	'</form>';
            echo '</div>';
        }
        echo '</div>';
        $this->sub_forum($row_sub['id']);
        $this->margin--;
    }
}
}

public function forum_react($id_user)
{
$this->sql->sql_connect('e_learning');
if ( isset($_POST['reakcia']) && ($_POST['reakcia']!='') )
{
    $sql_react = 'INSERT INTO forum (id_parent, id_tema, add_by, text, date, utime)
        VALUES (' . $_POST['react_id'] . ', ' . $_GET['tema'] . ', ' . $id_user . ', \'' . $_POST['reakcia'] . '\', \'' . date("Y-m-d G:i:s") . '\', ' . time() . ')';
    if ( $result_react = mysql_query($sql_react) ) {}
}
$this->sql->sql_close();
}

public function forum_add_new($id_user, $tema)
{
echo '<div style="border-top: 1px solid #00377E; margin-top: 10px; margin-bottom: 30px; padding-top: 10px;">';
echo 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&addnew')) ) . '">';
echo 		'<div><textarea name="prispevok" rows="3" cols="10" style="width: 100%;"></textarea></div>';
echo 		'<div><input style="font-size: 11px;" value="Pridaj" type="submit" /></div>';
echo 	'</form>';
echo '</div>';
}

public function forum_add_new_execute($id_user, $tema)
{
$this->sql->sql_connect('e_learning');
if ( isset($_POST['prispevok']) && ($_POST['prispevok']!='') )
{
    $sql_post = 'INSERT INTO forum (id_parent, id_tema, add_by, text, date, utime)
        VALUES (0, ' . $_GET['tema'] . ', ' . $id_user . ', \'' . $_POST['prispevok'] . '\', \'' . date("Y-m-d G:i:s") . '\', ' . time() . ')';
    if ( $result_post = mysql_query($sql_post) ) {}
}
$this->sql->sql_close();
}

/*** FORUM: END ***/

/*** LOGIN: START ***/

    public function get_user_name($id_user) {
        $this->sql->sql_connect('login');
        $sql_get_name = 'SELECT nick FROM users WHERE id_user=' . $id_user;
        $result_get_name = mysql_query($sql_get_name);
        $row_get_name = mysql_fetch_assoc($result_get_name);
        $nick = $row_get_name['nick'];
        $this->sql->sql_close();
        $this->sql->sql_connect('e_learning');
        return $nick;
    }

public function login($name, $pass)
{
if ( isset($_POST['login']) && isset($_POST['log_name']) && (trim($_POST['log_name'])!='') && isset($_POST['log_pass']) && (trim($_POST['log_pass'])!='') )
{
    $this->sql->sql_connect('login');
    $sql_login = 'SELECT id_user FROM users WHERE nick=\'' . $_POST['log_name'] . '\' AND pass=\'' . md5($_POST['log_pass']) . '\';';
    $result_login = mysql_fetch_assoc(mysql_query($sql_login));
    if ( $result_login )
    {
        $_SESSION['id_user'] = $result_login['id_user'];
        $sql_logged = "INSERT INTO logged_users (id_user, timestamp) VALUES ('" . $result_login['id_user'] . "', '" . time() . "')";
        $result_logged = mysql_query($sql_logged);
        ?>
<script type="text/javascript">
    document.location.href="<?php echo $_SERVER['REQUEST_URI']; ?>";
</script>
<?
}
else
{
$_SESSION = array();
session_destroy();
//echo 'No such user';
}
}
$this->sql->sql_free_result($result);
$this->sql->sql_close();
}

public function is_logged($id)
{
$this->sql->sql_connect('login');
$sql_log = 'SELECT timestamp FROM logged_users WHERE id_user=' . $id;
if ( $result_log = mysql_query($sql_log) )
{
if ( mysql_num_rows($result_log) > 0 )
{
$row_log = mysql_fetch_assoc($result_log);
$cas = time();
if ( ($cas-$row_log['timestamp']) < 900 )
{
    return true;
}
else
{
    $this->logout($id);
    return false;
}
}
else
{
$this->logout($id);
return false;
}
}
$this->sql->sql_free_result($result);
$this->sql->sql_close();
}

public function logout($id)
{
$this->sql->sql_connect('login');
$sql_logged = "DELETE FROM logged_users WHERE id_user=" . $id;
$result_logged = mysql_query($sql_logged);
$_SESSION['id_user'] = array();
$this->sql->sql_free_result($result);
$this->sql->sql_close();
}

/*** LOGIN: END ***/
}
?>