JobProvider.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Providers;
  3. use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
  4. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  5. use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\JobPeriod;
  6. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  7. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  8. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
  9. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  10. class JobProvider extends BaseModelDataProvider implements ClientArea
  11. {
  12. use ProductService;
  13. public function __construct()
  14. {
  15. parent::__construct(SnapshotJob::class);
  16. }
  17. protected function isModelProper($model)
  18. {
  19. if (in_array(get_parent_class($model), [
  20. 'ModulesGarden\Servers\ProxmoxCloudVps\Core\Models\ExtendedEloquentModel',
  21. 'ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel',
  22. 'Illuminate\Database\Eloquent\Model'
  23. ]))
  24. {
  25. return true;
  26. }
  27. return false;
  28. }
  29. public function read()
  30. {
  31. //period
  32. $period = $this->configuration()->getPermissionSnapshotJobPeriod();
  33. if($period && in_array(JobPeriod::HOURLY, $period)){
  34. $this->availableValues['period'][JobPeriod::HOURLY] = sl("lang")->tr(JobPeriod::HOURLY );
  35. }
  36. if($period && in_array(JobPeriod::DAILY, $period)){
  37. $this->availableValues['period'][JobPeriod::DAILY] = sl("lang")->tr(JobPeriod::DAILY);
  38. }
  39. if(!$period){
  40. $this->availableValues['period'] = [
  41. JobPeriod::HOURLY => sl("lang")->tr(JobPeriod::HOURLY ),
  42. JobPeriod::DAILY => sl("lang")->tr(JobPeriod::DAILY )
  43. ];
  44. }
  45. //days
  46. $this->availableValues['days'] = [];
  47. foreach (JobPeriod::DAYS_OF_WEEK as $day){
  48. $this->availableValues['days'][$day] = sl("lang")->tr($day);
  49. }
  50. if($this->formData){
  51. $this->data = $this->formData;
  52. return;
  53. }
  54. parent::read();
  55. $vmstate = $this->data['vmstate']==1 ? "on":"off" ;
  56. $this->data['vmstate']= $vmstate;
  57. //start_time
  58. if($this->data['start_time']){
  59. $this->data['start_time'] = substr($this->data['start_time'], 0,-3);
  60. }
  61. }
  62. public function create()
  63. {
  64. //vmstate
  65. if(isset($this->formData['vmstate'])){
  66. $vmstate = $this->formData['vmstate']=="on" ? 1:0;
  67. $this->formData['vmstate']= $vmstate ;
  68. }
  69. //hosting_id
  70. $this->formData['hosting_id']= $this->getWhmcsParamByKey('serviceid');
  71. //vm_id
  72. $this->formData['vm_id'] = $this->model->id;
  73. logModuleCall(
  74. 'proxmoxCloud',
  75. __FUNCTION__,
  76. $this->formData,
  77. 'Debug',
  78. $this->model
  79. );
  80. //fill from data
  81. $this->model->fill($this->formData)->save();
  82. sl('lang')->addReplacementConstant('name', $this->formData['name']);
  83. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Snapshot Job :name: has been created successfully')
  84. ->addData('createJobButtonStatus', $this->resourceGuard()->hasSnapshotJobLimit())
  85. ->setCallBackFunction('pmToggleCreateJobButton');
  86. }
  87. public function update(){
  88. //vmstate
  89. if(isset($this->formData['vmstate'])){
  90. $vmstate = $this->formData['vmstate']=="on" ? 1:0;
  91. $this->formData['vmstate']= $vmstate ;
  92. }
  93. parent::update();
  94. sl('lang')->addReplacementConstant('name', $this->formData['name']);
  95. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Snapshot Job :name: has updated successfully');
  96. }
  97. public function deleteMass()
  98. {
  99. foreach ($this->getRequestValue('massActions') as $id)
  100. {
  101. $this->model->where('id', $id)->delete();
  102. }
  103. return (new HtmlDataJsonResponse())->setMessageAndTranslate('The selected snapshot jobs have been deleted successfully')
  104. ->addData('createJobButtonStatus', $this->resourceGuard()->hasSnapshotJobLimit())
  105. ->setCallBackFunction('pmToggleCreateJobButton');
  106. }
  107. public function delete()
  108. {
  109. parent::delete();
  110. sl('lang')->addReplacementConstant('name', $this->formData['name']);
  111. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Snapshot Job :name: has been deleted successfully')
  112. ->addData('createJobButtonStatus', $this->resourceGuard()->hasSnapshotJobLimit())
  113. ->setCallBackFunction('pmToggleCreateJobButton');
  114. }
  115. }