<?
	include_once("/home/duane/translingo.org/includes/general.php");
	$smarty->assign('nomenu', '1');
	$smarty->display('lheader.tpl');

	$limit = 20;

	$pid = intval($_GET['pid']);
	if(intval($_POST['pid']) > 0)
		$pid = intval($_POST['pid']);

        $proj =& new DataObject_Projects();
        $proj->id = $pid;
        $proj->find(false);
        $proj->fetch();
	$proj2 = clone($proj);

	$edit = intval($_REQUEST['edit']);

	$start = intval($_REQUEST['start']);
        $smarty->assign('start', $start);

	$lang = stripslashes($_REQUEST['lang']);
        $smarty->assign('lang', $lang);

	$searchlang = intval($_REQUEST['searchlang']);
        $smarty->assign('searchlang', $searchlang);

	$fuzzy = intval($_REQUEST['fuzzy']);
        $smarty->assign('fuzzy', $fuzzy);

	$translated = intval($_REQUEST['translated']);
        $smarty->assign('translated', $translated);

	$search = stripslashes(rawurldecode($_REQUEST['search']));
	$search = mysqli_real_escape_string($_SESSION['_config']['mysqli'], doit($search));

        $smarty->assign('pid', $pid);
        $smarty->assign('lang', $lang);
        $smarty->assign('search', stripslashes($search));
        $smarty->assign('project_name', $proj2->short);

	if(intval($mf) > 0)
	{
		$trans =& new DataObject_Translations();
		$trans->id = intval($mf);
		$trans->memid = $user->id;
		$trans->dl = intval($mfnon);
		$trans->fuzzy = intval($mfnon);
		$trans->revision = gmdate("Y-m-d H:i:s");
		$trans->update();
	}
	if($process != "")
	{
		$trans =& new DataObject_Translations();
                $string = doit($_POST['msgstr']);
                $trans->string = $string;
		$trans->memid = $user->id;
		$trans->revision = gmdate("Y-m-d H:i:s");
		$trans->fuzzy = 0;
		if(intval($_POST['id']) > 0)
		{
			$trans->id = intval($_POST['id']);
			$trans->dl = 0;
			$trans->update();
		} else {
			$trans->pid = $pid;
			$trans->language = $lang;
			$trans->baseid = intval($_POST['baseid']);
			$trans->insert();
		}
                $rev =& new DataObject_Revisions();
                $rev->pid = $pid;
                $rev->baseid = intval($_POST['baseid']);
                $rev->language = $lang;
                $rev->memid = $user->id;
                $rev->revision = gmdate("Y-m-d H:i:s");
                $string = doit($_POST['msgstr']);
                $rev->string = $string;
                $rev->insert();
		unset($rev);
		unset($trans);
		unset($edit);
	}

	$smarty->assign('lang', $lang);

	if($searchlang <= 0)
	{
	        $smarty->assign('translang', " checked");
		$slquery = "select `translations`.*,`base`.`comment` as `comment`, `base`.`string` as `bstring`
				from `translations`,`base` WHERE `translations`.`pid`='$pid' and
				`translations`.`language`='$lang' and `translations`.`string` like '%$search%' and
				`base`.`id`=`translations`.`baseid`";
		if($fuzzy == 0)
			$slquery .= " and `fuzzy`=0";
		if($fuzzy == 1)
			$slquery .= " and `fuzzy`=1";

		$res = mysqli_query($_SESSION['_config']['mysqli'], $slquery);
		while($row = mysqli_fetch_assoc($res))
		{
			$validids[] = $row['baseid'];
		}
		$total = count($validids);
		$dbaseid = 0;
	} else {
	        $smarty->assign('baselang', " checked");
		$slquery = "select `base`.`id` as `dbaseid`, `base`.`string` as `bstring`, `base`.`comment` as `comment`
				from `base` WHERE `base`.`string` like '%$search%' and `pid`='$pid'";
		$res = mysqli_query($_SESSION['_config']['mysqli'], $slquery);
		while($row = mysqli_fetch_assoc($res))
		{
			$drow = mysqli_fetch_assoc(mysqli_query($_SESSION['_config']['mysqli'], "select * from `translations` where `language`='$lang' and `pid`='$pid' and
								`baseid`='".$row['dbaseid']."'"));
			if($translated == 0 && $drow['baseid'] > 0)
				continue;
			else if($translated == 1 && $drow['baseid'] == 0)
				continue;

			if($fuzzy == 0 && $drow['fuzzy'] == 1)
				continue;
			else if($fuzzy == 1 && $drow['fuzzy'] == 0)
				continue;
			$validids[] = $row['dbaseid'];
		}
		$total = count($validids);
		$dbaseid = 1;
	}

	$start = intval($start);

	if($total - $start >= $limit)
	{
		$next = $start + $limit;
		$smarty->assign('next', "searchproject.php?pid=$pid&start=$next&lang=$lang&search=".rawurlencode(recode("html..utf-8", $search))."&searchlang=".$searchlang."&fuzzy=".$fuzzy."&translated=".$translated);
	}

	if($start > 0)
	{
		$prev = $start - $limit;
		if($prev < 0)
			$prev = 0;
		$smarty->assign('previous', "searchproject.php?pid=$pid&start=$prev&lang=$lang&search=".rawurlencode(recode("html..utf-8", $search))."&searchlang=".$searchlang."&fuzzy=".$fuzzy."&translated=".$translated);
	}

	for($i = $start; $i < $start + $limit; $i++)
	{
		if(isset($show))
			unset($show);

		if($validids[$i] == 0)
			continue;
		$row = mysqli_fetch_assoc(mysqli_query($_SESSION['_config']['mysqli'],
				"select `base`.`string` as `bstring`, `base`.`comment` as `comment` from `base`
					where `id`='".$validids[$i]."'"));
		if($dbaseid == 1)
			$row['dbaseid'] = $validids[$i];

		$query = "select * from `translations` where `language`='$lang' and `pid`='$pid' and
						`baseid`='".$validids[$i]."'";
		$drow = mysqli_fetch_assoc(mysqli_query($_SESSION['_config']['mysqli'], $query));

		if($row['dbaseid'] > 0)
			$did = $row['dbaseid'];
		else
			$did = $drow['baseid'];
		
		$show->msgstr = $drow['string'];

		$show->babel = "";
		if($edit != $did || $edit == 0)
		{
			$show->edit = "searchproject.php?edit=".$did."&pid=$pid&start=$start&lang=$lang&search=".rawurlencode(recode("html..utf-8", $search))."&searchlang=".$searchlang."&fuzzy=".$fuzzy."&translated=".$translated."#edit";
			if($drow['id'] > 0 && $drow['fuzzy'] == 0)
				$show->mf = "<a href='searchproject.php?mf=".$drow['id']."&mfnon=1&pid=$pid&start=$start&lang=$lang&search=".rawurlencode(recode("html..utf-8", $search))."&searchlang=".$searchlang."&fuzzy=".$fuzzy."&translated=".$translated."'>"._("Mark Fuzzy")."</a>";
			else
				$show->mf = "<a href='searchproject.php?mf=".$drow['id']."&mfnon=0&pid=$pid&start=$start&lang=$lang&search=".rawurlencode(recode("html..utf-8", $search))."&searchlang=".$searchlang."&fuzzy=".$fuzzy."&translated=".$translated."'>"._("Not Fuzzy")."</a>";
			$show->diff = "diffproject.php?diff=".$did."&pid=$pid&start=$start&difflanguage=$lang&search=".rawurlencode(recode("html..utf-8", $search))."&searchlang=".$searchlang."&fuzzy=".$fuzzy."&translated=".$translated;
			$show->editrow = 0;
		} else {
			$show->pid = $pid;
			$show->id = intval($drow['id']);
			$show->baseid = $did;
			$show->lang = $lang;
			$show->start = $start;
			$show->editrow = 1;
			$show->searchlang = $searchlang;
			if(trim($show->msgstr) == "")
			{
				$show->msgstr = babelfish($row['bstring'], $lang);
				$show->babel = "<font color='red'>"._("PLEASE NOTE The website tried to obtain a dictionary translation for this string, so accuracy and grammar may not be very good!")."</font><br>";
			}
		}
		$show->comment = str_replace("\n", "<br>\n", $row['comment']);
		$show->msgid = $row['bstring'];
		if(trim($show->msgstr) == "")
			$show->msgstr = $row['bstring'];
		$show->fuzzy = $drow['fuzzy'];
		if($show->fuzzy == "" || $show->fuzzy == 0)
			$show->fuzzy = _("No");
		else
			$show->fuzzy = _("Yes");
		$smarty->append('lines', $show);
	}

	$smarty->display('searchproject.tpl');

	$smarty->display('lfooter.tpl');
?>