RangeVmProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  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\ProxmoxAddon\App\UI\Servers\Providers;
  20. use ModulesGarden\ProxmoxAddon as main;
  21. use ModulesGarden\ProxmoxAddon\App\Models\RangeVm;
  22. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server;
  23. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  24. use ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  25. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
  26. /**
  27. *
  28. * Description of RangeVmProvider
  29. *
  30. * @author Pawel Kopec <pawelk@modulesgardne.com>
  31. */
  32. class RangeVmProvider extends BaseModelDataProvider implements AdminArea
  33. {
  34. public function __construct()
  35. {
  36. parent::__construct(RangeVm::class);
  37. }
  38. public function read()
  39. {
  40. if (!$this->actionElementId)
  41. {
  42. return false;
  43. }
  44. $dbData = $this->model->where('server_id', $this->actionElementId)->first();
  45. if ($dbData !== null)
  46. {
  47. $this->data = $dbData->toArray();
  48. }
  49. $this->data['server_id'] = $this->actionElementId;
  50. }
  51. public function update()
  52. {
  53. //update & create
  54. if ($this->formData['vmid_from'] && $this->formData['vmid_to'])
  55. {
  56. $dbData = $this->model->where('server_id', $this->formData['server_id'])->first();
  57. if ($dbData === null)
  58. {
  59. $dbData = new RangeVm();
  60. }
  61. $dbData->fill($this->formData)->save();
  62. }
  63. else
  64. {//delete
  65. $this->model->where('server_id', $this->formData['server_id'])->delete();
  66. }
  67. $server = Server::find($this->formData['server_id']);
  68. main\Core\ServiceLocator::call('lang')->addReplacementConstant('name', $server->name);
  69. return (new HtmlDataJsonResponse())->setMessageAndTranslate('The VM range for the server :name: has been edited successfully')
  70. ->setStatusSuccess()
  71. ->setCallBackFunction($this->callBackFunction);
  72. }
  73. }