/lib/Form.php

https://github.com/xsist10/meta-builder · PHP · 213 lines · 161 code · 29 blank · 23 comment · 15 complexity · 54b3c407929cb48cc0de9866a2df2cbb MD5 · raw file

  1. <?php
  2. /*
  3. * Builder_Form
  4. *
  5. * Copyright(c) 2010, Thomas Shone
  6. * Licensed under the Creative Commons Attribution 3.0 Unported License.
  7. * http://www.shone.co.za
  8. *
  9. * Constructs forms from meta data
  10. */
  11. class Builder_Form extends Builder_Base
  12. {
  13. const RENDER_MODE_INPUT_ONLY = 1;
  14. const RENDER_MODE_LABEL_INPUT = 2;
  15. /**
  16. * Parse through the meta and determine if there are any 'file' type inputs
  17. * in this form. If there are return the appropriate encoding attribute.
  18. *
  19. * @param array $aMeta
  20. * @return string
  21. */
  22. private function ___GetFormEncoding($aMeta)
  23. {
  24. if (!empty($aMeta['groups']))
  25. {
  26. foreach ($aMeta['groups'] as $aGroup)
  27. {
  28. if (!empty($aGroup['rows']))
  29. {
  30. foreach ($aGroup['rows'] as $aRow)
  31. {
  32. if (!empty($aRow['element']))
  33. {
  34. foreach ($aRow['element'] as $aElement)
  35. {
  36. if ($aElement['type'] == 'file')
  37. {
  38. return 'enctype="multipart/form-data"';
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. return '';
  47. }
  48. protected function BuildGroups($aMeta)
  49. {
  50. foreach ($aMeta as $aGroup)
  51. {
  52. $this->sResult .= "<thead class=\"form-head\">\n";
  53. !empty($aGroup['heading']) && $this->BuildHeading($aGroup['heading']);
  54. !empty($aGroup['subheading']) && $this->BuildSubheading($aGroup['subheading']);
  55. $this->sResult .= "</thead>\n";
  56. $this->sResult .= "<tbody class=\"form-body\">\n";
  57. !empty($aGroup['copy']) && $this->BuildCopy($aGroup['copy']);
  58. if (!empty($aGroup['rows']))
  59. {
  60. foreach ($aGroup['rows'] as $aRow)
  61. {
  62. $this->BuildRow($aRow);
  63. }
  64. }
  65. $this->sResult .= "</tbody>\n";
  66. }
  67. }
  68. protected function BuildRow($aRow)
  69. {
  70. if (!empty($aRow['copy']))
  71. {
  72. $this->BuildInlineCopy($aRow['copy']);
  73. }
  74. // Check if all the fields are required
  75. $bRequired = true;
  76. if (!empty($aRow['element']))
  77. {
  78. foreach ($aRow['element'] as $aElement)
  79. {
  80. $bRequired &= !empty($aElement['validation']['required']);
  81. }
  82. }
  83. else
  84. {
  85. $bRequired = false;
  86. }
  87. #-> Build Label
  88. $sLabel = !empty($aRow['label']) ? $aRow['label'] : '&nbsp;';
  89. $sLabel .= $bRequired ? ' <span class="required">*</span>' : '';
  90. if (!empty($aRow['image']))
  91. {
  92. $sLabel = '<img align="left" src="' . BuildImage($aRow['image']) .'" /> '. $sLabel;
  93. }
  94. #-> Build Elements
  95. $sElement = '';
  96. if (!empty($aRow['element']))
  97. {
  98. $oElement = new Builder_Form_Element();
  99. foreach ($aRow['element'] as $aElement)
  100. {
  101. $sElement .= $oElement->Build($aElement, $this->aData);
  102. }
  103. };
  104. #-> Build the row's HTML
  105. $sRenderMode = !empty($aRow['render-mode'])
  106. ? $aRow['render-mode']
  107. : '';
  108. switch ($sRenderMode)
  109. {
  110. case self::RENDER_MODE_INPUT_ONLY:
  111. $this->sResult .= "<tr>\n";
  112. $this->sResult .= "<td class=\"form-value\"" . (!empty($aRow['id']) ? " id=\"" . $aRow['id'] ."-value\"" : "") . " colspan=\"2\">" . $sLabel . " " . $sElement . "</td>\n";
  113. $this->sResult .= "</tr>\n";
  114. break;
  115. case self::RENDER_MODE_LABEL_INPUT:
  116. default:
  117. $this->sResult .= "<tr>\n";
  118. $this->sResult .= "<td class=\"form-label\"" . (!empty($aRow['id']) ? " id=\"" . $aRow['id'] ."-label\"" : "") . ">" . $sLabel . "</td>\n";
  119. $this->sResult .= "<td class=\"form-value\"" . (!empty($aRow['id']) ? " id=\"" . $aRow['id'] ."-value\"" : "") . ">" . $sElement . "</td>\n";
  120. $this->sResult .= "</tr>\n";
  121. break;
  122. }
  123. }
  124. protected function BuildHeading($sHeading)
  125. {
  126. $this->sResult .= '<tr><td colspan="2" class="form-header">' . $sHeading . '</td></tr>' . "\n";
  127. }
  128. protected function BuildSubheading($sSubheading)
  129. {
  130. $this->sResult .= '<tr><td colspan="2" class="form-subheader">' . $sSubheading . '</td></tr>' . "\n";
  131. }
  132. protected function BuildInlineCopy($sCopy)
  133. {
  134. $this->sResult .= '<tr><td colspan="2" class="form-inline-copy">' . $sCopy . '</td></tr>' . "\n";
  135. }
  136. protected function BuildCopy($sCopy)
  137. {
  138. $this->sResult .= '<tr><td colspan="2" class="form-copy">' . $sCopy . '</td></tr>' . "\n";
  139. }
  140. protected function BuildIdentity($sIdentity)
  141. {
  142. $this->sResult .= "<input type=\"hidden\" name=\"input[identity]\" value=\"" . $sIdentity . "\">\n";
  143. }
  144. protected function BuildWorkflow($sWorkflow)
  145. {
  146. $this->sResult .= "<input type=\"hidden\" name=\"input[workflow]\" value=\"" . $sWorkflow . "\">\n";
  147. }
  148. protected function BuildAction($sAction)
  149. {
  150. $this->sResult .= "<input type=\"hidden\" name=\"input[action]\" value=\"" . $sAction . "\">\n";
  151. }
  152. public function Render($aMeta, $aData = array())
  153. {
  154. $aTimer['Start'] = microtime(true);
  155. $this->sResult = '';
  156. if (empty($aMeta['identity']))
  157. {
  158. throw new Exception('No identity specified');
  159. }
  160. //empty($aData) && $aData = Session::GetInput($aMeta['identity']);
  161. $this->SetConfig($aMeta, $aData);
  162. $sAction = !empty($aMeta['script']) ? $aMeta['script'] : 'form.php';
  163. $this->sResult .= "<form method=\"post\" ". $this->___GetFormEncoding($aMeta) ." action=\"" . BuildUrl($sAction) ."\" id=\"" . $aMeta['identity'] . "\">\n";
  164. $this->sResult .= "<table class=\"form\" id=\"" . $aMeta['identity'] . "-table\">\n";
  165. $this->Build($aMeta);
  166. $this->sResult .= "</table>\n";
  167. // Enable Validation for this Form
  168. $this->sResult .= "
  169. <script>
  170. $(document).ready(function()
  171. {
  172. $('#" . $aMeta['identity'] . "').validator({
  173. inputEvent: blur,
  174. messageClass: 'validation-error'
  175. });
  176. });
  177. </script>
  178. ";
  179. $this->sResult .= "</form>\n";
  180. $aTimer['End'] = microtime(true);
  181. // $this->sResult .= '<div class="timer">Time taken to generate: ' . ($aTimer['End'] - $aTimer['Start']) . 's </div>';
  182. return $this->sResult;
  183. }
  184. }