bsformrenderer.class.inc.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Bootstrap;
  19. use \Dict;
  20. use \MetaModel;
  21. use \Combodo\iTop\Renderer\FormRenderer;
  22. use \Combodo\iTop\Form\Form;
  23. /**
  24. * Description of formrenderer
  25. *
  26. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  27. */
  28. class BsFormRenderer extends FormRenderer
  29. {
  30. const ENUM_RENDER_MODE_EXPLODED = 'exploded';
  31. const ENUM_RENDER_MODE_JOINED = 'joined';
  32. public function __construct(Form $oForm = null)
  33. {
  34. parent::__construct($oForm);
  35. $this->AddSupportedField('HiddenField', 'BsSimpleFieldRenderer');
  36. $this->AddSupportedField('StringField', 'BsSimpleFieldRenderer');
  37. $this->AddSupportedField('TextAreaField', 'BsSimpleFieldRenderer');
  38. $this->AddSupportedField('SelectField', 'BsSimpleFieldRenderer');
  39. $this->AddSupportedField('RadioField', 'BsSimpleFieldRenderer');
  40. $this->AddSupportedField('CheckboxField', 'BsSimpleFieldRenderer');
  41. }
  42. /**
  43. * Registers a Renderer class for the specified Field class.
  44. *
  45. * If the Field class is not fully qualified, a default namespace will be prepend (see FormRenderer::AddSupportedField).
  46. * If the Renderer clas is not fully qualified, the default "Combodo\iTop\Renderer\Bootstrap\FieldRenderer" will be prepend.
  47. *
  48. * @param string $sFieldClass
  49. * @param string $sRendererClass
  50. */
  51. public function AddSupportedField($sFieldClass, $sRendererClass)
  52. {
  53. $sRendererClass = (strpos($sRendererClass, '\\') !== false) ? $sRendererClass : 'Combodo\\iTop\\Renderer\\Bootstrap\\FieldRenderer\\' . $sRendererClass;
  54. parent::AddSupportedField($sFieldClass, $sRendererClass);
  55. }
  56. public function Render()
  57. {
  58. $this->InitOutputs();
  59. foreach ($this->oForm->GetFields() as $oField)
  60. {
  61. $this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField);
  62. }
  63. return $this->aOutputs;
  64. }
  65. /**
  66. * Renders a field of $oObject identified by its attribute code ($sFieldId).
  67. *
  68. * $sMode allows to defined if the result must a traditional array
  69. * containing the differents parts for the field or a string concataning all
  70. * those parts in one html tag.
  71. *
  72. * Typically, $sMode 'joined' is used when RenderField is called directly from a twig template.
  73. * Otherwise, the 'exploded' parameter is used to allow the renderer to optimize the assets.
  74. *
  75. * $iDesiredFlags is only used with $sMode = 'joined' to set the field flags as an information.
  76. *
  77. * @param cmdbAbstractObject $oObject
  78. * @param string $sFieldId
  79. * @param integer $iDesiredFlags
  80. * @param string $sMode 'joined'|'exploded'
  81. * @return mixed
  82. */
  83. public function RenderField($oObject, $sFieldId, $iDesiredFlags = OPT_ATT_NORMAL, $sMode = 'joined')
  84. {
  85. // Building field
  86. $oAttDef = MetaModel::GetAttributeDef(get_class($oObject), $sFieldId);
  87. $oField = $oAttDef->GetFormField($oObject);
  88. $aOutput = $this->PrepareOutputForField($oField, $sMode);
  89. if ($sMode === static::ENUM_RENDER_MODE_JOINED)
  90. {
  91. $res = '<div class="form_field" data-field-id="' . $oField->GetId() . '" data-field-flags="' . $iDesiredFlags . '">' .
  92. $aOutput['html'] .
  93. '</div>';
  94. }
  95. else
  96. {
  97. $res = $aOutput;
  98. }
  99. return $res;
  100. }
  101. /**
  102. * Returns the output for the $oField. Output format depends on the $sMode.
  103. *
  104. * If $sMode = 'exploded', output is an has array with id / html / js_inline / js_files / css_inline / css_files / validators
  105. * Else if $sMode = 'joined', output is a string with everything in it
  106. *
  107. * @param Combodo\iTop\Form\Field\Field $oField
  108. * @param string $sMode 'exploded'|'joined'
  109. * @return mixed
  110. */
  111. protected function PrepareOutputForField($oField, $sMode = 'exploded')
  112. {
  113. $output = array(
  114. 'id' => $oField->GetId(),
  115. 'html' => '',
  116. 'js_inline' => '',
  117. 'css_inline' => '',
  118. 'js_files' => array(),
  119. 'css_files' => array(),
  120. 'validators' => null
  121. );
  122. $sFieldRendererClass = $this->GetFieldRendererClass($oField);
  123. // TODO : We might want to throw an exception instead when there is no renderer for that field
  124. if ($sFieldRendererClass !== null)
  125. {
  126. $oFieldRenderer = new $sFieldRendererClass($oField);
  127. $oFieldRenderer->SetEndpoint($this->GetEndpoint());
  128. $oRenderingOutput = $oFieldRenderer->Render();
  129. // HTML
  130. if ($oRenderingOutput->GetHtml() !== '')
  131. {
  132. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  133. {
  134. $output['html'] = $oRenderingOutput->GetHtml();
  135. }
  136. else
  137. {
  138. $output['html'] .= $oRenderingOutput->GetHtml();
  139. }
  140. }
  141. // JS files
  142. foreach ($oRenderingOutput->GetJsFiles() as $sJsFile)
  143. {
  144. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  145. {
  146. if (!in_array($sJsFile, $output['js_files']))
  147. {
  148. $output['js_files'][] = $sJsFile;
  149. }
  150. }
  151. else
  152. {
  153. $output['html'] .= '<script src="' . $sJsFile . '" type="text/javascript"></script>';
  154. }
  155. }
  156. // JS inline
  157. if ($oRenderingOutput->GetJs() !== '')
  158. {
  159. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  160. {
  161. $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs();
  162. }
  163. else
  164. {
  165. $output['html'] .= '<script type="text/javascript">' . $oRenderingOutput->GetJs() . '</script>';
  166. }
  167. }
  168. // CSS files
  169. foreach ($oRenderingOutput->GetCssFiles() as $sCssFile)
  170. {
  171. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  172. {
  173. if (!in_array($sCssFile, $output['css_files']))
  174. {
  175. $output['css_files'][] = $sCssFile;
  176. }
  177. }
  178. else
  179. {
  180. $output['html'] .= '<link href="' . $sCssFile . '" rel="stylesheet" />';
  181. }
  182. }
  183. // CSS inline
  184. if ($oRenderingOutput->GetCss() !== '')
  185. {
  186. if ($sMode === static::ENUM_RENDER_MODE_EXPLODED)
  187. {
  188. $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss();
  189. }
  190. else
  191. {
  192. $output['html'] .= '<style>' . $oRenderingOutput->GetCss() . '</style>';
  193. }
  194. }
  195. // Validators
  196. foreach ($oField->GetValidators() as $oValidator)
  197. {
  198. $output['validators'][$oValidator::GetName()] = array(
  199. 'reg_exp' => $oValidator->GetRegExp(),
  200. 'message' => Dict::S($oValidator->GetErrorMessage())
  201. );
  202. }
  203. // Subfields
  204. // TODO
  205. }
  206. return $output;
  207. }
  208. }