<?php

$lungime_stire = 300;

$submit = getuserdata('submit');
$arhiva = getuserdata('arch', 0);
$start = getuserdata('start', 0);
$length = 5;
$eroare = '';

function procesare_modul()
{
}

function scripturi_modul()
{
}

function afisare_modul()
{
	global $lungime_stire, $arhiva, $start, $length, $id_meniu;
    echo "<h1>Știri din domeniul IT</h1>";
    $q = mysql_query("SELECT * FROM stiri WHERE ((arhivat = 0) OR ($arhiva = 1)) and categorie = 2 and activ = 1");
    if ($r = mysql_fetch_assoc($q))
    {
        $numar_stiri = mysql_num_rows($q);
    }
    else
    {
        $numar_stiri = 0;
    }
    $pag_min = 1;
    $pag_max = ceil($numar_stiri / $length);
    $pag_cur = floor($start / $length) + 1;
    $start = ($pag_cur - 1) * $length;
    $q = mysql_query("SELECT * FROM stiri WHERE ((arhivat = 0) OR ($arhiva = 1)) and categorie = 2 and activ = 1 ORDER BY data DESC ".
                     (($arhiva == 1)?" LIMIT $start, $length":""));
    while ($r = mysql_fetch_assoc($q))
    {
		echo "<div class='stire'>";
		echo "<div class='titlu'><span style='font-style: italic; font-weight: normal'>".
		     substr($r['data'], 8, 2).'.'.substr($r['data'], 5, 2).'.'.substr($r['data'], 0, 4).
			 " - </span> {$r['titlu']}</div>";
	    $stire = strip_tags($r['descriere']);
		if (strlen($stire) > $lungime_stire)
		{
			echo "<div class='descriere'>";
			echo substr($stire, 0, $lungime_stire + strcspn($stire, " ", $lungime_stire));
			echo "...</div>";
			echo "<a href='#' onClick='openNews({$r['id']})' class='buton'>Citeste mai mult...</a>";
		}
		else
		{
			echo "<div class='descriere'>{$stire}</div>";
		}
		echo "</div>";
	}
	if ($arhiva)
	{
		echo "<div class='pagini'>Pagina: ";
		for ($i = $pag_min; ($i <= 3) && ($i <= $pag_max); $i++)
		{
		    if ($i != $pag_cur)
		        echo "<a href='{$_SERVER['PHP_SELF']}?id_meniu=$id_meniu&arch=1&start=".($length * ($i - 1))."'>$i</a>&nbsp;";
            else
                echo "$i&nbsp;";
		}
		if ($pag_cur > 5) echo " ... ";
		for ($i = (($pag_cur > 4) ? $pag_cur - 1 : 4); ($i <= $pag_cur + 1) && ($i <= $pag_max) && ($pag_cur != $pag_max); $i++)
		{
		    if ($i != $pag_cur)
		        echo "<a href='{$_SERVER['PHP_SELF']}?id_meniu=$id_meniu&arch=1&start=".($length * ($i - 1))."'>$i</a>&nbsp;";
            else
                echo "$i&nbsp;";
		}
		if ($pag_max - $pag_cur > 4) echo " ... ";
		for ($i = $pag_max - 2; $i <= $pag_max; $i++)
		{
		    if ( ($i > 3) && ( ($i > $pag_cur + 1) || ($pag_cur == $pag_max) ) )
		    {
		        if ($i != $pag_cur)
		            echo "<a href='{$_SERVER['PHP_SELF']}?id_meniu=$id_meniu&arch=1&start=".($length * ($i - 1))."'>$i</a>&nbsp;";
                else
                    echo "$i&nbsp;";
            }
		}
		echo "</div>";
	}
	else
	{
	    echo "<div class='pagini'><a href='{$_SERVER['PHP_SELF']}?id_meniu=$id_meniu&arch=1&start=".($numar_stiri + 1).
             "'>Arhivă &raquo;&raquo;&raquo;</a></div>"; 
	}
}

?>