123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- // Copyright (C) 2010-2016 Combodo SARL
- //
- // This file is part of iTop.
- //
- // iTop is free software; you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // iTop is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with iTop. If not, see <http://www.gnu.org/licenses/>
- namespace Combodo\iTop\Renderer\Bootstrap\FieldRenderer;
- use \Dict;
- use \Combodo\iTop\Renderer\FieldRenderer;
- use \Combodo\iTop\Renderer\RenderingOutput;
- /**
- * Description of BsSimpleFieldRenderer
- *
- * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
- */
- class BsSimpleFieldRenderer extends FieldRenderer
- {
- public function Render()
- {
- $oOutput = new RenderingOutput();
- $sFieldClass = get_class($this->oField);
- $sFieldId = 'field_' . spl_object_hash($this->oField);
- $sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
- // TODO : Shouldn't we have a field type so we don't have to maintain FQN classname ?
- // Rendering field in edition mode
- if (!$this->oField->GetReadOnly())
- {
- switch ($sFieldClass)
- {
- case 'Combodo\\iTop\\Form\\Field\\StringField':
- $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
- if ($this->oField->GetLabel() !== '')
- {
- $oOutput->AddHtml('<label for="' . $sFieldId . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
- }
- $oOutput->AddHtml('<div class="help-block"></div>');
- $oOutput->AddHtml('<input type="text" id="' . $sFieldId . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
- $oOutput->AddHtml('</div>');
- break;
- case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
- $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
- if ($this->oField->GetLabel() !== '')
- {
- $oOutput->AddHtml('<label for="' . $sFieldId . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
- }
- $oOutput->AddHtml('<div class="help-block"></div>');
- $oOutput->AddHtml('<textarea id="' . $sFieldId . '" name="' . $this->oField->GetId() . '" class="form-control" rows="8">' . $this->oField->GetCurrentValue() . '</textarea>');
- $oOutput->AddHtml('</div>');
- break;
- case 'Combodo\\iTop\\Form\\Field\\SelectField':
- $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
- if ($this->oField->GetLabel() !== '')
- {
- $oOutput->AddHtml('<label for="' . $sFieldId . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
- }
- $oOutput->AddHtml('<div class="help-block"></div>');
- $oOutput->AddHtml('<select id="' . $sFieldId . '" name="' . $this->oField->GetId() . '" ' . ( ($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '' ) . ' class="form-control">');
- 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)
- $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
- $oOutput->AddHtml('<option value="' . $sChoice . '" ' . $sSelectedAtt . ' >' . $sLabel . '</option>');
- }
- $oOutput->AddHtml('</select>');
- $oOutput->AddHtml('</div>');
- break;
- case 'Combodo\\iTop\\Form\\Field\\RadioField':
- case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
- $sFieldType = ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\RadioField') ? 'radio' : 'checkbox';
- $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '" id="' . $sFieldId . '">');
- if ($this->oField->GetLabel() !== '')
- {
- $oOutput->AddHtml('<div><label class="control-label">' . $this->oField->GetLabel() . '</label></div>');
- }
- $oOutput->AddHtml('<div class="help-block"></div>');
- $oOutput->AddHtml('<div class="btn-group" data-toggle="buttons">');
- $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('<label class="btn btn-default ' . $sCheckedClass . '"><input type="' . $sFieldType . '" name="' . $this->oField->GetId() . '" id="' . $this->oField->GetId() . $i . '" value="' . $sChoice . '" ' . $sCheckedAtt . ' />' . $sLabel . '</label>');
- $i++;
- }
- $oOutput->AddHtml('</div>');
- $oOutput->AddHtml('</div>');
- break;
- case 'Combodo\\iTop\\Form\\Field\\HiddenField':
- $oOutput->AddHtml('<input type="hidden" id="' . $sFieldId . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '"/>');
- break;
- }
- }
- // ... and in read-only mode
- else
- {
- // ... specific rendering for fields with mulltiple 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\\StringField':
- case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
- $oOutput->AddHtml('<div class="form-group">');
- if ($this->oField->GetLabel() !== '')
- {
- $oOutput->AddHtml('<label for="' . $sFieldId . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
- }
- $oOutput->AddHtml('<div class="form-control-static">' . $this->oField->GetCurrentValue() . '</div>');
- $oOutput->AddHtml('<input type="hidden" id="' . $sFieldId . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
- $oOutput->AddHtml('</div>');
- break;
- case 'Combodo\\iTop\\Form\\Field\\RadioField':
- case 'Combodo\\iTop\\Form\\Field\\SelectField': // TODO : This should be check for external key, as we would display it differently
- $aFieldChoices = $this->oField->GetChoices();
- $sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject');
- $oOutput->AddHtml('<div class="form-group">');
- if ($this->oField->GetLabel() !== '')
- {
- $oOutput->AddHtml('<label for="' . $sFieldId . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
- }
- $oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
- $oOutput->AddHtml('<input type="hidden" id="' . $sFieldId . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
- $oOutput->AddHtml('</div>');
- 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\\SelectField':
- case 'Combodo\\iTop\\Form\\Field\\HiddenField':
- $oOutput->AddJs(
- <<<EOF
- $("#$sFieldId").off("change").on("change", function(){
- $(this).closest(".form_handler").trigger("field_change", {
- id: $(this).attr("id"),
- name: $(this).attr("name"),
- value: $(this).val()
- });
- });
- EOF
- );
- break;
- case 'Combodo\\iTop\\Form\\Field\\RadioField':
- case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
- $oOutput->AddJs(
- <<<EOF
- $("#$sFieldId input").off("change").on("change", function(){
- $(this).closest(".form_handler").trigger("field_change", {
- id: $(this).closest("#$sFieldId").attr("id"),
- name: $(this).attr("name"),
- value: $(this).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\\TextAreaField':
- case 'Combodo\\iTop\\Form\\Field\\SelectField':
- case 'Combodo\\iTop\\Form\\Field\\HiddenField':
- case 'Combodo\\iTop\\Form\\Field\\RadioField':
- case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
- $oOutput->AddJs(
- <<<EOF
- $("[data-field-id='{$this->oField->GetId()}']").form_field($sFormFieldOptions);
- EOF
- );
- break;
- }
- return $oOutput;
- }
- }
|