N°8796 - Add PHP code style validation in iTop and extensions - format whole code base

This commit is contained in:
odain
2025-11-07 15:39:53 +01:00
parent 12f23113f5
commit 890a2568c8
2110 changed files with 53099 additions and 63885 deletions

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -59,4 +60,4 @@ class AbstractSimpleField extends Field
return parent::SetMandatory($bMandatory);
}
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -63,12 +63,9 @@ class BlobField extends AbstractSimpleField
public function GetDisplayValue()
{
if ($this->currentValue->IsEmpty())
{
if ($this->currentValue->IsEmpty()) {
$sValue = Dict::S('Portal:File:None');
}
else
{
} else {
$sFilename = $this->currentValue->GetFileName();
$iSize = utils::BytesToFriendlyFormat(utils::Strlen($this->currentValue->GetData()));
$sOpenLink = $this->GetDisplayUrl();

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -27,5 +27,5 @@ namespace Combodo\iTop\Form\Field;
class CheckboxField extends MultipleChoicesField
{
/** @inheritDoc */
const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
public const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -24,5 +24,4 @@ namespace Combodo\iTop\Form\Field;
*/
class DateField extends StringField
{
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -88,13 +88,13 @@ class DateTimeField extends StringField
return $this;
}
/**
* Set the DateOnly flag
*
* @param boolean $bDateOnly
*
* @return \Combodo\iTop\Form\Field\DateTimeField
*/
/**
* Set the DateOnly flag
*
* @param boolean $bDateOnly
*
* @return \Combodo\iTop\Form\Field\DateTimeField
*/
public function SetDateOnly(bool $bDateOnly)
{
$this->bDateOnly = $bDateOnly;
@@ -109,7 +109,6 @@ class DateTimeField extends StringField
return $this->bDateOnly;
}
/**
* Allow date time picker widget popup to be positioned relative to a specific dom element.
*

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -27,7 +27,6 @@ use Str;
*/
class DurationField extends AbstractSimpleField
{
/**
* Note: This is inspired by AttributeDuration::GetAsHTML()
*

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -34,17 +34,16 @@ class EmailField extends StringField
* @throws \ConfigException
* @throws \CoreException
*/
public function GetDisplayValue()
{
$sLabel = Str::pure2html($this->currentValue);
if (utils::StrLen($sLabel) > 128)
{
// Truncate the length to 128 characters, by removing the middle
$sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
}
public function GetDisplayValue()
{
$sLabel = Str::pure2html($this->currentValue);
if (utils::StrLen($sLabel) > 128) {
// Truncate the length to 128 characters, by removing the middle
$sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
}
$sUrlDecorationClass = utils::GetConfig()->Get('email_decoration_class');
$sUrlDecorationClass = utils::GetConfig()->Get('email_decoration_class');
return "<a class=\"mailto\" href=\"mailto:$this->currentValue\"><span class=\"form_field_decoration text_decoration $sUrlDecorationClass\"></span>$sLabel</a>";
}
return "<a class=\"mailto\" href=\"mailto:$this->currentValue\"><span class=\"form_field_decoration text_decoration $sUrlDecorationClass\"></span>$sLabel</a>";
}
}

View File

@@ -19,29 +19,29 @@ use Combodo\iTop\Form\Validator\MandatoryValidator;
*/
abstract class Field
{
/** @var string */
const ENUM_DISPLAY_MODE_COSY = 'cosy'; // Label above value
/** @var string */
const ENUM_DISPLAY_MODE_COMPACT = 'compact'; // Label and value side by side
/** @var string */
const ENUM_DISPLAY_MODE_DENSE = 'dense'; // Label and value side by side, closely
/** @var string */
public const ENUM_DISPLAY_MODE_COSY = 'cosy'; // Label above value
/** @var string */
public const ENUM_DISPLAY_MODE_COMPACT = 'compact'; // Label and value side by side
/** @var string */
public const ENUM_DISPLAY_MODE_DENSE = 'dense'; // Label and value side by side, closely
/** @var string */
const DEFAULT_LABEL = '';
public const DEFAULT_LABEL = '';
/** @var string */
const DEFAULT_DESCRIPTION = '';
public const DEFAULT_DESCRIPTION = '';
/** @var array */
const DEFAULT_METADATA = array();
public const DEFAULT_METADATA = [];
/** @var bool */
const DEFAULT_HIDDEN = false;
public const DEFAULT_HIDDEN = false;
/** @var bool */
const DEFAULT_READ_ONLY = false;
public const DEFAULT_READ_ONLY = false;
/** @var bool */
const DEFAULT_MANDATORY = false;
/** @var string */
const DEFAULT_DISPLAY_MODE = self::ENUM_DISPLAY_MODE_COSY;
public const DEFAULT_MANDATORY = false;
/** @var string */
public const DEFAULT_DISPLAY_MODE = self::ENUM_DISPLAY_MODE_COSY;
/** @var bool */
const DEFAULT_VALID = true;
public const DEFAULT_VALID = true;
/** @var string */
protected $sId;
@@ -93,7 +93,7 @@ abstract class Field
{
$this->sId = $sId;
// No space in such an id, that could be used as a DOM node id
$this->sGlobalId = 'field_' . str_replace(' ', '_', $sId) . '_' . uniqid();
$this->sGlobalId = 'field_'.str_replace(' ', '_', $sId).'_'.uniqid();
$this->sLabel = static::DEFAULT_LABEL;
$this->sDescription = static::DEFAULT_DESCRIPTION;
$this->aMetadata = static::DEFAULT_METADATA;
@@ -101,10 +101,10 @@ abstract class Field
$this->bReadOnly = static::DEFAULT_READ_ONLY;
$this->bMandatory = static::DEFAULT_MANDATORY;
$this->sDisplayMode = static::DEFAULT_DISPLAY_MODE;
$this->aValidators = array();
$this->aValidators = [];
$this->bValidationDisabled = false;
$this->bValid = static::DEFAULT_VALID;
$this->aErrorMessages = array();
$this->aErrorMessages = [];
$this->onFinalizeCallback = $onFinalizeCallback;
}
@@ -219,14 +219,14 @@ abstract class Field
return $this->bMandatory;
}
/**
*
* @return string
*/
/**
*
* @return string
*/
public function GetDisplayMode()
{
return $this->sDisplayMode;
}
{
return $this->sDisplayMode;
}
public function GetValidators()
{
@@ -269,7 +269,7 @@ abstract class Field
{
return $this->currentValue;
}
/**
* Sets the field formpath
* Usually Called by the form when adding the field
@@ -361,20 +361,20 @@ abstract class Field
if (false === $bMandatory) {
foreach ($this->aValidators as $iKey => $oValue) {
if ($oValue instanceof MandatoryValidator) {
unset($this->aValidators[$iKey]);
}
}
}
unset($this->aValidators[$iKey]);
}
}
}
$this->bMandatory = $bMandatory;
return $this;
}
/**
* @return AbstractValidator
* @since 3.1.0 N°6414
*/
/**
* @return AbstractValidator
* @since 3.1.0 N°6414
*/
protected function GetMandatoryValidatorInstance(): AbstractValidator
{
return new MandatoryValidator();
@@ -395,16 +395,16 @@ abstract class Field
return $this;
}
/**
*
* @param string $sDisplayMode
* @return $this
*/
/**
*
* @param string $sDisplayMode
* @return $this
*/
public function SetDisplayMode(string $sDisplayMode)
{
$this->sDisplayMode = $sDisplayMode;
return $this;
}
{
$this->sDisplayMode = $sDisplayMode;
return $this;
}
/**
*
@@ -480,10 +480,10 @@ abstract class Field
return $this;
}
/**
* @param AbstractValidator $oValidator
* @return $this
*/
/**
* @param AbstractValidator $oValidator
* @return $this
*/
public function AddValidator(AbstractValidator $oValidator)
{
$this->aValidators[] = $oValidator;
@@ -506,20 +506,20 @@ abstract class Field
}
/**
* @param string $sValidatorClassName validator class name, should be one of {@see AbstractValidator} children
* @return $this
* @since 3.1.0 N°6414
*/
final public function RemoveValidatorsOfClass(string $sValidatorClassName)
{
foreach ($this->aValidators as $iKey => $oValue) {
if ($oValue instanceof $sValidatorClassName) {
unset($this->aValidators[$iKey]);
}
}
* @param string $sValidatorClassName validator class name, should be one of {@see AbstractValidator} children
* @return $this
* @since 3.1.0 N°6414
*/
final public function RemoveValidatorsOfClass(string $sValidatorClassName)
{
foreach ($this->aValidators as $iKey => $oValue) {
if ($oValue instanceof $sValidatorClassName) {
unset($this->aValidators[$iKey]);
}
}
return $this;
}
return $this;
}
/**
* Note : Function is protected as aErrorMessages should not be add from outside
@@ -542,7 +542,7 @@ abstract class Field
*/
protected function EmptyErrorMessages()
{
$this->aErrorMessages = array();
$this->aErrorMessages = [];
return $this;
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -29,12 +29,12 @@ use Closure;
class FileUploadField extends AbstractSimpleField
{
/** @var bool DEFAULT_ALLOW_DELETE */
const DEFAULT_ALLOW_DELETE = true;
public const DEFAULT_ALLOW_DELETE = true;
/**
* @var bool DEFAULT_DISPLAY_OPENED
* @since 3.2.1 N°7534
*/
const DEFAULT_DISPLAY_OPENED = false;
public const DEFAULT_DISPLAY_OPENED = false;
/** @var string|null $sTransactionId */
protected $sTransactionId;
@@ -187,7 +187,7 @@ class FileUploadField extends AbstractSimpleField
*/
public function SetAllowDelete(bool $bAllowDelete)
{
$this->bAllowDelete = (boolean) $bAllowDelete;
$this->bAllowDelete = (bool) $bAllowDelete;
return $this;
}
@@ -199,7 +199,7 @@ class FileUploadField extends AbstractSimpleField
* @return FileUploadField
* @since 3.2.1 N°7534
*/
public function SetDisplayOpened(bool $bDisplayOpened) : FileUploadField
public function SetDisplayOpened(bool $bDisplayOpened): FileUploadField
{
$this->bDisplayOpened = $bDisplayOpened;
@@ -212,7 +212,7 @@ class FileUploadField extends AbstractSimpleField
* @return boolean
* @since 3.2.1 N°7534
*/
public function GetDisplayOpened() : bool
public function GetDisplayOpened(): bool
{
return $this->bDisplayOpened;
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -27,5 +27,5 @@ namespace Combodo\iTop\Form\Field;
class HiddenField extends TextField
{
/** @inheritDoc */
const DEFAULT_HIDDEN = true;
public const DEFAULT_HIDDEN = true;
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -34,12 +34,9 @@ class ImageField extends BlobField
*/
public function GetDisplayValue()
{
if ($this->currentValue->IsEmpty())
{
if ($this->currentValue->IsEmpty()) {
$sValue = Dict::S('Portal:File:None');
}
else
{
} else {
$sFilename = $this->currentValue->GetFileName();
$iSize = utils::BytesToFriendlyFormat(utils::StrLen($this->currentValue->GetData()));
$sOpenLink = $this->GetDisplayUrl();

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -26,5 +26,4 @@ namespace Combodo\iTop\Form\Field;
*/
class LabelField extends TextField
{
}

View File

@@ -32,11 +32,11 @@ use Combodo\iTop\Form\Validator\LinkedSetValidator;
class LinkedSetField extends AbstractSimpleField
{
/** @var bool DEFAULT_INDIRECT */
const DEFAULT_INDIRECT = false;
public const DEFAULT_INDIRECT = false;
/** @var bool DEFAULT_DISPLAY_OPENED */
const DEFAULT_DISPLAY_OPENED = false;
public const DEFAULT_DISPLAY_OPENED = false;
/** @var bool DEFAULT_DISPLAY_LIMITED_ACCESS_ITEMS */
const DEFAULT_DISPLAY_LIMITED_ACCESS_ITEMS = false;
public const DEFAULT_DISPLAY_LIMITED_ACCESS_ITEMS = false;
/** @var string $sTargetClass */
protected $sTargetClass;
@@ -71,9 +71,9 @@ class LinkedSetField extends AbstractSimpleField
$this->bIndirect = static::DEFAULT_INDIRECT;
$this->bDisplayOpened = static::DEFAULT_DISPLAY_OPENED;
$this->bDisplayLimitedAccessItems = static::DEFAULT_DISPLAY_LIMITED_ACCESS_ITEMS;
$this->aLimitedAccessItemIDs = array();
$this->aAttributesToDisplay = array();
$this->aLnkAttributesToDisplay = array();
$this->aLimitedAccessItemIDs = [];
$this->aAttributesToDisplay = [];
$this->aLnkAttributesToDisplay = [];
$this->sSearchEndpoint = null;
$this->sInformationEndpoint = null;
@@ -218,7 +218,7 @@ class LinkedSetField extends AbstractSimpleField
return $this;
}
/**
* Returns IDs of the linked items with a limited access (not visible or not editable)
*
@@ -280,25 +280,25 @@ class LinkedSetField extends AbstractSimpleField
public function GetLnkAttributesToDisplay(bool $bAttCodesOnly = false)
{
return ($bAttCodesOnly) ? array_keys($this->aLnkAttributesToDisplay) : $this->aLnkAttributesToDisplay;
}
}
/**
* @param array $aAttributesToDisplay
* @return $this
* @since 3.1.0 N°803
*/
public function SetLnkAttributesToDisplay(array $aAttributesToDisplay)
{
$this->aLnkAttributesToDisplay = $aAttributesToDisplay;
/**
* @param array $aAttributesToDisplay
* @return $this
* @since 3.1.0 N°803
*/
public function SetLnkAttributesToDisplay(array $aAttributesToDisplay)
{
$this->aLnkAttributesToDisplay = $aAttributesToDisplay;
$this->RemoveValidatorsOfClass(LinkedSetValidator::class);
$this->AddValidator(new LinkedSetValidator($aAttributesToDisplay));
$this->RemoveValidatorsOfClass(LinkedSetValidator::class);
$this->AddValidator(new LinkedSetValidator($aAttributesToDisplay));
return $this;
}
return $this;
}
/**
* @return string|null
/**
* @return string|null
*/
public function GetSearchEndpoint()
{

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -34,7 +34,7 @@ use Combodo\iTop\Form\Validator\MultipleChoicesValidator;
abstract class MultipleChoicesField extends AbstractSimpleField
{
/** @var bool DEFAULT_MULTIPLE_VALUES_ENABLED */
const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
public const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
/** @var bool $bMultipleValuesEnabled */
protected $bMultipleValuesEnabled;
@@ -48,8 +48,8 @@ abstract class MultipleChoicesField extends AbstractSimpleField
{
parent::__construct($sId, $onFinalizeCallback);
$this->bMultipleValuesEnabled = static::DEFAULT_MULTIPLE_VALUES_ENABLED;
$this->aChoices = array();
$this->currentValue = array();
$this->aChoices = [];
$this->currentValue = [];
$this->InitValidators();
}
@@ -60,14 +60,10 @@ abstract class MultipleChoicesField extends AbstractSimpleField
public function GetCurrentValue()
{
$value = null;
if (!empty($this->currentValue))
{
if ($this->bMultipleValuesEnabled)
{
if (!empty($this->currentValue)) {
if ($this->bMultipleValuesEnabled) {
$value = $this->currentValue;
}
else
{
} else {
reset($this->currentValue);
$value = current($this->currentValue);
}
@@ -84,17 +80,12 @@ abstract class MultipleChoicesField extends AbstractSimpleField
*/
public function SetCurrentValue($currentValue)
{
if (is_array($currentValue))
{
if (is_array($currentValue)) {
$this->currentValue = $currentValue;
}
elseif (is_null($currentValue))
{
$this->currentValue = array();
}
else
{
$this->currentValue = array($currentValue);
} elseif (is_null($currentValue)) {
$this->currentValue = [];
} else {
$this->currentValue = [$currentValue];
}
return $this;
}
@@ -147,8 +138,7 @@ abstract class MultipleChoicesField extends AbstractSimpleField
*/
public function RemoveValue($value)
{
if (array_key_exists($value, $this->currentValue))
{
if (array_key_exists($value, $this->currentValue)) {
unset($this->currentValue[$value]);
}
return $this;
@@ -199,49 +189,49 @@ abstract class MultipleChoicesField extends AbstractSimpleField
}
$this->aChoices[$sId] = $choice;
$this->InitValidators();
$this->InitValidators();
return $this;
}
return $this;
}
/**
* @param string $sId
*
* @return $this
*/
public function RemoveChoice(string $sId)
{
if (in_array($sId, $this->aChoices)) {
unset($this->aChoices[$sId]);
}
/**
* @param string $sId
*
* @return $this
*/
public function RemoveChoice(string $sId)
{
if (in_array($sId, $this->aChoices)) {
unset($this->aChoices[$sId]);
}
return $this;
}
return $this;
}
/**
* @param bool $bReadOnly
* @return MultipleChoicesField
* @since 3.1.0 N°6414
*/
public function SetReadOnly(bool $bReadOnly)
{
if ($bReadOnly) {
/** @noinspection PhpRedundantOptionalArgumentInspection */
$this->SetValidationDisabled(true);
} else {
$this->SetValidationDisabled(false);
}
/**
* @param bool $bReadOnly
* @return MultipleChoicesField
* @since 3.1.0 N°6414
*/
public function SetReadOnly(bool $bReadOnly)
{
if ($bReadOnly) {
/** @noinspection PhpRedundantOptionalArgumentInspection */
$this->SetValidationDisabled(true);
} else {
$this->SetValidationDisabled(false);
}
return parent::SetReadOnly($bReadOnly);
}
return parent::SetReadOnly($bReadOnly);
}
/**
* @return void
* @since 3.1.0 N°6414
*/
protected function InitValidators(): void
{
$this->RemoveValidatorsOfClass(MultipleChoicesValidator::class);
$this->AddValidator(new MultipleChoicesValidator($this->aChoices));
}
/**
* @return void
* @since 3.1.0 N°6414
*/
protected function InitValidators(): void
{
$this->RemoveValidatorsOfClass(MultipleChoicesValidator::class);
$this->AddValidator(new MultipleChoicesValidator($this->aChoices));
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -27,5 +27,5 @@ namespace Combodo\iTop\Form\Field;
class MultipleSelectField extends SelectField
{
/** @inheritDoc */
const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
public const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -26,5 +26,4 @@ namespace Combodo\iTop\Form\Field;
*/
class PasswordField extends TextField
{
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -34,17 +34,16 @@ class PhoneField extends StringField
* @throws \ConfigException
* @throws \CoreException
*/
public function GetDisplayValue()
{
$sLabel = Str::pure2html($this->currentValue);
if (utils::StrLen($sLabel) > 128)
{
// Truncate the length to 128 characters, by removing the middle
$sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
}
public function GetDisplayValue()
{
$sLabel = Str::pure2html($this->currentValue);
if (utils::StrLen($sLabel) > 128) {
// Truncate the length to 128 characters, by removing the middle
$sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
}
$sUrlDecorationClass = utils::GetConfig()->Get('phone_number_decoration_class');
$sUrlDecorationClass = utils::GetConfig()->Get('phone_number_decoration_class');
return "<a class=\"tel\" href=\"tel:$this->currentValue\"><span class=\"form_field_decoration text_decoration $sUrlDecorationClass\"></span>$sLabel</a>";
}
return "<a class=\"tel\" href=\"tel:$this->currentValue\"><span class=\"form_field_decoration text_decoration $sUrlDecorationClass\"></span>$sLabel</a>";
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -27,6 +27,6 @@ namespace Combodo\iTop\Form\Field;
class RadioField extends MultipleChoicesField
{
/** @inheritDoc */
const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
public const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -32,12 +32,12 @@ class SelectField extends MultipleChoicesField
{
// Overloaded constants
/** @inheritDoc */
const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
public const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
/** @var string DEFAULT_NULL_CHOICE_LABEL */
const DEFAULT_NULL_CHOICE_LABEL = 'UI:SelectOne';
public const DEFAULT_NULL_CHOICE_LABEL = 'UI:SelectOne';
/** @var bool DEFAULT_STARTS_WITH_NULL_CHOICE */
const DEFAULT_STARTS_WITH_NULL_CHOICE = true;
public const DEFAULT_STARTS_WITH_NULL_CHOICE = true;
/** @var bool $bStartsWithNullChoice */
protected $bStartsWithNullChoice;
@@ -62,11 +62,11 @@ class SelectField extends MultipleChoicesField
return $this->bStartsWithNullChoice;
}
/**
* @param bool $bStartsWithNullChoice
*
* @return $this
*/
/**
* @param bool $bStartsWithNullChoice
*
* @return $this
*/
public function SetStartsWithNullChoice(bool $bStartsWithNullChoice)
{
$this->bStartsWithNullChoice = $bStartsWithNullChoice;
@@ -82,9 +82,8 @@ class SelectField extends MultipleChoicesField
public function GetChoices()
{
$aChoices = parent::GetChoices();
if ($this->bStartsWithNullChoice && !array_key_exists(null, $aChoices))
{
$aChoices = array(null => Dict::S(static::DEFAULT_NULL_CHOICE_LABEL)) + $aChoices;
if ($this->bStartsWithNullChoice && !array_key_exists(null, $aChoices)) {
$aChoices = [null => Dict::S(static::DEFAULT_NULL_CHOICE_LABEL)] + $aChoices;
}
return $aChoices;

View File

@@ -38,9 +38,9 @@ use MetaModel;
class SelectObjectField extends AbstractSimpleField
{
/** @var int CONTROL_SELECT */
const CONTROL_SELECT = 1;
public const CONTROL_SELECT = 1;
/** @var int CONTROL_RADIO_VERTICAL */
const CONTROL_RADIO_VERTICAL = 2;
public const CONTROL_RADIO_VERTICAL = 2;
/** @var \DBSearch $oSearch */
protected $oSearch;
@@ -176,7 +176,8 @@ class SelectObjectField extends AbstractSimpleField
/**
* @return \DBSearch
*/
public function GetSearch() {
public function GetSearch()
{
return $this->oSearch;
}
@@ -217,14 +218,16 @@ class SelectObjectField extends AbstractSimpleField
/**
* @return int
*/
public function GetControlType() {
public function GetControlType()
{
return $this->iControlType;
}
/**
* @return string|null
*/
public function GetSearchEndpoint() {
public function GetSearchEndpoint()
{
return $this->sSearchEndpoint;
}
@@ -241,7 +244,8 @@ class SelectObjectField extends AbstractSimpleField
*
* @deprecated 3.1.0 N°6414 use ResetCurrentValueIfNotAmongAllowedValues instead
*/
public function VerifyCurrentValue(bool $bAlways = false) {
public function VerifyCurrentValue(bool $bAlways = false)
{
DeprecatedCallsLog::NotifyDeprecatedPhpMethod('3.1.0 N°6414 use ResetCurrentValueIfNotAmongAllowedValues instead');
$this->ResetCurrentValueIfNotAmongAllowedValues($bAlways);
}
@@ -259,7 +263,8 @@ class SelectObjectField extends AbstractSimpleField
*
* @since 3.1.0 N°6414 replaces VerifyCurrentValue$
*/
public function ResetCurrentValueIfNotAmongAllowedValues(bool $bAlways = false) {
public function ResetCurrentValueIfNotAmongAllowedValues(bool $bAlways = false)
{
if (!$this->GetReadOnly() || $bAlways) {
$oValuesSet = FieldHelper::GetObjectsSetFromSearchAndCurrentValueId($this->oSearch, $this->currentValue);

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -26,5 +26,4 @@ namespace Combodo\iTop\Form\Field;
*/
class SetField extends AbstractSimpleField
{
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -21,5 +21,4 @@ namespace Combodo\iTop\Form\Field;
class StringField extends TextField
{
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -29,14 +29,14 @@ use Combodo\iTop\Form\Form;
*/
class SubFormField extends Field
{
protected $oForm;
protected $oForm;
/**
* @inheritDoc
*/
/**
* @inheritDoc
*/
public function __construct(string $sId, Closure $onFinalizeCallback = null)
{
$this->oForm = new Form('subform_' . $sId);
$this->oForm = new Form('subform_'.$sId);
parent::__construct($sId, $onFinalizeCallback);
}
@@ -52,7 +52,7 @@ class SubFormField extends Field
/**
*
* @param \Combodo\iTop\Form\Form $oForm
*
*
* @return \Combodo\iTop\Form\Field\SubFormField
*/
public function SetForm(Form $oForm)
@@ -61,12 +61,12 @@ class SubFormField extends Field
return $this;
}
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
*
* @inheritDoc
*/
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
*
* @inheritDoc
*/
public function Validate()
{
return $this->oForm->Validate();
@@ -87,9 +87,8 @@ class SubFormField extends Field
*/
public function GetErrorMessages()
{
$aRet = array();
foreach ($this->oForm->GetErrorMessages() as $sSubFieldId => $aSubFieldMessages)
{
$aRet = [];
foreach ($this->oForm->GetErrorMessages() as $sSubFieldId => $aSubFieldMessages) {
$aRet[] = $sSubFieldId.': '.implode(', ', $aSubFieldMessages);
}
return $aRet;
@@ -104,14 +103,14 @@ class SubFormField extends Field
return $this->oForm->GetCurrentValues();
}
/**
*
* @param array $value
*
* @return \Combodo\iTop\Form\Field\SubFormField
*
* @throws \Exception
*/
/**
*
* @param array $value
*
* @return \Combodo\iTop\Form\Field\SubFormField
*
* @throws \Exception
*/
public function SetCurrentValue($value)
{
$this->oForm->SetCurrentValues($value);
@@ -124,13 +123,13 @@ class SubFormField extends Field
* @param boolean $bMandatory
*/
public function SetMandatory(bool $bMandatory)
{
foreach ($this->oForm->GetFields() as $oField) {
$oField->SetMandatory($bMandatory);
}
{
foreach ($this->oForm->GetFields() as $oField) {
$oField->SetMandatory($bMandatory);
}
return parent::SetMandatory($bMandatory);
}
return parent::SetMandatory($bMandatory);
}
/**
* Sets the read-only flag on all the fields on the form
@@ -139,12 +138,11 @@ class SubFormField extends Field
*/
public function SetReadOnly(bool $bReadOnly)
{
foreach ($this->oForm->GetFields() as $oField)
{
foreach ($this->oForm->GetFields() as $oField) {
$oField->SetReadOnly($bReadOnly);
$oField->SetMandatory(false);
}
return parent::SetReadOnly($bReadOnly);
return parent::SetReadOnly($bReadOnly);
}
/**
@@ -154,11 +152,10 @@ class SubFormField extends Field
*/
public function SetHidden(bool $bHidden)
{
foreach ($this->oForm->GetFields() as $oField)
{
foreach ($this->oForm->GetFields() as $oField) {
$oField->SetHidden($bHidden);
}
return parent::SetHidden($bHidden);
return parent::SetHidden($bHidden);
}
/**
@@ -170,15 +167,14 @@ class SubFormField extends Field
return $this->oForm->FindSubForm($sFormPath);
}
/**
* @throws \Exception
*/
public function OnFinalize()
/**
* @throws \Exception
*/
public function OnFinalize()
{
$sFormId = 'subform_' . $this->sId;
if ($this->sFormPath !== null)
{
$sFormId = $this->sFormPath . '-' . $sFormId;
$sFormId = 'subform_'.$this->sId;
if ($this->sFormPath !== null) {
$sFormId = $this->sFormPath.'-'.$sFormId;
}
$this->oForm->SetId($sFormId);

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -26,5 +26,4 @@ namespace Combodo\iTop\Form\Field;
*/
class TagSetField extends AbstractSimpleField
{
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -34,11 +34,11 @@ use AttributeText;
class TextAreaField extends TextField
{
/** @var string */
const ENUM_FORMAT_TEXT = 'text';
public const ENUM_FORMAT_TEXT = 'text';
/** @var string */
const ENUM_FORMAT_HTML = 'html';
public const ENUM_FORMAT_HTML = 'html';
/** @var string */
const DEFAULT_FORMAT = 'html';
public const DEFAULT_FORMAT = 'html';
/** @var string */
protected $sFormat;
@@ -121,17 +121,14 @@ class TextAreaField extends TextField
$this->sTransactionId = $sTransactionId;
return $this;
}
public function GetDisplayValue()
{
if ($this->GetFormat() == TextAreaField::ENUM_FORMAT_TEXT)
{
$sValue = \Str::pure2html($this->GetCurrentValue());
if ($this->GetFormat() == TextAreaField::ENUM_FORMAT_TEXT) {
$sValue = \Str::pure2html($this->GetCurrentValue());
$sValue = AttributeText::RenderWikiHtml($sValue);
return "<div>".str_replace("\n", "<br>\n", $sValue).'</div>';
}
else
{
return "<div>".str_replace("\n", "<br>\n", $sValue).'</div>';
} else {
$sValue = AttributeText::RenderWikiHtml($this->GetCurrentValue(), true /* wiki only */);
return "<div class=\"HTML\">".InlineImage::FixUrls($sValue).'</div>';
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -26,5 +26,4 @@ namespace Combodo\iTop\Form\Field;
*/
abstract class TextField extends AbstractSimpleField
{
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -30,41 +30,40 @@ use utils;
*/
class UrlField extends StringField
{
/** @var string */
const DEFAULT_TARGET = '_blank';
/** @var string */
public const DEFAULT_TARGET = '_blank';
/** @var string */
/** @var string */
protected $sTarget;
/**
* @inheritDoc
*/
public function __construct(string $sId, Closure $onFinalizeCallback = null)
{
parent::__construct($sId, $onFinalizeCallback);
$this->sTarget = static::DEFAULT_TARGET;
}
public function SetTarget($sTarget)
{
$this->sTarget = $sTarget;
return $this;
}
/**
* @inheritDoc
*/
public function GetDisplayValue()
{
$sLabel = Str::pure2html($this->currentValue);
if (utils::StrLen($sLabel) > 128)
{
// Truncate the length to 128 characters, by removing the middle
$sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
}
public function __construct(string $sId, Closure $onFinalizeCallback = null)
{
parent::__construct($sId, $onFinalizeCallback);
return "<a target=\"$this->sTarget\" href=\"$this->currentValue\">$sLabel</a>";
}
$this->sTarget = static::DEFAULT_TARGET;
}
public function SetTarget($sTarget)
{
$this->sTarget = $sTarget;
return $this;
}
/**
* @inheritDoc
*/
public function GetDisplayValue()
{
$sLabel = Str::pure2html($this->currentValue);
if (utils::StrLen($sLabel) > 128) {
// Truncate the length to 128 characters, by removing the middle
$sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
}
return "<a target=\"$this->sTarget\" href=\"$this->currentValue\">$sLabel</a>";
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -50,10 +50,10 @@ class Form
{
$this->sId = $sId;
$this->sTransactionId = null;
$this->aFields = array();
$this->aDependencies = array();
$this->aFields = [];
$this->aDependencies = [];
$this->bValid = true;
$this->aErrorMessages = array();
$this->aErrorMessages = [];
$this->iEditableFieldCount = null;
}
@@ -76,8 +76,7 @@ class Form
// Setting id for the form itself
$this->sId = $sId;
// Then setting formpath to its fields
foreach ($this->aFields as $oField)
{
foreach ($this->aFields as $oField) {
$oField->SetFormPath($sId);
}
@@ -129,26 +128,24 @@ class Form
*/
public function GetCurrentValues()
{
$aValues = array();
foreach ($this->aFields as $sId => $oField)
{
$aValues = [];
foreach ($this->aFields as $sId => $oField) {
$aValues[$sId] = $oField->GetCurrentValue();
}
return $aValues;
}
/**
*
* @param array $aValues Must be a hash array of "Field id" => "Field value"
*
* @return \Combodo\iTop\Form\Form
*
* @throws \Exception
*/
/**
*
* @param array $aValues Must be a hash array of "Field id" => "Field value"
*
* @return \Combodo\iTop\Form\Form
*
* @throws \Exception
*/
public function SetCurrentValues($aValues)
{
foreach ($aValues as $sId => $value)
{
foreach ($aValues as $sId => $value) {
$oField = $this->GetField($sId);
$oField->SetCurrentValue($value);
}
@@ -197,12 +194,9 @@ class Form
*/
protected function SetErrorMessages($aErrorMessages, $sFieldId = null)
{
if ($sFieldId === null)
{
if ($sFieldId === null) {
$this->aErrorMessages = $aErrorMessages;
}
else
{
} else {
$this->aErrorMessages[$sFieldId] = $aErrorMessages;
}
return $this;
@@ -219,9 +213,8 @@ class Form
*/
protected function AddErrorMessage($sErrorMessage, $sFieldId = '_main')
{
if (!isset($this->aErrorMessages[$sFieldId]))
{
$this->aErrorMessages[$sFieldId] = array();
if (!isset($this->aErrorMessages[$sFieldId])) {
$this->aErrorMessages[$sFieldId] = [];
}
$this->aErrorMessages[$sFieldId][] = $sErrorMessage;
return $this;
@@ -234,7 +227,7 @@ class Form
*/
protected function EmptyErrorMessages()
{
$this->aErrorMessages = array();
$this->aErrorMessages = [];
return $this;
}
@@ -246,9 +239,8 @@ class Form
*/
public function GetField($sId)
{
if (!array_key_exists($sId, $this->aFields))
{
throw new Exception('Field with ID "' . $sId . '" was not found in the Form of ID "' . $this->sId . '".');
if (!array_key_exists($sId, $this->aFields)) {
throw new Exception('Field with ID "'.$sId.'" was not found in the Form of ID "'.$this->sId.'".');
}
return $this->aFields[$sId];
}
@@ -269,7 +261,7 @@ class Form
* @param array $aDependsOnIds
* @return \Combodo\iTop\Form\Form
*/
public function AddField(Field $oField, $aDependsOnIds = array())
public function AddField(Field $oField, $aDependsOnIds = [])
{
$oField->SetFormPath($this->sId);
$this->aFields[$oField->GetId()] = $oField;
@@ -283,8 +275,7 @@ class Form
*/
public function RemoveField($sId)
{
if (array_key_exists($sId, $this->aFields))
{
if (array_key_exists($sId, $this->aFields)) {
unset($this->aFields[$sId]);
}
return $this;
@@ -310,9 +301,8 @@ class Form
*/
public function GetFieldDependencies($sFieldId)
{
if (!array_key_exists($sFieldId, $this->aDependencies))
{
throw new Exception('Field with ID "' . $sFieldId . '" had no dependancies declared in the Form.');
if (!array_key_exists($sFieldId, $this->aDependencies)) {
throw new Exception('Field with ID "'.$sFieldId.'" had no dependancies declared in the Form.');
}
return $this->aDependencies[$sFieldId];
}
@@ -325,8 +315,7 @@ class Form
*/
public function AddFieldDependencies($sFieldId, array $aDependsOnIds)
{
foreach ($aDependsOnIds as $sDependsOnId)
{
foreach ($aDependsOnIds as $sDependsOnId) {
$this->AddFieldDependency($sFieldId, $sDependsOnId);
}
return $this;
@@ -338,63 +327,56 @@ class Form
* @param string $sDependsOnId
* @return \Combodo\iTop\Form\Form
*/
public function AddFieldDependency($sFieldId, $sDependsOnId)
{
if (!array_key_exists($sFieldId, $this->aDependencies))
{
$this->aDependencies[$sFieldId] = array();
}
$this->aDependencies[$sFieldId][] = $sDependsOnId;
return $this;
}
public function AddFieldDependency($sFieldId, $sDependsOnId)
{
if (!array_key_exists($sFieldId, $this->aDependencies)) {
$this->aDependencies[$sFieldId] = [];
}
$this->aDependencies[$sFieldId][] = $sDependsOnId;
return $this;
}
/**
* Returns a hash array of the fields impacts on other fields. Key being the field that impacts the fields stored in the value as a regular array
* (It kind of reversed the dependencies array)
*
* eg :
* - 'service' => array('subservice', 'template')
* - 'subservice' => array()
* - ...
*
* @return array
*/
public function GetFieldsImpacts()
{
$aRes = array();
/**
* Returns a hash array of the fields impacts on other fields. Key being the field that impacts the fields stored in the value as a regular array
* (It kind of reversed the dependencies array)
*
* eg :
* - 'service' => array('subservice', 'template')
* - 'subservice' => array()
* - ...
*
* @return array
*/
public function GetFieldsImpacts()
{
$aRes = [];
foreach ($this->aDependencies as $sImpactedFieldId => $aDependentFieldsIds)
{
foreach ($aDependentFieldsIds as $sDependentFieldId)
{
if (!array_key_exists($sDependentFieldId, $aRes))
{
$aRes[$sDependentFieldId] = array();
}
$aRes[$sDependentFieldId][] = $sImpactedFieldId;
}
}
foreach ($this->aDependencies as $sImpactedFieldId => $aDependentFieldsIds) {
foreach ($aDependentFieldsIds as $sDependentFieldId) {
if (!array_key_exists($sDependentFieldId, $aRes)) {
$aRes[$sDependentFieldId] = [];
}
$aRes[$sDependentFieldId][] = $sImpactedFieldId;
}
}
return $aRes;
}
return $aRes;
}
/**
* Returns the number of editable fields in this form.
*
* @param bool $bForce
*
* @return integer
*/
/**
* Returns the number of editable fields in this form.
*
* @param bool $bForce
*
* @return integer
*/
public function GetEditableFieldCount($bForce = false)
{
// Count is usally done by the Finalize function but it can be done there if Finalize hasn't been called yet or if we choose to force it.
if (($this->iEditableFieldCount === null) || ($bForce === true))
{
if (($this->iEditableFieldCount === null) || ($bForce === true)) {
$this->iEditableFieldCount = 0;
foreach ($this->aFields as $oField)
{
if ($oField->IsEditable())
{
foreach ($this->aFields as $oField) {
if ($oField->IsEditable()) {
$this->iEditableFieldCount++;
}
}
@@ -421,10 +403,8 @@ class Form
public function HasVisibleFields()
{
$bRet = false;
foreach ($this->aFields as $oField)
{
if (!$oField->GetHidden())
{
foreach ($this->aFields as $oField) {
if (!$oField->GetHidden()) {
$bRet = true;
break;
}
@@ -434,13 +414,12 @@ class Form
/**
* Forces the form to a read only state by setting read only to true on all its fields
*
*
* @return \Combodo\iTop\Form\Form
*/
public function MakeReadOnly()
{
foreach ($this->GetFields() as $oField)
{
foreach ($this->GetFields() as $oField) {
$oField->SetReadOnly(true);
}
@@ -454,129 +433,114 @@ class Form
public function FindSubForm($sFormPath)
{
$ret = null;
if ($sFormPath == $this->sId)
{
if ($sFormPath == $this->sId) {
$ret = $this;
}
else
{
foreach ($this->aFields as $oField)
{
if ($oField instanceof SubFormField)
{
} else {
foreach ($this->aFields as $oField) {
if ($oField instanceof SubFormField) {
$ret = $oField->FindSubForm($sFormPath);
if ($ret !== null) break;
if ($ret !== null) {
break;
}
}
}
}
return $ret;
}
/**
* Resets CaseLog fields value in the form and its sub-forms
*
* @return Form
*/
/**
* Resets CaseLog fields value in the form and its sub-forms
*
* @return Form
*/
public function ResetCaseLogFields()
{
foreach($this->GetFields() as $oField)
{
if($oField instanceof CaseLogField)
{
$oField->SetCurrentValue(null);
}
elseif($oField instanceof SubFormField)
{
$oField->GetForm()->ResetCaseLogFields();
}
}
{
foreach ($this->GetFields() as $oField) {
if ($oField instanceof CaseLogField) {
$oField->SetCurrentValue(null);
} elseif ($oField instanceof SubFormField) {
$oField->GetForm()->ResetCaseLogFields();
}
}
return $this;
}
return $this;
}
/**
* Finalizes each field, following the dependencies so that a field can compute its value or other properties,
* depending on other fields
*
* @throws \Exception
*/
public function Finalize()
{
$aFieldList = array(); // Fields ordered by dependence
/**
* Finalizes each field, following the dependencies so that a field can compute its value or other properties,
* depending on other fields
*
* @throws \Exception
*/
public function Finalize()
{
$aFieldList = []; // Fields ordered by dependence
// Clone the dependency data : $aDependencies will be truncated as the fields are added to the list
$aDependencies = $this->aDependencies;
$bMadeProgress = true; // Safety net in case of circular references
foreach ($aDependencies as $sImpactedBy => $aSomeFields)
{
foreach ($aSomeFields as $i => $sSomeId)
{
if (!array_key_exists($sSomeId, $this->aFields))
{
throw new Exception('Unmet dependency : Field ' . $sImpactedBy . ' expecting field ' . $sSomeId . ' which is not in the Form');
foreach ($aDependencies as $sImpactedBy => $aSomeFields) {
foreach ($aSomeFields as $i => $sSomeId) {
if (!array_key_exists($sSomeId, $this->aFields)) {
throw new Exception('Unmet dependency : Field '.$sImpactedBy.' expecting field '.$sSomeId.' which is not in the Form');
}
}
}
while ($bMadeProgress && count($aFieldList) < count($this->aFields))
{
while ($bMadeProgress && count($aFieldList) < count($this->aFields)) {
$bMadeProgress = false;
foreach ($this->aFields as $sId => $oField)
{
if (array_key_exists($sId, $aFieldList))
foreach ($this->aFields as $sId => $oField) {
if (array_key_exists($sId, $aFieldList)) {
continue;
if (isset($aDependencies[$sId]) && count($aDependencies[$sId]) > 0) continue;
}
if (isset($aDependencies[$sId]) && count($aDependencies[$sId]) > 0) {
continue;
}
// Add the field at the end of the list
$aFieldList[$sId] = $oField;
$bMadeProgress = true;
// Track that this dependency has been solved
foreach ($aDependencies as $sImpactedBy => $aSomeFields)
{
foreach ($aSomeFields as $i => $sSomeId)
{
if ($sSomeId == $sId)
{
foreach ($aDependencies as $sImpactedBy => $aSomeFields) {
foreach ($aSomeFields as $i => $sSomeId) {
if ($sSomeId == $sId) {
unset($aDependencies[$sImpactedBy][$i]);
}
}
}
}
}
if (!$bMadeProgress)
{
throw new Exception('Unmet dependencies (might be a circular reference) : ' . implode(', ', array_keys($aDependencies)));
if (!$bMadeProgress) {
throw new Exception('Unmet dependencies (might be a circular reference) : '.implode(', ', array_keys($aDependencies)));
}
foreach ($aFieldList as $sId => $oField)
{
foreach ($aFieldList as $sId => $oField) {
$oField->OnFinalize();
if ($oField->IsEditable())
{
if ($oField->IsEditable()) {
$this->iEditableFieldCount++;
}
}
}
}
/**
* Validate the form and return if it's valid or not
*
*
* @return boolean
*/
public function Validate()
{
$this->SetValid(true);
$this->EmptyErrorMessages();
public function Validate()
{
$this->SetValid(true);
$this->EmptyErrorMessages();
foreach ($this->aFields as $oField) {
if (!$oField->Validate()) {
$this->SetValid(false);
foreach ($oField->GetErrorMessages() as $sErrorMessage) {
$this->AddErrorMessage(Dict::S($sErrorMessage), $oField->Getid());
}
}
}
foreach ($this->aFields as $oField) {
if (!$oField->Validate()) {
$this->SetValid(false);
foreach ($oField->GetErrorMessages() as $sErrorMessage) {
$this->AddErrorMessage(Dict::S($sErrorMessage), $oField->Getid());
}
}
}
return $this->GetValid();
}
return $this->GetValid();
}
}

View File

@@ -31,7 +31,7 @@ use CoreException;
*/
abstract class FormManager
{
/** @var \Combodo\iTop\Form\Form $oForm */
/** @var \Combodo\iTop\Form\Form $oForm */
protected $oForm;
/** @var \Combodo\iTop\Renderer\FormRenderer $oRenderer */
protected $oRenderer;
@@ -45,26 +45,22 @@ abstract class FormManager
*
* @return $this
*/
static function FromJSON($sJson)
public static function FromJSON($sJson)
{
// Overload in child class when needed
if (is_array($sJson))
{
if (is_array($sJson)) {
$aJson = $sJson;
}
else
{
} else {
$aJson = json_decode($sJson, true);
}
$oFormManager = new static();
$sFormRendererClass = $aJson['formrenderer_class'];
// N°7455 - Ensure form renderer class extends FormRenderer
if (false === is_a($sFormRendererClass, FormRenderer::class, true))
{
throw new CoreException('Form renderer class must extend '.FormRenderer::class);
}
// N°7455 - Ensure form renderer class extends FormRenderer
if (false === is_a($sFormRendererClass, FormRenderer::class, true)) {
throw new CoreException('Form renderer class must extend '.FormRenderer::class);
}
/** @var \Combodo\iTop\Renderer\FormRenderer $oFormRenderer */
$oFormRenderer = new $sFormRendererClass();
@@ -147,13 +143,13 @@ abstract class FormManager
public function ToJSON()
{
// Overload in child class when needed
return array(
return [
'id' => $this->oForm->GetId(),
'transaction_id' => $this->oForm->GetTransactionId(),
'formmanager_class' => $this->GetClass(),
'formrenderer_class' => get_class($this->GetRenderer()),
'formrenderer_endpoint' => $this->GetRenderer()->GetEndpoint()
);
'formrenderer_endpoint' => $this->GetRenderer()->GetEndpoint(),
];
}
abstract public function Build();
@@ -174,14 +170,14 @@ abstract class FormManager
*/
public function OnSubmit($aArgs = null)
{
$aData = array(
$aData = [
'valid' => true,
'messages' => array(
'success' => array(),
'warnings' => array(), // Not used as of today, just to show that the structure is ready for change like this.
'error' => array(),
),
);
'messages' => [
'success' => [],
'warnings' => [], // Not used as of today, just to show that the structure is ready for change like this.
'error' => [],
],
];
$this->CheckTransaction($aData);
@@ -198,7 +194,7 @@ abstract class FormManager
$isTransactionValid = \utils::IsTransactionValid($this->oForm->GetTransactionId(), false); //The transaction token is kept in order to preserve BC with ajax forms (the second call would fail if the token is deleted). (The GC will take care of cleaning the token for us later on)
if (!$isTransactionValid) {
$aData['messages']['error'] += [
'_main' => [\Dict::S('UI:Error:InvalidToken')] //This message is generic, if you override this method you should use a more precise message. @see \Combodo\iTop\Portal\Form\ObjectFormManager::CheckTransaction
'_main' => [\Dict::S('UI:Error:InvalidToken')], //This message is generic, if you override this method you should use a more precise message. @see \Combodo\iTop\Portal\Form\ObjectFormManager::CheckTransaction
];
$aData['valid'] = false;
}

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -17,11 +18,13 @@ use ScalarExpression;
*
* @since 3.1.0 N°6414
*/
class FieldHelper {
class FieldHelper
{
/**
* @since 3.1.0 N°6414 Method creation to factorize between uses in {@see \Combodo\iTop\Form\Field\Field} and {@see \Combodo\iTop\Form\Validator\SelectObjectValidator}
*/
public static function GetObjectsSetFromSearchAndCurrentValueId(DBSearch $oSearch, string $sCurrentValueId) {
public static function GetObjectsSetFromSearchAndCurrentValueId(DBSearch $oSearch, string $sCurrentValueId)
{
$oSearchForExistingCurrentValue = $oSearch->DeepClone();
$oCheckIdAgainstCurrentValueExpression = new BinaryExpression(
new FieldExpression('id', $oSearchForExistingCurrentValue->GetClassAlias()),
@@ -33,4 +36,4 @@ class FieldHelper {
return new DBObjectSet($oSearchForExistingCurrentValue);
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -11,12 +12,12 @@ namespace Combodo\iTop\Form\Validator;
*/
abstract class AbstractRegexpValidator extends AbstractValidator
{
public const VALIDATOR_NAME = 'abstract_regexp';
public const VALIDATOR_NAME = 'abstract_regexp';
/** @var string Override in children classes to set regexp to use for validation */
public const DEFAULT_REGEXP = '';
/** @var string Override in children classes to set regexp to use for validation */
public const DEFAULT_REGEXP = '';
protected string $sRegExp;
protected string $sRegExp;
public function __construct(?string $sErrorMessage = null)
{
@@ -36,21 +37,21 @@ abstract class AbstractRegexpValidator extends AbstractValidator
return [$this->sErrorMessage];
}
/**
* Returns the regular expression of the validator.
*
* @param boolean $bWithSlashes If true, surrounds $sRegExp with '/'. Used with preg_match & co
*
* @return string
*/
public function GetRegExp($bWithSlashes = false)
{
if ($bWithSlashes) {
$sRet = '/'.str_replace('/', '\\/', $this->sRegExp).'/';
} else {
$sRet = $this->sRegExp;
}
/**
* Returns the regular expression of the validator.
*
* @param boolean $bWithSlashes If true, surrounds $sRegExp with '/'. Used with preg_match & co
*
* @return string
*/
public function GetRegExp($bWithSlashes = false)
{
if ($bWithSlashes) {
$sRet = '/'.str_replace('/', '\\/', $this->sRegExp).'/';
} else {
$sRet = $this->sRegExp;
}
return $sRet;
}
}
return $sRet;
}
}

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -24,8 +25,7 @@ abstract class AbstractValidator
{
if (false === utils::IsNullOrEmptyString($sErrorMessage)) {
$this->sErrorMessage = $sErrorMessage;
}
else {
} else {
$this->sErrorMessage = static::DEFAULT_ERROR_MESSAGE;
}
}
@@ -56,4 +56,4 @@ abstract class AbstractValidator
{
return $this->sErrorMessage;
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -19,4 +20,4 @@ class CustomRegexpValidator extends AbstractRegexpValidator
$this->sRegExp = $sRegExp; // must be done after parent constructor call !
}
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -30,45 +30,45 @@ use utils;
*/
class LinkedSetValidator extends AbstractRegexpValidator
{
public const VALIDATOR_NAME = 'linkedset_validator';
private $aAttributesToDisplayCodes;
public const VALIDATOR_NAME = 'linkedset_validator';
private $aAttributesToDisplayCodes;
public function __construct($aAttributesToDisplayCodes)
{
$this->aAttributesToDisplayCodes = $aAttributesToDisplayCodes;
public function __construct($aAttributesToDisplayCodes)
{
$this->aAttributesToDisplayCodes = $aAttributesToDisplayCodes;
parent::__construct();
}
parent::__construct();
}
public function Validate($value): array
{
$aErrorMessages = [];
public function Validate($value): array
{
$aErrorMessages = [];
/** @var ormLinkSet $oSet */
$oSet = $value;
/** @var ormLinkSet $oSet */
$oSet = $value;
// validate each links...
/** @var \DBObject $oItem */
foreach ($oSet as $oItem) {
$aChanges = $oItem->ListChanges();
foreach ($aChanges as $sAttCode => $AttValue) {
if (!in_array($sAttCode, $this->aAttributesToDisplayCodes)) {
continue;
}
$res = $oItem->CheckValue($sAttCode);
if ($res !== true) {
$sAttLabel = $oItem->GetLabel($sAttCode);
$sItem = utils::IsNullOrEmptyString($oItem->Get('friendlyname'))
? Dict::S('UI:Links:NewItem')
: $oItem->Get('friendlyname');
$sIssue = Dict::Format('Core:CheckValueError', $sAttLabel, $sAttCode, $res);
$aErrorMessages[] = '<b>' . $sItem . ' : </b>' . $sIssue;
}
}
}
// validate each links...
/** @var \DBObject $oItem */
foreach ($oSet as $oItem) {
$aChanges = $oItem->ListChanges();
foreach ($aChanges as $sAttCode => $AttValue) {
if (!in_array($sAttCode, $this->aAttributesToDisplayCodes)) {
continue;
}
$res = $oItem->CheckValue($sAttCode);
if ($res !== true) {
$sAttLabel = $oItem->GetLabel($sAttCode);
$sItem = utils::IsNullOrEmptyString($oItem->Get('friendlyname'))
? Dict::S('UI:Links:NewItem')
: $oItem->Get('friendlyname');
$sIssue = Dict::Format('Core:CheckValueError', $sAttLabel, $sAttCode, $res);
$aErrorMessages[] = '<b>'.$sItem.' : </b>'.$sIssue;
}
}
}
$oSet->Rewind();
$oSet->Rewind();
return $aErrorMessages;
}
return $aErrorMessages;
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -10,50 +11,50 @@ use utils;
class MultipleChoicesValidator extends AbstractValidator
{
public const VALIDATOR_NAME = 'multiple_choices_validator';
public const VALIDATOR_NAME = 'multiple_choices_validator';
/** @var array List of possible choices */
private array $aChoices;
/** @var array List of possible choices */
private array $aChoices;
public function __construct(array $aChoices)
{
parent::__construct();
$this->aChoices = $aChoices;
}
public function __construct(array $aChoices)
{
parent::__construct();
$this->aChoices = $aChoices;
}
/**
* @param mixed $value Warning can either be an array (if multiple values are allowed in the field) or a primitive : {@see \Combodo\iTop\Form\Field\MultipleChoicesField::GetCurrentValue()}
*
* @return array|string[]
*/
public function Validate($value): array
{
$aErrorMessages = [];
if (false === is_array($value)) {
$this->CheckValueAgainstChoices($value, $aErrorMessages);
/**
* @param mixed $value Warning can either be an array (if multiple values are allowed in the field) or a primitive : {@see \Combodo\iTop\Form\Field\MultipleChoicesField::GetCurrentValue()}
*
* @return array|string[]
*/
public function Validate($value): array
{
$aErrorMessages = [];
if (false === is_array($value)) {
$this->CheckValueAgainstChoices($value, $aErrorMessages);
return $aErrorMessages;
}
return $aErrorMessages;
}
if (count($value) === 0) {
return [];
}
if (count($value) === 0) {
return [];
}
/** @noinspection PhpUnusedLocalVariableInspection */
foreach ($value as $sCode => $valueItem) {
if (utils::IsNullOrEmptyString($valueItem)) {
continue;
}
$this->CheckValueAgainstChoices($valueItem, $aErrorMessages);
}
/** @noinspection PhpUnusedLocalVariableInspection */
foreach ($value as $sCode => $valueItem) {
if (utils::IsNullOrEmptyString($valueItem)) {
continue;
}
$this->CheckValueAgainstChoices($valueItem, $aErrorMessages);
}
return $aErrorMessages;
}
return $aErrorMessages;
}
private function CheckValueAgainstChoices(string $sValue, array &$aErrorMessages): void
{
if (false === array_key_exists($sValue, $this->aChoices)) {
$aErrorMessages[] = "Value ({$sValue}) is not part of the field possible values list";
}
}
}
private function CheckValueAgainstChoices(string $sValue, array &$aErrorMessages): void
{
if (false === array_key_exists($sValue, $this->aChoices)) {
$aErrorMessages[] = "Value ({$sValue}) is not part of the field possible values list";
}
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -12,34 +13,34 @@ use utils;
class SelectObjectValidator extends AbstractValidator
{
public const VALIDATOR_NAME = 'select_object_validator';
public const VALIDATOR_NAME = 'select_object_validator';
/** @var \DBSearch $oSearch */
private $oSearch;
/** @var \DBSearch $oSearch */
private $oSearch;
public function __construct(DBSearch $oSearch)
{
parent::__construct();
public function __construct(DBSearch $oSearch)
{
parent::__construct();
$this->oSearch = $oSearch;
}
$this->oSearch = $oSearch;
}
public function Validate($value): array
{
if (utils::IsNullOrEmptyString($value)) {
return [];
}
if (($value === 0) || ($value === '0')) {
return [];
}
public function Validate($value): array
{
if (utils::IsNullOrEmptyString($value)) {
return [];
}
if (($value === 0) || ($value === '0')) {
return [];
}
$oSetForExistingCurrentValue = FieldHelper::GetObjectsSetFromSearchAndCurrentValueId($this->oSearch, $value);
$iObjectsCount = $oSetForExistingCurrentValue->CountWithLimit(1);
$oSetForExistingCurrentValue = FieldHelper::GetObjectsSetFromSearchAndCurrentValueId($this->oSearch, $value);
$iObjectsCount = $oSetForExistingCurrentValue->CountWithLimit(1);
if ($iObjectsCount === 0) {
return ["Value $value does not match the corresponding filter set"];
}
if ($iObjectsCount === 0) {
return ["Value $value does not match the corresponding filter set"];
}
return [];
}
}
return [];
}
}

View File

@@ -4,7 +4,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// 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.
@@ -21,7 +21,6 @@ namespace Combodo\iTop\Form\Validator;
use DeprecatedCallsLog;
/**
* Description of Validator
*