<?
	$ENTspace = array("&nbsp;", "&nbsp"); // Замена неразрывных пробелов
	
	/* Добавление события */
	if( isset( $_POST['eventAdd'] ) ) {
		$formeventId 		= newID('events','events','event');
		$formeventName 		= $_POST["inputName"];
		$formeventDate 		= $_POST["inputDate"];
		$formeventComment 	= $_POST["inputComment"];
		$formeventClient 	= $_POST["selectClient"];
		$formeventUser 		= $_POST["selectUser"];
		$formeventPriority 	= $_POST["selectPriority"];
		$formeventRepeat 	= $_POST["selectRepeat"];
		
		if($formeventRepeat == 'year'){
			$dateyear 	= '';
			$datemonth 	= substr($formeventDate, 5, 2);
			$dateday	= substr($formeventDate, 8, 2);
		} elseif($formeventRepeat == 'month'){
			$dateyear 	= '';
			$datemonth 	= '';
			$dateday	= substr($formeventDate, 8, 2);
		} elseif($formeventRepeat == 'day'){
			$dateyear 	= '';
			$datemonth 	= '';
			$dateday	= '';
		} else{
			$dateyear 	= substr($formeventDate, 0, 4);
			$datemonth 	= substr($formeventDate, 5, 2);
			$dateday	= substr($formeventDate, 8, 2);
		}

		$dom = new DOMDocument; 
		$dom->load($baseE);
		$xpath = new DOMXPath($dom);
		
		$events=$xpath->query('/events')->item(0);
		$event = $dom->createElement('event');
		$attribute1 = $dom->createAttribute("id");
		$attribute2 = $dom->createAttribute("user");
		$attribute3 = $dom->createAttribute("client");
		$attribute4 = $dom->createAttribute("priority");
		$attribute1->value = $formeventId;
		$event->appendChild($attribute1);
		$attribute2->value = $formeventUser;
		$event->appendChild($attribute2);
		$attribute3->value = $formeventClient;
		$event->appendChild($attribute3);
		$attribute4->value = $formeventPriority;
		$event->appendChild($attribute4);
		$event->appendChild($dom->createElement('name', $formeventName));
		$event->appendChild($dom->createElement('dateyear', $dateyear));
		$event->appendChild($dom->createElement('datemonth', $datemonth));
		$event->appendChild($dom->createElement('dateday', $dateday));
		$event->appendChild($dom->createElement('repeat', $formeventRepeat));
		$event->appendChild($dom->createElement('comment', mb_substr(strip_tags(str_replace($ENTspace, "&amp;#160;", $formeventComment)), 0, 50, 'UTF-8') . '...'));
		$event->appendChild($dom->createElement('uip', $sessionIP));
		$events->appendChild($event);
		$dom->save($baseE);
			
		// Добавление файла с комментарием
		$eventDir = ROOT . 'crmdata/upload/events_'.$formeventId;
		$eventFile = $eventDir . '/page_description.xml';
		if (!is_dir($eventDir)){
			mkdir($eventDir);
			file_put_contents($eventFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		} else {
			file_put_contents($eventFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		}
		if (file_exists($eventFile)){
			$dom = new DOMDocument; 
			$dom->load($eventFile);
			$xpath = new DOMXPath($dom);
			$nText = $xpath->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue = str_replace($ENTspace, "&amp;#160;", $formeventComment);
			$dom->save($eventFile);
		}
			
		// Добавление лога
		new_log("Добавление события:  $formeventName");

		$notices[] = 'Событие успешно добавлено';
	}
	
	/* Редактирование события */
	if( isset( $_POST['eventEdit'] ) ) {
		$oldeventId 		= $_POST["inputId"];
		$neweventId 		= $_POST["inputId"];
		
		$neweventName 		= $_POST["inputName"];
		$neweventName 		= $_POST["inputName"];
		$neweventDate 		= $_POST["inputDate"];
		$neweventComment 	= $_POST["inputComment"];
		$neweventClient 	= $_POST["selectClient"];
		$neweventUser 		= $_POST["selectUser"];
		$neweventPriority 	= $_POST["selectPriority"];
		$neweventRepeat 	= $_POST["selectRepeat"];
		
		if($neweventRepeat == 'year'){
			$dateyear 	= '';
			$datemonth 	= substr($neweventDate, 5, 2);
			$dateday	= substr($neweventDate, 8, 2);
		} elseif($neweventRepeat == 'month'){
			$dateyear 	= '';
			$datemonth 	= '';
			$dateday	= substr($neweventDate, 8, 2);
		} elseif($neweventRepeat == 'day'){
			$dateyear 	= '';
			$datemonth 	= '';
			$dateday	= '';
		} else{
			$dateyear 	= substr($neweventDate, 0, 4);
			$datemonth 	= substr($neweventDate, 5, 2);
			$dateday	= substr($neweventDate, 8, 2);
		}
		
		$parent = new DomDocument;
		$parent_node = $parent->createElement('event');
		$attribute1 = $parent->createAttribute("id");
		$attribute2 = $parent->createAttribute("user");
		$attribute3 = $parent->createAttribute("client");
		$attribute4 = $parent->createAttribute("priority");
		$attribute1->value = $neweventId;
		$parent_node->appendChild($attribute1);
		$attribute2->value = $neweventUser;
		$parent_node->appendChild($attribute2);
		$attribute3->value = $neweventClient;
		$parent_node->appendChild($attribute3);
		$attribute4->value = $neweventPriority;
		$parent_node->appendChild($attribute4);
		$parent_node->appendChild($parent->createElement('name', $neweventName));
		$parent_node->appendChild($parent->createElement('dateyear', $dateyear));
		$parent_node->appendChild($parent->createElement('datemonth', $datemonth));
		$parent_node->appendChild($parent->createElement('dateday', $dateday));
		$parent_node->appendChild($parent->createElement('repeat', $neweventRepeat));
		$parent_node->appendChild($parent->createElement('comment', mb_substr(strip_tags(str_replace($ENTspace, "&amp;#160;", $neweventComment)), 0, 50, 'UTF-8') . '...'));
		$parent_node->appendChild($parent->createElement('uip', $sessionIP));
		$parent->appendChild($parent_node);
		$dom = new DomDocument;
		$dom->load($baseE);
		$xpath = new DOMXpath($dom);
		$nodelist = $xpath->query('/events/event[@id="'.$oldeventId.'"]');
		$oldnode = $nodelist->item(0);
		$newnode = $dom->importNode($parent->documentElement, true);
		$oldnode->parentNode->replaceChild($newnode, $oldnode);
		$dom->save($baseE);	
		
		// Добавление файла с комментарием
		$eventDir = ROOT . 'crmdata/upload/events_'.$neweventId;
		$eventFile = $eventDir . '/page_description.xml';
		if (!is_dir($eventDir)){
			mkdir($eventDir);
			file_put_contents($eventFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		} else {
			file_put_contents($eventFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		}
		if (file_exists($eventFile)){
			$dom = new DOMDocument; 
			$dom->load($eventFile);
			$xpath = new DOMXPath($dom);
			$nText = $xpath->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue = str_replace($ENTspace, "&amp;#160;", $neweventComment);
			$dom->save($eventFile);
		}
		
		// Добавление лога
		new_log("Редактирование события:  $neweventName");

		$notices[] = 'Событие успешно сохранено';
	}
	
/* Редактирование задачи */
	if( isset( $_POST['SaveDescription'] ) ) {
		$dom 	= new DomDocument;
		$dom	->load($baseE);
		$xpath 	= new DOMXpath($dom);
		$eventslist 		= $xpath->query('/events/event[@id="'.$_GET["view"].'"]');
		$events_name 	= $xpath->query('/events/event[@id="'.$_GET["view"].'"]')->item(0)->getElementsByTagName("name")->item(0)->nodeValue;
		$events_descr 	= $xpath->query('/events/event[@id="'.$_GET["view"].'"]')->item(0)->getElementsByTagName("comment")->item(0)->nodeValue= mb_substr(strip_tags(str_replace($ENTspace, "&amp;#160;", $_POST["inputComment"])), 0, 50, 'UTF-8') . '...';
		$dom	->save($baseE);
		
		$eventDir = ROOT . 'crmdata/upload/events_'.$_GET["view"];
		$eventFile = $eventDir . '/page_description.xml';
		if (!is_dir($eventDir)){
			mkdir($eventDir);
			file_put_contents($eventFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		} else {
			file_put_contents($eventFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		}
		if (file_exists($eventFile)){
			$dom = new DOMDocument; 
			$dom->load($eventFile);
			$xpath = new DOMXPath($dom);
			$nText = $xpath->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue = str_replace($ENTspace, "&amp;#160;", $_POST["inputComment"]);
			$dom->save($eventFile);
		}
		
		// Добавление лога
		new_log("Редактирование события $events_name");
		
		$notices[] = 'Описание успешно сохранено';
	}
	
	/* Удаление события */
	if( isset( $_POST['eventDelete'] ) ) {
		$dom = new DOMDocument; 
		$dom->load($baseE);
		$xpath = new DOMXPath($dom); 
		
		$id = $_POST["eventDelId"];
		$eventName = $xpath->query('/events/event[@id="'. $id .'"]')->item(0)->getElementsByTagName("name")->item(0)->nodeValue;

		$events = $xpath->query('/events/event[@id="'.$id.'"]');
		$el = $events->item(0);
		$el->parentNode->removeChild($el);
		$dom->save($baseE);
		
		// Удаление Загрузок
		$dir 	= 	ROOT . 'crmdata/upload/events_'.$id;
		if (is_dir($dir)) {
			$files = glob($dir."/*");
			foreach ($files as $file) {      
				if (file_exists($file)) {
					unlink($file);
				}   
			}
			rmdir($dir);
		}

		// Добавление лога
		new_log("Удаление события:   $eventName");

		$notices[] = 'Событие успешно удалено';
	}
	
	/* Изменение важности */
	if( isset( $_POST['EventChangePrior'] ) ) {
		$dom = new DOMDocument;
		$dom->load($baseE);
		$xpath = new DOMXPath($dom);
		if(isset($_GET['view']) AND $_GET['view'] != ''){
			$events = $xpath->query('/events/event[@id="'.$_GET['view'].'"]');
		} elseif(isset($_GET['edit']) AND $_GET['edit'] != ''){
			$events = $xpath->query('/events/event[@id="'.$_GET['edit'].'"]');
		}
		if($_POST['EventChangePrior'] == '3'){
			$EventNewPriorTxt = 'high';
		} elseif($_POST['EventChangePrior'] == '2'){
			$EventNewPriorTxt = 'medium';
		} elseif($_POST['EventChangePrior'] == '1'){
			$EventNewPriorTxt = 'low';
		} elseif($_POST['EventChangePrior'] == '0'){
			$EventNewPriorTxt = 'none';
		}

		$EventNewPrior	= $events->item(0)->setAttribute('priority', $EventNewPriorTxt);
		
		$dom->save($baseE);

		$notices[] = 'Важность изменена';
		
	}
	
?>