/**
* Helper class to build interactive forms to be used either in stand-alone
* modal dialog or in "property-sheet" panes.
*
* @copyright Copyright (C) 2010-2012 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
class DesignerForm
{
protected $aFieldSets;
protected $sCurrentFieldSet;
protected $sScript;
protected $sReadyScript;
protected $sFormId;
protected $sFormPrefix;
protected $sParamsContainer;
protected $oParentForm;
protected $aSubmitParams;
protected $sSubmitTo;
protected $bReadOnly;
protected $sHierarchyPath; // Needed to manage the visibility of nested subform
protected $sHierarchyParent; // Needed to manage the visibility of nested subform
protected $sHierarchySelector; // Needed to manage the visibility of nested subform
protected $bDisplayed;
protected $aDefaultValues;
protected $sFieldsSuffix;
public function __construct()
{
$this->aFieldSets = array();
$this->sCurrentFieldSet = '';
$this->sScript = '';
$this->sReadyScript = '';
$this->sFormPrefix = '';
$this->sFieldsSuffix = '';
$this->sParamsContainer = '';
$this->sFormId = 'form_'.rand();
$this->oParentForm = null;
$this->bReadOnly = false;
$this->sHierarchyPath = '';
$this->sHierarchyParent = '';
$this->sHierarchySelector = '';
$this->StartFieldSet($this->sCurrentFieldSet);
$this->bDisplayed = true;
$this->aDefaultvalues = array();
}
public function AddField(DesignerFormField $oField)
{
if (!is_array($this->aFieldSets[$this->sCurrentFieldSet]))
{
$this->aFieldSets[$this->sCurrentFieldSet] = array();
}
$this->aFieldSets[$this->sCurrentFieldSet][] = $oField;
$oField->SetForm($this);
}
public function StartFieldSet($sLabel)
{
$this->sCurrentFieldSet = $sLabel;
if (!array_key_exists($this->sCurrentFieldSet, $this->aFieldSets))
{
$this->aFieldSets[$this->sCurrentFieldSet] = array();
}
}
public function Render($oP, $bReturnHTML = false)
{
$sFormId = $this->GetFormId();
if ($this->oParentForm == null)
{
$sReturn = '
';
}
if($this->sScript != '')
{
$oP->add_script($this->sScript);
}
if($this->sReadyScript != '')
{
$oP->add_ready_script($this->sReadyScript);
}
if ($bReturnHTML)
{
return $sReturn;
}
else
{
$oP->add($sReturn);
}
}
public function GetFieldSets()
{
return $this->aFieldSets;
}
public function SetSubmitParams($sSubmitToUrl, $aSubmitParams)
{
$this->sSubmitTo = $sSubmitToUrl;
$this->aSubmitParams = $aSubmitParams;
}
public function CopySubmitParams($oParentForm)
{
$this->sSubmitTo = $oParentForm->sSubmitTo;
$this->aSubmitParams = $oParentForm->aSubmitParams;
}
public function GetSubmitParams()
{
return array( 'url' => $this->sSubmitTo, 'params' => $this->aSubmitParams);
}
/**
* Helper to handle subforms hide/show
*/
public function SetHierarchyPath($sHierarchy)
{
$this->sHierarchyPath = $sHierarchy;
}
/**
* Helper to handle subforms hide/show
*/
public function GetHierarchyPath()
{
return $this->sHierarchyPath;
}
/**
* Helper to handle subforms hide/show
*/
public function SetHierarchyParent($sHierarchy)
{
$this->sHierarchyParent = $sHierarchy;
}
/**
* Helper to handle subforms hide/show
*/
public function GetHierarchyParent()
{
return $this->sHierarchyParent;
}
public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
{
$sReturn = '';
$sActionUrl = addslashes($this->sSubmitTo);
$sJSSubmitParams = json_encode($this->aSubmitParams);
$sFormId = $this->GetFormId();
if ($this->oParentForm == null)
{
$sReturn = '';
$sReturn .= ''; // for the return of the submit operation
}
else
{
$sReturn .= $sHiddenFields;
}
$this->AddReadyScript(
<<sScript != '')
{
$oP->add_script($this->sScript);
}
if($this->sReadyScript != '')
{
$oP->add_ready_script($this->sReadyScript);
}
if ($bReturnHTML)
{
return $sReturn;
}
else
{
$oP->add($sReturn);
}
}
public function StartRow($sFieldId = null)
{
if ($sFieldId != null)
{
return '';
}
return '
';
}
public function EndRow()
{
return '
';
}
public function RenderAsDialog($oPage, $sDialogId, $sDialogTitle, $iDialogWidth, $sOkButtonLabel, $sIntroduction = null, $bAutoOpen = true)
{
$this->SetPrefix('dlg_'); // To make sure that the controls have different IDs that the property sheet which may be displayed at the same time
$sDialogTitle = addslashes($sDialogTitle);
$sOkButtonLabel = addslashes($sOkButtonLabel);
$sCancelButtonLabel = Dict::S('UI:Button:Cancel');
$oPage->add("");
if ($sIntroduction != null)
{
$oPage->add('');
}
$this->Render($oPage);
$oPage->add('
');
$sAutoOpen = $bAutoOpen ? 'true' : 'false';
$oPage->add_ready_script(
<<aFieldSets as $sLabel => $aFields)
{
foreach($aFields as $oField)
{
$oField->ReadParam($aValues);
}
}
return $aValues;
}
public function SetPrefix($sPrefix)
{
$this->sFormPrefix = $sPrefix;
}
public function GetPrefix()
{
$sPrefix = '';
if ($this->oParentForm != null)
{
$sPrefix = $this->oParentForm->GetPrefix();
}
return $sPrefix.$this->sFormPrefix;
}
public function SetSuffix($sSuffix)
{
$this->sFieldsSuffix = $sSuffix;
}
public function GetSuffix()
{
$sSuffix = '';
if ($this->oParentForm != null)
{
$sSuffix = $this->oParentForm->GetSuffix();
}
return $sSuffix.$this->sFieldsSuffix;
}
public function SetReadOnly($bReadOnly = true)
{
$this->bReadOnly = $bReadOnly;
}
public function IsReadOnly()
{
if ($this->oParentForm == null)
{
return $this->bReadOnly;
}
else
{
return $this->oParentForm->IsReadOnly();
}
}
public function SetParamsContainer($sParamsContainer)
{
$this->sParamsContainer = $sParamsContainer;
}
public function GetParamsContainer()
{
if ($this->oParentForm == null)
{
return $this->sParamsContainer;
}
else
{
return $this->oParentForm->GetParamsContainer();
}
}
public function SetParentForm($oParentForm)
{
$this->oParentForm = $oParentForm;
}
public function SetDefaultValues($aDefaultValues)
{
if (!is_array($aDefaultValues)) return;
foreach($this->aFieldSets as $sLabel => $aFields)
{
foreach($aFields as $oField)
{
$oField->SetDefaultValueFrom($aDefaultValues);
}
}
}
public function GetDefaultValues()
{
return $this->aDefaultValues;
}
public function GetParentForm()
{
return $this->oParentForm;
}
public function GetFormId()
{
if ($this->oParentForm)
{
$this->oParentForm->GetFormId();
}
return $this->sFormId;
}
public function SetDisplayed($bDisplayed)
{
$this->bDisplayed = $bDisplayed;
}
public function IsDisplayed()
{
if ($this->oParentForm == null)
{
return $this->bDisplayed;
}
else
{
return ($this->bDisplayed && $this->oParentForm->IsDisplayed());
}
}
public function AddScript($sScript)
{
$this->sScript .= $sScript;
}
public function AddReadyScript($sScript)
{
$this->sReadyScript .= $sScript;
}
public function GetFieldId($sCode)
{
return $this->GetPrefix().'attr_'.$sCode;
}
public function GetFieldName($sCode)
{
return 'attr_'.$sCode.$this->GetSuffix();
}
public function GetParamName($sCode)
{
return 'attr_'.$sCode.$this->GetSuffix();
}
public function GetValidationArea($sId, $sContent = '')
{
return "$sContent";
}
public function GetAsyncActionClass()
{
return $this->sAsyncActionClass;
}
public function FindField($sFieldCode)
{
$oFoundField = false;
foreach($this->aFieldSets as $sLabel => $aFields)
{
foreach($aFields as $oField)
{
$oFoundField = $oField->FindField($sFieldCode);
if ($oFoundField !== false) break;
}
if ($oFoundField !== false) break;
}
return $oFoundField;
}
}
class DesignerTabularForm extends DesignerForm
{
protected $aTable;
public function __construct()
{
parent::__construct();
$this->aTable = array();
}
public function AddRow($aRow)
{
$this->aTable[] = $aRow;
}
public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
{
return $this->Render($oP, $bReturnHTML);
}
public function Render($oP, $bReturnHTML = false)
{
$sReturn = '';
if ($this->oParentForm == null)
{
$sFormId = $this->sFormId;
$sReturn = '