formrenderer.class.inc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. use \Combodo\iTop\Form\Field\Field;
  23. /**
  24. * Description of FormRenderer
  25. *
  26. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  27. */
  28. abstract class FormRenderer
  29. {
  30. const ENUM_RENDER_MODE_EXPLODED = 'exploded';
  31. const ENUM_RENDER_MODE_JOINED = 'joined';
  32. const DEFAULT_RENDERER_NAMESPACE = '';
  33. protected $oForm;
  34. protected $sEndpoint;
  35. protected $aSupportedFields;
  36. protected $sBaseLayout;
  37. protected $aOutputs;
  38. /**
  39. * Default constructor
  40. *
  41. * @param \Combodo\iTop\Form\Form $oForm
  42. */
  43. public function __construct(Form $oForm = null)
  44. {
  45. if ($oForm !== null)
  46. {
  47. $this->oForm = $oForm;
  48. }
  49. $this->sBaseLayout = '';
  50. $this->InitOutputs();
  51. }
  52. /**
  53. *
  54. * @return \Combodo\iTop\Form\Form
  55. */
  56. public function GetForm()
  57. {
  58. return $this->oForm;
  59. }
  60. /**
  61. *
  62. * @param \Combodo\iTop\Form\Form $oForm
  63. * @return \Combodo\iTop\Renderer\FormRenderer
  64. */
  65. public function SetForm(Form $oForm)
  66. {
  67. $this->oForm = $oForm;
  68. return $this;
  69. }
  70. /**
  71. *
  72. * @return string
  73. */
  74. public function GetEndpoint()
  75. {
  76. return $this->sEndpoint;
  77. }
  78. /**
  79. *
  80. * @param string $sEndpoint
  81. * @return \Combodo\iTop\Renderer\FormRenderer
  82. */
  83. public function SetEndpoint($sEndpoint)
  84. {
  85. $this->sEndpoint = $sEndpoint;
  86. return $this;
  87. }
  88. /**
  89. *
  90. * @return string
  91. */
  92. public function GetBaseLayout()
  93. {
  94. return $this->sBaseLayout;
  95. }
  96. /**
  97. *
  98. * @param string $sBaseLayout
  99. * @return \Combodo\iTop\Renderer\FormRenderer
  100. */
  101. public function SetBaseLayout($sBaseLayout)
  102. {
  103. $this->sBaseLayout = $sBaseLayout;
  104. return $this;
  105. }
  106. /**
  107. *
  108. * @param \Combodo\iTop\Form\Field\Field $oField
  109. * @return string
  110. * @throws Exception
  111. */
  112. public function GetFieldRendererClass(Field $oField)
  113. {
  114. if (array_key_exists(get_class($oField), $this->aSupportedFields))
  115. {
  116. return $this->aSupportedFields[get_class($oField)];
  117. }
  118. else
  119. {
  120. throw new Exception('Field type not supported by the renderer: ' . get_class($oField));
  121. }
  122. }
  123. /**
  124. * Returns the field identified by the id $sId in $this->oForm.
  125. *
  126. * @param string $sId
  127. * @return \Combodo\iTop\Renderer\FieldRenderer
  128. */
  129. public function GetFieldRendererClassFromId($sId)
  130. {
  131. return $this->GetFieldRendererClass($this->oForm->GetField($sId));
  132. }
  133. /**
  134. *
  135. * @return array
  136. */
  137. public function GetOutputs()
  138. {
  139. return $this->aOutputs;
  140. }
  141. /**
  142. * Registers a Renderer class for the specified Field class.
  143. *
  144. * If the Field class is not fully qualified, the default "Combodo\iTop\Form\Field" will be prepend.
  145. * If the Field class already had a registered Renderer, it is replaced.
  146. *
  147. * @param string $sFieldClass
  148. * @param string $sRendererClass
  149. */
  150. public function AddSupportedField($sFieldClass, $sRendererClass)
  151. {
  152. $sFieldClass = (strpos($sFieldClass, '\\') !== false) ? $sFieldClass : 'Combodo\\iTop\\Form\\Field\\' . $sFieldClass;
  153. $sRendererClass = (strpos($sRendererClass, '\\') !== false) ? $sRendererClass : static::DEFAULT_RENDERER_NAMESPACE . $sRendererClass;
  154. $this->aSupportedFields[$sFieldClass] = $sRendererClass;
  155. return $this;
  156. }
  157. /**
  158. *
  159. * @return \Combodo\iTop\Renderer\FormRenderer
  160. */
  161. public function InitOutputs()
  162. {
  163. $this->aOutputs = array();
  164. return $this;
  165. }
  166. /**
  167. *
  168. * @return array
  169. */
  170. public function Render($aRequestedFields = null)
  171. {
  172. $this->InitOutputs();
  173. foreach ($this->oForm->GetFields() as $oField)
  174. {
  175. if ($aRequestedFields !== null && !in_array($oField->GetId(), $aRequestedFields)) continue;
  176. $this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField);
  177. }
  178. return $this->aOutputs;
  179. }
  180. /**
  181. * Returns the output for the $oField. Output format depends on the $sMode.
  182. *
  183. * If $sMode = 'exploded', output is an has array with id / html / js_inline / js_files / css_inline / css_files / validators
  184. * Else if $sMode = 'joined', output is a string with everything in it
  185. *
  186. * @param \Combodo\iTop\Form\Field\Field $oField
  187. * @param string $sMode 'exploded'|'joined'
  188. * @return mixed
  189. */
  190. protected function PrepareOutputForField($oField, $sMode = 'exploded')
  191. {
  192. $output = array(
  193. 'id' => $oField->GetId(),
  194. 'html' => '',
  195. 'js_inline' => '',
  196. 'css_inline' => '',
  197. 'js_files' => array(),
  198. 'css_files' => array()
  199. );
  200. $sFieldRendererClass = $this->GetFieldRendererClass($oField);
  201. $oFieldRenderer = new $sFieldRendererClass($oField);
  202. $oFieldRenderer->SetEndpoint($this->GetEndpoint());
  203. $oRenderingOutput = $oFieldRenderer->Render();
  204. // HTML
  205. if ($oRenderingOutput->GetHtml() !== '')
  206. {
  207. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  208. {
  209. $output['html'] = $oRenderingOutput->GetHtml();
  210. }
  211. else
  212. {
  213. $output['html'] .= $oRenderingOutput->GetHtml();
  214. }
  215. }
  216. // JS files
  217. foreach ($oRenderingOutput->GetJsFiles() as $sJsFile)
  218. {
  219. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  220. {
  221. if (!in_array($sJsFile, $output['js_files']))
  222. {
  223. $output['js_files'][] = $sJsFile;
  224. }
  225. }
  226. else
  227. {
  228. $output['html'] .= '<script src="' . $sJsFile . '" type="text/javascript"></script>';
  229. }
  230. }
  231. // JS inline
  232. if ($oRenderingOutput->GetJs() !== '')
  233. {
  234. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  235. {
  236. $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs();
  237. }
  238. else
  239. {
  240. $output['html'] .= '<script type="text/javascript">' . $oRenderingOutput->GetJs() . '</script>';
  241. }
  242. }
  243. // CSS files
  244. foreach ($oRenderingOutput->GetCssFiles() as $sCssFile)
  245. {
  246. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  247. {
  248. if (!in_array($sCssFile, $output['css_files']))
  249. {
  250. $output['css_files'][] = $sCssFile;
  251. }
  252. }
  253. else
  254. {
  255. $output['html'] .= '<link href="' . $sCssFile . '" rel="stylesheet" />';
  256. }
  257. }
  258. // CSS inline
  259. if ($oRenderingOutput->GetCss() !== '')
  260. {
  261. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  262. {
  263. $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss();
  264. }
  265. else
  266. {
  267. $output['html'] .= '<style>' . $oRenderingOutput->GetCss() . '</style>';
  268. }
  269. }
  270. return $output;
  271. }
  272. }