BackupJobProvider.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. logModuleCall(
  38. 'proxmoxCloud',
  39. __FUNCTION__,
  40. $this->data,
  41. 'Debug',
  42. base64_decode($this->actionElementId)
  43. );
  44. //down
  45. $dow = $this->data['dow'];
  46. $this->data['dow'] = explode(",", $dow);
  47. //mailto
  48. $mailto = $this->data['mailto'];
  49. $this->data['mailto'] = $mailto ? "on" : "off";
  50. }
  51. $this->availableValues['compress'] = [
  52. // "0" => sl("lang")->abtr("None"),
  53. // "lzo" => sl("lang")->abtr("LZO (fast)"),
  54. // "gzip" => sl("lang")->abtr("GZIP (good)"),
  55. "zstd" => sl("lang")->abtr("ZSTD (fast and good)")
  56. ];
  57. $optionBackupCompress = $this->configuration()->getPermissionBackupCompress();
  58. if(empty($optionBackupCompress)){
  59. return;
  60. }
  61. foreach ($this->availableValues['compress'] as $k => $option){
  62. if($k=="0" && !in_array("none", $optionBackupCompress)){
  63. unset($this->availableValues['compress'][$k]);
  64. }
  65. if(!in_array($k, $optionBackupCompress)){
  66. unset($this->availableValues['compress'][$k]);
  67. }
  68. }
  69. }
  70. public function create()
  71. {
  72. $this->acl()->backupJob();
  73. $this->resourceGuard()->backupJobLimit();
  74. $storage = $this->configuration()->getBackupStorage();
  75. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  76. $maxFiles = $this->configuration()->getBackupMaxFiles();
  77. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  78. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  79. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
  80. $maxFiles = null;
  81. }
  82. $backupSchedule = new BackupSchedule();
  83. $backupSchedule->setApi($this->api());
  84. $backupSchedule->setAttributes([
  85. "vmid" => $this->vm()->getVmid(),
  86. "storage" => $storage,
  87. "remove" => $routing,
  88. "maxfiles" => $maxFiles,
  89. "starttime" => $this->formData['starttime'],
  90. "dow" => implode(",", $this->formData['dow']),
  91. "mode" => $this->formData['mode'],
  92. "compress" => $this->formData['compress'],
  93. "mailto" => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
  94. ]);
  95. $backupSchedule->create();
  96. return (new HtmlDataJsonResponse())
  97. ->setStatusSuccess()
  98. ->setMessageAndTranslate('The backup job has been created successfully')
  99. ->setRefreshTargetIds(['dackupJobDataTable'])
  100. ->addData('createButtonStatus', $this->resourceGuard()->hasBackupJobLimit())
  101. ->setCallBackFunction('toggleBackupButton');
  102. }
  103. public function update()
  104. {
  105. $this->acl()->backupJob();
  106. $storage = $this->configuration()->getBackupStorage();
  107. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  108. $maxFiles = $this->configuration()->getBackupMaxFiles();
  109. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  110. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  111. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
  112. $maxFiles = null;
  113. }
  114. $backupSchedule = new BackupSchedule();
  115. $backupSchedule->setApi($this->api());
  116. $backupSchedule->setAttributes([
  117. "id" => $this->formData['backupScheduleId'],
  118. "vmid" => $this->vm()->getVmid(),
  119. "storage" => $storage,
  120. "remove" => $routing,
  121. "maxfiles" => $maxFiles,
  122. "starttime" => $this->formData['starttime'],
  123. "dow" => implode(",", $this->formData['dow']),
  124. "mode" => $this->formData['mode'],
  125. "compress" => $this->formData['compress'],
  126. "mailto" => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
  127. ]);
  128. $backupSchedule->update();
  129. return (new HtmlDataJsonResponse())
  130. ->setStatusSuccess()
  131. ->setMessageAndTranslate('The backup job has been updated successfully');
  132. }
  133. public function delete()
  134. {
  135. $this->acl()->backupJob();
  136. $backupSchedule = new BackupSchedule();
  137. $backupSchedule->setApi($this->api());
  138. $backupSchedule->setId($this->formData['backupScheduleId']);
  139. $backupSchedule->delete();
  140. return (new HtmlDataJsonResponse())
  141. ->setStatusSuccess()
  142. ->setMessageAndTranslate('The backup job has been deleted successfully')
  143. ->setRefreshTargetIds(['dackupJobDataTable'])
  144. ->addData('createButtonStatus', 1)
  145. ->setCallBackFunction('toggleBackupButton');
  146. }
  147. }