From f2fabe4eecbe757d2b2fc3bcece151cd3e41c21b Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Thu, 11 Feb 2016 14:23:35 +0000 Subject: [PATCH] Prerequisites to the custom fields SVN:trunk[3908] --- .../bootstrap/bsformrenderer.class.inc.php | 190 +----------------- sources/renderer/formrenderer.class.inc.php | 135 ++++++++++++- 2 files changed, 135 insertions(+), 190 deletions(-) diff --git a/sources/renderer/bootstrap/bsformrenderer.class.inc.php b/sources/renderer/bootstrap/bsformrenderer.class.inc.php index dc4b78d9e..633059788 100644 --- a/sources/renderer/bootstrap/bsformrenderer.class.inc.php +++ b/sources/renderer/bootstrap/bsformrenderer.class.inc.php @@ -19,8 +19,6 @@ namespace Combodo\iTop\Renderer\Bootstrap; -use \Dict; -use \MetaModel; use \Combodo\iTop\Renderer\FormRenderer; use \Combodo\iTop\Form\Form; @@ -31,8 +29,7 @@ use \Combodo\iTop\Form\Form; */ class BsFormRenderer extends FormRenderer { - const ENUM_RENDER_MODE_EXPLODED = 'exploded'; - const ENUM_RENDER_MODE_JOINED = 'joined'; + const DEFAULT_RENDERER_NAMESPACE = 'Combodo\\iTop\\Renderer\\Bootstrap\\FieldRenderer\\'; public function __construct(Form $oForm = null) { @@ -44,189 +41,4 @@ class BsFormRenderer extends FormRenderer $this->AddSupportedField('RadioField', 'BsSimpleFieldRenderer'); $this->AddSupportedField('CheckboxField', 'BsSimpleFieldRenderer'); } - - /** - * Registers a Renderer class for the specified Field class. - * - * If the Field class is not fully qualified, a default namespace will be prepend (see FormRenderer::AddSupportedField). - * If the Renderer clas is not fully qualified, the default "Combodo\iTop\Renderer\Bootstrap\FieldRenderer" will be prepend. - * - * @param string $sFieldClass - * @param string $sRendererClass - */ - public function AddSupportedField($sFieldClass, $sRendererClass) - { - $sRendererClass = (strpos($sRendererClass, '\\') !== false) ? $sRendererClass : 'Combodo\\iTop\\Renderer\\Bootstrap\\FieldRenderer\\' . $sRendererClass; - - parent::AddSupportedField($sFieldClass, $sRendererClass); - } - - public function Render() - { - $this->InitOutputs(); - - foreach ($this->oForm->GetFields() as $oField) - { - $this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField); - } - - return $this->aOutputs; - } - - /** - * Renders a field of $oObject identified by its attribute code ($sFieldId). - * - * $sMode allows to defined if the result must a traditional array - * containing the differents parts for the field or a string concataning all - * those parts in one html tag. - * - * Typically, $sMode 'joined' is used when RenderField is called directly from a twig template. - * Otherwise, the 'exploded' parameter is used to allow the renderer to optimize the assets. - * - * $iDesiredFlags is only used with $sMode = 'joined' to set the field flags as an information. - * - * @param cmdbAbstractObject $oObject - * @param string $sFieldId - * @param integer $iDesiredFlags - * @param string $sMode 'joined'|'exploded' - * @return mixed - */ - public function RenderField($oObject, $sFieldId, $iDesiredFlags = OPT_ATT_NORMAL, $sMode = 'joined') - { - // Building field - $oAttDef = MetaModel::GetAttributeDef(get_class($oObject), $sFieldId); - $oField = $oAttDef->GetFormField($oObject); - - $aOutput = $this->PrepareOutputForField($oField, $sMode); - - if ($sMode === static::ENUM_RENDER_MODE_JOINED) - { - $res = '
' . - $aOutput['html'] . - '
'; - } - else - { - $res = $aOutput; - } - - return $res; - } - - /** - * Returns the output for the $oField. Output format depends on the $sMode. - * - * If $sMode = 'exploded', output is an has array with id / html / js_inline / js_files / css_inline / css_files / validators - * Else if $sMode = 'joined', output is a string with everything in it - * - * @param Combodo\iTop\Form\Field\Field $oField - * @param string $sMode 'exploded'|'joined' - * @return mixed - */ - protected function PrepareOutputForField($oField, $sMode = 'exploded') - { - $output = array( - 'id' => $oField->GetId(), - 'html' => '', - 'js_inline' => '', - 'css_inline' => '', - 'js_files' => array(), - 'css_files' => array(), - 'validators' => null - ); - - $sFieldRendererClass = $this->GetFieldRendererClass($oField); - // TODO : We might want to throw an exception instead when there is no renderer for that field - if ($sFieldRendererClass !== null) - { - $oFieldRenderer = new $sFieldRendererClass($oField); - $oFieldRenderer->SetEndpoint($this->GetEndpoint()); - - $oRenderingOutput = $oFieldRenderer->Render(); - - // HTML - if ($oRenderingOutput->GetHtml() !== '') - { - if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) - { - $output['html'] = $oRenderingOutput->GetHtml(); - } - else - { - $output['html'] .= $oRenderingOutput->GetHtml(); - } - } - - // JS files - foreach ($oRenderingOutput->GetJsFiles() as $sJsFile) - { - if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) - { - if (!in_array($sJsFile, $output['js_files'])) - { - $output['js_files'][] = $sJsFile; - } - } - else - { - $output['html'] .= ''; - } - } - // JS inline - if ($oRenderingOutput->GetJs() !== '') - { - if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) - { - $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs(); - } - else - { - $output['html'] .= ''; - } - } - - // CSS files - foreach ($oRenderingOutput->GetCssFiles() as $sCssFile) - { - if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) - { - if (!in_array($sCssFile, $output['css_files'])) - { - $output['css_files'][] = $sCssFile; - } - } - else - { - $output['html'] .= ''; - } - } - // CSS inline - if ($oRenderingOutput->GetCss() !== '') - { - if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) - { - $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss(); - } - else - { - $output['html'] .= ''; - } - } - - // Validators - foreach ($oField->GetValidators() as $oValidator) - { - $output['validators'][$oValidator::GetName()] = array( - 'reg_exp' => $oValidator->GetRegExp(), - 'message' => Dict::S($oValidator->GetErrorMessage()) - ); - } - - // Subfields - // TODO - } - - return $output; - } - } diff --git a/sources/renderer/formrenderer.class.inc.php b/sources/renderer/formrenderer.class.inc.php index 966b44aea..5251a248c 100644 --- a/sources/renderer/formrenderer.class.inc.php +++ b/sources/renderer/formrenderer.class.inc.php @@ -19,6 +19,7 @@ namespace Combodo\iTop\Renderer; +use \Dict; use \Combodo\iTop\Form\Form; /** @@ -28,6 +29,10 @@ use \Combodo\iTop\Form\Form; */ abstract class FormRenderer { + const ENUM_RENDER_MODE_EXPLODED = 'exploded'; + const ENUM_RENDER_MODE_JOINED = 'joined'; + const DEFAULT_RENDERER_NAMESPACE = ''; + protected $oForm; protected $sEndpoint; protected $aSupportedFields; @@ -115,6 +120,7 @@ abstract class FormRenderer return $this->aOutputs; } + /** * Registers a Renderer class for the specified Field class. * @@ -127,6 +133,7 @@ abstract class FormRenderer public function AddSupportedField($sFieldClass, $sRendererClass) { $sFieldClass = (strpos($sFieldClass, '\\') !== false) ? $sFieldClass : 'Combodo\\iTop\\Form\\Field\\' . $sFieldClass; + $sRendererClass = (strpos($sRendererClass, '\\') !== false) ? $sRendererClass : static::DEFAULT_RENDERER_NAMESPACE . $sRendererClass; $this->aSupportedFields[$sFieldClass] = $sRendererClass; @@ -139,5 +146,131 @@ abstract class FormRenderer return $this; } - abstract public function Render(); + public function Render() + { + $this->InitOutputs(); + + foreach ($this->oForm->GetFields() as $oField) + { + $this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField); + } + + return $this->aOutputs; + } + + /** + * Returns the output for the $oField. Output format depends on the $sMode. + * + * If $sMode = 'exploded', output is an has array with id / html / js_inline / js_files / css_inline / css_files / validators + * Else if $sMode = 'joined', output is a string with everything in it + * + * @param Combodo\iTop\Form\Field\Field $oField + * @param string $sMode 'exploded'|'joined' + * @return mixed + */ + protected function PrepareOutputForField($oField, $sMode = 'exploded') + { + $output = array( + 'id' => $oField->GetId(), + 'html' => '', + 'js_inline' => '', + 'css_inline' => '', + 'js_files' => array(), + 'css_files' => array(), + 'validators' => null + ); + + $sFieldRendererClass = $this->GetFieldRendererClass($oField); + // TODO : We might want to throw an exception instead when there is no renderer for that field + if ($sFieldRendererClass !== null) + { + $oFieldRenderer = new $sFieldRendererClass($oField); + $oFieldRenderer->SetEndpoint($this->GetEndpoint()); + + $oRenderingOutput = $oFieldRenderer->Render(); + + // HTML + if ($oRenderingOutput->GetHtml() !== '') + { + if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) + { + $output['html'] = $oRenderingOutput->GetHtml(); + } + else + { + $output['html'] .= $oRenderingOutput->GetHtml(); + } + } + + // JS files + foreach ($oRenderingOutput->GetJsFiles() as $sJsFile) + { + if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) + { + if (!in_array($sJsFile, $output['js_files'])) + { + $output['js_files'][] = $sJsFile; + } + } + else + { + $output['html'] .= ''; + } + } + // JS inline + if ($oRenderingOutput->GetJs() !== '') + { + if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) + { + $output['js_inline'] .= ' ' . $oRenderingOutput->GetJs(); + } + else + { + $output['html'] .= ''; + } + } + + // CSS files + foreach ($oRenderingOutput->GetCssFiles() as $sCssFile) + { + if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) + { + if (!in_array($sCssFile, $output['css_files'])) + { + $output['css_files'][] = $sCssFile; + } + } + else + { + $output['html'] .= ''; + } + } + // CSS inline + if ($oRenderingOutput->GetCss() !== '') + { + if ($sMode === static::ENUM_RENDER_MODE_EXPLODED) + { + $output['css_inline'] .= ' ' . $oRenderingOutput->GetCss(); + } + else + { + $output['html'] .= ''; + } + } + + // Validators + foreach ($oField->GetValidators() as $oValidator) + { + $output['validators'][$oValidator::GetName()] = array( + 'reg_exp' => $oValidator->GetRegExp(), + 'message' => Dict::S($oValidator->GetErrorMessage()) + ); + } + + // Subfields + // TODO + } + + return $output; + } }