BackupJobProvider.php 6.8 KB

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