<?
	$ENTspace = array("&nbsp;", "&nbsp"); // Замена неразрывных пробелов
	
	if (!file_exists($baseQ)) {
  		file_put_contents($baseQ,'<?xml version="1.0" encoding="utf-8"?><messages></messages>');
	} else {
		if( isset( $_POST['QchatMessSend'] ) and $_POST['QchatMess'] != '' ) {
			$formQchatId 		= newID('qchat','messages','message');
			$formQchatUserName 	= $auth->getLogin();
			$formQchatMess 		= $_POST["QchatMess"];
			$formQchatDate 		= date("Y-m-d");
			$formQchatTime 		= date("H:i:s");
			
			$dom = new DOMDocument; 
			$dom->load($baseQ);
			$xpath = new DOMXPath($dom);
			
			$messages=$xpath->query('/messages')->item(0);
			$message = $dom->createElement('message');
			$attribute1 = $dom->createAttribute("id");
			$attribute2 = $dom->createAttribute("user");
			$attribute1->value = $formQchatId;
			$message->appendChild($attribute1);
			$attribute2->value = $formQchatUserName;
			$message->appendChild($attribute2);
			$message->appendChild($dom->createElement('text', $formQchatMess));
			$message->appendChild($dom->createElement('date', $formQchatDate));
			$message->appendChild($dom->createElement('time', $formQchatTime));
			$messages->appendChild($message);
			$dom->save($baseQ);
			
			$xmlqchat 	= simplexml_load_file($baseQ);
			$countQchat = count($xmlqchat->xpath('/messages/message'));
			
			if($countQchat > 500) {
				$dom = new DOMDocument; 
				$dom->load($baseQ);
				$xpath = new DOMXPath($dom); 

				$messages = $xpath->query('/messages/message[1]');
				$el = $messages->item(0);
				$el->parentNode->removeChild($el);
				$dom->save($baseQ); 
			}
		}

		
	}
?>
<div class="panel panel-default">
	<div class="panel-heading">
		<h3 class="panel-title"><i class="fa fa-fw fa-comments-o" aria-hidden="true"></i> Общий мини-чат<i title="Обновить мини-чат" onclick="$('#qchat').attr('src','/<?php echo $folder; ?>/qchat');" class="fa fa-fw fa-refresh" aria-hidden="true" style="float:right;cursor:pointer;"></i></h3>
	</div>
	<div class="panel-body">
		<iframe id="qchat" src="/<?php echo $folder; ?>/qchat" style="width:100%;min-height:50px;max-height:200px;border:none;" frameborder="0">Ваш браузер не поддерживает встроенные фреймы!</iframe>
		<form autocomplete="off" method="post">
			<div class="input-group">
				<input type="text" class="form-control" name="QchatMess" placeholder="Ваше сообщение" required />
				<span class="input-group-btn">
					<button class="btn btn-info" type="submit" name="QchatMessSend"><i class="fa fa-fw fa-arrow-right" aria-hidden="true"></i></button>
				</span>
			</div>
		</form>
	</div>
</div>

