Foundations for the new form system

SVN:trunk[3889]
This commit is contained in:
Guillaume Lajarige
2016-01-28 10:37:32 +00:00
parent 3ecd768982
commit 1f4ca07b5f
22 changed files with 2204 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?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\Form\Field;
use \Combodo\iTop\Form\Field\MultipleChoicesField;
/**
* Description of CheckboxField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class CheckboxField extends MultipleChoicesField
{
const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
}

View File

@@ -0,0 +1,250 @@
<?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\Form\Field;
use \Closure;
use \Combodo\iTop\Form\Validator\Validator;
use \Combodo\iTop\Form\Validator\MandatoryValidator;
/**
* Description of Field
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
abstract class Field
{
const DEFAULT_LABEL = '';
const DEFAULT_READ_ONLY = false;
const DEFAULT_MANDATORY = false;
const DEFAULT_VALID = true;
protected $sId;
protected $sLabel;
protected $bReadOnly;
protected $bMandatory;
protected $aValidators;
protected $bValid;
protected $aErrorMessages;
protected $currentValue;
protected $onFinalizeCallback;
/**
*
* @param Closure $callback (Used in the $oForm->AddField($sId, ..., function() use ($oManager, $oForm, '...') { ... } ); )
*/
public function __construct($sId, Closure $onFinalizeCallback = null)
{
$this->sId = $sId;
$this->sLabel = static::DEFAULT_LABEL;
$this->bReadOnly = static::DEFAULT_READ_ONLY;
$this->bMandatory = static::DEFAULT_MANDATORY;
$this->aValidators = array();
$this->bValid = static::DEFAULT_VALID;
$this->aErrorMessages = array();
$this->onFinalizeCallback = $onFinalizeCallback;
}
public function GetId()
{
return $this->sId;
}
public function GetLabel()
{
return $this->sLabel;
}
public function GetReadOnly()
{
return $this->bReadOnly;
}
public function GetMandatory()
{
return $this->bMandatory;
}
public function GetValidators()
{
return $this->aValidators;
}
/**
* Returns the current validation state of the field (true|false).
* It DOESN'T make the validation, see Validate() instead.
*
* @return boolean
*/
public function GetValid()
{
return $this->bValid;
}
public function GetErrorMessages()
{
return $this->aErrorMessages;
}
public function GetCurrentValue()
{
return $this->currentValue;
}
public function SetLabel($sLabel)
{
$this->sLabel = $sLabel;
return $this;
}
public function SetReadOnly($bReadOnly)
{
$this->bReadOnly = $bReadOnly;
return $this;
}
public function SetMandatory($bMandatory)
{
// Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
if ($bMandatory && !$this->bMandatory)
{
$this->AddValidator(new MandatoryValidator());
}
if (!$bMandatory)
{
foreach ($this->aValidators as $iKey => $oValue)
{
if ($oValue::Getname() === MandatoryValidator::GetName())
{
unset($this->aValidators[$iKey]);
}
}
}
$this->bMandatory = $bMandatory;
return $this;
}
public function SetValidators($aValidators)
{
$this->aValidators = $aValidators;
return $this;
}
/**
* Note : Function is protected as bValid should not be set from outside
*
* @param boolean $bValid
* @return \Combodo\iTop\Form\Field\Field
*/
protected function SetValid($bValid)
{
$this->bValid = $bValid;
return $this;
}
/**
* Note : Function is protected as aErrorMessages should not be set from outside
*
* @param array $aErrorMessages
* @return \Combodo\iTop\Form\Field\Field
*/
protected function SetErrorMessages($aErrorMessages)
{
$this->aErrorMessages = $aErrorMessages;
return $this;
}
public function SetCurrentValue($currentValue)
{
$this->currentValue = $currentValue;
return $this;
}
public function SetOnFinalizeCallback(Closure $onFinalizeCallback)
{
$this->onFinalizeCallback = $onFinalizeCallback;
return $this;
}
public function AddValidator(Validator $oValidator)
{
$this->aValidators[] = $oValidator;
return $this;
}
public function RemoveValidator(Validator $oValidator)
{
foreach ($this->aValidators as $iKey => $oValue)
{
if ($oValue === $oValidator)
{
unset($this->aValidators[$iKey]);
}
}
return $this;
}
/**
* Note : Function is protected as aErrorMessages should not be add from outside
*
* @param string $sErrorMessage
* @return \Combodo\iTop\Field\Field
*/
protected function AddErrorMessage($sErrorMessage)
{
$this->aErrorMessages[] = $sErrorMessage;
return $this;
}
/**
* Note : Function is protected as aErrorMessages should not be set from outside
*
* @return \Combodo\iTop\Field\Field
*/
protected function EmptyErrorMessages()
{
$this->aErrorMessages = array();
return $this;
}
public function OnCancel()
{
// Overload when needed
}
public function OnFinalize()
{
if ($this->onFinalizeCallback !== null)
{
// Note : We MUST have a temp variable to call the Closure. otherwise it won't work when the Closure is a class member
$callback = $this->onFinalizeCallback;
$callback();
}
}
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
*
* @return boolean
*/
abstract public function Validate();
}

View File

