<?php
if (!defined("MODULE_FILE")) {
	Header("Location: ../../index.php");
	exit;
}
get_lang($conf['name']);
include("config/config_teachers.php");

function navigation(){
global $db,$prefix,$admin_file, $user,$conf, $pagetitle;
	open();
	echo "<font class=\"title\">"._REZUME."</font>";
	echo "[ <a href=\"index.php?name=".$conf['name']."\" title=\""._HOME."\">"._HOME."</a>";
	echo " | <a href=\"index.php?name=".$conf['name']."&op=list_t\" title=\""._LIST."\">"._LIST."</a> ]<hr>";
	
	close();
	$pagetitle=_REZUME;
	}

function cats(){ 
global $db,$prefix,$admin_file, $user,$conf, $conft, $pagetitle; 
open();


$kolvo_kolonok=3; //количество колонок
$width_tab = 100/$kolvo_kolonok;
$i = 1;


$result_cat = $db->sql_query("select * from ".$prefix."_teachers_categories WHERE modul='teachers' ORDER BY id;"); 
echo ""; 
echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\">"; 
while (list($cid, $modul, $title, $catinfo, $img) = $db->sql_fetchrow($result_cat)) {



if (($i-1)%$kolvo_kolonok == 0) {echo "<tr>";}echo "<td width=\"".$width_tab."%\"  valign=\"top\">"; 

echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\"><tr> 
<td width=\"10%\" valign=\"top\"><img src=\"images/categories/".$img."\" alt=\"".$title."\"></td> 
<td><div align=\"left\"><b><a href=\"index.php?name=teachers&cid=".$cid."\">".$title."</a></b><br><div class=\"morelink\">".$catinfo."</div>
</tr></table>"; 

echo "</td>"; if ($i%$kolvo_kolonok == 0) {echo "</tr>";}$i++;


} 
echo "</table><hr>"; 
close(); 
            open();
              list($s_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(lid) FROM ".$prefix."_teachers WHERE offer='0' AND status!='0'"));
				list($l_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(lid) FROM ".$prefix."_teachers WHERE offer='1' AND status!='0'"));
				list($rezume_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(tid) FROM ".$prefix."_teachers"));
				echo "<center>На данный момент в нашем каталоге <b>$rezume_num</b> резюме</center><br><br>";
            close();
}
	
function teachers() {
	global $db,$prefix,$admin_file, $user, $conf, $num, $conft;
	include("config/config_teachers.php");
	head();
	navigation();
	
//CATEGORIES
	if ($conft['cat']=="1") {
		cats();
		if (isset($_REQUEST['cid'])) {
			$cid=$_REQUEST['cid'];
			$result = $db->sql_query("select * from ".$prefix."_teachers WHERE cid='".$cid."' ORDER by 'tid';");
			$result_cat = $db->sql_query("select * from ".$prefix."_teachers_categories WHERE id='".$cid."';");
			list($curcid, $modul, $title) = $db->sql_fetchrow($result_cat);
			//open();
			//echo "<center><h2>".$title."</h2></center>";
			//close();
			$link="cid=".$cid."&";
		} else {
			if ($conft['catdef'] == "all") {
				$result = $db->sql_query("select * from ".$prefix."_teachers ORDER by 'tid';");
				//open();
				//echo "<center><h2>"._ALL."</h2></center>";
				//close();
			} elseif (($conft['catdef'] !== "all") && ($conft['catdef'] !== "no")) {
				$cid=$conft['catdef'];
				$result = $db->sql_query("select * from ".$prefix."_teachers WHERE cid='".$cid."' ORDER by 'tid';");
				$result_cat = $db->sql_query("select * from ".$prefix."_teachers_categories WHERE id='".$cid."';");
				list($curcid, $modul, $title) = $db->sql_fetchrow($result_cat);
				open();
				//echo "<center><h2>".$title."</h2></center>";
				echo "<hr>";
				close();
			}
		}
	} elseif ($conft['cat']=="0") {
		$result = $db->sql_query("select * from ".$prefix."_teachers ORDER by 'tid';");
		//open();
		//echo "<center><h2>"._ALL."</h2></center>";
		//close();
		$link="";
	}
	
//PAGES
	if (isset($_REQUEST['pag'])) {
		$num = intval($_REQUEST['pag']);
		} else  $num=1; 
	$numcol = $num * $conft['numa']; 
//MODUL	
		$i=0;
			$content="notnull";
			while (list($tid, $name, $text, $salary, $author, $authormail, $telefone, $address, $time, $rate, $c_rate, $foto, $rate_users, $cid, $numcomments) = $db->sql_fetchrow($result)) {
					
				if ($foto=="") $foto="uploads/teachers/none.jpg";
				$i++;
				$s_rate=$rate/$c_rate;
				$new_ratea= " "._MYRATE.": <select name=\"new_rate\" class=\"".$conf['style']."\"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select>&nbsp;&nbsp;<input type=\"submit\" value=\"OK\" class=\"fbutton\">";
				$rusers=explode(",", $rate_users);
				foreach($rusers as $value) if ($value==$user[1]) $new_ratea=_FINE;
				if (!is_user()) $new_ratea="";
				
				
				if ($conft['rate']=="1") {
				    $text_rating = "<form name=\"post\" action=\"index.php?name=".$conf['name']."\" method=\"post\">"._RATING. ": <b>".round($s_rate,2)."</b> "._C_RATE.": <b>".$c_rate."</b>".$new_ratea."
					<input type=\"hidden\" name=\"op\" value=\"save_rate\"/>
					<input type=\"hidden\" name=\"cid\" value=\"".$cid."\"/>
					<input type=\"hidden\" name=\"pag\" value=\"".$num."\"/>
					<input type=\"hidden\" name=\"tid\" value=\"".$tid."\"/></form>";
				} else { $text_rating = _RATE_CLOSE; }
				
				
				$text=bb_decode($text);
				if ($content!=="")	$content = "<table width=\"100%\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
 					<tr>
 		  		    <td valign=\"top\" width=\"148\">
					<table   border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
                      <tr>
                        <td width=\"148\" height=\"188\" align=\"center\" background=\"uploads/teachers/bg.jpg\"><div align=\"center\"><img src=\"".$foto."\"></div></td>
                      </tr>
                    </table></td>
    			    <td valign=\"top\"><div align=\"left\">
    			    <table width=\"100%\" border=\"0\"  cellpadding=\"3\" cellspacing=\"0\">
  <tr>
    <td colspan=\"2\"><table width=\"100%\" border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">
        <tr>
          <td><h3 id=\"titlebasic\"><a href=\"index.php?name=teachers&op=view&tid=".$tid."\">
".$author."</a></h3></td>
          <td align=\"right\">"._ADDTIME." <font class=\"block-title\">".format_time($time)."</font></td>
        </tr>
      </table><hr><br></td>
  </tr>
  <tr>
    <td width=\"25%\">"._ZAG."</td>
    <td width=\"75%\" class=\"block-title\">".$name."</td>
  </tr>
  <tr>
    <td>"._SALARY."</td>
    <td class=\"option\">".$salary."</td>
  </tr>
    <tr>
    <td colspan=\"2\"></td>
  </tr>
  <tr>
    <td align=\"top\">"._INFORMATION."<br><div class=\"small\">"._DESC."</div></td>
    <td>".cutstr($text, 450)."</td>
  </tr>
  <tr>
    <td colspan=\"2\"><br><br><div align=\"right\" class=\"morelink\"><a href=\"index.php?name=teachers&op=view&tid=".$tid."\">подробнее</a></div></td>
  </tr>
</table>
    	    		</div></td>
  					</tr>
					</table><hr>";



//PAGES
			if ($i-1 >= ($numcol-$conft['numa'])) { 
				if ($foto=="") $foto="uploads/teachers/none.jpg";
				if ($content!=="") open();
				echo $content;
				if ($content!=="") close();
				if ($i==$numcol) $content="";
			}
			}
			if ($i>$conft['numa']) num_pages($conf['name'], $i, ceil($i/$conft['numa']), $conft['numa'], $link);

	foot();	

}

function view() {
	global $db,$prefix,$admin_file, $user, $conf, $num, $conft;
	include("config/config_teachers.php");
	head();
	navigation();
	//if ($conft['cat']=="1") cats();
	open();
	$tid=$_REQUEST['tid'];
	$cur_user = $user[1];
		
	$result = $db->sql_query("select * from ".$prefix."_teachers WHERE tid='".$tid."';");
	list($tid, $name, $text, $salary, $author, $authormail, $telefone, $address, $time, $rate, $c_rate, $foto, $rate_users) = $db->sql_fetchrow($result);
			
				if ($foto=="") $foto="uploads/teachers/none.jpg";
				$s_rate=$rate/$c_rate;
				$new_ratea= " "._MYRATE.": <select name=\"new_rate\" class=\"".$conf['style']."\"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select>&nbsp;&nbsp;<input type=\"submit\" value=\"OK\" class=\"fbutton\">";
				$rusers=explode(",", $rate_users);
				foreach($rusers as $value) if ($value==$user[1]) $new_ratea=_FINE;
				if (!is_user()) $new_ratea="";
				
				
				if ($conft['rate']=="1") {
				    $text_rating = "<form name=\"post\" action=\"index.php?name=".$conf['name']."\" method=\"post\">"._RATING. ": <b>".round($s_rate,2)."</b> "._C_RATE.": <b>".$c_rate."</b>".$new_ratea."
					<input type=\"hidden\" name=\"op\" value=\"save_rate\"/>
					<input type=\"hidden\" name=\"rate_v\" value=\"rate_v\"/>
					<input type=\"hidden\" name=\"cid\" value=\"".$cid."\"/>
					<input type=\"hidden\" name=\"pag\" value=\"".$num."\"/>
					<input type=\"hidden\" name=\"tid\" value=\"".$tid."\"/></form>";
				} else { $text_rating = _RATE_CLOSE; }
	
	echo  "<table width=\"100%\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
 					<tr>
 		  		    <td valign=\"top\" width=\"148\">
					<table   border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
                      <tr>
                        <td width=\"148\" height=\"188\" align=\"center\" background=\"uploads/teachers/bg.jpg\"><div align=\"center\"><img src=\"".$foto."\"></div></td>
                      </tr>
                    </table></td>
    			    <td valign=\"top\"><div align=\"left\">
    			    <table width=\"100%\" border=\"0\"  cellpadding=\"3\" cellspacing=\"0\">
  <tr>
    <td colspan=\"2\"><table width=\"100%\" border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">
        <tr>
          <td><h3 id=\"titlebasic\">".$author."</h3></td>
          <td align=\"right\">"._ADDTIME." <font class=\"block-title\">".format_time($time)."</font></td>
        </tr>
      </table><hr><br></td>
  </tr>
  <tr>
    <td width=\"25%\">"._ZAG."</td>
    <td width=\"75%\" class=\"block-title\">".$name."</td>
  </tr>
  <tr>
    <td>"._SALARY."</td>
    <td class=\"option\">".$salary."</td>
  </tr>
    <tr>
    <td colspan=\"2\"></td>
  </tr>
  <tr>
    <td align=\"top\">"._INFORMATION."<br><div class=\"small\">"._DESC."</div></td>
    <td>".$text."</td>
  </tr>
   <tr>
    <td colspan=\"2\"><h3 id=\"titlebasic\">"._CONTACTS."</h3><hr></td>
  </tr>
  <tr>
    <td>"._EMAIL."</td>
    <td><a href=\"mailto:$authormail\"><b>".$authormail."</b></a></td>
  </tr>
  <tr>
    <td>"._PHONE."</td>
    <td>".$telefone."</td>
  </tr>
  <tr>
    <td>"._ADRES."</td>
    <td>".$address."</td>
  </tr>
</table>
    	    		</div></td>
  					</tr>
					</table><hr>";

			close();
			
			//Вывод коментариев
			open();
			echo "<h2>"._COMMENTS."</h2>";
			close();
			open();
			$result = $db->sql_query("select * from ".$prefix."_teachers_comments WHERE tid='".$tid."' ORDER BY `date` DESC;");
			echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\">";
			while (list($comid, $comment, $user, $date) = $db->sql_fetchrow($result)) {
				echo "<tr><td valign=\"top\" width=\"100\"><div align=\"center\"><b><span style=\"color: green\">".$user."</span></b>:<br><span style=\"font-size: 5px\">".$date."</span></div></td><td valign=\"top\"><div align=\"left\">".$comment."</div><hr></td></tr>";
			}
			
			
			echo "</table>";
			close();
			//Форма коментариев
			
			if (is_user()) {
			open();
				echo "<form name=\"post\" action=\"index.php?name=".$conf['name']."\" method=\"post\">";
				echo "<div class=\"left\">"._USER.":</div><div class=\"center\" align=\"left\"><b>".$cur_user."</b></div>";
				echo "<div class=\"left\">"._COMMENT.":</div><div class=\"center\"><input name=\"comment\" type=\"text\" size=\"65\" class=\"admin\"/></div>"
				."<input type=\"hidden\" name=\"op\" value=\"comm_save\"/>"
				."<input type=\"hidden\" name=\"tid\" value=\"".$tid."\"/>"
				."<input type=\"hidden\" name=\"user\" value=\"".$cur_user."\"/>"
				."<center><input type=\"submit\" value=\""._SEND."\" class=\"fbutton\"/></center></div></form>";
				echo "</form>";
			close();
			}
			
foot();
}


function save_rate() {
global $db,$prefix,$admin_file, $user,$conf;
	$tid = $_REQUEST['tid'];
	$new_rate = $_REQUEST['new_rate'];
	$pag = $_REQUEST['pag'];
	$cid = $_REQUEST['cid'];
	$result = $db->sql_query("SELECT tid, name, text, rate, c_rate, foto, rate_users FROM ".$prefix."_teachers WHERE tid='$tid'");
	list($tid, $name, $text, $rate, $c_rate, $foto, $rate_users) = $db->sql_fetchrow($result);
	
	$c_rate++;
	$rate=$rate+$new_rate;
				$point=",";
				if ($rate_users=="") $point="";
				$rate_users_new=$rate_users.$point.$user[1];
	$db->sql_query("UPDATE ".$prefix."_teachers SET c_rate='$c_rate', rate='$rate', rate_users='$rate_users_new' WHERE tid='$tid'");
	if (isset($_REQUEST['cid']) && $cid !=="") {
		 $cur="&cid=".$cid ;
	} else { $cur ="";}
	
	$rate_v = $_REQUEST['rate_v'];
	if ($rate_v !== "rate_v") {
	Header("Location: /index.php?name=".$conf['name']."&pag=".$pag.$cur);
	} else Header("Location: /index.php?name=".$conf['name']."&op=view&tid=".$tid);
	
	}

function list_t() {
global $admin_file, $prefix, $db, $conf, $conft;
	head();
	navigation();
	open();
	$result = $db->sql_query("select * from ".$prefix."_teachers;");
	
		
		echo "<table summary=\"\" width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" class=\"sort\" id=\"sort_id\"><tr><th>"._ID."</th><th width=\"30%\">"._AUTOR_L."</th><th>"._ZAG_L."</th><th>"._SALARY_L."</th><th>"._ADDTIME_L."</th></tr>";
			while (list($tid, $name, $text, $salary, $author, $authormail, $telefone, $address, $time, $rate, $c_rate, $foto) = $db->sql_fetchrow($result)) {
			echo "<tr class=\"bgcolor1\"><td align=\"center\" width=\"35\">".$tid."</td>"
			."<td class=\"help\"><div align=\"left\"><a href=\"index.php?name=teachers&op=view&tid=".$tid."\">".$author."</a></div></td>"
			."<td align=\"left\" width=\"100\">".$name."</td>"
			."<td align=\"left\" width=\"100\">".$salary."</td>"
			."<td align=\"left\" width=\"100\">".format_time($time)."</td></tr>";
			}
			
		echo "</table>";
	close();
	foot();
}



switch($op) {
	
	case "view":
	view();
	break;
	
	
	case "comm_save":
	$tid=$_REQUEST['tid'];
	$cur_user=$_REQUEST['user'];
	$comment = $_REQUEST['comment'];
	list($numcomments) = $db->sql_fetchrow($db->sql_query("SELECT Count(comid) FROM ".$prefix."_teachers_comments WHERE tid='$tid'"));
	$numcomments++;
	$db->sql_query("INSERT INTO ".$prefix."_teachers_comments (comid, comment, user, date, tid) VALUES (NULL, '$comment', '$cur_user', '".date('Y-m-d')."', '$tid')");
	$db->sql_query("UPDATE ".$prefix."_teachers SET numcomments='$numcomments' WHERE tid='$tid'");
	referer("index.php?name=teachers&op=teachers&op=view&tid=".$tid);


	break;
			
	case "save_rate":
	save_rate();
	break;
	
	case "list_t":
	list_t();
	break;
	
	default:
	teachers();
	break;

	}
?>