<?php
/*
* BuilderForm
*
* Copyright(c) 2010, Thomas Shone
* Licensed under the Creative Commons Attribution 3.0 Unported License.
* http://www.shone.co.za
*
* Constructs forms from meta data
*/
class BuilderForm extends BuilderBase
{
protected function BuildGroups($aMeta)
{
foreach ($aMeta as $aGroup)
{
$this->sResult .= "<thead class=\"form-head\">\n";
!empty($aGroup['heading']) && $this->BuildHeading($aGroup['heading']);
!empty($aGroup['subheading']) && $this->BuildSubheading($aGroup['subheading']);
$this->sResult .= "</thead>\n";
$this->sResult .= "<tbody class=\"form-body\">\n";
!empty($aGroup['copy']) && $this->BuildCopy($aGroup['copy']);
if (!empty($aGroup['rows']))
{
foreach ($aGroup['rows'] as $aRow)
{
$this->BuildRow($aRow);
}
}
$this->sResult .= "</tbody>\n";
}
}
protected function BuildRow($aRow)
{
if (!empty($aRow['copy']))
{
$this->BuildInlineCopy($aRow['copy']);
}
// Check if all the fields are required
$bRequired = true;
if (!empty($aRow['element']))
{
foreach ($aRow['element'] as $aElement)
{
$bRequired &= !empty($aElement['validation']['required']);
}
}
else
{
$bRequired = false;
}
$sLabel = !empty($aRow['label']) ? $aRow['label'] : ' ';
$sLabel .= $bRequired ? ' <span class="required">*</span>' : '';
if (!empty($aRow['image']))
{
$sLabel = '<img align="left" src="' . BuildUrl('html/images/'. $aRow['image']) .'" /> '. $sLabel;
}
$this->sResult .= "<tr>\n";
$this->sResult .= "<td class=\"form-label\"" . (!empty($aRow['id']) ? " id=\"" . $aRow['id'] ."-label\"" : "") . ">" . $sLabel . "</td>\n";
$this->sResult .= "<td class=\"form-value\"" . (!empty($aRow['id']) ? " id=\"" . $aRow['id'] ."-value\"" : "") . ">";
$oElement = new BuilderFormElement();
if (!empty($aRow['element']))
{
foreach ($aRow['element'] as $aElement)
{
$this->sResult .= $oElement->Build($aElement, $this->aData);
}
}
$this->sResult .= "</td>\n";
$this->sResult .= "</tr>\n";
}
protected function BuildHeading($sHeading)
{
$this->sResult .= '<tr><td colspan="2" class="form-header">' . $sHeading . '</td></tr>' . "\n";
}
protected function BuildSubheading($sSubheading)
{
$this->sResult .= '<tr><td colspan="2" class="form-subheader">' . $sSubheading . '</td></tr>' . "\n";
}
protected function BuildInlineCopy($sCopy)
{
$this->sResult .= '<tr><td colspan="2" class="form-inline-copy">' . $sCopy . '</td></tr>' . "\n";
}
protected function BuildCopy($sCopy)
{
$this->sResult .= '<tr><td colspan="2" class="form-copy">' . $sCopy . '</td></tr>' . "\n";
}
protected function BuildIdentity($sIdentity)
{
$this->sResult .= "<input type=\"hidden\" name=\"input[identity]\" value=\"" . $sIdentity . "\">\n";
}
protected function BuildWorkflow($sWorkflow)
{
$this->sResult .= "<input type=\"hidden\" name=\"input[workflow]\" value=\"" . $sWorkflow . "\">\n";
}
public function Render($aMeta, $aData = array())
{
if (empty($aMeta['identity']))
{
throw new Exception('No identity specified');
}
/* For later release
if (empty($aMeta['workflow']))
{
throw new Exception('No workflow specified');
}*/
empty($aData) && $aData = Session::GetInput($aMeta['identity']);
$this->SetConfig($aMeta, $aData);
$sAction = !empty($aMeta['script']) ? $aMeta['script'] : 'form.php';
$this->sResult = '';
$this->sResult .= "<form method=\"post\" ". $this->___GetFormEncoding($aMeta) ." action=\"" . BuildUrl($sAction) ."\" id=\"" . $aMeta['identity'] . "\">\n";
$this->sResult .= "<table class=\"form\" id=\"" . $aMeta['identity'] . "-table\">\n";
$this->Build($aMeta);
$this->sResult .= "</table>\n";
$this->sResult .= "
<script>
$(document).ready(function() {
$('form#". $aMeta['identity'] ."').validationEngine({
success : false,
failure : function() {}
});
});
</script>
";
$this->sResult .= "</form>\n";
return $this->sResult;
}
/**
* Parse through the meta and determine if there are any 'file' type inputs
* in this form. If there are return the appropriate encoding attribute.
*
* @param array $aMeta
* @return string
*/
private function ___GetFormEncoding($aMeta)
{
if (!empty($aMeta['group']))
{
foreach ($aMeta['group'] as $aGroup)
{
if (!empty($aGroup['rows']))
{
foreach ($aGroup['rows'] as $aRow)
{
if (!empty($aRow['element']))
{
foreach ($aRow['element'] as $aElement)
{
if ($aElement['type'] == 'file')
{
return 'enctype="multipart/form-data"';
}
}
}
}
}
}
}
return '';
}
}