JobForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Snapshot\Forms;
  3. use MGProvision\Proxmox\v2\models\Kvm;
  4. use ModulesGarden\Servers\ProxmoxVps\App\Enum\ConfigurableOption;
  5. use ModulesGarden\Servers\ProxmoxVps\App\Enum\JobPeriod;
  6. use ModulesGarden\Servers\ProxmoxVps\App\UI\Validators\SnapshotValidator;
  7. use ModulesGarden\Servers\ProxmoxVps\App\UI\Validators\StartTimeValidator;
  8. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  9. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Hidden;
  10. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Number;
  11. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Select;
  12. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Switcher;
  13. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Text;
  14. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Textarea;
  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. $formData = $this->getRequestValue("formData");
  29. //id
  30. $id = false;
  31. if(is_numeric($this->getRequestValue("actionElementId"))){
  32. $id = $this->getRequestValue("actionElementId");
  33. }
  34. if ($formData && $formData['id']){
  35. $id = $formData['id'];
  36. }
  37. //hourly
  38. $isHourly = true;
  39. if($formData && $formData['period'] ){
  40. $isHourly = $formData['period'] == JobPeriod::HOURLY;
  41. } elseif (!$formData && $id){
  42. $this->loadProvider();
  43. $this->dataProvider->read();
  44. $isHourly = $this->dataProvider->getValueById('period') == JobPeriod::HOURLY ;
  45. }elseif (!$formData && !$id){
  46. $period = $this->configuration()->getPermissionSnapshotJobPeriod();
  47. if($period && in_array(JobPeriod::HOURLY, $period)){
  48. $isHourly = true;
  49. }elseif ($period ){
  50. $isHourly = false;
  51. }
  52. }
  53. //id
  54. if(is_numeric($this->getRequestValue("actionElementId"))){
  55. $this->addField(new Hidden("id"));
  56. }
  57. //period
  58. $field = new Select('period');
  59. $field->addHtmlAttribute('bi-event-change', "reloadVueModal");
  60. $this->addField($field);
  61. //run_every
  62. if($isHourly){
  63. $field = new Number('run_every',1,48);
  64. $field->setPlaceholder("1");
  65. $this->addField($field);
  66. }
  67. //days
  68. if(!$isHourly){
  69. $field = new Select('days');
  70. $field->enableMultiple();
  71. $this->addField($field);
  72. }
  73. //start_time
  74. if(!$isHourly){
  75. $field = new Text('start_time');
  76. $field->setPlaceholder("00:00");
  77. $field->addValidator(new StartTimeValidator(false));
  78. $this->addField($field);
  79. }
  80. //name
  81. $field = new Text('name');
  82. $field->notEmpty();
  83. $field->addValidator(new SnapshotValidator(true));
  84. $this->addField($field);
  85. //vmstate
  86. if ($this->vm() instanceof Kvm)
  87. {
  88. $field = new Switcher("vmstate");
  89. $this->addField($field);
  90. }
  91. //description
  92. $field = new Textarea("description");
  93. $this->addField($field);
  94. }
  95. }