<?php
$folder_arr = explode("/", $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

$folder 	= $folder_arr[1];

define('ROOT', $_SERVER['DOCUMENT_ROOT'] . '/' . $folder . '/');

$baseR 		= ROOT . 'crmdata/database/requests.xml';

/* Новый ID материала */
function newID(){
	global $baseR;
	
	$dom 	= new DOMDocument;
	$dom	->load($baseR);
	$xpath 	= new DOMXPath($dom);
	
	$num = $xpath->query('/requests/request');
	if($num->length > 0){
		$lastID = $xpath->query('/requests/request[last()]')->item(0)->getAttribute('id');
	} else {
		$lastID = 0;
	}
	return $lastID + 1;
}

$dom 	= new DOMDocument; 
$dom	->load($baseR);
$xpath 	= new DOMXPath($dom);

$requests	=$xpath->query('/requests')->item(0);
$request 	= $dom->createElement('request');
$attribute1 = $dom->createAttribute("id");
$attribute1	->value = newID();
$request	->appendChild($attribute1);

$request->appendChild($dom->createElement('uname', $_POST['username']));
$request->appendChild($dom->createElement('usname', $_POST['usersurname']));
$request->appendChild($dom->createElement('company', $_POST['company']));
$request->appendChild($dom->createElement('service', $_POST['service']));
$request->appendChild($dom->createElement('phone', $_POST['phone']));
$request->appendChild($dom->createElement('email', $_POST['email']));
$request->appendChild($dom->createElement('web', $_POST['website']));
$request->appendChild($dom->createElement('addr', $_POST['address']));
$request->appendChild($dom->createElement('sourse', $_POST['sourse']));
$request->appendChild($dom->createElement('promo', $_POST['promocode']));

$request->appendChild($dom->createElement('date', date("Y-m-d H:i:s")));
$requests->appendChild($request);
$dom->save($baseR);
