| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;
- use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\ConfigurableOption;
- class DiskSizeValidator extends BaseValidator
- {
- use WhmcsParams;
- protected $additionalField;
- protected $minValue = 0;
- protected $maxValue = 0;
- protected $required = true;
- public function __construct( $additionalField, $required = true)
- {
- $this->additionalField = $additionalField;
- $this->required = $required;
- }
- protected function validate($data, $additionalData = null)
- {
- if (!$this->required && empty($data))
- {
- return true;
- }
- $resurceManager = new ResourceManager();
- $additionalSize = 0;
- if($additionalData->get('formData')[$this->additionalField] ){
- $additionalSize = (int) $additionalData->get('formData')[$this->additionalField];
- }
- $diskResource = $resurceManager->disk();
- # find available space in VDC
- $diskResource->setTotal($diskResource->getTotal()-$additionalSize);
- if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE)) {
- $this->maxValue = $this->getWhmcsConfigOption(ConfigurableOption::STORAGE) - $diskResource->getUsed();
- } else {
- $this->maxValue = $diskResource->free();
- }
- # reduce available space to server limits
- if ($this->maxValue > $diskResource->getMax()) {
- $this->maxValue = $diskResource->getMax();
- }
- $this->minValue = $diskResource->getMin();
- # reject floating point
- if(preg_match("/\./", $data)){
- $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
- return false;
- }
- # accept if no limits
- if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
- {
- return true;
- }
- //Min & Max
- if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
- {
- return true;
- }
- //Min
- else
- {
- if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
- {
- return true;
- }
- }
- if ($this->minValue === $this->maxValue)
- {
- $this->addValidationError('PleaseProvideANumericValue');
- return false;
- }
- if (is_numeric($this->minValue) && is_numeric($this->maxValue))
- {
- $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
- }
- else
- {
- if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
- {
- $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
- }
- }
- return false;
- }
- }
|