NodeSelect.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Sep 10, 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\IpManagement\Fields;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  23. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\AjaxFields\Select;
  24. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  25. /**
  26. * Description of NodeSelect
  27. *
  28. * @author Pawel Kopec <pawelk@modulesgardne.com>
  29. */
  30. class NodeSelect extends Select implements AdminArea
  31. {
  32. private $serverId;
  33. public function prepareAjaxData()
  34. {
  35. $this->serverId = $this->getRequestValue('sid', null);
  36. $this->availableValues[] = ['key' => 0, 'value' => sl('lang')->absoluteT('Any')];
  37. if ($this->serverId > 0)
  38. {
  39. $servers = main\Core\Models\Whmcs\Server::where('id', $this->serverId);
  40. }
  41. else
  42. {
  43. $servers = main\Core\Models\Whmcs\Server::whereIn("type", ["proxmoxVPS", "ProxmoxCloudVps"])->where('disabled', '0');
  44. }
  45. $nodeRepository = new proxmox\repository\NodeRepository();
  46. $this->value = [0];
  47. if ($this->getRequestValue('id'))
  48. {
  49. $this->value = [main\App\Models\IpAddress::where('id', $this->getRequestValue('id'))->first()->node];
  50. }
  51. foreach ($servers->get() as $server)
  52. {
  53. /* @var $server main\Core\Models\Whmcs\Server */
  54. try
  55. {
  56. $host = $server->ipaddress ? $server->ipaddress : $server->hostname;
  57. if(is_numeric($server->port)){
  58. $host .=":".$server->port;
  59. }
  60. $api = new proxmox\Api($host, $server->username, $server->accesshash, decrypt($server->password));
  61. $nodeRepository->setApi($api);
  62. foreach ($nodeRepository->fetch() as $node)
  63. {
  64. $this->availableValues[] = [
  65. 'key' => $node->getNode(),
  66. 'value' => $node->getNode()
  67. ];
  68. }
  69. }
  70. catch (proxmox\ProxmoxApiException $ex)
  71. {//login to proxmox host failed
  72. if ($ex->getCode() != 401)
  73. {
  74. throw $ex;
  75. }
  76. }
  77. }
  78. }
  79. }