formrenderer.class.inc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 \Exception;
  20. use \Dict;
  21. use \Combodo\iTop\Form\Form;
  22. /**
  23. * Description of FormRenderer
  24. *
  25. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  26. */
  27. abstract class FormRenderer
  28. {
  29. const ENUM_RENDER_MODE_EXPLODED = 'exploded';
  30. const ENUM_RENDER_MODE_JOINED = 'joined';
  31. const DEFAULT_RENDERER_NAMESPACE = '';
  32. protected $oForm;
  33. protected $sEndpoint;
  34. protected $aSupportedFields;
  35. protected $sBaseLayout;
  36. protected $aOutputs;
  37. /**
  38. * Default constructor
  39. *
  40. * @param \Combodo\iTop\Form\Form $oForm
  41. */
  42. public function __construct(Form $oForm = null)
  43. {
  44. if ($oForm !== null)
  45. {
  46. $this->oForm = $oForm;
  47. }
  48. $this->sBaseLayout = '';
  49. $this->InitOutputs();
  50. }
  51. public function GetForm()
  52. {
  53. return $this->oForm;
  54. }
  55. public function SetForm($oForm)
  56. {
  57. $this->oForm = $oForm;
  58. return $this;
  59. }
  60. public function GetEndpoint()
  61. {
  62. return $this->sEndpoint;
  63. }
  64. public function SetEndpoint($sEndpoint)
  65. {
  66. $this->sEndpoint = $sEndpoint;
  67. return $this;
  68. }
  69. public function GetBaseLayout()
  70. {
  71. return $this->sBaseLayout;
  72. }
  73. public function SetBaseLayout($sBaseLayout)
  74. {
  75. $this->sBaseLayout = $sBaseLayout;
  76. return $this;
  77. }
  78. public function GetFieldRendererClass($oField)
  79. {
  80. if (array_key_exists(get_class($oField), $this->aSupportedFields))
  81. {
  82. return $this->aSupportedFields[get_class($oField)];
  83. }
  84. else
  85. {
  86. throw new Exception('Field type not supported by the renderer: '.get_class($oField));
  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. $oFieldRenderer = new $sFieldRendererClass($oField);
  159. $oFieldRenderer->SetEndpoint($this->GetEndpoint());
  160. $oRenderingOutput = $oFieldRenderer->Render();
  161. // HTML
  162. if ($oRenderingOutput->GetHtml() !== '')
  163. {
  164. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  165. {
  166. $output['html'] = $oRenderingOutput->GetHtml();
  167. }
  168. else
  169. {
  170. $output['html'] .= $oRenderingOutput->GetHtml();
  171. }
  172. }
  173. // JS files
  174. foreach ($oRenderingOutput->GetJsFiles() as $sJsFile)
  175. {
  176. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  177. {
  178. if (!in_array($sJsFile, $output['js_files']))
  179. {
  180. $output['js_files'][] = $sJsFile;
  181. }
  182. }
  183. else
  184. {
  185. $output['html'] .= '<script src="' . $sJsFile . '" type="text/javascript"></script>';
  186. }
  187. }
  188. // JS inline
  189. if ($oRenderingOutput->GetJs() !== '')
  190. {
  191. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  192. {
  193. $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs();
  194. }
  195. else
  196. {
  197. $output['html'] .= '<script type="text/javascript">' . $oRenderingOutput->GetJs() . '</script>';
  198. }
  199. }
  200. // CSS files
  201. foreach ($oRenderingOutput->GetCssFiles() as $sCssFile)
  202. {
  203. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  204. {
  205. if (!in_array($sCssFile, $output['css_files']))
  206. {
  207. $output['css_files'][] = $sCssFile;
  208. }
  209. }
  210. else
  211. {
  212. $output['html'] .= '<link href="' . $sCssFile . '" rel="stylesheet" />';
  213. }
  214. }
  215. // CSS inline
  216. if ($oRenderingOutput->GetCss() !== '')
  217. {
  218. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  219. {
  220. $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss();
  221. }
  222. else
  223. {
  224. $output['html'] .= '<style>' . $oRenderingOutput->GetCss() . '</style>';
  225. }
  226. }
  227. return $output;
  228. }
  229. }