<?
if( isset( $_POST['search'] ) ) {
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$query	= mb_strtolower(rawurldecode($_POST['query']), 'UTF-8');
	$letters	= $xpath->query('/letters/letter[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]'));
} elseif( isset( $_POST['searchTags'] ) ) {
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$query	= mb_strtolower(rawurldecode($_POST['selectLetterTags']), 'UTF-8');
	$letters	= $xpath->query('/letters/letter[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]'));
} elseif(isset($_GET['folder']) AND $_GET['folder'] == 'important' ) {
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[priority="high" and toid="'.$_SESSION["userid"].'" or priority="high" and coppyid="'.$_SESSION["userid"].'"]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[priority="high" and toid="'.$_SESSION["userid"].'" or priority="high" and coppyid="'.$_SESSION["userid"].'"]'));
} elseif(isset($_GET['folder']) AND $_GET['folder'] == 'inbox' ) {
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[toid="'.$_SESSION["userid"].'" or coppyid="'.$_SESSION["userid"].'"]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[toid="'.$_SESSION["userid"].'" or coppyid="'.$_SESSION["userid"].'"]'));
} elseif(isset($_GET['folder']) AND $_GET['folder'] == 'outbox' ) {
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[fromid="'.$_SESSION["userid"].'"]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[fromid="'.$_SESSION["userid"].'"]'));
} elseif(isset($_GET['folder']) AND $_GET['folder'] == 'notread' ) {
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[@status="notread" and toid="'.$_SESSION["userid"].'" or @status="notread" and coppyid="'.$_SESSION["userid"].'"]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[@status="notread" and toid="'.$_SESSION["userid"].'" or @status="notread" and coppyid="'.$_SESSION["userid"].'"]'));
} elseif( isset( $_POST['UserFilter'] ) ) {
	/* Список - после фильтрации */
	if( isset( $_POST["radioGroup"] ) ) {
		$filterGroup = '@group="'.$_POST["radioGroup"].'"';
		echo '<script>$(function() {$("form#filter-list input[value*=\''.$_POST["radioGroup"].'\']").prop("checked", true);$(".filter-buttons").fadeIn("500");});</script>';
	} else {
		$filterGroup = '';
	}
	
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[toid="'.$_SESSION["userid"].'" or fromid="'.$_SESSION["userid"].'" or coppyid="'.$_SESSION["userid"].'" '.$filterGroup.']');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[toid="'.$_SESSION["userid"].'" or fromid="'.$_SESSION["userid"].'" or coppyid="'.$_SESSION["userid"].'" '.$filterGroup.']'));
} else {
	/* Список - стандартный вид */
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[toid="'.$_SESSION["userid"].'" or fromid="'.$_SESSION["userid"].'" or coppyid="'.$_SESSION["userid"].'"]');
	$countUserLetters 	= count($xmlletters->xpath('/letters/letter[toid="'.$_SESSION["userid"].'" or fromid="'.$_SESSION["userid"].'" or coppyid="'.$_SESSION["userid"].'"]'));
}
if( isset($_GET['view']) and $_GET['view'] != '' ) {
	
	$dom = new DOMDocument;
	$dom->load($baseLT);
	$xpath = new DOMXPath($dom);
	$letters = $xpath->query('/letters/letter[@id="'.$_GET['view'].'"]');
	$letterDir = ROOT . 'crmdata/upload/letters_'.$_GET['view'];
	$letterFile = $letterDir . '/page_description.xml';
	foreach ($letters as $letter) {
		$id 			= $letter->getAttribute('id');
		$status 		= $letter->getAttribute('status');
		
		$subject 		= $letter->getElementsByTagName("subject")->item(0)->nodeValue;
		
		$textSm			= $letter->getElementsByTagName("text")->item(0)->nodeValue;
		
		$fromid			= $letter->getElementsByTagName("fromid")->item(0)->nodeValue;
		$fromname		= $letter->getElementsByTagName("fromname")->item(0)->nodeValue;
		$toid			= $letter->getElementsByTagName("toid")->item(0)->nodeValue;
		$toname			= $letter->getElementsByTagName("toname")->item(0)->nodeValue;
		$coppyid		= $letter->getElementsByTagName("coppyid")->item(0)->nodeValue;
		$coppyname		= $letter->getElementsByTagName("coppyname")->item(0)->nodeValue;
		
		$priority		= $letter->getElementsByTagName("priority")->item(0)->nodeValue;
		$date			= $letter->getElementsByTagName("date")->item(0)->nodeValue;
		$tags			= $letter->getElementsByTagName("tags")->item(0)->nodeValue;
		
		$taskid			= $letter->getElementsByTagName("taskid")->item(0)->nodeValue;
		$taskname		= $letter->getElementsByTagName("taskname")->item(0)->nodeValue;
		$clientid		= $letter->getElementsByTagName("clientid")->item(0)->nodeValue;
		$clientname		= $letter->getElementsByTagName("clientname")->item(0)->nodeValue;
		$dealid			= $letter->getElementsByTagName("dealid")->item(0)->nodeValue;
		$dealnum		= $letter->getElementsByTagName("dealnum")->item(0)->nodeValue;
		$eventid		= $letter->getElementsByTagName("eventid")->item(0)->nodeValue;
		$eventname		= $letter->getElementsByTagName("eventname")->item(0)->nodeValue;
	}
	if($toid == $_SESSION["userid"]){
		$dom = new DOMDocument;
		$dom->load($baseLT);
		$xpath = new DOMXPath($dom);
		$newstatus	= $xpath->query('/letters/letter[@id="'.$_GET['view'].'" and toid="'.$_SESSION["userid"].'"]')->item(0)->setAttribute('status', 'read');
		$dom->save($baseLT);
	}
	if(file_exists($letterFile)){
		$doz 	= new DOMDocument;
		$doz	->load($letterFile);
		$xpathz = new DOMXPath($doz);
		$text 	= $xpathz->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue;
	} else {
		$text 	= $textSm;
	}
}
?>