Location: PHPKode > projects > Phparchive > phparchive.0.1.86.beta/includes/handleform.inc.php
<?php
/**
 * @version $Id: handleform.inc.php,v 1.21 2004/02/21 16:13:29 freedev Exp $
 * @author Vincenzo D'Amore <hide@address.com>
 * @package wa_includes
 */

/**
 * update last action value
 */ 
function FormUpdateLastAction(&$Form)
{
	if (!empty($Form["WACurrentAction"]))
		$Form["WALastAction"] = $Form["WACurrentAction"];
}

/**
 * set new form Action action
 */ 
function FormSetAction(&$Form, $sAction)
{
	FormUpdateLastAction($Form);
	$Form["WACurrentAction"] = $sAction;
}

/**
 * Build main and children forms
 */ 
function WAFormShow($_POST, $idMainForm, $idContext)
{
	$ChildUIDForm = "";
	StartMainTable();

	if (!empty($idMainForm))
	{
		$_SESSION["gIdForm"] = $idMainForm;
		if ($_SESSION["gIdCurrentForm"] != $_SESSION["gIdForm"])
		{
			$_SESSION["gIdCurrentForm"] = $_SESSION["gIdForm"];
			ClearAllForms();
		}
	}
	
	$_SESSION["giIdContext"] = $idContext;

	if ((isset($_SESSION["gIdForm"])) &&
		($formUID = BuildForm($_SESSION["gIdForm"], 0)))
	{
		$aForms = &$_SESSION["gaForms"];

//		echo "<pre>"; print_r($aForms); echo "</pre>";
//		echo "<pre>"; print_r($_POST); echo "</pre>";

		$Form = &$aForms[$formUID];
		$Form["WAUIDParent"] = "";
		if (empty($Form["WACurrentAction"]))
			$Form["WACurrentAction"] = "L";
		$aMessages = array();
		$aMessages[$formUID] = "";
		$TableRel = null;
		FormActionsHandle($Form, $_POST, $aMessages, 0, $TableRel);

		$ChildUIDForm = $formUID;

		FormContentBuild($Form, $_POST, $aMessages, 0, $TableRel, $ChildUIDForm);
	}
	else
	{
		echo "<span class='ErrorMsg'>".WA_MSG_WARNING_FORMDISABLED."</span>";
	}
	EndMainTable($ChildUIDForm);
}

/**
 * handling the form actions
 */

