BackupJobProvider.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Providers;
  20. use MGProvision\Proxmox\v2\models\BackupSchedule;
  21. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  22. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  23. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  24. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  26. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  27. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  28. class BackupJobProvider extends BaseDataProvider implements ClientArea
  29. {
  30. use ApiService;
  31. use ProductService;
  32. public function read()
  33. {
  34. if ($this->actionElementId)
  35. {
  36. $this->data = json_decode(base64_decode($this->actionElementId), true);
  37. $this->data['starttime'] == "" ? : "off";
  38. //down
  39. $dow = $this->data['dow'];
  40. $this->data['dow'] = explode(",", $dow);
  41. //mailto
  42. $mailto = $this->data['mailto'];
  43. $this->data['mailto'] = $mailto ? "on" : "off";
  44. }
  45. $this->availableValues['compress'] = [
  46. // "0" => sl("lang")->abtr("None"),
  47. // "lzo" => sl("lang")->abtr("LZO (fast)"),
  48. // "gzip" => sl("lang")->abtr("GZIP (good)"),
  49. "zstd" => sl("lang")->abtr("ZSTD (fast and good)")
  50. ];
  51. $optionBackupCompress = $this->configuration()->getPermissionBackupCompress();
  52. if(empty($optionBackupCompress)){
  53. return;
  54. }
  55. foreach ($this->availableValues['compress'] as $k => $option){
  56. if($k=="0" && !in_array("none", $optionBackupCompress)){
  57. unset($this->availableValues['compress'][$k]);
  58. }
  59. if(!in_array($k, $optionBackupCompress)){
  60. unset($this->availableValues['compress'][$k]);
  61. }
  62. }
  63. }
  64. public function create()
  65. {
  66. $this->acl()->backupJob();
  67. $this->resourceGuard()->backupJobLimit();
  68. $storage = $this->configuration()->getBackupStorage();
  69. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  70. $maxFiles = $this->configuration()->getBackupMaxFiles();
  71. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  72. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  73. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
  74. $maxFiles = null;
  75. }
  76. $backupSchedule = new BackupSchedule();
  77. $backupSchedule->setApi($this->api());
  78. $backupSchedule->setAttributes([
  79. "vmid" => $this->vm()->getVmid(),
  80. "storage" => $storage,
  81. "remove" => $routing,
  82. "maxfiles" => $maxFiles,
  83. "starttime" => $this->formData['starttime'],
  84. "dow" => implode(",", $this->formData['dow']),
  85. "mode" => $this->formData['mode'],
  86. "compress" => $this->formData['compress'],
  87. "mailto" => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
  88. ]);
  89. $backupSchedule->create();
  90. return (new HtmlDataJsonResponse())
  91. ->setStatusSuccess()
  92. ->setMessageAndTranslate('The backup job has been created successfully')
  93. ->setRefreshTargetIds(['dackupJobDataTable'])
  94. ->addData('createButtonStatus', $this->resourceGuard()->hasBackupJobLimit())
  95. ->setCallBackFunction('toggleBackupButton');
  96. }
  97. public function update()
  98. {
  99. $this->acl()->backupJob();
  100. $storage = $this->configuration()->getBackupStorage();
  101. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  102. $maxFiles = $this->configuration()->getBackupMaxFiles();
  103. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  104. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  105. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
  106. $maxFiles = null;
  107. }
  108. $backupSchedule = new BackupSchedule();
  109. $backupSchedule->setApi($this->api());
  110. $backupSchedule->setAttributes([
  111. "id" => $this->formData['backupScheduleId'],
  112. "vmid" => $this->vm()->getVmid(),
  113. "storage" => $storage,
  114. "remove" => $routing,
  115. "maxfiles" => $maxFiles,
  116. "starttime" => $this->formData['starttime'],
  117. "dow" => implode(",", $this->formData['dow']),
  118. "mode" => $this->formData['mode'],
  119. "compress" => $this->formData['compress'],
  120. "mailto" => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
  121. ]);
  122. $backupSchedule->update();
  123. return (new HtmlDataJsonResponse())
  124. ->setStatusSuccess()
  125. ->setMessageAndTranslate('The backup job has been updated successfully');
  126. }
  127. public function delete()
  128. {
  129. $this->acl()->backupJob();
  130. $backupSchedule = new BackupSchedule();
  131. $backupSchedule->setApi($this->api());
  132. $backupSchedule->setId($this->formData['backupScheduleId']);
  133. $backupSchedule->delete();
  134. return (new HtmlDataJsonResponse())
  135. ->setStatusSuccess()
  136. ->setMessageAndTranslate('The backup job has been deleted successfully')
  137. ->setRefreshTargetIds(['dackupJobDataTable'])
  138. ->addData('createButtonStatus', 1)
  139. ->setCallBackFunction('toggleBackupButton');
  140. }
  141. }