formrenderer.class.inc.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. );
  157. $sFieldRendererClass = $this->GetFieldRendererClass($oField);
  158. // TODO : We might want to throw an exception instead when there is no renderer for that field
  159. if ($sFieldRendererClass !== null)
  160. {
  161. $oFieldRenderer = new $sFieldRendererClass($oField);
  162. $oFieldRenderer->SetEndpoint($this->GetEndpoint());
  163. $oRenderingOutput = $oFieldRenderer->Render();
  164. // HTML
  165. if ($oRenderingOutput->GetHtml() !== '')
  166. {
  167. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  168. {
  169. $output['html'] = $oRenderingOutput->GetHtml();
  170. }
  171. else
  172. {
  173. $output['html'] .= $oRenderingOutput->GetHtml();
  174. }
  175. }
  176. // JS files
  177. foreach ($oRenderingOutput->GetJsFiles() as $sJsFile)
  178. {
  179. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  180. {
  181. if (!in_array($sJsFile, $output['js_files']))
  182. {
  183. $output['js_files'][] = $sJsFile;
  184. }
  185. }
  186. else
  187. {
  188. $output['html'] .= '<script src="' . $sJsFile . '" type="text/javascript"></script>';
  189. }
  190. }
  191. // JS inline
  192. if ($oRenderingOutput->GetJs() !== '')
  193. {
  194. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  195. {
  196. $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs();
  197. }
  198. else
  199. {
  200. $output['html'] .= '<script type="text/javascript">' . $oRenderingOutput->GetJs() . '</script>';
  201. }
  202. }
  203. // CSS files
  204. foreach ($oRenderingOutput->GetCssFiles() as $sCssFile)
  205. {
  206. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  207. {
  208. if (!in_array($sCssFile, $output['css_files']))
  209. {
  210. $output['css_files'][] = $sCssFile;
  211. }
  212. }
  213. else
  214. {
  215. $output['html'] .= '<link href="' . $sCssFile . '" rel="stylesheet" />';
  216. }
  217. }
  218. // CSS inline
  219. if ($oRenderingOutput->GetCss() !== '')
  220. {
  221. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  222. {
  223. $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss();
  224. }
  225. else
  226. {
  227. $output['html'] .= '<style>' . $oRenderingOutput->GetCss() . '</style>';
  228. }
  229. }
  230. }
  231. return $output;
  232. }
  233. }