DiskSizeValidator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Validators;
  3. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
  4. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  5. class DiskSizeValidator extends BaseValidator
  6. {
  7. protected $minValue = 0;
  8. protected $maxValue = 0;
  9. protected $required = true;
  10. public function __construct( $required = true)
  11. {
  12. $this->required = $required;
  13. }
  14. protected function validate($data, $additionalData = null)
  15. {
  16. if (!$this->required && empty($data))
  17. {
  18. return true;
  19. }
  20. $resurceManager = new ResourceManager();
  21. $diskId = $additionalData->get('formData')['id'];
  22. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  23. $hdd = $vm->getHardDiskRepostiory()->findById($diskId);
  24. $size = $hdd->getGb();
  25. $diskResource = $resurceManager ->disk();
  26. $diskResource->setUsed($diskResource->getUsed() - $size);
  27. $this->maxValue = $diskResource->free();
  28. $this->minValue = $diskResource->getMin();
  29. logModuleCall(
  30. 'proxmoxCloud',
  31. __FUNCTION__,
  32. $hdd,
  33. 'Debug',
  34. $hdd->getName()
  35. );
  36. if(!$hdd->isMaster()){
  37. $this->minValue = $size;
  38. }
  39. if(preg_match("/\./", $data)){
  40. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  41. return false;
  42. }
  43. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  44. {
  45. return true;
  46. }
  47. //Min & Max
  48. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  49. {
  50. return true;
  51. }
  52. //Min
  53. else
  54. {
  55. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  56. {
  57. return true;
  58. }
  59. }
  60. if ($this->minValue === $this->maxValue)
  61. {
  62. $this->addValidationError('PleaseProvideANumericValue');
  63. return false;
  64. }
  65. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  66. {
  67. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  68. }
  69. else
  70. {
  71. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  72. {
  73. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  74. }
  75. }
  76. return false;
  77. }
  78. }