OsTemplateSelect.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Fields;
  3. use MGProvision\Proxmox\v2\repository\ClusterResourcesRepository;
  4. use MGProvision\Proxmox\v2\repository\FileRepository;
  5. use MGProvision\Proxmox\v2\repository\StorageRepository;
  6. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  7. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  8. use ModulesGarden\ProxmoxAddon\Core\Helper\AdvancedUserHelper;
  9. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  10. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\AjaxFields\Select;
  11. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  12. class OsTemplateSelect extends Select implements ClientArea
  13. {
  14. use ProductService;
  15. use ApiService;
  16. public function prepareAjaxData()
  17. {
  18. session_write_close();
  19. $advancedUser = new AdvancedUserHelper($this->getWhmcsParamByKey('userid'));
  20. if ($this->configuration()->isQemu())
  21. {
  22. $this->loadQemuTemplates();
  23. if ($advancedUser->isAdvanced() || $this->configuration()->isPermissionIsoImage())
  24. {
  25. $this->availableValues[] = [
  26. "key" => 'installationFromIso',
  27. "value" => sl('lang')->abtr('Installation From ISO'),
  28. ];
  29. }
  30. if($this->availableValues[0]['key']=="installationFromIso"){
  31. $this->callBackFunction = 'pcOsTemplateLoadShowIsoFields';
  32. }
  33. }
  34. if ($this->configuration()->isLxc())
  35. {
  36. $this->loadLxcTemplates();
  37. }
  38. if($this->availableValues[0]['key']){
  39. $this->value = $this->availableValues[0]['key'];
  40. }
  41. $this->data['additionalData']['showItemDescription'] = true;
  42. }
  43. private function defaultNode()
  44. {
  45. if ($this->getRequestValue('location'))
  46. {
  47. return $this->getRequestValue('location');
  48. }
  49. else
  50. {
  51. return $this->getNode()->getNode();
  52. }
  53. }
  54. private function loadQemuTemplates()
  55. {
  56. $clusterResurces = new ClusterResourcesRepository();
  57. $clusterResurces->findKvmTemplate();
  58. //default node
  59. $defaultNode = $this->defaultNode();
  60. //search templates on specific node
  61. if (!$this->configuration()->isOsTemplatesInAllNodes())
  62. {
  63. $clusterResurces->findByNodes($defaultNode);
  64. }
  65. foreach ($clusterResurces->fetchWithUniqueNames($defaultNode) as $entity)
  66. {
  67. if ($entity->isCustom() && !$entity->matchName($this->getWhmcsParamByKey("serviceid")))
  68. {
  69. continue;
  70. }
  71. if ($this->configuration()->isPermissionOsTemplates() && !in_array($entity->getName(), $this->configuration()->getPermissionOsTemplates()))
  72. {
  73. continue;
  74. }
  75. $id = "{$entity->getNode()}/{$entity->getVmid()}";
  76. $name = sl('lang')->abtr('template', $entity->getName());
  77. $description = $entity->getVm()->config()['description'];
  78. if(!$description){
  79. $description = $entity->getName();
  80. }
  81. $desc = sl('lang')->abtr('template_desc',$description );
  82. $this->availableValues[] = [
  83. "key" => $id,
  84. "value" => $name,
  85. 'description' => $name != $desc ? $desc : null,
  86. ];
  87. }
  88. }
  89. private function loadLxcTemplates()
  90. {
  91. $defaultNode = $this->defaultNode();
  92. $storageRepository = new StorageRepository();
  93. $storageRepository->findByNodes([$defaultNode])
  94. ->findEnabed();
  95. $fileRepository = new FileRepository();
  96. $fileRepository->setApi($this->api());
  97. $fileRepository->findLxcTemplates();
  98. $fileRepository->findByNodes([$defaultNode]);
  99. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  100. foreach ($fileRepository->fetch() as $file)
  101. {
  102. if ($this->configuration()->isPermissionOsTemplates() && !in_array($file->getVolid(), $this->configuration()->getPermissionOsTemplates()))
  103. {
  104. continue;
  105. }
  106. $this->availableValues[] = [
  107. "key" => $file->getVolid(),
  108. "value" => sl('lang')->abtr('template', $file->getFriendlyName()),
  109. ];
  110. }
  111. }
  112. }