function FormActionsHandle(&$Form, &$_POST, &$aMessage, $iCurrentForm, &$TableRel)
{
	$bRC = True;
	$aForms = &$_SESSION["gaForms"];
	$formUID = $Form["WAUID"];
	$sSql = "";
	$sMissingFields = "";

//	echo "FormActionsHandle $formUID $iCurrentForm<br>";

	FormUpdateLastAction($Form);

	if ((isset($_POST[$formUID."_sFormAction"])) &&
		(!empty($_POST[$formUID."_sFormAction"])) )
		$Form["WACurrentAction"] = $_POST[$formUID."_sFormAction"];

//	echo "Entering: ParentForm:".$Form["WAUIDParent"]." Form[".$formUID."][CurrentAction] = ".
//		$Form["WACurrentAction"] . " Form[".$formUID."][WALastAction] = ".$Form["WALastAction"] . "<br>";
//	echo "<pre>"; print_r($Form); echo "</pre><br>";
//	echo "<pre>"; print_r($TableRel); echo "</pre><br>";

//	$PrepFunctions = array();
//	$PostFunctions = array();

	// The user cannot submit two or more times
	// data insert or modify actions
	if ($Form["WALastAction"] == $Form["WACurrentAction"])
	{
		switch ($Form["WACurrentAction"])
		{
			case "SM":
			case "SI":
			case "I":
			case "M":
			case "D":
				return false;
		}
	}
	
	if (!empty($Form["WACustomScript"]))
	{
		include_once($Form["WACustomScript"]);
	}

	$bCustomEventHandler = False;
	if ((!empty($Form["WAEventHandler"])) &&
		(function_exists($Form["WAEventHandler"])) )
		$bCustomEventHandler = $Form["WAEventHandler"]($Form, $_POST, $aMessage, $iCurrentForm, $TableRel, true);

	restore_error_handler();

	if (@isset($_POST[($formUID."_hdnDeleteList")]))
	{
		$sDeleteList = ereg_replace("([^[:digit:]]+(,))", "", $_POST[$formUID."_hdnDeleteList"]);
		$_POST[$formUID."_hdnDeleteList"] = $sDeleteList;
	}

	switch ($Form["WACurrentAction"])
	{
	case "C":
		DelKeyInfo($Form, $_POST);
		ClearContents($Form, $_POST);
		break;
	case "V":
		SetKeyInfo($Form, $_POST);
		break;
	case "B":
		break;
	case "F":
//		ShowArrayContent($_POST);
//		if ($Form["WALastAction"] != $Form["WACurrentAction"]);
			SaveContents($Form, $_POST, $aMessage);
		DelKeyInfo($Form, $_POST);
		break;
	case "VI":
		$KeyValue = "";
		SaveContents($Form, $_POST, $aMessage);
		if (empty($aMessage[$formUID]) )
			if (!CreateInsertSql($Form, $sSql, $sMissingFields, $KeyValue, false) )
			{
				$aMessage[$formUID] .= "<span class='ErrorMsg'>". WA_MSG_ERROR_VINSERT. $sMissingFields ."</span><br>";
				FormSetAction($Form, "V");
			}
			else
				$aMessage[$formUID] .= "<span class='NotifyMsg'>".WA_MSG_SUCCESS_VINSERT."</span><br>";
//		echo htmlentities($sSql);
		break;
	case "VM":
		SetKeyInfo($Form, $_POST);
		SaveContents($Form, $_POST, $aMessage);
		if (!ValidateForm($Form, $sMissingFields, $bExecute))
		{
			if (empty($sMissingFields))
				$aMessage[$formUID] .= "<span class='WarningMsg'>".WA_MSG_WARNING_VMODIFY."</span><br>";
			else
				$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_VMODIFY. $sMissingFields ."</span><br>";
			FormSetAction($Form, "V");
		}
		else
				$aMessage[$formUID] .= "<span class='NotifyMsg'>".WA_MSG_SUCCESS_VMODIFY."</span><br>";
//		echo htmlentities($sSql);
		break;
	case "VD2":
		if ((empty($sDeleteList)) && (!empty($Form["WAKeyValue"])))
			$sDeleteList = $Form["WAKeyValue"];
		// follow in VD
	case "VD":
		$sMessage = "";
//		ShowArrayContent($Form);
		if (!empty($sDeleteList))
			if (!CheckIntegrity($Form, $sDeleteList, $sMessage, $iCurrentForm))
			{
				$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_VDELETE_INTEGRITY." ".$sMessage."</span><br>";
				if ($Form["WACurrentAction"] == "VD2")
					FormSetAction($Form, "V");
				else
					FormSetAction($Form, "F");
				$sDeleteList = "";
			}
			else
				$aMessage[$formUID] .= "<span class='NotifyMsg'>".WA_MSG_VERIFY_DELETE."</span><br>";
//		echo htmlentities($sSql);
		break;
	case "D2":
		if ((empty($sDeleteList)) && (!empty($Form["WAKeyValue"])))
			$sDeleteList = $Form["WAKeyValue"];
		// follow in VD
	case "D":
		if (!empty($sDeleteList))
			if (!CheckIntegrity($Form, $sDeleteList, $sMessage , $iCurrentForm))
			{
				$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_VDELETE_INTEGRITY." ".$sMessage."</span><br>";
				FormSetAction($Form, "F");
				$_POST[$formUID."_hdnDeleteList"] = "";
			}
			else
			{
				if (!CreateDeleteSql($Form, $sDeleteList, $sSql) )
				{
					$aMessage[$formUID] .= "<span class='ErrorMsg'> SQL CREATE ".WA_MSG_ERROR_DELETE."</span><br>";
				}
				else
				{
					UnlinkAttachments($Form, $Form["WATableKeyField"], $sDeleteList);
					DeleteMultiValue($Form, $sDeleteList);
					if (ExecuteSql($sSql, $Form))
					{
						$_POST[$formUID."_hdnDeleteList"]="";
						$aMessage[$formUID] .= "<span class='NotifyMsg'>".WA_MSG_SUCCESS_DELETE."</span><br>";
					}
					else
						$aMessage[$formUID] .= "<span class='ErrorMsg'> SQL EXECUTE ".WA_MSG_ERROR_DELETE."</span><br>";
				}
			}
		if (empty($Form["WAKeyValue"]))
			FormSetAction($Form, "F");
		else
			FormSetAction($Form, "C");
		DelKeyInfo($Form, $_POST);
		ClearContents($Form, $_POST);
//		echo htmlentities($sSql);
		break;
	case "I":
	case "SI":
		$KeyValue = "";
		if (!CreateInsertSql($Form, $sSql, $sMissingFields, $KeyValue, true) )
		{
			$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_INSERT. $sMissingFields ."</span><br>";
		}
		else
		{
			if (ExecuteSql($sSql, $Form))
			{
				$aMessage[$formUID] .= "<span class='NotifyMsg'>".WA_MSG_SUCCESS_INSERT."</span><br>";
				if ($Form["WACurrentAction"] == "SI")
				{
					if (!empty($Form["WAUIDParent"]))
					{
						$FormParent = &$aForms[$Form["WAUIDParent"]];
						$FieldsParent = &$FormParent["WAFields"];
						$FieldParent = &$FieldsParent[$TableRel["WAField"]];
						if ($FieldParent["WAIdType"] == WA_LIST) 
						{
							if (!empty($FieldParent["WATag"]))
								$FieldParent["WATag"] .= ",";
							$FieldParent["WATag"] .= $KeyValue;
						}
						else
							$FieldParent["WATag"] = $KeyValue;
					}
				}
				$Form["WAKeyValue"] = $KeyValue;
				if (CheckMultiList($Form))
				{
//					DeleteMultiValue($Form, $KeyValue);
					InsertMultiValue($Form);
				}
			}
			else
				$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_INSERT."</span><br>";
		}
		if ($Form["WACurrentAction"] == "SI")
		{
			$FormParent = &$aForms[$Form["WAUIDParent"]];
			$FormParent["WAUIDChild"] = "";
		}
//		echo htmlentities($sSql);
		break;
	case "M":
	case "SM":
		if (!ValidateForm($Form, $sMissingFields, $bExecute))
		{
			$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_MODIFY. $sMissingFields ."</span><br>";
		}
		else
		{
			if (CreateModifySql($Form, $sSql, $sMissingFields))
			{
				if ((!$bExecute) || ExecuteSql($sSql, $Form))
				{
					$aMessage[$formUID] .= "<span class='NotifyMsg'>".WA_MSG_SUCCESS_MODIFY."</span><br>";
					// Force Content reload.
					$Form["WALoadedContents"] = false;
					if ($Form["WACurrentAction"] == "SM")
					{
						if (!empty($Form["WAUIDParent"]))
						{
							$FormParent = &$aForms[$Form["WAUIDParent"]];
							$FieldsParent = &$FormParent["WAFields"];
							$FieldParent = &$FieldsParent[$TableRel["WAField"]];
							if ($FieldParent["WAIdType"] == WA_LIST) 
							{
								if (!empty($FieldParent["WATag"]))
									$FieldParent["WATag"] .= ",";
								$FieldParent["WATag"] .= $Form["WAKeyValue"];
							}
							else
								$FieldParent["WATag"] = $Form["WAKeyValue"];
						}
					}
				}
				else
				{
					$aMessage[$formUID] .= "<span class='ErrorMsg'> ".WA_MSG_ERROR_MODIFY."</span><br>";
				}
			}
			if (CheckMultiList($Form))
			{
				DeleteMultiValue($Form, $Form["WAKeyValue"]);
				InsertMultiValue($Form);
			}
		}
		if ($Form["WACurrentAction"] == "SM")
		{
			$FormParent = &$aForms[$Form["WAUIDParent"]];
			$FormParent["WAUIDChild"] = "";
		}
//		echo htmlentities($sSql);
		break;
	case "SD":
	case "S2":
	case "S":
		SaveContents($Form, $_POST, $aMessage);
		if (!empty($Form["WAUIDParent"]))
		{
			$FormParent = &$aForms[$Form["WAUIDParent"]];
			$FieldsParent = &$FormParent["WAFields"];
			$FieldParent = &$FieldsParent[$TableRel["WAField"]];
			if ($FieldParent["WAIdType"] == WA_LIST) 
			{
				if (!empty($Form["WAKeyValue"]))
				{
					if (!empty($FieldParent["WATag"]))
						$FieldParent["WATag"] .= ",";
					$FieldParent["WATag"] .= $Form["WAKeyValue"];
				}
				if (!empty($sDeleteList))
				{
					if (!empty($FieldParent["WATag"]))
						$FieldParent["WATag"] .= ",";
					$FieldParent["WATag"] .= $sDeleteList;
				}
				$FieldParent["WATag"] = WAUniqueStringList($FieldParent["WATag"]);
			}
			else
			{
				if (!empty($Form["WAKeyValue"]))
				{
					$FieldParent["WATag"] = $Form["WAKeyValue"];
				}
				elseif (!empty($sDeleteList))
				{
					$tmparr = WASplit(",", $sDeleteList);
					$FieldParent["WATag"] = $tmparr[0];
				}
				else
					$FieldParent["WATag"] = "";
			}
			if ($Form["WACurrentAction"] != "SD")
				$FormParent["WAUIDChild"] = "";
		}
		if ($Form["WACurrentAction"] != "SD")
			$Form["WAUIDChild"] = "";
		break;
	case "SP":
		$Form["WAUIDChild"] = "";
		$Form["WAChildKeyField"] = "";
		break;
	case "SF":
		SaveContents($Form, $_POST, $aMessage);
		SetKeyInfo($Form, $_POST);
//		ShowArrayContent($Form);
//		ShowArrayContent($_POST);
		if (!empty($Form["WAUIDChild"]))
		{
			FormSetAction($Form, "V");
			$FormChild = &$aForms[$Form["WAUIDChild"]];
			FormSetAction($FormChild, "V");
//			$FormChild["WATableKeyField"] = "";
			$FormChild["WAKeyValue"] = "";
			SetKeyInfo($FormChild, $_POST);
			ClearContents($FormChild, $_POST);
		}
		break;
	case "SC":
//		ShowArrayContent($Form);
		SetKeyInfo($Form, $_POST);
		SaveContents($Form, $_POST, $aMessage);
		if (!empty($Form["WAUIDChild"]))
		{
			$FormChild = &$aForms[$Form["WAUIDChild"]];
			FormSetAction($FormChild, "F");
			FormSetAction($Form, "V");
			if (!SetKeyInfo($FormChild, $_POST))
			{
				ClearContents($FormChild, $_POST);
			}
		}
		break;
	default:
		if (!$bCustomEventHandler)
		{
			DelKeyInfo($Form, $_POST);
			ClearContents($Form, $_POST);
			$Form["WAUIDParent"] = "";
			$bRC = False;
		}
	}

	if ((!empty($Form["WAEventHandler"])) &&
		(function_exists($Form["WAEventHandler"])) )
		$bCustomEventHandler = $Form["WAEventHandler"]($Form, $_POST, $aMessage, $iCurrentForm, $TableRel, false);
//	echo "Exiting: ParentForm:".$Form["WAUIDParent"]." Form[".$formUID."][CurrentAction] = " . $Form["WACurrentAction"] . " - [" . $Form["WAActions"] . "]<br>";
//	echo $aMessage[$formUID]."<br>";
	if ($iCurrentForm == 0)
		ChildActionsHandle($Form, $_POST, $aMessage, $iCurrentForm);

	return $bRC;
}

