BackupJobProvider.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\ProxmoxCloudVps\App\UI\BackupJob\Providers;
  20. use MGProvision\Proxmox\v2\models\BackupSchedule;
  21. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  22. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  23. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  24. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  25. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  26. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  27. use function ModulesGarden\Servers\ProxmoxCloudVps\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. //down
  38. $dow = $this->data['dow'];
  39. $this->data['dow'] = explode(",", $dow);
  40. //mailto
  41. $mailto = $this->data['mailto'];
  42. $this->data['mailto'] = $mailto ? "on" : "off";
  43. }
  44. $this->availableValues['compress'] = [
  45. "0" => sl("lang")->abtr("None"),
  46. "lzo" => sl("lang")->abtr("LZO (fast)"),
  47. "gzip" => sl("lang")->abtr("GZIP (good)"),
  48. "zstd" => sl("lang")->abtr("ZSTD (fast and good)")
  49. ];
  50. $optionBackupCompress = $this->configuration()->getPermissionBackupCompress();
  51. if(empty($optionBackupCompress)){
  52. return;
  53. }
  54. foreach ($this->availableValues['compress'] as $k => $option){
  55. if($k=="0" && !in_array("none", $optionBackupCompress)){
  56. unset($this->availableValues['compress'][$k]);
  57. }
  58. if(!in_array($k, $optionBackupCompress)){
  59. unset($this->availableValues['compress'][$k]);
  60. }
  61. }
  62. }
  63. public function create()
  64. {
  65. $this->acl()->backupJob();
  66. $this->resourceGuard()->backupJobLimit();
  67. $storage = $this->configuration()->getBackupStorage();
  68. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  69. $maxFiles = $this->configuration()->getBackupMaxFiles();
  70. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  71. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  72. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles())=="-1"){
  73. $maxFiles = null;
  74. }
  75. $backupSchedule = new BackupSchedule();
  76. $backupSchedule->setApi($this->api());
  77. $vmid = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->vmid;
  78. $backupSchedule->setAttributes([
  79. "vmid" => $vmid,
  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(['backupJobDataTable'])
  94. ->addData('createButtonStatus', $this->resourceGuard()->hasBackupJobLimit())
  95. ->setCallBackFunction('pmToggleBackupButton');
  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, $this->configuration()->getBackupMaxFiles())=="-1"){
  106. $maxFiles = null;
  107. }
  108. $backupSchedule = new BackupSchedule();
  109. $backupSchedule->setApi($this->api());
  110. $vmid = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->vmid;
  111. $backupSchedule->setAttributes([
  112. "id" => $this->formData['backupScheduleId'],
  113. "vmid" => $vmid,
  114. "storage" => $storage,
  115. "remove" => $routing,
  116. "maxfiles" => $maxFiles,
  117. "starttime" => $this->formData['starttime'],
  118. "dow" => implode(",", $this->formData['dow']),
  119. "mode" => $this->formData['mode'],
  120. "compress" => $this->formData['compress'],
  121. "mailto" => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
  122. ]);
  123. $backupSchedule->update();
  124. return (new HtmlDataJsonResponse())
  125. ->setStatusSuccess()
  126. ->setMessageAndTranslate('The backup job has been updated successfully');
  127. }
  128. public function delete()
  129. {
  130. $this->acl()->backupJob();
  131. $backupSchedule = new BackupSchedule();
  132. $backupSchedule->setApi($this->api());
  133. $backupSchedule->setId($this->formData['backupScheduleId']);
  134. $backupSchedule->delete();
  135. return (new HtmlDataJsonResponse())
  136. ->setStatusSuccess()
  137. ->setMessageAndTranslate('The backup job has been deleted successfully')
  138. ->setRefreshTargetIds(['backupJobDataTable'])
  139. ->addData('createButtonStatus', 1)
  140. ->setCallBackFunction('pmToggleBackupButton');
  141. }
  142. }