<?php

define ( 'saltconta_SoriaDev', true );/// �������� ������ ��� ������� � �� � ������ �������
require("dbcon2.php");
require("receiverval2.php");

$srvcauseid = $_REQUEST['srvcauseid'];
$myid = $_REQUEST['myid'];
	
$User = get_user(pg_escape_custom($myid));

if( $User )
{
	$srvformid = 0;

	$sql="SELECT srvcause.srvformid
				FROM robender.srvcause
				WHERE srvcause.srvcauseid =" . intval($srvcauseid);
				
	$query = pg_query($link,  $sql);

	if(!$query){
		echo "Error: " . pg_last_error($link);
	}
	else {
		if(pg_num_rows($query) > 0){
			while($row = pg_fetch_assoc($query)){
				$srvformid = $row["srvformid"]; 
			}
		}
	}

	$sql="SELECT formitems.itemdescr,forms.replacer, forms.htmlid, forms.htmllabel, forms.htmllisttype, forms.htmlajaxfunc, forms.htmlplaceholder ,forms.enumgroupid
				FROM robender.forms,robender.formitems 
				WHERE forms.dynamicfield = 1
					AND forms.itemid=formitems.itemid
					AND forms.srvformid='".$srvformid."'
				ORDER BY forms.orderpos";
	
	//echo ($sql);
	
	$htmlpage="";
	
	$query = pg_query($link,  $sql);
	if(!$query)
	{
		echo "Error: " . pg_last_error($link);
	}
	else
	{
		if (pg_num_rows($query) > 0) 
		{
			while($row = pg_fetch_assoc($query)) 
			{
				$itemdesc = $row["itemdescr"];
				$replacer =$row["replacer"];
				$currow = $itemdesc;

				if ($replacer || $replacer!="")
				{
					if ($replacer=='REPLACEWITHMYID'){
						//$currow = str_replace('REPLACEWITHMYID',$myid,$itemdesc);	
					}
					else {
						$currow = str_replace('REPLACEME',$replacer,$itemdesc); 
					}
				}
				
				$currow = enumreplacer($link,'ENUMERATORGRUOUPID',$row["htmllisttype"], $row["enumgroupid"], $currow);
				$currow = locationreplacer($link,'OFFICELOCATIONTO',$row["htmllisttype"],  $currow);
				$currow = htmlreplacer('HTMLIDREPLACER',$row["htmlid"], $currow);
				$currow = htmlreplacer('HTMLLABELREPLACER',$row["htmllabel"], $currow);
				$currow = htmlreplacer('HTMLPLACEHOLDERREPLACER',$row["htmlplaceholder"], $currow);
				$currow = htmlreplacer('HTMLAJAXPLACER',$row["htmlajaxfunc"], $currow);
				$currow = htmlreplacer('CHATURIREPLACER', $config['chatURI'], $currow);

				$currow = htmlreplacer('TIMESTAMPREPLACER', time(), $currow);

				$htmlpage .= user_data_replacer($currow, $row["htmlid"]);
			}
		}
	}
	
	echo $htmlpage;
}
else
{
	echo "Пользователь не зарегистрирован! " . $myid ;
}			


function htmlreplacer($replacerconst, $replacervalue, $currowvalue)
{
	$currow = $currowvalue;
	
	//$replacervalue = mb_convert_encoding($replacervalue,  "utf-8", "windows-1251");
	
	
	//if ($replacervalue || $replacervalue!="")
	{
		//echo($replacervalue);
		$currow = str_replace($replacerconst, $replacervalue, $currowvalue, $count);
		
	}
	
	return $currow;
}

/**
 * 	Функция добавляет пользовательские значения
 * 	в соответствующие плейсхолдеры.
 */
function user_data_replacer( $input, $htmlid )
{
	global $User;

	$for_fields = array(
		'ContactPhone',
		'LocationDetail',
	);

	/**
	 * 	Если не перечислены поля на замену, нужно очистить
	 * 	входную строку от плейсхолдеров.
	 */
	$replacements = in_array($htmlid, $for_fields) ? array(
		$User['locationdetail'],
		$User['contactphone'],
	) : '';

	return preg_replace(
		array(
			"#USER_REPLACER_LOCATIONDETAIL#ui",
			"#USER_REPLACER_CONTACTPHONE#ui",
		),
		$replacements,
		$input
	);
}


function locationreplacer($link, $replacerconst,$htmllisttype, $currowvalue)
{
	$currow = $currowvalue;
	if ($htmllisttype!="" && $htmllisttype!=0)
	{
		$enum='';
		$sql="SELECT locationname	
					FROM robender.officelocation";
		
		$query = pg_query($link,  $sql);

		if(!$query){
			echo "Error: " . pg_last_error($link);
		}
		else {
			if (pg_num_rows($query) > 0) 
			{
				while($row = pg_fetch_assoc($query)) 
				{
					if( $htmllisttype == 2 ){
						$elem = $row["locationname"];
						$enum = $enum. ' <option value="'.$elem.'">'.$elem.'</option>';
					}
				}
			}
		}
		$currow = str_replace($replacerconst, $enum, $currowvalue);
	}
	return $currow;
}

function enumreplacer($link, $replacerconst,$htmllisttype, $enumgroupid,  $currowvalue)
{
	$currow = $currowvalue;
	
	if ($htmllisttype && $enumgroupid){
		if ($htmllisttype!="" && $htmllisttype!=0)
		{
			/**
			 * 	Для разных групп разная сортировка.
			 */
			switch( $htmllisttype ){
				case '5':
					$orderby = "substring(enumelement, '\d+')::int NULLS FIRST";
				break;
				default:
					$orderby = "enumelement";
			}

			$sql="SELECT enumid, enumgroupid, enumelement  
						FROM robender.enumtab 
						WHERE enumgroupid='".$enumgroupid."'
						ORDER BY {$orderby}";
			
			$query = pg_query($link,  $sql);

			if( !$query ){
				echo "Error: " . pg_last_error($link);
			}
			else {
				if (pg_num_rows($query) > 0) 
				{
					$enum='';
					while($row = pg_fetch_assoc($query)) 
					{
						$elem = $row["enumelement"]; //��� ��������
						
						if ($htmllisttype==1)
						{
							$enum = $enum. '<label class="radio radio-checkbox"><input type="checkbox" name="HTMLIDREPLACER[]" value="'.$elem.'">'.$elem.'</label>';
						}

						if ($htmllisttype==2)
						{	
							$enum = $enum. ' <option value="'.$elem.'">'.$elem.'</option>';
						}

						// Маршруты следования
						if ($htmllisttype==3){
							$enum = $enum. '<div class="item"><input type="text" name="HTMLIDREPLACER[]" placeholder="'.$elem.'" /></div>';
						}
						
						//
						if ($htmllisttype==4 || $htmllisttype==5)
						{
							$enum = $enum. ' <option value="'.$elem.'">'.$elem.'</option>';
						}
					}
				}
			}
			
			$currow = str_replace($replacerconst,$enum,$currowvalue);
		}
	}
	
	return $currow;
}