function FormButtonsShow(&$Form, &$TableRel)
{
	if (!empty($Form["WAActions"]))
	{
		// No content loaded
		if (empty($Form["WAKeyValue"]))
		{
			switch ($Form["WACurrentAction"])
			{
			case "V":
			case "C":
			case "B":
			case "SP":
				FormButtonSearchInsertRow($Form, $TableRel);
				break;
			case "F":
				FormButtonTableView($Form, $TableRel);
				break;
			case "VD":
				FormButtonMultiDeleteVerify($Form);
				break;
			case "VI":
				FormButtonVerifyRow($Form, $TableRel);
				break;
			default:
				FormButtonSearchInsertRow($Form, $TableRel);
				break;
			}
		}
		else
		{
			switch ($Form["WACurrentAction"])
			{
			case "V":
			case "B":
			case "I":
			case "M":
			case "SP":
			case "SI":
			case "SM":
			case "SD":
			case "S":
				FormButtonRowLoaded($Form, $TableRel);
				break;
			case "VM":
			case "VD2":
				FormButtonVerifyRow($Form, $TableRel);
				break;
			default:
			}
		}
	}
	else
	{
		echo "<BR><span class='WarningMsg'>".WA_MSG_WARNING_NOCONTENT."</span><br>";
	}
}

