<?php include "functions.php"; ?>
<?php if (isset ($_POST["data"])) {
	$searchitem = $_POST["data"];
	if (strpos($searchitem, ' ', -1) !== FALSE) { $class = "highlight highlight_s"; } else { $class = "highlight"; }
	$replace = "<div class='$class'>".preg_quote($searchitem, '/')."</div>";
	$pattern = "/.*$searchitem.*/mi";
	$unique1 = $unique2 = $unique3 = $unique4 = array();
	
	/* Search Unique Words Database */ {
	$filecontents = file_get_contents($file["unique_entries"]); if (preg_match_all($pattern, $filecontents, $matches)) { $unique1 = $matches[0]; }
	if(!empty($unique1)) { 
		$unique1 = array_unique($unique1);
		$x = 0;
		foreach ($unique1 as $key) {
			$exploded[$x] = explode("	", $key); 
			array_shift($exploded[$x]); 
			$exploded[$x] = array_values($exploded[$x]); 
			$exploded[$x][1] = str_ireplace($searchitem, $replace, $exploded[$x][1]);
			$exploded[$x][5] = str_ireplace($searchitem, $replace, $exploded[$x][2]);
			$exploded[$x][3] = str_ireplace($searchitem, $replace, $exploded[$x][3]);
			$exploded[$x][4] = str_ireplace($searchitem, $replace, $exploded[$x][4]);
			$html[$x] = "
			<div class='aresult'>
				<div class='arabic arabicfont'>".$exploded[$x][0]."</div> — <div class='root arabicfont'><a href='/?s=".$exploded[$x][2]."'>".$exploded[$x][5]."</a></div> — <div class='translit'> ".$exploded[$x][4]." </div><br>
				<div class='meaning'> ".$exploded[$x][1]." </div><br><div class='meaning'>[ ...".$exploded[$x][3]."... ]</div>
			</div>
			";
			$x++;
		}
		$x = 0;

		if (!empty($html)) { echo '<div class="asearchresult" id="unqsrch" data-control="unqsrch">'.minmax("down", "unqsrch").'
			<div class="scttitledsp">Unique Words :: <span class="digit whtfnt">[ '.count ($html).' ]</span></div>';
			foreach ($html as $key) { echo ($html[$x]); $x++; } echo '</div>';
		}
	}
	else { echo "<div class='nosearchresult'>No match found in dictionary.<br><small>Please note the dictionary is being built.</small></div>"; }
	} /* Search Unique Words Database */
	unset($matches); unset($key); unset($html);
	echo '<hr size="1" color="#343434" style="margin-top: 10px; margin-bottom: 10px; ">';

	/* Search MY SENTENCES Database */ {
	$filecontents = file_get_contents($file["my_sentences"]); if (preg_match_all($pattern, $filecontents, $matches)) { $unique2 = $matches[0]; }
	if (!empty($unique2)) { $x = 0;
		foreach ($unique2 as $key) { 
			$data = str_ireplace ($searchitem, "<span class='highlight'> ".$searchitem." </span>", $key);
			$data = explode("\t", $data);
			$html[$x] = "<div class='aresult'><a href='/?s=".$data[0]."'><span class='kgreenf digit'>[ ".$data[0]." ]</span></a> ".$data[1]."</div>";
			$x++;
		}
		if (!empty($html)) { echo '<div class="asearchresult" id="mysentences" data-control="mysentences">'.minmax("down", "mysentences").'
			<div class="scttitledsp">In-Verse phrases :: <span class="digit whtfnt">[ '.count ($html).' ]</span></div>';
			foreach ($html as $key) { echo ($key); } 
			echo '</div>';
		}
	}
	else { echo "<div class='nosearchresult'>No match found in translated phrases.<br><small>Please note the Quran is being translated properly.</small></div>"; }
	}
	unset($matches); unset($key); unset($html);
	echo '<hr size="1" color="#343434" style="margin-top: 10px; margin-bottom: 10px; ">';

	/* ADHOC Translations */ {
	$filecontents = file_get_contents($file["adhoc_dict"]); if (preg_match_all($pattern, $filecontents, $matches)) { $unique3 = $matches[0]; }
	if (!empty($unique3)) { $x = 0; $data = array(); 
		$adhocnotice = "<div class='adhocnotice'>ADHOC (and crude) Translations. Please note this is just a cursory reference for how the word maybe used. These words were copy pasted from various pre-available sources on the in bulk. These entries have only been formatted for reading reference.</div>";
		$separator = '<strong class="bar">|</strong>';
		echo '<div class="asearchresult" id="adhocsrch" data-control="adhocsrch">'.minmax("down", "adhocsrch").'
		<div class="scttitledsp">Ad hoc (and Crude) Dictionary :: <span class="digit whtfnt">[ '.count ($unique3).' ]</span></div>';
		echo $adhocnotice;
		echo "<div class='adhoctransresults'>";
		foreach ($unique3 as $key) { 
			$key_array = str_replace("\r\n", "", $key);
			$key_array = str_ireplace ($searchitem, "<span class='highlight' style='font-family:English, Arabic;'> ".$searchitem." </span>", $key_array);
			$key_array = str_replace("|", $separator, $key_array);
			$key_array = explode ("	", $key_array);
			echo "<div class='aresult'><strong class='english'>English</strong>$key_array[0]<br><br><strong class='arabic'>Arabic</strong>$key_array[1]</div>";
			$x++;
		}
		echo "</div></div>";
	}
	else { echo "<div class='nosearchresult'>No match found in ADHOC (and crude) Translations.<br><small>Please note this is just a cursory reference for how the word maybe used.</small></div>"; }
	}
	unset($matches); unset($key); unset($html);
	echo '<hr size="1" color="#343434" style="margin-top: 10px; margin-bottom: 10px; ">';

	/* Search Traditional Translation */ {
	$filecontents = file_get_contents($file["translations"]); if (preg_match_all($pattern, $filecontents, $matches)) { $unique4 = $matches[0]; }
	if(!empty($unique4)) {
		$fx = 0;
		$unique4 = array_unique($unique4);
		foreach ($unique4 as $key) { 
			$linedata = explode("|", $key);
			$linedata_verse = $linedata[0];
			$bx = 0;
			foreach ($linedata as $d) { if (stripos($d, $searchitem) !== FALSE) { $linedata[0] = $linedata_verse; } else { unset($linedata[$bx]); } $bx++; }
			$htmltrans[$fx] = $linedata;
			$fx++;
		}
		global $translations_data;
		if (!empty ($htmltrans[0])) {
			$fx = 0;
			$bx = count($htmltrans);	
			foreach ($htmltrans as $aversetrans) { $htmltrans[$fx]["full"] = "";
				foreach ($aversetrans as $key=>$value) {
					if ($key > 1) { $htmltrans[$fx]["full"] = $htmltrans[$fx]["full"].
						'<div class="translationtext" lang="'.$translations_data[$key]["language"].'" style="display: block;"><div class="bullet">⦿</div><a class="digit kgreenf" href="/?s='.$htmltrans[$fx][0].'" class="searchtransloc">[ '.$htmltrans[$fx][0].' ] </a>'.$htmltrans[$fx][$key].'<div class="translationname"> - '.$translations_data[$key]["fname"].'</div></div>';
					}
				}
				$fx++;
			}
			$x = 0; $fullres = array(); foreach ($htmltrans as $key) { $key["full"] = str_ireplace($searchitem, $replace, $key["full"]); $fullres[$x] = $key["full"]; $x++; }
			echo '<div class="asearchresult" id="trdtrnslt" data-control="trdtrnslt">'.minmax("down", "trdtrnslt").'
			<div class="scttitledsp">Traditional Translations\' Text of :: <span class="digit whtfnt">[ '.count ($fullres).' ] </span>verses</div>';
			echo '<div class="tradtranslations">';
			foreach ($fullres as $key) { echo ($key); }
			echo '</div>';
		} 
	}
	else { echo "<div class='nosearchresult'>No match found in translations.<br><small>Please note that only a handful of only English and Urdu translations are available.</small></div>"; }
	} /* Search Traditional Translation */ 
}	?>