namespace Combodo\iTop\Renderer\Bootstrap\FieldRenderer; use \utils; use \Dict; use \UserRights; use \InlineImage; use \Combodo\iTop\Renderer\FieldRenderer; use \Combodo\iTop\Renderer\RenderingOutput; use \Combodo\iTop\Form\Field\TextAreaField; /** * Description of BsSimpleFieldRenderer * * @author Guillaume Lajarige */ class BsSimpleFieldRenderer extends FieldRenderer { /** * Returns a RenderingOutput for the FieldRenderer's Field * * @return \Combodo\iTop\Renderer\RenderingOutput */ public function Render() { $oOutput = new RenderingOutput(); $sFieldClass = get_class($this->oField); $sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : ''; // Rendering field in edition mode if (!$this->oField->GetReadOnly() && !$this->oField->GetHidden()) { switch ($sFieldClass) { case 'Combodo\\iTop\\Form\\Field\\DateTimeField': $oOutput->AddHtml('
'); if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
'); $oOutput->AddHtml('
'); $oOutput->AddHtml(''); $oOutput->AddHtml(''); $oOutput->AddHtml('
'); $oOutput->AddHtml('
'); $sJSFormat = json_encode($this->oField->GetJSDateTimeFormat()); $oOutput->AddJs( <<oField->GetGlobalId()}').datetimepicker({format: $sJSFormat}); EOF ); break; case 'Combodo\\iTop\\Form\\Field\\StringField': $oOutput->AddHtml('
'); if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
'); $oOutput->AddHtml(''); $oOutput->AddHtml('
'); break; case 'Combodo\\iTop\\Form\\Field\\TextAreaField': case 'Combodo\\iTop\\Form\\Field\\CaseLogField': $bRichEditor = ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML); $oOutput->AddHtml('
'); if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
'); // First the edition area $oOutput->AddHtml('
'); $oOutput->AddHtml(''); $oOutput->AddHtml('
'); // Then the previous entries if necessary if ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\CaseLogField') { $aEntries = $this->oField->GetEntries(); if (count($aEntries) > 0) { $oOutput->AddHtml('
'); for ($i = 0; $i < count($aEntries); $i++) { $sEntryDate = $aEntries[$i]['date']; $sEntryUser = $aEntries[$i]['user_login']; $sEntryHeader = Dict::Format('UI:CaseLog:Header_Date_UserName', $sEntryDate, $sEntryUser); // Only the last 2 entries are expanded by default $sEntryContentExpanded = ($i < 2) ? 'true' : 'false'; $sEntryHeaderButtonClass = ($i < 2) ? '' : 'collapsed'; $sEntryContentClass = ($i < 2) ? 'in' : ''; $sEntryContentId = 'caselog_field_entry_content-' . $this->oField->GetGlobalId() . '-' . $i; // Note : We use CKEditor stylesheet to format this $oOutput->AddHtml( <<
{$sEntryHeader}
{$aEntries[$i]['message_html']}
EOF ); } $oOutput->AddHtml('
'); } } $oOutput->AddHtml(''); // Some additional stuff if we are displaying it with a rich editor if ($bRichEditor) { $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage())); $oOutput->AddJs( <<oField->GetGlobalId()}').addClass('htmlEditor'); $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'}); EOF ); if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null)) { $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId()))); } } break; case 'Combodo\\iTop\\Form\\Field\\SelectField': case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField': $oOutput->AddHtml('
'); if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
'); $oOutput->AddHtml(''); $oOutput->AddHtml('
'); break; case 'Combodo\\iTop\\Form\\Field\\RadioField': case 'Combodo\\iTop\\Form\\Field\\CheckboxField': $sFieldType = ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\RadioField') ? 'radio' : 'checkbox'; $oOutput->AddHtml('
'); if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml('
'); } $oOutput->AddHtml('
'); $oOutput->AddHtml('
'); $i = 0; foreach ($this->oField->GetChoices() as $sChoice => $sLabel) { // Note : The test is a double equal on purpose as the type of the value received from the XHR is not always the same as the type of the allowed values. (eg : string vs int) $sCheckedAtt = ($this->oField->IsAmongValues($sChoice)) ? 'checked' : ''; $sCheckedClass = ($this->oField->IsAmongValues($sChoice)) ? 'active' : ''; $oOutput->AddHtml(''); $i++; } $oOutput->AddHtml('
'); $oOutput->AddHtml('
'); break; case 'Combodo\\iTop\\Form\\Field\\HiddenField': $oOutput->AddHtml(''); break; } } // ... and in read-only mode (or hidden) else { // ... specific rendering for fields with multiple values if (($this->oField instanceof Combodo\iTop\Form\Field\MultipleChoicesField) && ($this->oField->GetMultipleValuesEnabled())) { // TODO } // ... clasic rendering for fields with only one value else { switch ($sFieldClass) { case 'Combodo\\iTop\\Form\\Field\\LabelField': $oOutput->AddHtml('
'); // Showing label / value only if read-only but not hidden if (!$this->oField->GetHidden()) { if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('
'); } $oOutput->AddHtml('
'); break; case 'Combodo\\iTop\\Form\\Field\\StringField': case 'Combodo\\iTop\\Form\\Field\\TextAreaField': $bEncodeHtmlEntities = (($sFieldClass === 'Combodo\\iTop\\Form\\Field\\TextAreaField') && ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML)) ? false : true; $oOutput->AddHtml('
'); // Showing label / value only if read-only but not hidden if (!$this->oField->GetHidden()) { if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
')->AddHtml($this->oField->GetCurrentValue(), $bEncodeHtmlEntities)->AddHtml('
'); } $oOutput->AddHtml(''); $oOutput->AddHtml('
'); break; case 'Combodo\\iTop\\Form\\Field\\DateTimeField': $oOutput->AddHtml('
'); // Showing label / value only if read-only but not hidden if (!$this->oField->GetHidden()) { if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('
'); } $oOutput->AddHtml(''); $oOutput->AddHtml('
'); break; case 'Combodo\\iTop\\Form\\Field\\RadioField': case 'Combodo\\iTop\\Form\\Field\\SelectField': case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField': $aFieldChoices = $this->oField->GetChoices(); $sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject'); $oOutput->AddHtml('
'); // Showing label / value only if read-only but not hidden if (!$this->oField->GetHidden()) { if ($this->oField->GetLabel() !== '') { $oOutput->AddHtml(''); } $oOutput->AddHtml('
' . $sFieldValue . '
'); } $oOutput->AddHtml(''); $oOutput->AddHtml('
'); break; } } } // JS FieldChange trigger (:input are not always at the same depth) switch ($sFieldClass) { case 'Combodo\\iTop\\Form\\Field\\StringField': case 'Combodo\\iTop\\Form\\Field\\TextAreaField': case 'Combodo\\iTop\\Form\\Field\\CaseLogField': case 'Combodo\\iTop\\Form\\Field\\SelectField': case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField': case 'Combodo\\iTop\\Form\\Field\\HiddenField': $oOutput->AddJs( <<oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){ var me = this; $(this).closest(".field_set").trigger("field_change", { id: $(me).attr("id"), name: $(me).closest(".form_field").attr("data-field-id"), value: $(me).val() }); }); EOF ); break; case 'Combodo\\iTop\\Form\\Field\\DateTimeField': // We need the focusout event has the datepicker widget seems to override the change event $oOutput->AddJs( <<oField->GetGlobalId()}").off("change keyup focusout").on("change keyup focusout", function(){ var me = this; $(this).closest(".field_set").trigger("field_change", { id: $(me).attr("id"), name: $(me).closest(".form_field").attr("data-field-id"), value: $(me).val() }); }); EOF ); break; case 'Combodo\\iTop\\Form\\Field\\RadioField': case 'Combodo\\iTop\\Form\\Field\\CheckboxField': $oOutput->AddJs( <<oField->GetGlobalId()} input").off("change").on("change", function(){ var me = this; $(this).closest(".field_set").trigger("field_change", { id: $(me).closest("#{$this->oField->GetGlobalId()}").attr("id"), name: $(me).attr("name"), value: $(me).val() }); }); EOF ); break; } // JS Form field widget construct $aValidators = array(); foreach ($this->oField->GetValidators() as $oValidator) { $aValidators[$oValidator::GetName()] = array( 'reg_exp' => $oValidator->GetRegExp(), 'message' => Dict::S($oValidator->GetErrorMessage()) ); } $sFormFieldOptions = json_encode(array( 'validators' => $aValidators )); switch ($sFieldClass) { case 'Combodo\\iTop\\Form\\Field\\StringField': case 'Combodo\\iTop\\Form\\Field\\SelectField': case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField': case 'Combodo\\iTop\\Form\\Field\\HiddenField': case 'Combodo\\iTop\\Form\\Field\\RadioField': case 'Combodo\\iTop\\Form\\Field\\CheckboxField': case 'Combodo\\iTop\\Form\\Field\\DateTimeField': $oOutput->AddJs( <<AddJs( <<AddJs(InlineImage::FixImagesWidth()); break; } return $oOutput; } }