function FormButtonMultiDeleteVerify(&$Form)
{
	// Back
	$sCurrentPage = 'getFormElementByName(document.forms["formSearch_'.$Form["WAUID"].'"], "PageN").value';
	$sRowsPerPage = 'getFormElementByName(document.forms["formSearch_'.$Form["WAUID"].'"], "RowsPerPage").value';

//		javascript:PageNumber('" . $formUID . "', ".$sCurrentPage.",".$sRowsPerPage.")
	FormActionButton(WA_CMD_BACK, 'javascript:PageNumber("' . $Form["WAUID"] . '", '.$sCurrentPage.', '.$sRowsPerPage.');');
	// Delete
	if (($Form["WACurrentAction"] == "VD") && stristr($Form["WAActions"], "D"))
	{
		FormActionButton(WA_CMD_DELETE, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "D");');
	}
	if ($Form["WAUIDParent"])
		FormActionButton(WA_CMD_CLOSE, 'javascript:ShowParentForm("'.$Form["WAUIDParent"].'");');
	// Help
	FormActionButton(WA_CMD_HELP, 'javascript:ShowHelpForm("'.$Form["WAUID"].'", "'.$Form["WAKeyValue"].'");');
}

function FormButtonTableView($Form, $TableRel)
{
	// New
	FormActionButton(WA_CMD_NEW, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "C");');
	// Back
	FormActionButton(WA_CMD_BACK, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "B");');
	// Multiple Select
	if ((!empty($Form["WAUIDParent"])) &&
		(($TableRel["WAIdRelType"] == WA_REL_ONE) ||
		 ($TableRel["WAIdRelType"] == WA_REL_LIST)) )
	{
		FormActionButton(WA_CMD_SELECT, 'javascript:MultiRecordsAction("'.$Form["WAUID"].'", "'.WA_MSG_WARNING_NOELEMSELECTED.'", "S2" );');
	}
	// Multiple Delete Check
	if (stristr($Form["WAActions"], "D"))
	{
		FormActionButton(WA_CMD_DELETE, 'javascript:MultiRecordsAction("'.$Form["WAUID"].'", "'.WA_MSG_WARNING_NOELEMSELECTED.'", "VD" );');
	}
	if ($Form["WAUIDParent"])
		FormActionButton(WA_CMD_CLOSE, 'javascript:ShowParentForm("'.$Form["WAUIDParent"].'");');

	// Export CSV
	if (stristr($Form["WAActions"], "E"))
		FormActionButton(WA_CMD_EXPORT, 'javascript:ShowPopupForm("'.$Form["WAUID"].'", "'.$Form["WAKeyValue"].'", "export.php");');

	// Help
	FormActionButton(WA_CMD_HELP, 'javascript:ShowHelpForm("'.$Form["WAUID"].'", "'.$Form["WAKeyValue"].'");');
}