@@ -0,0 +1,32 @@
<?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\Form\Field;
use \Combodo\iTop\Form\Field\TextField;
/**
* Description of HiddenField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class HiddenField extends TextField
{
}

View File

@@ -0,0 +1,177 @@
<?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\Form\Field;
use \Closure;
use \Combodo\iTop\Form\Field\Field;
/**
* Description of MultipleChoicesField
*
* Choices = Set of items that can be picked
* Values = Items that have been picked
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
abstract class MultipleChoicesField extends Field
{
const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
protected $bMultipleValuesEnabled;
protected $aChoices;
public function __construct($sId, Closure $onFinalizeCallback = null)
{
parent::__construct($sId, $onFinalizeCallback);
$this->bMultipleValuesEnabled = static::DEFAULT_MULTIPLE_VALUES_ENABLED;
$this->aChoices = array();
$this->currentValue = array();
}
public function GetCurrentValue()
{
$value = null;
if (!empty($this->currentValue))
{
if ($this->bMultipleValuesEnabled)
{
$value = $this->currentValue;
}
else
{
reset($this->currentValue);
$value = current($this->currentValue);
}
}
return $value;
}
/**
* Sets the current value for the MultipleChoicesField.
*
* @param mixed $currentValue Can be either an array of values (in case of multiple values) or just a simple value
* @return \Combodo\iTop\Form\Field\MultipleChoicesField
*/
public function SetCurrentValue($currentValue)
{
if (is_array($currentValue))
{
$this->currentValue = $currentValue;
}
elseif (is_null($currentValue))
{
$this->currentValue = array();
}
else
{
$this->currentValue = array($currentValue);
}
return $this;
}
public function GetMultipleValuesEnabled()
{
return $this->bMultipleValuesEnabled;
}
public function SetMultipleValuesEnabled($bMultipleValuesEnabled)
{
$this->bMultipleValuesEnabled = $bMultipleValuesEnabled;
return $this;
}
public function SetValues($aValues)
{
$this->currentValue = $aValues;
return $this;
}
public function AddValue($value)
{
$this->currentValue = $value;
return $this;
}
public function RemoveValue($value)
{
if (array_key_exists($value, $this->currentValue))
{
unset($this->currentValue[$sId]);
}
return $this;
}
public function IsAmongValues($value)
{
return in_array($value, $this->currentValue);
}
public function GetChoices()
{
return $this->aChoices;
}
public function SetChoices($aChoices)
{
$this->aChoices = $aChoices;
return $this;
}
public function AddChoice($sId, $choice = null)
{
if ($choice === null)
{
$choice = $sId;
}
$this->aChoices[$sId] = $choice;
return $this;
}
public function RemoveChoice($sId)
{
if (in_array($sId, $this->aChoices))
{
unset($this->aChoices[$sId]);
}
return $this;
}
public function Validate()
{
$this->SetValid(true);
$this->EmptyErrorMessages();
foreach ($this->GetValidators() as $oValidator)
{
foreach ($this->currentValue as $value)
{
if (!preg_match($oValidator->GetRegExp(true), $value))
{
$this->SetValid(false);
$this->AddErrorMessage($oValidator->GetErrorMessage());
}
}
}
return $this->GetValid();
}
}

View File

@@ -0,0 +1,33 @@
<?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\Form\Field;
use \Combodo\iTop\Form\Field\MultipleChoicesField;
/**
* Description of RadioField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class RadioField extends MultipleChoicesField
{
const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
}

View File

@@ -0,0 +1,84 @@
<?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\Form\Field;
use \Closure;
use \Combodo\iTop\Form\Field\MultipleChoicesField;
/**
* Description of SelectField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class SelectField extends MultipleChoicesField
{
const DEFAULT_NULL_CHOICE_LABEL = 'TOTR: - Choisir une valeur -';
const DEFAULT_STARTS_WITH_NULL_CHOICE = true;
protected $bStartsWithNullChoice;
public function __construct($sId, Closure $onFinalizeCallback = null)
{
parent::__construct($sId, $onFinalizeCallback);
$this->bStartsWithNullChoice = static::DEFAULT_STARTS_WITH_NULL_CHOICE;
}
/**
* Returns if the select starts with a dummy choice before its choices.
* This can be useful when you want to force the user to explicitly select a choice.
*
* @return boolean
*/
public function GetStartsWithNullChoice()
{
return $this->bStartsWithNullChoice;
}
public function SetStartsWithNullChoice($bStartsWithNullChoice)
{
$this->bStartsWithNullChoice = $bStartsWithNullChoice;
if (!array_key_exists(null, $this->aChoices))
{
$this->aChoices = array(null => static::DEFAULT_NULL_CHOICE_LABEL) + $this->aChoices;
}
return $this;
}
/**
* Sets the choices for the fields
* Overloads the methods for the super class in order to put a dummy choice first if necessary.
*
* @param array $aChoices
* @return \Combodo\iTop\Form\Field\SelectField
*/
public function SetChoices($aChoices)
{
if ($this->bStartsWithNullChoice && !array_key_exists(null, $aChoices))
{
$aChoices = array(null => static::DEFAULT_NULL_CHOICE_LABEL) + $aChoices;
}
parent::SetChoices($aChoices);
return $this;
}
}

View File

@@ -0,0 +1,32 @@
<?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\Form\Field;
use \Combodo\iTop\Form\Field\TextField;
/**
* Description of StringField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class StringField extends TextField
{
}

View File

@@ -0,0 +1,32 @@
<?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\Form\Field;
use \Combodo\iTop\Form\Field\TextField;
/**
* Description of TextAreaField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class TextAreaField extends TextField
{
}

View File

@@ -0,0 +1,54 @@
<?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\Form\Field;
use \Combodo\iTop\Form\Field\Field;
/**
* Description of TextField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
abstract class TextField extends Field
{
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
*
* @return boolean
*/
public function Validate()
{
$this->SetValid(true);
$this->EmptyErrorMessages();
foreach ($this->GetValidators() as $oValidator)
{
if (!preg_match($oValidator->GetRegExp(true), $this->GetCurrentValue()))
{
$this->SetValid(false);
$this->AddErrorMessage($oValidator->GetErrorMessage());
}
}
return $this->GetValid();
}
}