<?
if( isset( $_POST['search'] ) ) {
	$dom = new DOMDocument;
	$dom->load($baseE);
	$xpath = new DOMXPath($dom);
	$query	= mb_strtolower(rawurldecode($_POST['query']), 'UTF-8');
	$events	= $xpath->query('/events/event[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countevent 	= count($xmlevent->xpath('/events/event[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]'));
} elseif( isset( $_POST['EventFilter'] ) ) {
	/* Список - после фильтрации */
	if( isset( $_POST["radioPriority"] ) ) {
		$filterPriority = '[@priority="'.$_POST["radioPriority"].'"]';
		echo '<script>$(function() {$("form#filter-list input[value*=\''.$_POST["radioPriority"].'\']").prop("checked", true); $(".filter-buttons").fadeIn("500");});</script>';
	} else {
		$filterPriority = '';
	}
	$dom = new DOMDocument;
	$dom->load($baseE);
	$xpath = new DOMXPath($dom);
	$events = $xpath->query('/events/event'.$filterPriority.'');
	$countevent 	= count($xmlclient->xpath('/events/event'.$filterPriority.''));
} else {
	/* Список - стандартный вид */
	$dom = new DOMDocument;
	$dom->load($baseE);
	$xpath = new DOMXPath($dom);
	$events = $xpath->query('/events/event');
	$countevent = count($xmlevent->xpath('/events/event'));
}
if( isset($_GET['edit']) and $_GET['edit'] != '' or isset($_GET['view']) and $_GET['view'] != '' ) {
	$dom = new DOMDocument;
	$dom->load($baseE);
	$xpath = new DOMXPath($dom);
	if(isset($_GET['edit']) AND $_GET['edit'] != '') {
		$events = $xpath->query('/events/event[@id="'.$_GET['edit'].'"]');
		$eventDir = ROOT . 'crmdata/upload/events_'.$_GET['edit'];
	}
	if(isset($_GET['view']) AND $_GET['view'] != '') {
		$events = $xpath->query('/events/event[@id="'.$_GET['view'].'"]');
		$eventDir = ROOT . 'crmdata/upload/events_'.$_GET['view'];
	}
	$eventFile = $eventDir . '/page_description.xml';
	foreach ($events as $event) {
		$eventId 		= $event->getAttribute('id');
		$eventName 		= $event->getElementsByTagName("name")->item(0)->nodeValue;
		$eventPriority 	= $event->getAttribute('priority');
		$eventClient 	= $event->getAttribute('client');
		$eventUser 		= $event->getAttribute('user');
		$eventRepeat 	= $event->getElementsByTagName("repeat")->item(0)->nodeValue;
		$eventComment 	= $event->getElementsByTagName("comment")->item(0)->nodeValue;
		$eventDateYear 	= $event->getElementsByTagName("dateyear")->item(0)->nodeValue;
		$eventDateMonth = $event->getElementsByTagName("datemonth")->item(0)->nodeValue;
		$eventDateDay 	= $event->getElementsByTagName("dateday")->item(0)->nodeValue;
		
		if($eventDateYear 	!= ''){ $eventDateYear 	= $eventDateYear; 	} else { $eventDateYear 	= date("Y"); }
		if($eventDateMonth 	!= ''){ $eventDateMonth = $eventDateMonth; 	} else { $eventDateMonth 	= date("m"); $eventDateYear 	= date("Y"); }
		if($eventDateDay 	!= ''){ $eventDateDay 	= $eventDateDay;	} else { $eventDateDay 		= date("d"); $eventDateMonth 	= date("m"); $eventDateYear 	= date("Y"); }
		
		if($eventRepeat == 'none')		{ $eventRepeatText = '- нет -';}
		if($eventRepeat == 'year')	{ $eventRepeatText = 'Ежегодно';}
		if($eventRepeat == 'month')	{ $eventRepeatText = 'Ежемесячно';}
		if($eventRepeat == 'day')	{ $eventRepeatText = 'Ежедневно';}
	
		$eventDate = $eventDateYear . '-' . $eventDateMonth . '-' . $eventDateDay;
		
		$date				= date("Y-m-d");
		$datetimeFrom 		= DateTime::createFromFormat( 'Y-m-d', $date );
		$datetimeTo 		= DateTime::createFromFormat( 'Y-m-d', $eventDate );
		
		$daysY = date('L')?366:365;
		$daysM = date('t');
		
		$UIP 			= $event->getElementsByTagName("uip")->item(0)->nodeValue;
	}
	if( isset($_GET['view']) and $_GET['view'] != '' ) {
		$doc = new DOMDocument;
		$doc->load($baseC);
		$xpathc = new DOMXPath($doc);
		$clients = $xpathc->query('/clients/client[@id="'.$eventClient.'"]');
		foreach ($clients as $client) {
			$ClientId 		= $client->getAttribute('id');
			$ClientName 	= $client->getElementsByTagName("clientname")->item(0)->nodeValue;
		}
		$dom = new DOMDocument;
		$dom->load($baseU);
		$xpath 	= new DOMXPath($dom);
		$users 	= $xpath->query('/users/user[@login="'.$eventUser.'"]');
		foreach ($users as $user) {
			$UserID 		= $user->getAttribute("id");
			$UserSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
			$UserName 		= $user->getElementsByTagName("username")->item(0)->nodeValue;
		}
	}
	
	if(file_exists($eventFile)){
		$doz 	= new DOMDocument;
		$doz	->load($eventFile);
		$xpathz = new DOMXPath($doz);
		$eventComment = $xpathz->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue;
	} else {
		$eventComment = $TaskCommentSm;
	}
	
}
?>