function FormButtonVerifyRow(&$Form, &$TableRel)
{
	// New
	FormActionButton(WA_CMD_NEW, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "C");');
	// Save
	if ((!empty($Form["WAUIDParent"])) &&
		(($TableRel["WAIdRelType"] == WA_REL_ONE) ||
		 ($TableRel["WAIdRelType"] == WA_REL_LIST)) )
	{
		if ($Form["WACurrentAction"] == "VM")
			FormActionButton(WA_CMD_SELECT_SAVE, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "SM");');
		elseif ($Form["WACurrentAction"] == "VI")
			FormActionButton(WA_CMD_SELECT_INSERT, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "SI");');
		else
		{
			FormActionButton(WA_CMD_SELECT, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "S");');
		}
	}
	FormActionButton(WA_CMD_BACK, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "B");');
	if ($Form["WACurrentAction"] == "VI")
	{
		FormActionButton(WA_CMD_INSERT, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "I");');
	}
	elseif ($Form["WACurrentAction"] == "VM")
	{
		FormActionButton(WA_CMD_SAVE, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "M");');
	}
	if (($Form["WACurrentAction"] == "VD2") && stristr($Form["WAActions"], "D"))
	{
		FormActionButton(WA_CMD_DELETE, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "D2");');
	}
	// Print
	if (stristr($Form["WAActions"], "P"))
	{
		$sUrl = "printpreview.php?FormUID=".$Form["WAUID"];
		if ($GLOBALS['WA_PRIVATE_SESSIONS'])
			$sUrl = AddSessionInUrl($sUrl);
		FormActionButton(WA_CMD_PRINT, 'javascript:ShowAttachment("'.$sUrl.'");');
	}
	if ($Form["WAUIDParent"])
		FormActionButton(WA_CMD_CLOSE, 'javascript:ShowParentForm("'.$Form["WAUIDParent"].'");');
	// Help
	FormActionButton(WA_CMD_HELP, 'javascript:ShowHelpForm("'.$Form["WAUID"].'", "'.$Form["WAKeyValue"].'");');
}

function FormButtonRowLoaded(&$Form, &$TableRel)
{
	// New
	FormActionButton(WA_CMD_NEW, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "C");');
	// Select
	if ((!empty($Form["WAUIDParent"])) &&
		(($TableRel["WAIdRelType"] == WA_REL_ONE) ||
		 ($TableRel["WAIdRelType"] == WA_REL_LIST)) )
	{
		FormActionButton(WA_CMD_SELECT, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "S");');
	}
	// Save
	if (stristr($Form["WAActions"], "M"))
		if (!empty($Form["WAKeyValue"]))
			FormActionButton(WA_CMD_SAVE, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "VM");');
	if (stristr($Form["WAActions"], "D"))
	{
		FormActionButton(WA_CMD_DELETE, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "VD2");');
	}
	// Print
	if (stristr($Form["WAActions"], "P"))
	{
		$sUrl = "printpreview.php?FormUID=".$Form["WAUID"];
		if ($GLOBALS['WA_PRIVATE_SESSIONS'])
			$sUrl = AddSessionInUrl($sUrl);
		FormActionButton(WA_CMD_PRINT, 'javascript:ShowAttachment("'.$sUrl.'");');
	}
	// Reset
	FormResetButton();
	if ($Form["WAUIDParent"])
		FormActionButton(WA_CMD_CLOSE, 'javascript:ShowParentForm("'.$Form["WAUIDParent"].'");');
	// Help
	FormActionButton(WA_CMD_HELP, 'javascript:ShowHelpForm("'.$Form["WAUID"].'", "'.$Form["WAKeyValue"].'");');
}

function FormButtonSearchInsertRow(&$Form, &$TableRel)
{
	// New
	FormActionButton(WA_CMD_NEW, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "C");');
	// Select
	if ((!empty($Form["WAUIDParent"])) &&
		(($TableRel["WAIdRelType"] == WA_REL_ONE) ||
		 ($TableRel["WAIdRelType"] == WA_REL_LIST)) )
	{
		FormActionButton(WA_CMD_SELECT, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "S");');
	}
	// Search
	if (stristr($Form["WAActions"], "F") && (empty($Form["WAKeyValue"])))
		FormActionButton(WA_CMD_SEARCH, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "F");');
	// Insert
	if (stristr($Form["WAActions"], "I"))
		if (empty($Form["WAKeyValue"]))
			FormActionButton(WA_CMD_INSERT, 'javascript:formBuildSubmit("'.$Form["WAUID"].'", "VI");');
	// Print
	if (stristr($Form["WAActions"], "P"))
	{
		$sUrl = "printpreview.php?FormUID=".$Form["WAUID"];
		if ($GLOBALS['WA_PRIVATE_SESSIONS'])
			$sUrl = AddSessionInUrl($sUrl);
		FormActionButton(WA_CMD_PRINT, 'javascript:ShowAttachment("'.$sUrl.'");');
	}
	// Reset
	FormResetButton();
	if ($Form["WAUIDParent"])
		FormActionButton(WA_CMD_CLOSE, 'javascript:ShowParentForm("'.$Form["WAUIDParent"].'");');
	// Help
	FormActionButton(WA_CMD_HELP,'javascript:ShowHelpForm("'.$Form["WAUID"].'", "'.$Form["WAKeyValue"].'");');
}

/**
 *
 *
 */

function FormActionButton($value, $onclick)
{
	echo "\n<input class='Buttons' type='button' name='cmdAction' value='".$value."' onclick='".$onclick."'>&nbsp;";
}

/**
 *
 *
 */

function FormResetButton()
{
	echo "\n<input class='Buttons' type='reset' name='cmdReset' value='".WA_CMD_RESET."'>&nbsp;";
}

