OsTemplateSelect.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $VmList = $clusterResurces->fetchWithUniqueNames($defaultNode);
  66. usort($VmList, function($a, $b) {return strcmp($a->getName(), $b->getName());});
  67. foreach ($VmList as $entity)
  68. {
  69. if ($entity->isCustom() && !$entity->matchName($this->getWhmcsParamByKey("serviceid")))
  70. {
  71. continue;
  72. }
  73. if (!$entity->isCustom() && $this->configuration()->isPermissionOsTemplates() && !in_array($entity->getName(), $this->configuration()->getPermissionOsTemplates()))
  74. {
  75. continue;
  76. }
  77. $id = "{$entity->getNode()}/{$entity->getVmid()}";
  78. $name = sl('lang')->abtr('template', $entity->getName());
  79. $description = $entity->getVm()->config()['description'];
  80. if(!$description){
  81. $description = $entity->getName();
  82. }
  83. $desc = sl('lang')->abtr('template_desc',$description );
  84. $this->availableValues[] = [
  85. "key" => $id,
  86. "value" => $name,
  87. 'description' => $name != $desc ? $desc : null,
  88. ];
  89. }
  90. }
  91. private function loadLxcTemplates()
  92. {
  93. $defaultNode = $this->defaultNode();
  94. $storageRepository = new StorageRepository();
  95. $storageRepository->findByNodes([$defaultNode])->findEnabed();
  96. $fileRepository = new FileRepository();
  97. $fileRepository->setApi($this->api());
  98. $fileRepository->findLxcTemplates();
  99. $fileRepository->findByNodes([$defaultNode]);
  100. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  101. foreach ($fileRepository->fetch() as $file)
  102. {
  103. if ($this->configuration()->isPermissionOsTemplates() && !in_array($file->getVolid(), $this->configuration()->getPermissionOsTemplates()))
  104. {
  105. continue;
  106. }
  107. $this->availableValues[] = [
  108. "key" => $file->getVolid(),
  109. "value" => sl('lang')->abtr('template', $file->getFriendlyName()),
  110. ];
  111. }
  112. }
  113. }