NodeSelect.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Templates\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. /**
  25. * Description of NodeSelect
  26. *
  27. * @author Pawel Kopec <pawelk@modulesgardne.com>
  28. */
  29. class NodeSelect extends Select implements AdminArea
  30. {
  31. private $formData;
  32. public function returnAjaxData()
  33. {
  34. $this->formData = $this->getRequestValue('formData', []);
  35. $options = [];
  36. foreach ($this->getOptions() as $id => $option)
  37. {
  38. $options[$id] = [
  39. 'title' => $option,
  40. 'selected' => $this->formData['id'] && main\App\Models\IpAddress::where('id', $this->formData['id'])->where("node", $option)->count()
  41. ];
  42. }
  43. return (new main\Core\UI\ResponseTemplates\RawDataJsonResponse($options));
  44. }
  45. private function getOptions()
  46. {
  47. $options = ['0' => main\Core\ServiceLocator::call('lang')->absoluteT('Any')];
  48. if ($this->formData['sid'] > 0)
  49. {
  50. $servers = main\Core\Models\Whmcs\Server::where('id', $this->formData['sid']);
  51. }
  52. else
  53. {
  54. $servers = main\Core\Models\Whmcs\Server::whereIn("type", ["proxmoxVPS", "ProxmoxCloudVps"])->where('disabled', '0');
  55. }
  56. $nodeRepository = new proxmox\repository\NodeRepository();
  57. foreach ($servers->get() as $server)
  58. {
  59. /* @var $server main\Core\Models\Whmcs\Server */
  60. try
  61. {
  62. $host = $server->ipaddress ? $server->ipaddres : $server->hostname;
  63. $api = new proxmox\Api($host, $server->username, $server->accesshash, decrypt($server->password));
  64. $nodeRepository->setApi($api);
  65. foreach ($nodeRepository->fetch() as $node)
  66. {
  67. $options[$node->getNode()] = $node->getNode();
  68. }
  69. }
  70. catch (\Exception $ex)
  71. {//login to proxmox host failed
  72. }
  73. }
  74. return $options;
  75. }
  76. }