function CreateFormHeader($formUID, &$_POST)
{
	$sOrderBy = "";
	$sOrderAscDesc = "";
	$sPageNumber = "";
	$sFormAction = "";
	$sDeleteList = ",";
	$sRowsPerPage = "";

	if (@isset($_POST[($formUID."_hdnOrderBy")])) 
		$sOrderBy = $_POST[($formUID."_hdnOrderBy")];
	if (@isset($_POST[($formUID."_hdnOrderAscDesc")]))
		$sOrderAscDesc = $_POST[($formUID."_hdnOrderAscDesc")];
	if (@isset($_POST[($formUID."_hdnPageNumber")]))
		$sPageNumber = $_POST[($formUID."_hdnPageNumber")];
	if (@isset($_POST[($formUID."_hdnRowsPerPage")]))
		$sRowsPerPage = $_POST[($formUID."_hdnRowsPerPage")];
	if (@isset($_POST[$formUID."_hdnDeleteList"]))
		$sDeleteList = $_POST[$formUID."_hdnDeleteList"];
	if (@isset($_POST[($formUID."_sFormAction")])) 
		$sFormAction = $_POST[$formUID."_sFormAction"];

	echo "<form name='formBuild_".$formUID."' method='post' class='Form' onsubmit='return setSubmitFired();' action=''>";
	if ($GLOBALS['WA_PRIVATE_SESSIONS'])
		echo '<INPUT type="hidden" name="'. WA_SESSION_NAME.'" value="'.session_id().'" >';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnChildIdForm" value="">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnChildKeyField" value="">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnChildRelType" value="">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnOrderBy" value="'.$sOrderBy.'">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnOrderAscDesc" value="'.$sOrderAscDesc.'">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnReorderScript" value="0">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnPageNumber" value="'.$sPageNumber.'">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnRowsPerPage" value="'.$sRowsPerPage.'">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_hdnDeleteList" value="';
	if ($sFormAction == "VD") 
		echo $sDeleteList; 
	echo '">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_sFormAction" value="">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_KeyField" value="">';
	echo "\n".'<input type="hidden" name="'.$formUID.'_KeyValue" value="">';
	echo "\n".'<input type="hidden" name="Dummy1" value="">';
	echo "\n".'<input type="hidden" name="Dummy2" value="">';
}

