formrenderer.class.inc.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. namespace Combodo\iTop\Renderer;
  19. use \Dict;
  20. use \Combodo\iTop\Form\Form;
  21. /**
  22. * Description of FormRenderer
  23. *
  24. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  25. */
  26. abstract class FormRenderer
  27. {
  28. const ENUM_RENDER_MODE_EXPLODED = 'exploded';
  29. const ENUM_RENDER_MODE_JOINED = 'joined';
  30. const DEFAULT_RENDERER_NAMESPACE = '';
  31. protected $oForm;
  32. protected $sEndpoint;
  33. protected $aSupportedFields;
  34. protected $sBaseLayout;
  35. protected $aOutputs;
  36. /**
  37. * Default constructor
  38. *
  39. * @param \Combodo\iTop\Form\Form $oForm
  40. */
  41. public function __construct(Form $oForm = null)
  42. {
  43. if ($oForm !== null)
  44. {
  45. $this->oForm = $oForm;
  46. }
  47. $this->sBaseLayout = '';
  48. $this->InitOutputs();
  49. }
  50. public function GetForm()
  51. {
  52. return $this->oForm;
  53. }
  54. public function SetForm($oForm)
  55. {
  56. $this->oForm = $oForm;
  57. return $this;
  58. }
  59. public function GetEndpoint()
  60. {
  61. return $this->sEndpoint;
  62. }
  63. public function SetEndpoint($sEndpoint)
  64. {
  65. $this->sEndpoint = $sEndpoint;
  66. return $this;
  67. }
  68. public function GetBaseLayout()
  69. {
  70. return $this->sBaseLayout;
  71. }
  72. public function SetBaseLayout($sBaseLayout)
  73. {
  74. $this->sBaseLayout = $sBaseLayout;
  75. return $this;
  76. }
  77. public function GetFieldRendererClass($oField)
  78. {
  79. if (array_key_exists(get_class($oField), $this->aSupportedFields))
  80. {
  81. return $this->aSupportedFields[get_class($oField)];
  82. }
  83. else
  84. {
  85. // TODO : We might want to throw an exception.
  86. return null;
  87. }
  88. }
  89. /**
  90. * Returns the field identified by the id $sId in $this->oForm.
  91. *
  92. * @param string $sId
  93. * @return Combodo\iTop\Renderer\FieldRenderer
  94. */
  95. public function GetFieldRendererClassFromId($sId)
  96. {
  97. return $this->GetFieldRendererClass($this->oForm->GetField($sId));
  98. }
  99. /**
  100. *
  101. * @return array
  102. */
  103. public function GetOutputs()
  104. {
  105. return $this->aOutputs;
  106. }
  107. /**
  108. * Registers a Renderer class for the specified Field class.
  109. *
  110. * If the Field class is not fully qualified, the default "Combodo\iTop\Form\Field" will be prepend.
  111. * If the Field class already had a registered Renderer, it is replaced.
  112. *
  113. * @param string $sFieldClass
  114. * @param string $sRendererClass
  115. */
  116. public function AddSupportedField($sFieldClass, $sRendererClass)
  117. {
  118. $sFieldClass = (strpos($sFieldClass, '\\') !== false) ? $sFieldClass : 'Combodo\\iTop\\Form\\Field\\' . $sFieldClass;
  119. $sRendererClass = (strpos($sRendererClass, '\\') !== false) ? $sRendererClass : static::DEFAULT_RENDERER_NAMESPACE . $sRendererClass;
  120. $this->aSupportedFields[$sFieldClass] = $sRendererClass;
  121. return $this;
  122. }
  123. public function InitOutputs()
  124. {
  125. $this->aOutputs = array();
  126. return $this;
  127. }
  128. public function Render()
  129. {
  130. $this->InitOutputs();
  131. foreach ($this->oForm->GetFields() as $oField)
  132. {
  133. $this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField);
  134. }
  135. return $this->aOutputs;
  136. }
  137. /**
  138. * Returns the output for the $oField. Output format depends on the $sMode.
  139. *
  140. * If $sMode = 'exploded', output is an has array with id / html / js_inline / js_files / css_inline / css_files / validators
  141. * Else if $sMode = 'joined', output is a string with everything in it
  142. *
  143. * @param Combodo\iTop\Form\Field\Field $oField
  144. * @param string $sMode 'exploded'|'joined'
  145. * @return mixed
  146. */
  147. protected function PrepareOutputForField($oField, $sMode = 'exploded')
  148. {
  149. $output = array(
  150. 'id' => $oField->GetId(),
  151. 'html' => '',
  152. 'js_inline' => '',
  153. 'css_inline' => '',
  154. 'js_files' => array(),
  155. 'css_files' => array(),
  156. 'validators' => null
  157. );
  158. $sFieldRendererClass = $this->GetFieldRendererClass($oField);
  159. // TODO : We might want to throw an exception instead when there is no renderer for that field
  160. if ($sFieldRendererClass !== null)
  161. {
  162. $oFieldRenderer = new $sFieldRendererClass($oField);
  163. $oFieldRenderer->SetEndpoint($this->GetEndpoint());
  164. $oRenderingOutput = $oFieldRenderer->Render();
  165. // HTML
  166. if ($oRenderingOutput->GetHtml() !== '')
  167. {
  168. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  169. {
  170. $output['html'] = $oRenderingOutput->GetHtml();
  171. }
  172. else
  173. {
  174. $output['html'] .= $oRenderingOutput->GetHtml();
  175. }
  176. }
  177. // JS files
  178. foreach ($oRenderingOutput->GetJsFiles() as $sJsFile)
  179. {
  180. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  181. {
  182. if (!in_array($sJsFile, $output['js_files']))
  183. {
  184. $output['js_files'][] = $sJsFile;
  185. }
  186. }
  187. else
  188. {
  189. $output['html'] .= '<script src="' . $sJsFile . '" type="text/javascript"></script>';
  190. }
  191. }
  192. // JS inline
  193. if ($oRenderingOutput->GetJs() !== '')
  194. {
  195. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  196. {
  197. $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs();
  198. }
  199. else
  200. {
  201. $output['html'] .= '<script type="text/javascript">' . $oRenderingOutput->GetJs() . '</script>';
  202. }
  203. }
  204. // CSS files
  205. foreach ($oRenderingOutput->GetCssFiles() as $sCssFile)
  206. {
  207. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  208. {
  209. if (!in_array($sCssFile, $output['css_files']))
  210. {
  211. $output['css_files'][] = $sCssFile;
  212. }
  213. }
  214. else
  215. {
  216. $output['html'] .= '<link href="' . $sCssFile . '" rel="stylesheet" />';
  217. }
  218. }
  219. // CSS inline
  220. if ($oRenderingOutput->GetCss() !== '')
  221. {
  222. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  223. {
  224. $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss();
  225. }
  226. else
  227. {
  228. $output['html'] .= '<style>' . $oRenderingOutput->GetCss() . '</style>';
  229. }
  230. }
  231. // Validators
  232. foreach ($oField->GetValidators() as $oValidator)
  233. {
  234. $output['validators'][$oValidator::GetName()] = array(
  235. 'reg_exp' => $oValidator->GetRegExp(),
  236. 'message' => Dict::S($oValidator->GetErrorMessage())
  237. );
  238. }
  239. // Subfields
  240. // TODO
  241. }
  242. return $output;
  243. }
  244. }