JobForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Forms;
  3. use MGProvision\Proxmox\v2\models\Kvm;
  4. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  5. use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\JobPeriod;
  6. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\SnapshotValidator;
  7. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\StartTimeValidator;
  8. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Hidden;
  9. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Number;
  10. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Select;
  11. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Switcher;
  12. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Text;
  13. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Textarea;
  14. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  15. trait JobForm
  16. {
  17. public function setJobConfirmMessage(){
  18. $limit = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOTS, $this->configuration()->getSnapshotMaxFiles());
  19. if ($limit && $limit != "-1")
  20. {
  21. sl("lang")->addReplacementConstant("snapshotFilesLimit" , $limit);
  22. $this->setInternalAlertMessage( sl("lang")->tr('Your snapshot limit is :snapshotFilesLimit:. When you exceed this limit, the last snapshot will be replaced with a new one.'));
  23. $this->setInternalAlertMessageRaw(true);
  24. }
  25. }
  26. protected function initFields()
  27. {
  28. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  29. $formData = $this->getRequestValue("formData");
  30. //id
  31. $id = false;
  32. if(is_numeric($this->getRequestValue("actionElementId"))){
  33. $id = $this->getRequestValue("actionElementId");
  34. }
  35. if ($formData && $formData['id']){
  36. $id = $formData['id'];
  37. }
  38. //hourly
  39. $isHourly = true;
  40. if($formData && $formData['period'] ){
  41. $isHourly = $formData['period'] == JobPeriod::HOURLY;
  42. } elseif (!$formData && $id){
  43. $this->loadProvider();
  44. $this->dataProvider->read();
  45. $isHourly = $this->dataProvider->getValueById('period') == JobPeriod::HOURLY ;
  46. }elseif (!$formData && !$id){
  47. $period = $this->configuration()->getPermissionSnapshotJobPeriod();
  48. if($period && in_array(JobPeriod::HOURLY, $period)){
  49. $isHourly = true;
  50. }elseif ($period ){
  51. $isHourly = false;
  52. }
  53. }
  54. //id
  55. if(is_numeric($this->getRequestValue("actionElementId"))){
  56. $this->addField(new Hidden("id"));
  57. }
  58. //period
  59. $field = new Select('period');
  60. $field->addHtmlAttribute('bi-event-change', "reloadVueModal");
  61. $this->addField($field);
  62. //run_every
  63. if($isHourly){
  64. $field = new Number('run_every',1,48);
  65. $field->setPlaceholder("1");
  66. $this->addField($field);
  67. }
  68. //days
  69. if(!$isHourly){
  70. $field = new Select('days');
  71. $field->enableMultiple();
  72. $this->addField($field);
  73. }
  74. //start_time
  75. if(!$isHourly){
  76. $field = new Text('start_time');
  77. $field->setPlaceholder("00:00");
  78. $field->addValidator(new StartTimeValidator(false));
  79. $this->addField($field);
  80. }
  81. //name
  82. $field = new Text('name');
  83. $field->notEmpty();
  84. $field->addValidator(new SnapshotValidator(true));
  85. $this->addField($field);
  86. //vmstate
  87. if ($vm instanceof Kvm)
  88. {
  89. $field = new Switcher("vmstate");
  90. $this->addField($field);
  91. }
  92. //description
  93. $field = new Textarea("description");
  94. $this->addField($field);
  95. }
  96. }