function FormContentBuild(&$Form, &$_POST, &$aMessage, $iCurrentForm, &$TableRel, &$ChildUIDForm)
{
	$aForms = &$_SESSION["gaForms"];
	$formUID = $Form["WAUID"];
	$Fields = &$Form["WAFields"];
	$bSummary = ($Form["WAUIDChild"] > 0 ? True : False);

//	echo "FormContentBuild ".$formUID." ".$Form["WAUIDChild"];
	$FormParent = "";

	if (!empty($Form["WAUIDParent"]))
	{
		$FormParent = &$aForms[$Form["WAUIDParent"]];
		$FieldsParent = &$FormParent["WAFields"];
	}

	$bVerify = ($Form["WACurrentAction"] == "VD2") || ($Form["WACurrentAction"] == "VI") || ($Form["WACurrentAction"] == "VM");

	CreateFormHeader($formUID, $_POST);
	echo '<a name="FormHRef'.$formUID.'"></a>';
	echo "<table border='0' cellpadding='0' cellspacing='0' width='100%' ";
	echo (empty($Form["WAUIDChild"])) ? "class='FormTableE'>" : "class='FormTable'>";
	echo "<tr><td colspan='2' ";
	if (!is_array($TableRel))
	{
		echo (empty($Form["WAUIDChild"])) ? 'class="FormTitleE">' : 'class="FormTitle">';
		echo htmlentities(stripcslashes($Form["WADescr"]), ENT_QUOTES);
	}
	else
	{
		echo (!$bSummary) ? 'class="FormTitleE">' : 'class="FormTitle">';
		echo htmlentities(stripcslashes($TableRel["WADescr"]), ENT_QUOTES);
	}
	echo "</td></tr>";
	echo "<tr><td colspan='2'>".$aMessage[$formUID]."</td></tr>";

	echo '<tr><td class="ButtonsTable">';

	if (!$bSummary)
		FormButtonsShow($Form, $TableRel);
	echo "</td></tr>";
	echo '<tr><td>';

	if (($Form["WACurrentAction"] != "F") &&
		($Form["WACurrentAction"] != "VD"))
	{
		echo '<table border="0" cellpadding="2" cellspacing="2">';

		$_POST[$formUID."_hdnPageNumber"] = 0;

		$formValues = array();
		$formValues["Form"] = &$Form;
		$formValues["bVerify"] = $bVerify;
		$formValues["bSummary"] = $bSummary;
		$formValues["CurrentForm"] = $iCurrentForm;
		
		if ((!empty($Form["WAUIDParent"])) && ($TableRel["WAIdRelType"] == WA_REL_MULTI))
		{
			$Form["WAHiddenKeyField"] = $TableRel["WAFieldChild"];
			$Form["WAHiddenKeyValue"] = $FieldsParent[$TableRel["WAField"]]["WATag"];
		}
		else
		{
			$Form["WAHiddenKeyField"] = "";
			$Form["WAHiddenKeyValue"] = "";
		}
		CreateForm($formValues, $_POST);
		echo "</table>";
	}
	echo '</td></tr>';
	echo "</table>";
	echo "</form>";
	
	if (($Form["WACurrentAction"] == "F") ||
		($Form["WACurrentAction"] == "VD") )
	{
		$OrderBy = "";
		$AscDesc = "";
		if (@isset($_POST[$formUID."_hdnOrderBy"])) 
		{
			if (($_POST[$formUID."_hdnOrderAscDesc"] != "asc") 
				&& ($_POST[$formUID."_hdnOrderAscDesc"] != "desc")) 
				$_POST[$formUID."_hdnOrderAscDesc"] = "asc";
			else if ($_POST[$formUID."_hdnOrderAscDesc"] == "desc") 
				$_POST[$formUID."_hdnOrderAscDesc"] = "asc";
			else if ($_POST[$formUID."_hdnOrderAscDesc"] == "asc") 
				$_POST[$formUID."_hdnOrderAscDesc"] = "desc";
			$OrderBy = $_POST[$formUID."_hdnOrderBy"];
			$AscDesc = $_POST[$formUID."_hdnOrderAscDesc"];
		}

		if ((!empty($Form["WAUIDParent"])) && ($TableRel["WAIdRelType"] == WA_REL_MULTI))
		{
//			echo $TableRel["WAFieldChild"]." = ".$TableRel["WAField"]."<br>";
//			echo $Fields[$TableRel["WAFieldChild"]]["WATag"]." = ".$FieldsParent[$TableRel["WAField"]]["WATag"]."<br>";
			
			$Fields[$TableRel["WAFieldChild"]]["WATag"] = $FieldsParent[$TableRel["WAField"]]["WATag"];
		}

		$sSql = "";
		$sMessage = "";
		if (CreateSearchSql($Form, $OrderBy, $AscDesc, $sSql, $sMessage))
		{
			$iPageNumber = 0;
			$iRowsPerPage = WA_ROWS_PER_PAGE;

			if (@isset($_POST[$formUID."_hdnPageNumber"]))
				$iPageNumber = (int)$_POST[$formUID."_hdnPageNumber"];

			if (@isset($_POST[$formUID."_hdnRowsPerPage"]))
				$iRowsPerPage = (int)$_POST[$formUID."_hdnRowsPerPage"];

			if ($iRowsPerPage > WA_MAX_ROWS_PER_PAGE)
				$iRowsPerPage = WA_MAX_ROWS_PER_PAGE;
			elseif ($iRowsPerPage < WA_ROWS_PER_PAGE)
				$iRowsPerPage = WA_ROWS_PER_PAGE;

			$sDeleteList = "";
			if (@isset($_POST[$formUID."_hdnDeleteList"]))
				$sDeleteList = $_POST[$formUID."_hdnDeleteList"];

			if ((!empty($Form["WAUIDParent"])) && ($TableRel["WAIdRelType"] == WA_REL_MULTI))
			{
				$Form["WAHiddenKeyField"] = $TableRel["WAFieldChild"];
			}
			else
				$Form["WAHiddenKeyField"] = "";

			$bDelete = CreateSearchResults($Form, $sSql, $iPageNumber, $iRowsPerPage, $sDeleteList);
		}
		else
		{
			$aMessage[$formUID] .= "<span class='ErrorMsg'>".WA_MSG_ERROR_STD." ".$sMessage."</span>";
			echo "<table><tr><td colspan='2'>".$aMessage[$formUID]."</td></tr></table>";
		}
//		echo $sSql."<br>".$aMessage[$formUID]."<br>";
	}
	echo "<!--- Content Table End -->";
	echo "</td></tr>";
  	echo "<tr><td>";
//	ShowChildForms($Form, $_POST, $formUID, $aMessage, 0);
	ShowChildForms($Form, $_POST, $ChildUIDForm, $aMessage, $iCurrentForm);
}

function DeleteOldAttachments(&$Form)
{
	$bRC = False;
	$Fields = &$Form["WAFields"];
	foreach ($Fields as $Field)
	{
		if ($Field["WAIdType"] == WA_FILE)
		{
			if ((empty($Field["WAValue"])) ||
				($Field["WAValue"] == $Field["WATag"]))
			{
				$bRC = True;
			}
			else
			{
				$sUserDir = $GLOBALS['WA_UPLOAD_DIR'].$_SESSION["giIdUser"];
				$path_parts = pathinfo($Field["WAValue"]);
				$sUserFile = $path_parts["basename"].".".$path_parts["extension"];
				if (is_dir($sUserDir))
					if (unlink($sUserDir."/".$sUserFile))
					{
						$bRC = True;
					}
			}
		}
	}
	return $bRC;
}

