formrenderer.class.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * Returns an array of Output for the form fields.
  168. *
  169. * @param array $aFieldIds An array of field ids. If specified, renders only those fields
  170. * @return array
  171. */
  172. public function Render($aFieldIds = null)
  173. {
  174. $this->InitOutputs();
  175. foreach ($this->oForm->GetFields() as $oField)
  176. {
  177. if ($aFieldIds !== null && !in_array($oField->GetId(), $aFieldIds))
  178. {
  179. continue;
  180. }
  181. $this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField);
  182. }
  183. return $this->aOutputs;
  184. }
  185. /**
  186. * Returns the output for the $oField. Output format depends on the $sMode.
  187. *
  188. * If $sMode = 'exploded', output is an has array with id / html / js_inline / js_files / css_inline / css_files / validators
  189. * Else if $sMode = 'joined', output is a string with everything in it
  190. *
  191. * @param \Combodo\iTop\Form\Field\Field $oField
  192. * @param string $sMode 'exploded'|'joined'
  193. * @return mixed
  194. */
  195. protected function PrepareOutputForField($oField, $sMode = 'exploded')
  196. {
  197. $output = array(
  198. 'id' => $oField->GetId(),
  199. 'html' => '',
  200. 'js_inline' => '',
  201. 'css_inline' => '',
  202. 'js_files' => array(),
  203. 'css_files' => array()
  204. );
  205. $sFieldRendererClass = $this->GetFieldRendererClass($oField);
  206. $oFieldRenderer = new $sFieldRendererClass($oField);
  207. $oFieldRenderer->SetEndpoint($this->GetEndpoint());
  208. $oRenderingOutput = $oFieldRenderer->Render();
  209. // HTML
  210. if ($oRenderingOutput->GetHtml() !== '')
  211. {
  212. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  213. {
  214. $output['html'] = $oRenderingOutput->GetHtml();
  215. }
  216. else
  217. {
  218. $output['html'] .= $oRenderingOutput->GetHtml();
  219. }
  220. }
  221. // JS files
  222. foreach ($oRenderingOutput->GetJsFiles() as $sJsFile)
  223. {
  224. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  225. {
  226. if (!in_array($sJsFile, $output['js_files']))
  227. {
  228. $output['js_files'][] = $sJsFile;
  229. }
  230. }
  231. else
  232. {
  233. $output['html'] .= '<script src="' . $sJsFile . '" type="text/javascript"></script>';
  234. }
  235. }
  236. // JS inline
  237. if ($oRenderingOutput->GetJs() !== '')
  238. {
  239. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  240. {
  241. $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs();
  242. }
  243. else
  244. {
  245. $output['html'] .= '<script type="text/javascript">' . $oRenderingOutput->GetJs() . '</script>';
  246. }
  247. }
  248. // CSS files
  249. foreach ($oRenderingOutput->GetCssFiles() as $sCssFile)
  250. {
  251. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  252. {
  253. if (!in_array($sCssFile, $output['css_files']))
  254. {
  255. $output['css_files'][] = $sCssFile;
  256. }
  257. }
  258. else
  259. {
  260. $output['html'] .= '<link href="' . $sCssFile . '" rel="stylesheet" />';
  261. }
  262. }
  263. // CSS inline
  264. if ($oRenderingOutput->GetCss() !== '')
  265. {
  266. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  267. {
  268. $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss();
  269. }
  270. else
  271. {
  272. $output['html'] .= '<style>' . $oRenderingOutput->GetCss() . '</style>';
  273. }
  274. }
  275. return $output;
  276. }
  277. }