function CheckIntegrity(&$FormCurrent, $sDeleteList, &$sMessage, $iCurrentForm)
{
	$bRC = False;
	$LinkedContents = array();
	$bRC = False;
	$sWhere = "";
	$gaForms = &$_SESSION["gaForms"];
	
	if ($formUID = BuildForm($FormCurrent["WAIdForm"], $iCurrentForm + 1000, False))
	{
		$Form = &$gaForms[$formUID];
		if (!empty($sDeleteList))
		{
			$rec = new WARecordset();
			$TablesRel = &$Form["WATablesRel"];
			$bFoundLinks = False;
			reset($TablesRel);
			// Controllo per i contenuti child...
			foreach ($TablesRel as $TableRel)
			{
				$formUIDChild = BuildForm($TableRel["WAIdFormChild"], $iCurrentForm + 1001, False);
				if ($formUIDChild)
				{
					$FormRel = &$gaForms[$formUIDChild];
	//				echo "<pre>"; print_r($FormRel); echo "</pre>"; 
					if (($TableRel["WAIdRelType"] == WA_REL_MULTI) &&
						($TableRel["WAIdFormChild"] != $Form["WAIdForm"]))
					{
						$sSql = "SELECT * FROM " . $FormRel["WATableName"];
						$sDeleteList2 = $sDeleteList;
						$token = strtok($sDeleteList2, ",");
						$sWhere = "";
						while ($token) 
						{
							if (!empty($sWhere))
								$sWhere .= " or ";
							$sWhere .= $TableRel["WAFieldChild"] . "=" . $token;
						    $token = strtok(",");
						}
						$sSql .= " WHERE " . $sWhere;
						$rec->SetConnection($FormRel);
						$rec->WAOpenRecordset($sSql);
//						echo $sSql."<br>";
						if ($rec->WAFetchRow())
						{
							$bFoundLinks = True;
							$LinkedContents[$TableRel["WADescr"]] = $TableRel["WADescr"];
						}
						$rec->WACloseRecordset();
					}
					unset($gaForms[$formUIDChild]);
				}
			}
			
			if (!$bFoundLinks)
			{
				$recTablesRel = new WARecordset();
				$recTablesRel->SetupFromGlobalCnn();
				$sSqlFields = "SELECT WAIdForm, WAField, WADescription, WARefFieldValue, WAIdType, WARefTable FROM tbfields WHERE WAIdLinkedForm = ".$Form["WAIdForm"];
		    	$recTablesRel->WAOpenRecordset($sSqlFields);
		    	if ($recTablesRel->WAFetchRow())
		    	{
			    	do 
			    	{
						if ($formUIDRel = BuildForm($recTablesRel->WAResult("WAIdForm"), $iCurrentForm + 1001, False))
						{
							$FormRel = &$gaForms[$formUIDRel];
							if ($recTablesRel->WAResult("WAIdType") != WA_LIST)
							{
								$sSql = "SELECT * FROM " . $FormRel["WATableName"];
								$sDeleteList2 = $sDeleteList;
								$token = strtok($sDeleteList2, ",");
								while ($token) 
								{
									$sWhere = " WHERE ".$recTablesRel->WAResult("WAField")." = ".$token;
									$rec = new WARecordset();
									$rec->SetConnection($FormRel);
//							    	echo "NEXT 2: ".$sSql.$sWhere."<br>";
									$rec->WAOpenRecordset($sSql.$sWhere);
									if ($rec->WAFetchRow())
									{
										$LinkedContents[$FormRel["WAIdForm"]] = $FormRel["WADescr"]."-&gt;".$recTablesRel->WAResult("WADescription");
										$bFoundLinks = True;
									}
									$rec->WACloseRecordset();
								    $token = strtok(",");
									if ($bFoundLinks)
										break;
								}
							}
							else
							{	
								$sSql = "SELECT * FROM " . $recTablesRel->WAResult("WAField");
								$sDeleteList2 = $sDeleteList;
								$token = strtok($sDeleteList2, ",");
								while ($token) 
								{
									$sWhere = " WHERE ".$recTablesRel->WAResult("WARefFieldValue")." = ".$token;
									$rec = new WARecordset();
									$rec->SetConnection($FormRel);
//							    	echo "NEXT 3: ".$sSql.$sWhere."<br>";
									$rec->WAOpenRecordset($sSql.$sWhere);
									if ($rec->WAFetchRow())
									{
										$LinkedContents[$FormRel["WAIdForm"]] = $FormRel["WADescr"]."-&gt;".$recTablesRel->WAResult("WADescription");
										$bFoundLinks = True;
									}
									$rec->WACloseRecordset();
								    $token = strtok(",");
									if ($bFoundLinks)
										break;
								}
							}
							unset($gaForms[$formUIDRel]);
						}
			    	} while ($recTablesRel->WAFetchRow());
			    }
		    	$recTablesRel->WACloseRecordset();
			}
			if (!$bFoundLinks)
				$bRC = True;
			else		
			{
				reset($LinkedContents);
				while (list($Key, $Value) = each ($LinkedContents)) 
				{
					if (!empty($sMessage))
						$sMessage .= ", ";
					$sMessage .= $Value;
				}
			}
		}
		unset($gaForms[$formUID]);
	}
	return $bRC;
}

/**
 * Return an unique list of elements comma separated
 *
 */
 
function WAUniqueStringList($sList)
{
	if (!empty($sList))
	{
		$tmparr = WASplit(',', $sList);
		$sList = "";
		$tmparr = array_unique($tmparr);
		foreach ($tmparr as $v) 
		{
			if (!empty($sList))
				$sList .= ",";
			$sList .= $v;
	    }
	}
    return $sList;
}


?>
Return current item: Phparchive